src.nth.io/

summaryrefslogtreecommitdiff
path: root/doorbell-viewport/files/doorbell-viewport-debug
blob: 36989dfe1e123c6e9e1f71930592c61c796f67a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash
# doorbell-viewport-debug: CLI debug tool
#
# Commands:
#   show           Turn display on
#   hide           Turn display off
#   test-display   Test display power cycle (on -> 3s -> off)
#   test-touch     List touch devices and capabilities
#   test-stream    Fetch RTSP URL from Protect and play via mpv
#   test-protect   Test Protect API authentication and camera info

set -e

ENV_FILE="/etc/doorbell-viewport/doorbell-viewport.env"
CMD="${1:-help}"

_load_env() {
    if [ -f "$ENV_FILE" ]; then
        set -a
        # shellcheck disable=SC1090
        source "$ENV_FILE"
        set +a
    else
        echo "Warning: $ENV_FILE not found" >&2
    fi
}

_display_on() {
    case "${DOORBELL_VIEWPORT_DISPLAY_BACKEND:-vcgencmd}" in
        vcgencmd)
            vcgencmd display_power 1
            ;;
        drm|panel)
            BACKLIGHT=$(ls /sys/class/backlight/ 2>/dev/null | head -1)
            if [ -n "$BACKLIGHT" ]; then
                MAX=$(cat "/sys/class/backlight/$BACKLIGHT/max_brightness")
                echo "$MAX" > "/sys/class/backlight/$BACKLIGHT/brightness"
            else
                echo "No backlight device found" >&2
            fi
            ;;
    esac
}

_display_off() {
    case "${DOORBELL_VIEWPORT_DISPLAY_BACKEND:-vcgencmd}" in
        vcgencmd)
            vcgencmd display_power 0
            ;;
        drm|panel)
            BACKLIGHT=$(ls /sys/class/backlight/ 2>/dev/null | head -1)
            if [ -n "$BACKLIGHT" ]; then
                echo "0" > "/sys/class/backlight/$BACKLIGHT/brightness"
            else
                echo "No backlight device found" >&2
            fi
            ;;
    esac
}

cmd_show() {
    _load_env
    echo "Turning display on..."
    _display_on
    echo "Display on"
}

cmd_hide() {
    _load_env
    echo "Turning display off..."
    _display_off
    echo "Display off"
}

cmd_test_display() {
    _load_env
    echo "Testing display power cycle..."
    echo "ON..."
    _display_on
    sleep 3
    echo "OFF..."
    _display_off
    echo "Done"
}

cmd_test_touch() {
    echo "Touch devices:"
    python3 - <<'PYEOF'
import evdev
for path in evdev.list_devices():
    try:
        dev = evdev.InputDevice(path)
        caps = dev.capabilities()
        has_mt = (
            evdev.ecodes.EV_ABS in caps
            and any(
                code == evdev.ecodes.ABS_MT_POSITION_X
                for code, _ in caps[evdev.ecodes.EV_ABS]
            )
        )
        has_btn = (
            evdev.ecodes.EV_KEY in caps
            and evdev.ecodes.BTN_TOUCH in [code for code, _ in caps.get(evdev.ecodes.EV_KEY, [])]
        )
        print(f"  {path}: {dev.name!r} (multitouch={has_mt}, btn_touch={has_btn})")
        dev.close()
    except Exception as e:
        print(f"  {path}: error: {e}")
PYEOF
    echo ""
    echo "To monitor live touch events interactively:"
    echo "  python3 -m evdev.evtest"
}

