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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
// Scrypted Viewport — v1 Scripts-plugin script
//
// Architecture
// ------------
// One parent device (DeviceProvider + DeviceCreator + HttpRequestHandler)
// spawns N child "Viewport" devices via the Scrypted UI. Each child holds
// the per-viewport binding (host, camera, orientation, idle timeout,
// brightness) as plain device settings and is editable from its own
// Settings page in the Scrypted UI. The parent owns the camera event
// subscriptions, the snapshot push loop, the per-stream safety timer,
// and the inbound `POST <base>/state` handler.
//
// Install
// -------
// 1. In Scrypted: Plugins → install "Scripts" if needed.
// 2. + Add Device → Scripts plugin → New Script.
// 3. Paste this entire file. Save.
// 4. Open the new "Scrypted Viewport" device. Click "+ Add Device" on
// its page to create a viewport binding:
// Viewport name e.g. "mudroom" (becomes the routing key + mDNS name)
// IP or hostname e.g. "192.168.1.42"
// Camera pick from the dropdown of Camera devices
// Orientation portrait (480×800) or landscape (800×480)
// 5. The script POSTs /config to the device immediately and re-issues it
// every 5 minutes so a reboot or DHCP renumber re-syncs.
// 6. Edit a viewport's settings from its own device page in the UI. The
// script re-registers and re-subscribes whenever a setting changes.
//
// v1 limits (path to v2: a packaged plugin with FFmpeg streaming)
// ---------------------------------------------------------------
// - Snapshot-rate (~1 fps) via Camera.takePicture(). Live-rate MJPEG over
// POST /stream is v2.
// - Manual IP per viewport (no mDNS-SD discovery). Use a DHCP reservation.
// - Camera must respect picture.width/height OR be paired with a snapshot
// plugin that resizes. Otherwise /frame returns 400.
// The Scripts plugin (in @scrypted/core) evaluates this file inside the
// scryptedEval sandbox. The runtime pre-injects the SDK names as scope
// locals — no `import` is needed (or allowed: any `import ... from
// "@scrypted/sdk"` compiles to a require() that fails to resolve).
// All we do here is `declare` each one so TypeScript is happy; the
// declarations erase at compile time and the values come from the
// runtime scope.
declare const sdk: any;
declare const ScryptedDeviceBase: any;
declare const ScryptedDeviceType: any;
declare const ScryptedInterface: any;
declare const systemManager: any;
declare const endpointManager: any;
declare const mediaManager: any;
declare const deviceManager: any;
declare const log: any;
declare const device: any;
declare const require: any;
// Loose type aliases — purely cosmetic for the rest of the script's
// signatures, since the runtime values are `any`.
type DeviceCreator = any;
type DeviceCreatorSettings = any;
type DeviceProvider = any;
type EventListenerRegister = any;
type HttpRequest = any;
type HttpRequestHandler = any;
type HttpResponse = any;
type Setting = any;
type Settings = any;
type SettingValue = any;
// Tuning constants. Frame interval is also exposed on the parent's
// Settings page so it can be tweaked without editing the script.
const DEFAULT_FRAME_INTERVAL_MS = 1_000;
const REREGISTER_INTERVAL_MS = 5 * 60_000;
const HTTP_TIMEOUT_MS = 1_000;
const DEFAULT_IDLE_TIMEOUT_MS = 60_000;
const DEFAULT_BRIGHTNESS = 80;
// ============================================================================
// Child: one viewport binding
// ============================================================================
class Viewport extends ScryptedDeviceBase implements Settings {
constructor(public provider: ScryptedViewportProvider, nativeId: string) {
super(nativeId);
}
get host(): string { return this.storage.getItem("host") || ""; }
get cameraId(): string { return this.storage.getItem("cameraId") || ""; }
get orientation(): "portrait" | "landscape" {
const v = this.storage.getItem("orientation");
return v === "landscape" ? "landscape" : "portrait";
}
get idleTimeoutMs(): number {
const v = this.storage.getItem("idle_timeout_ms");
return v ? Math.max(0, parseInt(v, 10) || 0) : DEFAULT_IDLE_TIMEOUT_MS;
}
get brightness(): number {
const v = this.storage.getItem("brightness");
return v ? Math.max(0, Math.min(100, parseInt(v, 10) || 0)) : DEFAULT_BRIGHTNESS;
}
get frameIntervalMs(): number {
const v = this.storage.getItem("frame_interval_ms");
const parsed = v ? parseInt(v, 10) : NaN;
return Number.isFinite(parsed) ? Math.max(33, parsed) : DEFAULT_FRAME_INTERVAL_MS;
}
// Which camera-event types wake this viewport. Empty = tap-only,
// never woken by Scrypted. Default = all three (doorbell + motion +
// person detection).
get triggers(): Set<string> {
const v = this.storage.getItem("triggers");
if (v === null) return new Set(["doorbell", "motion", "person"]);
try { return new Set(JSON.parse(v)); } catch { return new Set(); }
}
async getSettings(): Promise<Setting[]> {
const settings: Setting[] = [
{
group: "Binding",
key: "host",
title: "IP or hostname",
description: "Viewport's address on the LAN. Set this manually — find it via your DHCP table, or `dns-sd -G v4 viewport-<mac>.local` on macOS, or `avahi-resolve -n viewport-<mac>.local` on Linux. The info screen on the device itself shows its MAC + IP.",
placeholder: "192.168.1.42",
value: this.host,
} as any,
{
group: "Binding",
key: "cameraId",
title: "Camera",
description: "Camera whose events drive this viewport's wake/sleep, and whose snapshots get streamed.",
type: "device",
deviceFilter: `interfaces.includes('${ScryptedInterface.Camera}')`,
value: this.cameraId,
} as any,
{
group: "Binding",
key: "triggers",
title: "Wake triggers",
description: "Which camera-event types automatically wake the viewport. Clear all of them for tap-only mode (the viewport never wakes from Scrypted; user must tap to see the camera).",
choices: ["doorbell", "motion", "person"],
multiple: true,
value: Array.from(this.triggers),
} as any,
{
group: "Display",
key: "orientation",
title: "Orientation",
description: "Panel orientation. Frames are sent at this effective resolution.",
choices: ["portrait", "landscape"],
value: this.orientation,
} as any,
{
group: "Display",
key: "brightness",
title: "Brightness (0–100)",
description: "Sent to the device via /config. Gamma-corrected on the panel.",
type: "number",
value: this.brightness,
} as any,
{
group: "Display",
key: "idle_timeout_ms",
title: "Idle timeout (ms)",
description: "How long the device stays awake after the last paint before it sleeps itself. 0 disables; non-zero must be ≥ 5000.",
type: "number",
value: this.idleTimeoutMs,
} as any,
{
group: "Display",
key: "frame_interval_ms",
title: "Frame push interval (ms)",
description: `How often a JPEG snapshot is POSTed during an active stream. Lower = smoother but more network + CPU; clamped to ≥ 33 ms (~30 fps max). Default ${DEFAULT_FRAME_INTERVAL_MS}.`,
type: "number",
value: this.frameIntervalMs,
} as any,
];
// Live device snapshot: GET /state + /config in parallel with a
// short timeout, then render as a read-only "Status" section. If
// the device is offline we still surface the binding fields so the
// operator can change them — the status fields just say "offline".
if (this.host) {
try {
const ctrl = AbortSignal.timeout(1500);
const [stateRes, configRes] = await Promise.all([
fetch(`http://${this.host}/state`, { signal: ctrl }).then(r => r.json()),
fetch(`http://${this.host}/config`, { signal: ctrl }).then(r => r.json()),
]);
settings.push(
{ group: "Status (live)", key: "_st_name", title: "name", value: stateRes.name, readonly: true } as any,
{ group: "Status (live)", key: "_st_mac", title: "mac", value: stateRes.mac, readonly: true } as any,
{ group: "Status (live)", key: "_st_ip", title: "ip", value: stateRes.ip, readonly: true } as any,
{ group: "Status (live)", key: "_st_state", title: "state", value: stateRes.state, readonly: true } as any,
{ group: "Status (live)", key: "_st_cfg", title: "configured", value: String(stateRes.configured), readonly: true } as any,
{ group: "Status (live)", key: "_st_uptime", title: "uptime (ms)", value: String(stateRes.uptime_ms), readonly: true } as any,
{ group: "Status (live)", key: "_st_last", title: "last frame (ms ago)", value: String(stateRes.last_frame_ms_ago ?? "(none)"), readonly: true } as any,
{ group: "Status (live)", key: "_st_fr", title: "frames received", value: String(stateRes.frames_received), readonly: true } as any,
{ group: "Status (live)", key: "_st_err", title: "decode errors", value: String(stateRes.decode_errors), readonly: true } as any,
{ group: "Status (live)", key: "_st_post", title: "state post failures", value: String(stateRes.state_post_failures), readonly: true } as any,
{ group: "Status (live)", key: "_st_res", title: "resolution", value: stateRes.resolution, readonly: true } as any,
{ group: "Status (live)", key: "_st_heap", title: "free heap (bytes)", value: String(stateRes.free_heap), readonly: true } as any,
{ group: "Status (live)", key: "_st_psram", title: "free PSRAM (bytes)", value: String(stateRes.free_psram), readonly: true } as any,
{ group: "Status (live)", key: "_st_ver", title: "firmware", value: stateRes.version, readonly: true } as any,
{ group: "Status (live)", key: "_cfg_scrypt",title: "config: scrypted URL",value: configRes.scrypted ?? "(not set)", readonly: true } as any,
);
} catch (e) {
settings.push({ group: "Status (live)", key: "_st_err", title: "device", value: `offline / unreachable (${(e as Error).message})`, readonly: true } as any);
}
}
return settings;
}
async putSetting(key: string, value: SettingValue) {
if (key.startsWith("_")) return; // ignore read-only status fields
if (key === "triggers") {
// multi-select arrives as array; serialise to JSON for storage
this.storage.setItem("triggers", JSON.stringify(Array.isArray(value) ? value : []));
} else {
this.storage.setItem(key, String(value ?? ""));
}
await this.provider.onBindingChanged(this);
}
}
// ============================================================================
// Parent: provider + HTTP handler + global tuning
// ============================================================================
class ScryptedViewportProvider extends ScryptedDeviceBase
implements DeviceProvider, DeviceCreator, HttpRequestHandler, Settings {
private viewports = new Map<string, Viewport>(); // nativeId -> child instance
private listeners = new Map<string, EventListenerRegister>(); // nativeId -> camera event listener
private streams = new Map<string, { // viewport name -> stream control
interval: NodeJS.Timeout;
timeout: NodeJS.Timeout;
abort: AbortController;
}>();
private scryptedBase = "";
constructor(nativeId?: string) {
super(nativeId);
this.start().catch(e => this.console.error("start failed", e));
}
// ------------------------------------------------------------------------
// Lifecycle
// ------------------------------------------------------------------------
private get childIds(): string[] {
try { return JSON.parse(this.storage.getItem("childIds") || "[]"); }
catch { return []; }
}
private set childIds(ids: string[]) {
this.storage.setItem("childIds", JSON.stringify(ids));
}
private async start() {
// endpointManager.getInsecurePublicLocalEndpoint() takes a nativeId
// (string) — passing this.id (numeric Scrypted DB ID) throws
// "invalid nativeId N". this.nativeId is the right key, and an
// omitted nativeId falls back to the plugin's own endpoint.
const raw = await endpointManager.getInsecurePublicLocalEndpoint(this.nativeId);
this.scryptedBase = raw.replace(/\/$/, "");
this.console.log(`Scrypted Viewport up. Callback URL base: ${this.scryptedBase}`);
// Re-discover every known child so Scrypted reattaches its storage
// to the nativeId. Without this, `new Viewport(...)` instantiates
// with `this.storage === undefined` and every storage-backed getter
// (host / cameraId / orientation / ...) throws on script reload.
// Then eagerly instantiate so each child's registration + camera
// event subscription happen at plugin load.
for (const nativeId of this.childIds) {
try {
await deviceManager.onDeviceDiscovered({
providerNativeId: this.nativeId,
nativeId,
name: nativeId, // overridden by Scrypted from its existing record
type: ScryptedDeviceType.SmartDisplay,
interfaces: [ScryptedInterface.Settings],
});
await this.getDevice(nativeId);
}
catch (e) { this.console.warn(`load child ${nativeId} failed:`, (e as Error).message); }
}
// Periodic re-register so a device that rebooted or got a new IP
// re-syncs without manual intervention.
setInterval(() => {
for (const v of this.viewports.values()) {
this.registerViewport(v).catch(() => {});
}
}, REREGISTER_INTERVAL_MS);
}
// ------------------------------------------------------------------------
// DeviceProvider
// ------------------------------------------------------------------------
async getDevice(nativeId: string): Promise<Viewport> {
let v = this.viewports.get(nativeId);
if (!v) {
v = new Viewport(this, nativeId);
this.viewports.set(nativeId, v);
this.attachListener(v);
await this.registerViewport(v);
}
return v;
}
async releaseDevice(id: string, nativeId: string) {
const v = this.viewports.get(nativeId);
if (v) {
this.stopStream(v.name, /*sendSleep=*/ false);
this.detachListener(nativeId);
this.viewports.delete(nativeId);
}
this.childIds = this.childIds.filter(x => x !== nativeId);
}
// ------------------------------------------------------------------------
// DeviceCreator — "+ Add Device" form on the parent
// ------------------------------------------------------------------------
async getCreateDeviceSettings(): Promise<Setting[]> {
return [
{
key: "name",
title: "Viewport name",
description: 'Routing key sent back in callbacks + basis for the device\'s mDNS hostname (viewport-<name>.local). Lowercase, no spaces. Example: "mudroom".',
placeholder: "mudroom",
},
{
key: "host",
title: "IP or hostname (optional)",
description: "Leave blank to auto-resolve via mDNS (viewport-<name>.local). Set manually if mDNS doesn't reach this network.",
placeholder: "auto-resolve via mDNS",
},
{
key: "cameraId",
title: "Camera",
type: "device",
deviceFilter: `interfaces.includes('${ScryptedInterface.Camera}')`,
},
{
key: "orientation",
title: "Orientation",
choices: ["portrait", "landscape"],
value: "portrait",
},
];
}
async createDevice(settings: DeviceCreatorSettings): Promise<string> {
const name = String(settings.name || "viewport").trim();
const nativeId = `vp_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 6)}`;
// 1. Register the device with Scrypted FIRST. deviceManager
// materialises the storage container only after discovery —
// calling getDeviceStorage before this returns undefined and
// setItem() throws "Cannot read properties of undefined".
await deviceManager.onDeviceDiscovered({
providerNativeId: this.nativeId,
nativeId,
name,
type: ScryptedDeviceType.SmartDisplay,
interfaces: [ScryptedInterface.Settings],
});
// 2. Now safe to seed the child's storage from the form values.
// display_name is the canonical user-facing name; v.name (the
// ScryptedDeviceBase one) is async-loaded from Scrypted's record
// and races with our first registerViewport call, so we mirror
// it into storage as a stable fallback for register/log paths.
const childStore = deviceManager.getDeviceStorage(nativeId);
childStore.setItem("display_name", name);
childStore.setItem("host", String(settings.host || ""));
childStore.setItem("cameraId", String(settings.cameraId || ""));
childStore.setItem("orientation", String(settings.orientation || "portrait"));
this.childIds = [...this.childIds, nativeId];
this.console.log(`created viewport "${name}" (${nativeId})`);
// Kick off the first register cycle (mDNS resolve + POST /config).
// Fire-and-forget — the new device shows up immediately either way.
const child = await this.getDevice(nativeId);
if (child) this.registerViewport(child).catch(() => {});
return nativeId;
}
// ------------------------------------------------------------------------
// Per-binding plumbing (camera subscription + /config registration)
// ------------------------------------------------------------------------
onBindingChanged = async (v: Viewport): Promise<void> => {
const nid = v.nativeId!;
this.detachListener(nid);
// Any active stream for this viewport is now stale (camera may have
// changed). Stop it cleanly; the next event/wake will start fresh.
this.stopStream(v.name, /*sendSleep=*/ false);
this.attachListener(v);
await this.registerViewport(v);
};
private attachListener(v: Viewport) {
if (!v.cameraId) return;
const cam = systemManager.getDeviceById(v.cameraId);
const tag = v.name || v.storage.getItem("display_name") || v.nativeId;
if (!cam) {
this.console.warn(`viewport "${tag}": camera ${v.cameraId} not found`);
return;
}
const ifaces = [
ScryptedInterface.BinarySensor, // doorbell
ScryptedInterface.MotionSensor, // motion
ScryptedInterface.ObjectDetector, // person / etc
];
const reg = cam.listen(ifaces, (source, details, data) => {
this.handleCameraEvent(v, details, data);
});
this.listeners.set(v.nativeId!, reg);
this.console.log(`viewport "${tag}": subscribed to "${cam.name}"`);
}
private detachListener(nativeId: string) {
const reg = this.listeners.get(nativeId);
if (reg) {
try { reg.removeListener(); } catch {}
this.listeners.delete(nativeId);
}
}
private async registerViewport(v: Viewport) {
// Guard against transient empty names. Scrypted occasionally hands
// us a Viewport whose `.name` hasn't resolved yet (race between
// device-record load and event delivery); POSTing /config with an
// empty viewport just gets a 400 from the firmware. Fall back to
// the stored display name from storage if it's there, else skip.
const name = (v.name && v.name.trim()) || v.storage.getItem("display_name") || "";
if (!name) {
this.console.warn(`register skipped — empty name on ${v.nativeId}; will retry on next event`);
return;
}
if (!v.host) {
this.console.warn(`register "${name}" skipped — no host. Set the viewport's "IP or hostname" field; see the README for how to find it via mDNS from your shell.`);
return;
}
try {
await this.postJSON(`http://${v.host}/config`, {
viewport: name,
scrypted: this.scryptedBase,
idle_timeout_ms: v.idleTimeoutMs,
orientation: v.orientation,
brightness: v.brightness,
});
// Cache the name in storage so a future empty-.name event can
// still find it. createDevice + putSetting always update this.
v.storage.setItem("display_name", name);
this.console.log(`registered "${name}" (${v.host})`);
} catch (e) {
this.console.warn(`register "${name}" failed:`, (e as Error).message);
}
}
// ------------------------------------------------------------------------
// Camera event → stream
// ------------------------------------------------------------------------
private handleCameraEvent(v: Viewport, details: any, data: any) {
const iface = details.eventInterface;
const allowed = v.triggers;
let trigger = false;
if (allowed.has("doorbell") && iface === ScryptedInterface.BinarySensor && data === true) trigger = true;
if (allowed.has("motion") && iface === ScryptedInterface.MotionSensor && data === true) trigger = true;
if (allowed.has("person") && iface === ScryptedInterface.ObjectDetector) {
const detections = data?.detections ?? [];
if (detections.some((d: any) => d?.className === "person")) trigger = true;
}
if (!trigger) return;
this.console.log(`event ${iface} -> "${v.name}": wake`);
this.startStream(v).catch(e => this.console.error("startStream failed", e));
}
private async startStream(v: Viewport) {
// Race rule: cancel pending operations on every callback before
// beginning a fresh stream.
this.stopStream(v.name, /*sendSleep=*/ false);
if (!v.host || !v.cameraId) return;
await this.postJSON(`http://${v.host}/state`, { state: "wake" });
const abort = new AbortController();
const interval = setInterval(() => {
this.pushFrame(v, abort).catch(e => {
if (!abort.signal.aborted) this.console.warn(`pushFrame "${v.name}":`, (e as Error).message);
});
}, v.frameIntervalMs);
const timeoutMs = v.idleTimeoutMs > 0 ? v.idleTimeoutMs : DEFAULT_IDLE_TIMEOUT_MS;
const timeout = setTimeout(() => {
this.console.log(`"${v.name}": Scrypted-side stream timeout — stopping`);
this.stopStream(v.name);
}, timeoutMs);
this.streams.set(v.name, { interval, timeout, abort });
}
private stopStream(name: string, sendSleep = true) {
const s = this.streams.get(name);
if (!s) return;
s.abort.abort();
clearInterval(s.interval);
clearTimeout(s.timeout);
this.streams.delete(name);
if (sendSleep) {
const v = this.findByName(name);
if (v?.host) {
this.postJSON(`http://${v.host}/state`, { state: "sleep" }).catch(() => {});
}
}
}
private findByName(name: string): Viewport | undefined {
for (const v of this.viewports.values()) if (v.name === name) return v;
return undefined;
}
private async pushFrame(v: Viewport, abort: AbortController) {
if (abort.signal.aborted) return;
const cam: any = systemManager.getDeviceById(v.cameraId);
if (!cam) return;
const w = v.orientation === "portrait" ? 480 : 800;
const h = v.orientation === "portrait" ? 800 : 480;
const picture = await cam.takePicture({
picture: { width: w, height: h },
reason: "event",
});
const buf: Buffer = await mediaManager.convertMediaObjectToBuffer(picture, "image/jpeg");
const res = await fetch(`http://${v.host}/frame`, {
method: "POST",
headers: { "Content-Type": "image/jpeg" },
body: buf,
signal: abort.signal,
});
if (res.status === 409) {
this.console.log(`"${v.name}" returned 409 — device went to sleep, stopping stream`);
this.stopStream(v.name, /*sendSleep=*/ false);
} else if (res.status === 400) {
const reason = await res.text().catch(() => "");
this.console.warn(`"${v.name}" returned 400: ${reason}`);
} else if (!res.ok) {
this.console.warn(`"${v.name}" /frame -> ${res.status}`);
}
}
// ------------------------------------------------------------------------
// Inbound: device → Scrypted POST /state
// ------------------------------------------------------------------------
async onRequest(request: HttpRequest, response: HttpResponse) {
if (request.method !== "POST") { response.send("", { code: 405 }); return; }
if (!request.url.endsWith("/state")) { response.send("", { code: 404 }); return; }
let body: any;
try { body = JSON.parse(request.body); }
catch { response.send("invalid JSON", { code: 400 }); return; }
const { viewport, state } = body ?? {};
const v = typeof viewport === "string" ? this.findByName(viewport) : undefined;
if (!v) { response.send(`unknown viewport: ${viewport}`, { code: 404 }); return; }
if (state !== "wake" && state !== "sleep") {
response.send("state must be wake or sleep", { code: 400 });
return;
}
this.console.log(`recv "${viewport}" -> ${state} (device-initiated)`);
if (state === "wake") {
await this.startStream(v);
} else {
this.stopStream(v.name, /*sendSleep=*/ false);
}
response.send("", { code: 204 });
}
// ------------------------------------------------------------------------
// Parent Settings — informational only; per-viewport tuning lives on each
// child's own Settings page.
// ------------------------------------------------------------------------
async getSettings(): Promise<Setting[]> {
const count = this.viewports.size;
return [
{
key: "viewport_count",
title: "Registered viewports",
description: "Number of child viewport bindings under this parent. Each one's host / camera / brightness / orientation / fps lives on its own Settings page.",
value: String(count),
readonly: true,
} as any,
{
key: "callback_base",
title: "Callback base URL",
description: "Endpoint the firmware POSTs back to for tap-initiated wake/sleep.",
value: this.scryptedBase || "(not yet resolved)",
readonly: true,
} as any,
];
}
async putSetting(_key: string, _value: SettingValue) {}
// ------------------------------------------------------------------------
// Tiny HTTP helper
// ------------------------------------------------------------------------
private async postJSON(url: string, body: any) {
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(HTTP_TIMEOUT_MS),
});
if (!res.ok && res.status !== 204) {
const text = await res.text().catch(() => "");
throw new Error(`POST ${url} -> ${res.status} ${text}`);
}
}
}
export default ScryptedViewportProvider;
|