cmd_test_stream() {
    _load_env
    if [ -z "$DOORBELL_VIEWPORT_PROTECT_HOST" ]; then
        echo "Error: DOORBELL_VIEWPORT_PROTECT_HOST not set" >&2
        exit 1
    fi

    echo "Fetching RTSP URL from $DOORBELL_VIEWPORT_PROTECT_HOST..."
    RTSP_URL=$(python3 - <<PYEOF
import os, sys, requests, urllib3
urllib3.disable_warnings()
host = os.environ['DOORBELL_VIEWPORT_PROTECT_HOST']
user = os.environ['DOORBELL_VIEWPORT_PROTECT_USERNAME']
passwd = os.environ['DOORBELL_VIEWPORT_PROTECT_PASSWORD']
camera_id = os.environ['DOORBELL_VIEWPORT_CAMERA_ID']
s = requests.Session()
s.verify = False
r = s.post(f'https://{host}/api/auth/login',
           json={'username': user, 'password': passwd}, timeout=10)
r.raise_for_status()
r2 = s.get(f'https://{host}/proxy/protect/api/cameras/{camera_id}', timeout=10)
r2.raise_for_status()
for ch in r2.json().get('channels', []):
    if ch.get('isRtspEnabled') and ch.get('rtspAlias'):
        print(f"rtsp://{host}:7447/{ch['rtspAlias']}")
        sys.exit(0)
print('no-rtsp-url', file=sys.stderr)
sys.exit(1)
PYEOF
    )

    if [ -z "$RTSP_URL" ]; then
        echo "Failed to get RTSP URL" >&2
        exit 1
    fi

    echo "Playing: $RTSP_URL"
    echo "(press q to quit)"
    mpv \
        --vo=drm \
        "--video-rotate=${DOORBELL_VIEWPORT_ORIENTATION:-270}" \
        --fullscreen \
        --no-border \
        --no-osc \
        --no-input-default-bindings \
        --really-quiet \
        "$RTSP_URL"
}

cmd_test_protect() {
    _load_env
    if [ -z "$DOORBELL_VIEWPORT_PROTECT_HOST" ]; then
        echo "Error: DOORBELL_VIEWPORT_PROTECT_HOST not set" >&2
        exit 1
    fi

    python3 - <<PYEOF
import os, requests, urllib3
urllib3.disable_warnings()
host = os.environ['DOORBELL_VIEWPORT_PROTECT_HOST']
user = os.environ['DOORBELL_VIEWPORT_PROTECT_USERNAME']
passwd = os.environ['DOORBELL_VIEWPORT_PROTECT_PASSWORD']
camera_id = os.environ['DOORBELL_VIEWPORT_CAMERA_ID']
s = requests.Session()
s.verify = False

print(f"Connecting to {host}...")
r = s.post(f'https://{host}/api/auth/login',
           json={'username': user, 'password': passwd}, timeout=10)
r.raise_for_status()
print("Authentication: OK")

r2 = s.get(f'https://{host}/proxy/protect/api/cameras/{camera_id}', timeout=10)
r2.raise_for_status()
data = r2.json()
print(f"Camera name:   {data.get('name', 'unknown')}")
print(f"Camera type:   {data.get('type', 'unknown')}")
channels = [ch for ch in data.get('channels', []) if ch.get('isRtspEnabled')]
print(f"RTSP channels: {len(channels)}")
for ch in channels:
    name = ch.get('name', '')
    alias = ch.get('rtspAlias', '')
    print(f"  {name}: rtsp://{host}:7447/{alias}")
print("Protect connection: OK")
PYEOF
}

case "$CMD" in
    show)          cmd_show ;;
    hide)          cmd_hide ;;
    test-display)  cmd_test_display ;;
    test-touch)    cmd_test_touch ;;
    test-stream)   cmd_test_stream ;;
    test-protect)  cmd_test_protect ;;
    help|*)
        echo "Usage: doorbell-viewport-debug <command>"
        echo ""
        echo "Commands:"
        echo "  show           Turn display on"
        echo "  hide           Turn display off"
        echo "  test-display   Test display power cycle (on -> 3s -> off)"
        echo "  test-touch     List touch devices and capabilities"
        echo "  test-stream    Fetch RTSP URL and play via mpv"
        echo "  test-protect   Test Protect API connection and camera info"
        echo ""
        echo "Config: $ENV_FILE"
        ;;
esac