Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
More than one camera: the admin lists cameras, viewers pick one or see them side by side #28
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/17-more-than-one-camera-the-admin-lists-cam → main 6 commits fork 3 threads · 3 unresolved
9 files
changed +564
−54
Software/README.md +7
| @@ -192,6 +192,13 @@ again every 2 s and the live picture comes back in the same stream, without | ||
| 192 | 192 | a reload. The camera is independent of the arm server: a camera that is down |
| 193 | 193 | does not put the arm on the simulation, and the other way round. |
| 194 | 194 | |
| 195 | **Cameras are a list the owner edits** on `/admin/camera-list` (name + source, such as | |
| 196 | `<arm host>:8080`), kept in `cameras.json` under `FORGE_APP_DATA`; the first | |
| 197 | start lists the arm host's camera 1. Each is streamed once from its source | |
| 198 | and shared by everyone watching, shows a grey offline frame while its source | |
| 199 | is down (the other cameras keep playing) and reconnects by itself. Viewers | |
| 200 | get a tab per camera and can turn several on for a grid; that choice is | |
| 201 | their own browser's. A camera added or removed shows up within a few seconds. | |
| 195 | 202 | **Egress in `forge.json`.** `forge.json` lists `100.89.95.68:8101` (the arm) |
| 196 | 203 | and `100.89.95.68:8080` (the camera) under `egress`: the destinations the app |
| 197 | 204 | may reach on the Pi. The declaration does nothing until a site admin grants |
Software/so101/bench/fake-serve.ts +5 −1
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | * PORT, HOST and FORGE_APP_DATA override the port, the 127.0.0.1 bind and the temp dir (how the forge runs it). |
| 6 | 6 | * ARM_SERVER=ws://<host>:8101 puts a real arm's `so101 serve` behind the same page: the relay in src/relay.ts |
| 7 | 7 | * sends the arm API, /follow and the cameras there while it answers, and to the simulation when it does not. |
| 8 | * The owner lists the cameras on /admin/camera-list (kept in cameras.json under the data dir); the first one comes from ARM_SERVER. | |
| 8 | 9 | * Anyone may watch; only the signed-in person whose turn it is may drive (src/lobby.ts). The forge signs who that is |
| 9 | 10 | * with FORGE_APP_IDENTITY_KEY. Run on one's own machine, away from the forge and with no key, every visitor is the owner. |
| 10 | 11 | * The owner's settings (pause, turn length, speed cap, announcement) and their log live beside the chat (src/admin.ts). |
| @@ -14,6 +15,7 @@ import { tmpdir } from "node:os"; | ||
| 14 | 15 | import { join } from "node:path"; |
| 15 | 16 | import { FeetechBus } from "../src/bus.ts"; |
| 16 | 17 | import { FakeBus } from "../src/fake-bus.ts"; |
| 18 | import { CameraHub } from "../src/cameras.ts"; | |
| 17 | 19 | import { LeaderCalibrations } from "../src/leader-calibrations.ts"; |
| 18 | 20 | import { Lobby } from "../src/lobby.ts"; |
| 19 | 21 | import { Moderator, modelFromEnv } from "../src/moderator.ts"; |
| @@ -43,7 +45,9 @@ try { | ||
| 43 | 45 | const identityKey = process.env.FORGE_APP_IDENTITY_KEY ?? ""; |
| 44 | 46 | const local = !identityKey && !process.env.FORGE_APP_ENV; |
| 45 | 47 | const lobby = new Lobby({ identityKey, local, chatFile: join(dir, "chat.json"), adminFile: join(dir, "admin.json"), moderator: new Moderator({ file: join(dir, "crew.json"), model: modelFromEnv(process.env) }) }); |
| 48 | const cameras = new CameraHub({ file: join(dir, "cameras.json"), seed: remote ? [{ name: "Camera 1", source: remote.cameras[0]! }] : [] }); | |
| 49 | ||
| 46 | 50 | const leaders = new LeaderCalibrations({ file: join(dir, "leader-calibrations.json") }); |
| 47 | const relay = await startRelay({ host, port, simulation: `http://127.0.0.1:${simulation.port}`, remote, problem, lobby, leaders }); | |
| 51 | const relay = await startRelay({ host, port, simulation: `http://127.0.0.1:${simulation.port}`, remote, problem, lobby, cameras, leaders }); | |
| 48 | 52 | const { mode, host: armHost } = relay.link(); |
| 49 | 53 | console.log(`arm on http://${host}:${relay.port} (${mode === "live" ? `live arm ${armHost}` : "simulated"}; data in ${dir}${local ? "; local: every visitor drives as the owner" : ""})`); |
Software/so101/src/admin-page.ts +1 −1
| @@ -112,7 +112,7 @@ export function renderAdmin(p: AdminPage): string { | ||
| 112 | 112 | "Admin · SO-101", |
| 113 | 113 | `<header> |
| 114 | 114 | <h1>Admin</h1> |
| 115 | <div class="state"><span class="pill${s.paused ? " paused" : ""}">${s.paused ? "Paused" : "Open"}</span><span>${d ? `<b>${esc(d.name)}</b> has control` : "Nobody has control"}</span><span><b>${p.snapshot.queue.length}</b> waiting</span><a href="/">Back to the arm</a></div> | |
| 115 | <div class="state"><span class="pill${s.paused ? " paused" : ""}">${s.paused ? "Paused" : "Open"}</span><span>${d ? `<b>${esc(d.name)}</b> has control` : "Nobody has control"}</span><span><b>${p.snapshot.queue.length}</b> waiting</span><a href="/admin/camera-list">Cameras</a><a href="/">Back to the arm</a></div> | |
| 116 | 116 | </header> |
| 117 | 117 | ${flash} |
| 118 | 118 | <section aria-labelledby="now"><h2 id="now">Right now</h2> |
Software/so101/src/camera-list.html added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | ||
| 2 | <html lang="en"> | |
| 3 | <head> | |
| 4 | <meta charset="utf-8"> | |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| 6 | <title>Admin · cameras</title> | |
| 7 | <style> | |
| 8 | :root { color-scheme: light dark; : #8884; : #888; } | |
| 9 | body { font: 15px/1.5 system-ui, sans-serif; max-width: 42rem; margin: 2rem auto; padding: 0 1rem; } | |
| 10 | li { display: flex; gap: .75rem; align-items: baseline; padding: .4rem 0; border-bottom: 1px solid var(); list-style: none; } | |
| 11 | ul { padding: 0; } li span.src { color: var(); flex: 1; overflow-wrap: anywhere; } | |
| 12 | form { display: flex; gap: .5rem; flex-wrap: wrap; } input, button { font: inherit; padding: .3rem .5rem; } li button { padding: .5rem .9rem; } label { align-self: center; } #msg { color: #c33; min-height: 1.5em; } | |
| 13 | </style> | |
| 14 | </head> | |
| 15 | <body> | |
| 16 | <p><a href="/">← the arm</a> · <a href="/admin">Admin</a></p> | |
| 17 | <h1>Cameras</h1> | |
| 18 | <p>Each camera is streamed once from its source and shared by everyone watching. Viewers see a change within a few seconds.</p> | |
| 19 | <noscript><p>This page needs JavaScript to list and change cameras.</p></noscript> | |
| 20 | <ul id="list"></ul> | |
| 21 | <form id="add"> | |
| 22 | <label for="name">Name</label> | |
| 23 | <input id="name" placeholder="name, e.g. Overhead" maxlength="40" required> | |
| 24 | <label for="source">Source</label> | |
| 25 | <input id="source" placeholder="source, e.g. 100.89.95.68:8080" size="32" required> | |
| 26 | <button>Add camera</button> | |
| 27 | </form> | |
| 28 | <p id="msg" role="status"></p> | |
| 29 | <script> | |
| 30 | const $ = (id) => document.getElementById(id); | |
| 31 | const say = (t) => { $("msg").textContent = t; }; | |
| 32 | async function call(method, path, body) { | |
| 33 | const r = await fetch(path, { method, headers: { "content-type": "application/json" }, body: body && JSON.stringify(body) }); | |
| 34 | const j = await r.json().catch(() => ({})); | |
| 35 | if (!r.ok) throw new Error(r.status === 401 ? "sign in as the owner to change cameras" : j.error || `failed (${r.status})`); | |
| 36 | return j; | |
| 37 | } | |
| 38 | async function load() { | |
| 39 | try { | |
| 40 | const cams = await call("GET", "/admin/cameras"); | |
| 41 | $("list").replaceChildren(...cams.map((c) => { | |
| 42 | const li = document.createElement("li"); | |
| 43 | const name = document.createElement("b"); name.textContent = c.name; | |
| 44 | const src = document.createElement("span"); src.className = "src"; src.textContent = c.source; | |
| 45 | const rm = document.createElement("button"); rm.textContent = "Remove"; rm.setAttribute("aria-label", `Remove ${c.name}`); | |
| 46 | rm.onclick = () => call("DELETE", `/admin/cameras/${c.id}`).then(load, (e) => say(e.message)); | |
| 47 | li.append(name, src, rm); | |
| 48 | return li; | |
| 49 | })); | |
| 50 | say(""); | |
| 51 | } catch (e) { say(e.message); } | |
| 52 | } | |
| 53 | $("add").onsubmit = (e) => { | |
| 54 | e.preventDefault(); | |
| 55 | call("POST", "/admin/cameras", { name: $("name").value, source: $("source").value }).then(() => { $("add").reset(); load(); }, (err) => say(err.message)); | |
| 56 | }; | |
| 57 | load(); | |
| 58 | </script> | |
| 59 | </body> | |
| 60 | </html> |
Software/so101/src/cameras.test.ts added +215
| @@ -0,0 +1,215 @@ | ||
| 1 | import { afterEach, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, readFileSync } from "node:fs"; | |
| 3 | import { createServer, type Server } from "node:http"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { CameraError, CameraHub, parseName, parseSource } from "./cameras.ts"; | |
| 7 | import { sign } from "./identity.ts"; | |
| 8 | import { Lobby } from "./lobby.ts"; | |
| 9 | import { OFFLINE_FRAME } from "./offline-frame.ts"; | |
| 10 | import { startRelay, type RunningRelay } from "./relay.ts"; | |
| 11 | ||
| 12 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); | |
| 13 | const until = async (check: () => boolean | Promise<boolean>, ms = 3000) => { | |
| 14 | for (const end = Date.now() + ms; Date.now() < end; await sleep(20)) if (await check()) return; | |
| 15 | throw new Error("timed out"); | |
| 16 | }; | |
| 17 | const jpeg = (tag: string) => Buffer.concat([Buffer.from([0xff, 0xd8]), Buffer.from(tag), Buffer.from([0xff, 0xd9])]); | |
| 18 | ||
| 19 | /** A µStreamer-like source: one frame at once, then one every 20 ms; counts its connections. */ | |
| 20 | class Source { | |
| 21 | server: Server; | |
| 22 | connections = 0; | |
| 23 | port = 0; | |
| 24 | readonly tag: string; | |
| 25 | constructor(tag: string) { | |
| 26 | this.tag = tag; | |
| 27 | this.server = createServer((req, res) => { | |
| 28 | this.connections++; | |
| 29 | res.writeHead(200, { "content-type": "multipart/x-mixed-replace;boundary=boundarydonotcross" }); | |
| 30 | const send = () => { | |
| 31 | const f = jpeg(this.tag); | |
| 32 | res.write(`--boundarydonotcross\r\nContent-Type: image/jpeg\r\nContent-Length: ${f.length}\r\n\r\n`); | |
| 33 | res.write(f); | |
| 34 | res.write("\r\n"); | |
| 35 | }; | |
| 36 | send(); | |
| 37 | const timer = setInterval(send, 20); | |
| 38 | res.on("close", () => clearInterval(timer)); | |
| 39 | }); | |
| 40 | } | |
| 41 | async start(port = 0) { | |
| 42 | await new Promise<void>((r) => this.server.listen(port, "127.0.0.1", r)); | |
| 43 | this.port = (this.server.address() as { port: number }).port; | |
| 44 | return this; | |
| 45 | } | |
| 46 | stop() { | |
| 47 | this.server.closeAllConnections(); | |
| 48 | return new Promise<void>((r) => this.server.close(() => r())); | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | /** Read a response until the bytes so far hold `needle`. */ | |
| 53 | async function readUntil(reader: { read(): Promise<{ done: boolean; value?: Uint8Array }> }, needle: string | Buffer): Promise<Buffer> { | |
| 54 | let got = Buffer.alloc(0); | |
| 55 | while (!got.includes(needle)) { | |
| 56 | const { done, value } = await reader.read(); | |
| 57 | if (done) break; | |
| 58 | got = Buffer.concat([got, value!]); | |
| 59 | } | |
| 60 | return got; | |
| 61 | } | |
| 62 | ||
| 63 | /** What a camera stream sends until `needle` shows, read through the relay. */ | |
| 64 | async function frameOf(base: string, id: number, needle: string | Buffer): Promise<Buffer> { | |
| 65 | const controller = new AbortController(); | |
| 66 | const res = await fetch(`${base}/camera/${id}/stream`, { signal: controller.signal }); | |
| 67 | const got = await readUntil(res.body!.getReader(), needle); | |
| 68 | controller.abort(); | |
| 69 | return got; | |
| 70 | } | |
| 71 | ||
| 72 | describe("sources and names", () => { | |
| 73 | test("a source is normalised and anything odd is refused", () => { | |
| 74 | expect(parseSource("100.89.95.68:8080")).toBe("http://100.89.95.68:8080/stream"); | |
| 75 | expect(parseSource(" http://cam.local:8081/ ")).toBe("http://cam.local:8081/stream"); | |
| 76 | expect(parseSource("https://cams.example/two/stream")).toBe("https://cams.example/two/stream"); | |
| 77 | for (const bad of ["", " ", 5, "ftp://x", "http://u:p@host", "http://", "ws://host:1"]) expect(() => parseSource(bad)).toThrow(CameraError); | |
| 78 | expect(parseName(" Overhead ")).toBe("Overhead"); | |
| 79 | for (const bad of ["", "x".repeat(41), "a\nb", null]) expect(() => parseName(bad)).toThrow(CameraError); | |
| 80 | }); | |
| 81 | }); | |
| 82 | ||
| 83 | describe("the list", () => { | |
| 84 | test("it is kept in a file, ids are never reused, and a seed fills an empty start", () => { | |
| 85 | const file = join(mkdtempSync(join(tmpdir(), "so101-cams-")), "cameras.json"); | |
| 86 | const hub = new CameraHub({ file, seed: [{ name: "Camera 1", source: "http://a:8080/stream" }] }); | |
| 87 | expect(hub.list().map((c) => c.name)).toEqual(["Camera 1"]); | |
| 88 | const two = hub.add("Side", "b:8081"); | |
| 89 | expect(hub.remove(1)).toBe(true); | |
| 90 | expect(hub.remove(1)).toBe(false); | |
| 91 | const again = new CameraHub({ file, seed: [{ name: "ignored", source: "http://x/stream" }] }); | |
| 92 | expect(again.list()).toEqual([{ ...two, online: null }]); | |
| 93 | expect(again.add("Third", "c:8082").id).toBe(3); | |
| 94 | expect(JSON.parse(readFileSync(file, "utf8")).cameras).toHaveLength(2); | |
| 95 | }); | |
| 96 | ||
| 97 | test("at most eight", () => { | |
| 98 | const hub = new CameraHub(); | |
| 99 | for (let i = 0; i < 8; i++) hub.add(`c${i}`, `h${i}:1`); | |
| 100 | expect(() => hub.add("one more", "h:9")).toThrow(/at most 8/); | |
| 101 | }); | |
| 102 | }); | |
| 103 | ||
| 104 | describe("through the relay", () => { | |
| 105 | let relay: RunningRelay | undefined; | |
| 106 | let hub: CameraHub | undefined; | |
| 107 | const sources: Source[] = []; | |
| 108 | afterEach(async () => { | |
| 109 | await Promise.race([relay?.close(), sleep(500)]); // Bun can keep a closed server's connection open (see relay.test.ts) | |
| 110 | hub?.close(); | |
| 111 | for (const s of sources.splice(0)) await s.stop(); | |
| 112 | relay = hub = undefined; | |
| 113 | }); | |
| 114 | const setup = async () => { | |
| 115 | hub = new CameraHub({ feed: { retryMs: 50, offlineEveryMs: 50, lingerMs: 50 } }); | |
| 116 | relay = await startRelay({ port: 0, simulation: "http://127.0.0.1:1", cameras: hub }); | |
| 117 | return `http://127.0.0.1:${relay.port}`; | |
| 118 | }; | |
| 119 | const owner = { "content-type": "application/json" }; | |
| 120 | ||
| 121 | test("two sources both stream; one upstream connection serves any number of viewers", async () => { | |
| 122 | const one = await new Source("AAAA").start(); | |
| 123 | const two = await new Source("BBBB").start(); | |
| 124 | sources.push(one, two); | |
| 125 | const base = await setup(); | |
| 126 | hub!.add("One", `127.0.0.1:${one.port}`); | |
| 127 | hub!.add("Two", `127.0.0.1:${two.port}`); | |
| 128 | expect((await frameOf(base, 1, "AAAA")).includes("AAAA")).toBe(true); | |
| 129 | expect((await frameOf(base, 2, "BBBB")).includes("BBBB")).toBe(true); | |
| 130 | ||
| 131 | expect(one.connections).toBe(1); // the first viewer's, closed again | |
| 132 | const three = await new Source("CCCC").start(); | |
| 133 | sources.push(three); | |
| 134 | hub!.add("Three", `127.0.0.1:${three.port}`); | |
| 135 | const watchers = await Promise.all([1, 2, 3].map(() => fetch(`${base}/camera/3/stream`))); | |
| 136 | expect(watchers[0]!.headers.get("content-type")).toBe("multipart/x-mixed-replace;boundary=armframe"); | |
| 137 | const readers = watchers.map((w) => w.body!.getReader()); | |
| 138 | await Promise.all(readers.map((r) => readUntil(r, "CCCC"))); | |
| 139 | expect(three.connections).toBe(1); // three viewers, one upstream | |
| 140 | await Promise.all(readers.map((r) => r.cancel())); | |
| 141 | }); | |
| 142 | ||
| 143 | test("a camera that is down shows the offline frame while the other plays, and comes back", async () => { | |
| 144 | const live = await new Source("LIVE").start(); | |
| 145 | const dying = await new Source("DEAD").start(); | |
| 146 | sources.push(live); | |
| 147 | const base = await setup(); | |
| 148 | hub!.add("Live", `127.0.0.1:${live.port}`); | |
| 149 | hub!.add("Dying", `127.0.0.1:${dying.port}`); | |
| 150 | expect((await frameOf(base, 2, "DEAD")).includes("DEAD")).toBe(true); | |
| 151 | const port = dying.port; | |
| 152 | await dying.stop(); | |
| 153 | ||
| 154 | const watcher = await fetch(`${base}/camera/2/stream`); | |
| 155 | const reader = watcher.body!.getReader(); | |
| 156 | expect((await readUntil(reader, OFFLINE_FRAME)).includes(OFFLINE_FRAME)).toBe(true); | |
| 157 | expect((await frameOf(base, 1, "LIVE")).includes("LIVE")).toBe(true); | |
| 158 | await until(async () => ((await (await fetch(`${base}/cameras`)).json()) as { online: boolean }[]).map((c) => c.online).includes(false)); | |
| 159 | expect(await (await fetch(`${base}/camera/2/state`)).json()).toEqual({ ok: false }); | |
| 160 | ||
| 161 | const back = await new Source("BACK").start(port); // the same address answers again | |
| 162 | sources.push(back); | |
| 163 | expect((await readUntil(reader, "BACK")).includes("BACK")).toBe(true); | |
| 164 | await reader.cancel(); | |
| 165 | }); | |
| 166 | ||
| 167 | test("a camera added or removed shows at once, without a restart; a removed one's viewers are let go", async () => { | |
| 168 | const one = await new Source("ONE1").start(); | |
| 169 | sources.push(one); | |
| 170 | const base = await setup(); | |
| 171 | expect(await (await fetch(`${base}/cameras`)).json()).toEqual([]); | |
| 172 | expect((await fetch(`${base}/camera/1/stream`)).status).toBe(404); | |
| 173 | ||
| 174 | const added = await fetch(`${base}/admin/cameras`, { method: "POST", headers: owner, body: JSON.stringify({ name: "Desk <b>", source: `127.0.0.1:${one.port}` }) }); | |
| 175 | expect(added.status).toBe(201); | |
| 176 | expect(await (await fetch(`${base}/cameras`)).json()).toEqual([{ id: 1, name: "Desk <b>", online: null }]); | |
| 177 | const watcher = await fetch(`${base}/camera/1/stream`); | |
| 178 | const reader = watcher.body!.getReader(); | |
| 179 | await reader.read(); | |
| 180 | expect((await fetch(`${base}/admin/cameras/1`, { method: "DELETE" })).status).toBe(200); | |
| 181 | await until(async () => (await reader.read()).done === true); // the viewer's stream ends | |
| 182 | expect(await (await fetch(`${base}/cameras`)).json()).toEqual([]); | |
| 183 | expect((await fetch(`${base}/camera/1/snapshot`)).status).toBe(404); | |
| 184 | }); | |
| 185 | ||
| 186 | test("bad input is refused at the boundary", async () => { | |
| 187 | const base = await setup(); | |
| 188 | const post = (body: string) => fetch(`${base}/admin/cameras`, { method: "POST", headers: owner, body }); | |
| 189 | expect((await post("{nope")).status).toBe(400); | |
| 190 | expect((await post(JSON.stringify({ name: "", source: "h:1" }))).status).toBe(400); | |
| 191 | expect((await post(JSON.stringify({ name: "x", source: "file:///etc/passwd" }))).status).toBe(400); | |
| 192 | expect((await fetch(`${base}/admin/cameras/99`, { method: "DELETE" })).status).toBe(404); | |
| 193 | expect((await fetch(`${base}/admin/cameras`, { method: "POST", headers: { ...owner, "sec-fetch-site": "cross-site" }, body: "{}" })).status).toBe(403); | |
| 194 | expect((await fetch(`${base}/admin/camera-list`)).headers.get("content-type")).toContain("text/html"); | |
| 195 | }); | |
| 196 | ||
| 197 | test("only the owner changes the list", async () => { | |
| 198 | hub = new CameraHub(); | |
| 199 | const key = "k".repeat(32); | |
| 200 | const lobby = new Lobby({ identityKey: key }); | |
| 201 | relay = await startRelay({ port: 0, simulation: "http://127.0.0.1:1", cameras: hub, lobby }); | |
| 202 | const base = `http://127.0.0.1:${relay.port}`; | |
| 203 | const as = (identity: string | null) => { | |
| 204 | const headers: Record<string, string> = { ...owner }; | |
| 205 | if (identity) Object.assign(headers, { "x-forge-identity": identity, "x-forge-signature": sign(identity, key) }); | |
| 206 | return fetch(`${base}/admin/cameras`, { method: "POST", headers, body: JSON.stringify({ name: "x", source: "h:1" }) }); | |
| 207 | }; | |
| 208 | const later = Date.now() + 60_000; | |
| 209 | expect((await as(null)).status).toBe(401); | |
| 210 | expect((await as(`u1|alice|user|${later}`)).status).toBe(403); | |
| 211 | expect(hub.list()).toEqual([]); | |
| 212 | expect((await as(`u2|trevato|admin|${later}`)).status).toBe(201); | |
| 213 | lobby.close(); | |
| 214 | }); | |
| 215 | }); |
Software/so101/src/cameras.ts added +143
| @@ -0,0 +1,143 @@ | ||
| 1 | /** | |
| 2 | * The cameras the owner has listed: a name and a source each, kept in a file, changed live. | |
| 3 | * | |
| 4 | * Each camera is proxied the same way, by a CameraFeed (src/camera-feed.ts): one upstream MJPEG connection, opened | |
| 5 | * while someone watches and shared by everyone who does, so the camera's machine serves one stream however many tabs | |
| 6 | * are open; when the upstream is down every viewer gets the offline picture until it is back. | |
| 7 | */ | |
| 8 | ||
| 9 | import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 10 | import { dirname } from "node:path"; | |
| 11 | import { CameraFeed, type FeedOptions } from "./camera-feed.ts"; | |
| 12 | ||
| 13 | export interface CameraSpec { | |
| 14 | id: number; | |
| 15 | name: string; | |
| 16 | /** The stream's URL, normalised: scheme, host, port, path. */ | |
| 17 | source: string; | |
| 18 | } | |
| 19 | ||
| 20 | export interface CameraInfo extends CameraSpec { | |
| 21 | /** true while frames arrive, false when the source is down, null when nobody is watching so it is not being tried. */ | |
| 22 | online: boolean | null; | |
| 23 | } | |
| 24 | ||
| 25 | export class CameraError extends Error {} | |
| 26 | ||
| 27 | export const MAX_CAMERAS = 8; | |
| 28 | ||
| 29 | /** "host:8080", "http://host:8080/" or a full stream URL → the stream URL. Throws a CameraError saying what is wrong. */ | |
| 30 | export function parseSource(text: unknown): string { | |
| 31 | if (typeof text !== "string" || !text.trim()) throw new CameraError("a camera needs a source, like 192.168.1.5:8080"); | |
| 32 | const raw = text.trim(); | |
| 33 | let url: URL; | |
| 34 | try { | |
| 35 | url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `http://${raw}`); | |
| 36 | } catch { | |
| 37 | throw new CameraError(`"${raw.slice(0, 80)}" is not a URL`); | |
| 38 | } | |
| 39 | if (url.protocol !== "http:" && url.protocol !== "https:") throw new CameraError("a camera source must be http:// or https://"); | |
| 40 | if (url.username || url.password || url.hash) throw new CameraError("a camera source takes a scheme, a host, a port and a path, nothing else"); | |
| 41 | return url.pathname === "/" && !url.search ? `${url.origin}/stream` : url.href; | |
| 42 | } | |
| 43 | ||
| 44 | export function parseName(text: unknown): string { | |
| 45 | const name = typeof text === "string" ? text.trim() : ""; | |
| 46 | if (!name || name.length > 40 || /[\u0000-\u001f\u007f]/.test(name)) throw new CameraError("a camera's name is 1 to 40 characters"); | |
| 47 | return name; | |
| 48 | } | |
| 49 | ||
| 50 | export interface HubOptions { | |
| 51 | /** The list's file; none keeps it in memory. */ | |
| 52 | file?: string; | |
| 53 | /** What the list holds when there is no file yet. */ | |
| 54 | seed?: { name: string; source: string }[]; | |
| 55 | /** Timing of each camera's feed. */ | |
| 56 | feed?: FeedOptions; | |
| 57 | } | |
| 58 | ||
| 59 | export class CameraHub { | |
| 60 | private specs: CameraSpec[] = []; | |
| 61 | private next = 1; | |
| 62 | private readonly feeds = new Map<number, CameraFeed>(); | |
| 63 | private readonly file?: string; | |
| 64 | private readonly feedOptions: FeedOptions; | |
| 65 | ||
| 66 | constructor(options: HubOptions = {}) { | |
| 67 | this.file = options.file; | |
| 68 | this.feedOptions = options.feed ?? {}; | |
| 69 | if (this.file && existsSync(this.file)) { | |
| 70 | try { | |
| 71 | const saved = JSON.parse(readFileSync(this.file, "utf8")) as { next?: number; cameras?: CameraSpec[] }; | |
| 72 | this.specs = (saved.cameras ?? []).filter((c) => Number.isSafeInteger(c.id) && typeof c.name === "string" && typeof c.source === "string"); | |
| 73 | this.next = Math.max(saved.next ?? 1, ...this.specs.map((c) => c.id + 1)); | |
| 74 | } catch (err) { | |
| 75 | console.error(`cameras: ${this.file} unreadable (${(err as Error).message}); starting from the default`); | |
| 76 | this.seed(options.seed); | |
| 77 | } | |
| 78 | } else this.seed(options.seed); | |
| 79 | } | |
| 80 | ||
| 81 | private seed(seed: HubOptions["seed"]): void { | |
| 82 | this.specs = []; | |
| 83 | this.next = 1; | |
| 84 | for (const s of seed ?? []) this.specs.push({ id: this.next++, name: s.name, source: s.source }); | |
| 85 | this.save(); | |
| 86 | } | |
| 87 | ||
| 88 | list(): CameraInfo[] { | |
| 89 | return this.specs.map((c) => { | |
| 90 | const state = this.feeds.get(c.id)?.state; | |
| 91 | return { ...c, online: state === "live" ? true : state === "offline" ? false : null }; | |
| 92 | }); | |
| 93 | } | |
| 94 | ||
| 95 | has(id: number): boolean { | |
| 96 | return this.specs.some((c) => c.id === id); | |
| 97 | } | |
| 98 | ||
| 99 | add(name: unknown, source: unknown): CameraSpec { | |
| 100 | if (this.specs.length >= MAX_CAMERAS) throw new CameraError(`at most ${MAX_CAMERAS} cameras`); | |
| 101 | const spec = { id: this.next++, name: parseName(name), source: parseSource(source) }; | |
| 102 | this.specs.push(spec); | |
| 103 | this.save(); | |
| 104 | return spec; | |
| 105 | } | |
| 106 | ||
| 107 | remove(id: number): boolean { | |
| 108 | const at = this.specs.findIndex((c) => c.id === id); | |
| 109 | if (at < 0) return false; | |
| 110 | this.specs.splice(at, 1); | |
| 111 | this.feeds.get(id)?.close(); | |
| 112 | this.feeds.delete(id); | |
| 113 | this.save(); | |
| 114 | return true; | |
| 115 | } | |
| 116 | ||
| 117 | /** The shared feed of a listed camera (opened on first use); null for an unknown camera. Removing the camera closes it. */ | |
| 118 | feed(id: number): CameraFeed | null { | |
| 119 | const spec = this.specs.find((c) => c.id === id); | |
| 120 | if (!spec) return null; | |
| 121 | let feed = this.feeds.get(id); | |
| 122 | if (!feed) this.feeds.set(id, (feed = new CameraFeed(spec.source, spec.name, this.feedOptions))); | |
| 123 | return feed; | |
| 124 | } | |
| 125 | ||
| 126 | /** A listed camera's source URL, or null when unknown. */ | |
| 127 | source(id: number): string | null { | |
| 128 | return this.specs.find((c) => c.id === id)?.source ?? null; | |
| 129 | } | |
| 130 | ||
| 131 | close(): void { | |
| 132 | for (const feed of this.feeds.values()) feed.close(); | |
| 133 | this.feeds.clear(); | |
| 134 | } | |
| 135 | ||
| 136 | private save(): void { | |
| 137 | if (!this.file) return; | |
| 138 | mkdirSync(dirname(this.file), { recursive: true }); | |
| 139 | const tmp = `${this.file}.tmp`; | |
| 140 | writeFileSync(tmp, JSON.stringify({ next: this.next, cameras: this.specs })); | |
| 141 | renameSync(tmp, this.file); | |
| 142 | } | |
| 143 | } |
Software/so101/src/relay.test.ts +3 −4
| @@ -122,10 +122,9 @@ describe("relay", () => { | ||
| 122 | 122 | expect(await page.text()).toContain("<title>SO-101</title>"); |
| 123 | 123 | expect(page.headers.get("x-content-type-options")).toBe("nosniff"); |
| 124 | 124 | expect(page.headers.get("content-security-policy")).toContain("frame-ancestors 'none'"); |
| 125 | // No camera to show: the offline picture, at once, to anyone. | |
| 126 | const shown = await cameraShows(`${base}/camera/1/stream`, PNG); | |
| 127 | expect(shown.type).toBe("multipart/x-mixed-replace;boundary=armframe"); | |
| 128 | expect(shown.ms).toBeLessThan(1000); | |
| 125 | // No camera is listed while the arm is simulated: the owner adds them on /admin, until then there is nothing to show. | |
| 126 | expect(await (await fetch(`${base}/cameras`)).json()).toEqual([]); | |
| 127 | expect((await fetch(`${base}/camera/1/stream`)).status).toBe(404); | |
| 129 | 128 | expect((await fetch(`${base}/camera/1/state`)).status).toBe(404); |
| 130 | 129 | }); |
| 131 | 130 |
Software/so101/src/relay.ts +52 −12
| @@ -15,13 +15,16 @@ | ||
| 15 | 15 | * WS /live the lobby: the turn, the queue, the chat |
| 16 | 16 | * GET /admin, POST /admin the owner's page: pause, end a turn, take control, the settings, the log |
| 17 | 17 | * GET /link {"mode": "live"|"simulated", "host": "<host:port>"|null, "problem": "…"|null} |
| 18 | * GET /camera/<1|2>/stream the arm machine's camera, one upstream connection shared by every viewer | |
| 19 | * (src/camera-feed.ts); the "camera offline" picture when there is none or it is down | |
| 20 | * GET /camera/<1|2>/<state|snapshot> passed through to the camera (µStreamer on :8080 and :8081 by default) | |
| 18 | * GET /cameras the listed cameras: [{"id", "name", "online"}] | |
| 19 | * GET /camera/<id>/<stream|state|snapshot> one listed camera, one upstream connection shared by all its viewers (src/cameras.ts, src/camera-feed.ts); | |
| 20 | * the "camera offline" picture when its source is down | |
| 21 | * GET /admin/camera-list the owner's page for the camera list | |
| 22 | * POST /admin/cameras {"name", "source"}, DELETE /admin/cameras/<id> the owner changes the list; viewers see it within seconds | |
| 21 | 23 | * GET|PUT|DELETE /leader/calibration the signed-in person's own leader calibration (src/leader-map.js), any time, |
| 22 | 24 | * turn or no turn: GET answers it or null, PUT {"joints": {...}} stores it |
| 23 | 25 | */ |
| 24 | 26 | |
| 27 | import { readFileSync } from "node:fs"; | |
| 25 | 28 | import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; |
| 26 | 29 | import type { Duplex } from "node:stream"; |
| 27 | 30 | import { Readable } from "node:stream"; |
| @@ -29,7 +32,8 @@ import { pipeline } from "node:stream/promises"; | ||
| 29 | 32 | import WebSocket, { WebSocketServer } from "ws"; |
| 30 | 33 | import { AdminError, changeFromForm } from "./admin.ts"; |
| 31 | 34 | import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts"; |
| 32 | import { CameraFeed, type FeedOptions } from "./camera-feed.ts"; | |
| 35 | import type { FeedOptions } from "./camera-feed.ts"; | |
| 36 | import { CameraError, CameraHub } from "./cameras.ts"; | |
| 33 | 37 | import { LOCAL as LOCAL_PERSON } from "./identity.ts"; |
| 34 | 38 | import { LeaderCalibrations } from "./leader-calibrations.ts"; |
| 35 | 39 | import { CalibrationError } from "./leader-map.js"; |
| @@ -61,6 +65,8 @@ export interface RelayOptions { | ||
| 61 | 65 | /** The simulated arm's HTTP base, e.g. http://127.0.0.1:41234. */ |
| 62 | 66 | simulation: string; |
| 63 | 67 | remote?: Remote | null; |
| 68 | /** The camera list; by default in memory, seeded with the first camera of `remote`. */ | |
| 69 | cameras?: CameraHub; | |
| 64 | 70 | /** A setting that could not be used (shown on /link while simulating). */ |
| 65 | 71 | problem?: string | null; |
| 66 | 72 | /** How long the arm server has to answer /status before it counts as unreachable. */ |
| @@ -85,11 +91,13 @@ export interface RunningRelay { | ||
| 85 | 91 | |
| 86 | 92 | const SCHEMES: Record<string, "http:" | "https:"> = { "http:": "http:", "https:": "https:", "ws:": "http:", "wss:": "https:" }; |
| 87 | 93 | const MAX_BODY = 1_000_000; |
| 94 | const CAMERA = /^\/camera\/(\d{1,9})\/(stream|state|snapshot)$/; | |
| 95 | const ADMIN_CAMERA = /^\/admin\/cameras(?:\/(\d{1,9}))?$/; | |
| 96 | const CAMERA_PAGE = readFileSync(new URL("./camera-list.html", import.meta.url), "utf8"); | |
| 88 | 97 | const MAX_FORM = 16_384; |
| 89 | 98 | /** Served from this process whichever arm is live: they describe the app, not the arm. */ |
| 90 | 99 | const LOCAL = /^\/(?:index\.html|leader-map\.js)?$|^\/(?:model|vendor|printables|app)(?:\/|$)/; |
| 91 | 100 | const LEADER = "/leader/calibration"; |
| 92 | const CAMERA = /^\/camera\/([12])\/(stream|state|snapshot)$/; | |
| 93 | 101 | const FLOOR = { |
| 94 | 102 | "x-content-type-options": "nosniff", |
| 95 | 103 | "referrer-policy": "same-origin", |
| @@ -157,7 +165,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 157 | 165 | const probeEveryMs = options.probeEveryMs ?? 5000; |
| 158 | 166 | const readTimeoutMs = options.readTimeoutMs ?? 2500; |
| 159 | 167 | const lobby = options.lobby ?? null; |
| 160 | const feeds = [1, 2].map((n) => new CameraFeed(remote?.cameras[n - 1] ?? null, `camera ${n}`, options.camera)); | |
| 168 | const cameras = options.cameras ?? new CameraHub({ feed: options.camera, seed: remote ? [{ name: "Camera 1", source: remote.cameras[0]! }] : [] }); | |
| 161 | 169 | const leaders = options.leaders ?? new LeaderCalibrations(); |
| 162 | 170 | |
| 163 | 171 | // ------------------------------------------------------------------ the link |
| @@ -252,6 +260,29 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 252 | 260 | } |
| 253 | 261 | }; |
| 254 | 262 | |
| 263 | /** The owner's changes to the camera list. */ | |
| 264 | const administer = async (req: IncomingMessage, res: ServerResponse, id: number | null) => { | |
| 265 | const who = lobby ? lobby.who(req) : null; | |
| 266 | if (lobby && !who?.owner) return json(res, who ? 403 : 401, { error: "only the owner may change the cameras" }); | |
| 267 | if (req.method === "GET" && id === null) return json(res, 200, cameras.list()); | |
| 268 | if (req.method === "POST" && id === null) { | |
| 269 | let input: { name?: unknown; source?: unknown }; | |
| 270 | try { | |
| 271 | input = JSON.parse((await readBody(req)).toString("utf8") || "{}"); | |
| 272 | } catch { | |
| 273 | return json(res, 400, { error: "send JSON: {\"name\", \"source\"}" }); | |
| 274 | } | |
| 275 | try { | |
| 276 | return json(res, 201, cameras.add(input.name, input.source)); | |
| 277 | } catch (err) { | |
| 278 | if (err instanceof CameraError) return json(res, 400, { error: err.message }); | |
| 279 | throw err; | |
| 280 | } | |
| 281 | } | |
| 282 | if (req.method === "DELETE" && id !== null) return cameras.remove(id) ? json(res, 200, { ok: true }) : json(res, 404, { error: "no such camera" }); | |
| 283 | return json(res, 405, { error: "GET or POST /admin/cameras, DELETE /admin/cameras/<id>" }); | |
| 284 | }; | |
| 285 | ||
| 255 | 286 | /** The owner's page. Anyone else is told so; a change is a form post, answered with a redirect back. */ |
| 256 | 287 | const admin = async (lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL) => { |
| 257 | 288 | const who = lobby.who(req); |
| @@ -341,6 +372,13 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 341 | 372 | const path = url.pathname + url.search; |
| 342 | 373 | const reading = req.method === "GET" || req.method === "HEAD"; |
| 343 | 374 | if (!reading && !sameOrigin(req)) return json(res, 403, { error: "only this page may change the arm" }); |
| 375 | if (req.method === "GET" && url.pathname === "/cameras") return json(res, 200, cameras.list().map(({ id, name, online }) => ({ id, name, online }))); | |
| 376 | if (req.method === "GET" && url.pathname === "/admin/camera-list") { | |
| 377 | res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" }); | |
| 378 | return res.end(CAMERA_PAGE); | |
| 379 | } | |
| 380 | const cameraAdmin = ADMIN_CAMERA.exec(url.pathname); | |
| 381 | if (cameraAdmin) return await administer(req, res, cameraAdmin[1] === undefined ? null : Number(cameraAdmin[1])); | |
| 344 | 382 | if (lobby && url.pathname === "/admin") return await admin(lobby, req, res, url); |
| 345 | 383 | if (url.pathname === LEADER) return await leaderCalibration(req, res); |
| 346 | 384 | if (!reading && lobby) { |
| @@ -354,14 +392,16 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 354 | 392 | const camera = CAMERA.exec(url.pathname); |
| 355 | 393 | if (camera) { |
| 356 | 394 | if (!reading) return json(res, 405, { error: "cameras are read-only" }); |
| 357 | if (camera[2] === "stream") return feeds[Number(camera[1]) - 1]!.serve(req, res); | |
| 358 | if (!remote) return json(res, 404, { error: "no camera: the arm is simulated" }); | |
| 359 | const stream = remote.cameras[Number(camera[1]) - 1]!; | |
| 360 | const target = stream.replace(/\/[^/]*$/, `/${camera[2]}`) + url.search; | |
| 395 | const id = Number(camera[1]); | |
| 396 | const source = cameras.source(id); | |
| 397 | if (source === null) return json(res, 404, { error: "no such camera" }); | |
| 398 | if (camera[2] === "stream") return cameras.feed(id)!.serve(req, res); | |
| 399 | if (camera[2] === "state") return json(res, 200, { ok: cameras.list().find((c) => c.id === id)?.online ?? null }); | |
| 400 | const target = source.replace(/\/[^/]*$/, "/snapshot") + url.search; | |
| 361 | 401 | try { |
| 362 | 402 | return await pass(res, await ask(target, req, undefined, readTimeoutMs, true)); |
| 363 | 403 | } catch (err) { |
| 364 | return json(res, 502, { error: `camera ${camera[1]} did not answer: ${reason(err)}` }); | |
| 404 | return json(res, 502, { error: `camera ${id} did not answer: ${reason(err)}` }); | |
| 365 | 405 | } |
| 366 | 406 | } |
| 367 | 407 | |
| @@ -515,7 +555,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 515 | 555 | new Promise<void>((resolve) => { |
| 516 | 556 | clearInterval(timer); |
| 517 | 557 | turnEnded?.(); |
| 518 | for (const feed of feeds) feed.close(); | |
| 558 | if (!options.cameras) cameras.close(); | |
| 519 | 559 | for (const c of inFlight) c.abort(); |
| 520 | 560 | for (const client of wss.clients) client.terminate(); |
| 521 | 561 | wss.close(); |
Software/so101/src/viewer.html +78 −36
| @@ -27,7 +27,8 @@ | ||
| 27 | 27 | urdf-viewer { display: block; width: 100%; height: 100%; } |
| 28 | 28 | @media (max-aspect-ratio: 1/1) { #stage { flex-direction: column; } } |
| 29 | 29 | #top { position: absolute; top: 8px; right: 10px; z-index: 5; display: flex; gap: 8px; align-items: center; font-size: 13px; background: rgba(0,0,0,.65); padding: 5px 6px 5px 10px; border-radius: 8px; flex-wrap: wrap; justify-content: flex-end; } |
| 30 | #top .seg button { padding: 5px 9px; min-height: 30px; } #top .seg button.on { background: var(); border-color: var(); } | |
| 30 | #top .seg { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; } @media (max-width: 480px) { #top { left: 10px; } } | |
| 31 | #top .seg button { padding: 5px 9px; min-height: 30px; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } #top .seg button.on { background: var(); border-color: var(); } | |
| 31 | 32 | #link { color: var(); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: min(46ch, 60vw); } #link.on { color: var(); } #link.warn { color: var(); } |
| 32 | 33 | .dot { width: 8px; height: 8px; border-radius: 50%; background: var(); display: inline-block; } .dot.on { background: var(); } |
| 33 | 34 | #banner { position: absolute; top: 54px; left: 50%; transform: translateX(-50%); z-index: 5; max-width: min(640px, 90vw); padding: 8px 14px; border-radius: 8px; font-size: 13px; background: rgba(0,0,0,.75); border: 1px solid var(); display: none; } |
| @@ -128,12 +129,10 @@ | ||
| 128 | 129 | <div id="shell"> |
| 129 | 130 | <div id="app"> |
| 130 | 131 | <div id="stage"> |
| 131 | <div class="pane on" id="pane1"><img id="cam1" alt="camera 1"><span class="tag" id="tag1">camera 1</span></div> | |
| 132 | <div class="pane" id="pane2"><img id="cam2" alt="camera 2"><span class="tag" id="tag2">camera 2</span></div> | |
| 133 | 132 | <div class="pane on" id="pane3"><urdf-viewer id="model" up="Z" ignore-limits display-shadow ambient-color="#b9c0c8"></urdf-viewer><span class="tag" id="tag3">3D · drag a joint to move the arm · drag empty space to orbit</span></div> |
| 134 | 133 | </div> |
| 135 | 134 | <div id="top"> |
| 136 | <span class="seg"><button id="v1" class="on">Cam 1</button><button id="v2" hidden>Cam 2</button><button id="v3" class="on">3D</button></span> | |
| 135 | <span class="seg"><button id="v3" class="on">3D</button></span> | |
| 137 | 136 | <span id="link" hidden></span><span id="live"></span><span class="dot" id="dot"></span><span id="state">…</span> |
| 138 | 137 | <button id="zoneBadge" title="safe zone settings">zone</button> |
| 139 | 138 | <button id="stop" class="danger" hidden title="space">Stop</button><button id="fs" title="F">Fullscreen</button> |
| @@ -328,20 +327,71 @@ | ||
| 328 | 327 | } |
| 329 | 328 | |
| 330 | 329 | // ---------------------------------------------------------------- cameras |
| 330 | // The owner lists the cameras (/admin/camera-list); this page polls the list, so a camera added or removed shows within seconds. Which are | |
| 331 | // shown is this viewer's own choice (tabs: pick one, or turn several on for a grid), kept in this browser and nobody else's. | |
| 331 | 332 | // Plain <img> streams work in every browser; a canvas comparison once a second notices a frozen picture and reconnects. |
| 332 | const camUrl = (n) => q.get(`stream${n}`) || q.get(n === 1 ? "stream" : "") || (relayed ? `/camera/${n}/stream` : `${location.protocol}//${location.hostname}:${8079 + n}/stream`); | |
| 333 | const cams = [1, 2].map((n) => ({ n, img: $(`cam${n}`), url: camUrl(n), sig: "", unchanged: 0, live: false })); | |
| 333 | const cams = []; | |
| 334 | const direct = (n) => q.get(`stream${n}`) || q.get(n === 1 ? "stream" : "") || `${location.protocol}//${location.hostname}:${8079 + n}/stream`; | |
| 334 | 335 | const canvas = document.createElement("canvas"); canvas.width = 32; canvas.height = 18; |
| 335 | 336 | const ctx = canvas.getContext("2d", { willReadFrequently: true }); |
| 336 | 337 | const fresh = (url) => url + (url.includes("?") ? "&" : "?") + Date.now(); |
| 337 | for (const c of cams) { | |
| 338 | c.img.crossOrigin = "anonymous"; | |
| 339 | c.img.onerror = () => { if ($(`pane${c.n}`).classList.contains("on")) setTimeout(() => (c.img.src = fresh(c.url)), 3000); }; | |
| 338 | const saved = (() => { try { const s = JSON.parse(localStorage.getItem("so101.panes") || "null"); return Array.isArray(s) ? s : null; } catch { return null; } })(); | |
| 339 | let chosen = saved ? new Set(saved) : null; // pane keys: "cam:<id>" and "pane3"; null until the viewer has chosen | |
| 340 | const keep = () => { try { localStorage.setItem("so101.panes", JSON.stringify([...document.querySelectorAll(".pane.on")].map((p) => p.dataset.key))); } catch {} }; | |
| 341 | const shown = () => document.querySelectorAll(".pane.on").length; | |
| 342 | const showCam = (c, on) => { c.pane.classList.toggle("on", on); c.btn.classList.toggle("on", on); c.btn.setAttribute("aria-pressed", String(on)); c.img.src = on ? fresh(c.url) : ""; }; | |
| 343 | function addCam(info) { | |
| 344 | const c = { id: info.id, url: info.url, sig: "", unchanged: 0, live: false, online: info.online }; | |
| 345 | c.img = document.createElement("img"); c.img.alt = info.name; c.img.crossOrigin = "anonymous"; | |
| 346 | c.tag = document.createElement("span"); c.tag.className = "tag"; | |
| 347 | c.pane = document.createElement("div"); c.pane.className = "pane"; c.pane.dataset.key = `cam:${info.id}`; | |
| 348 | c.pane.append(c.img, c.tag); | |
| 349 | c.btn = document.createElement("button"); | |
| 350 | c.img.onerror = () => { if (c.pane.classList.contains("on")) setTimeout(() => { if (c.pane.classList.contains("on")) c.img.src = fresh(c.url); }, 3000); }; | |
| 351 | c.img.onclick = () => { $("hud").hidden = !$("hud").hidden; }; | |
| 352 | c.btn.onclick = () => { | |
| 353 | const on = !c.pane.classList.contains("on"); | |
| 354 | if (!on && shown() === 1) return; // keep one pane | |
| 355 | showCam(c, on); keep(); viewer.recenter?.(); | |
| 356 | }; | |
| 357 | $("stage").insertBefore(c.pane, $("pane3")); | |
| 358 | $("top").querySelector(".seg").insertBefore(c.btn, $("v3")); | |
| 359 | cams.push(c); | |
| 360 | return c; | |
| 361 | } | |
| 362 | function removeCam(c) { | |
| 363 | c.img.src = ""; c.pane.remove(); c.btn.remove(); | |
| 364 | cams.splice(cams.indexOf(c), 1); | |
| 365 | if (!shown() && cams[0]) showCam(cams[0], true); | |
| 366 | } | |
| 367 | function label(c, info) { | |
| 368 | c.online = info.online; | |
| 369 | c.tag.textContent = info.online === false ? `${info.name} · offline` : info.name; | |
| 370 | c.btn.textContent = info.name; c.btn.title = info.online === false ? `${info.name}: offline` : info.name; | |
| 371 | c.img.alt = info.name; | |
| 372 | } | |
| 373 | function reconcile(list) { | |
| 374 | for (const c of [...cams]) if (!list.some((i) => i.id === c.id)) removeCam(c); | |
| 375 | for (const info of list) { | |
| 376 | let c = cams.find((x) => x.id === info.id); | |
| 377 | if (!c) { | |
| 378 | c = addCam({ ...info, url: relayed ? `/camera/${info.id}/stream` : info.url }); | |
| 379 | showCam(c, chosen ? chosen.has(c.pane.dataset.key) : cams.length === 1); // the first camera by default; a saved choice otherwise | |
| 380 | } | |
| 381 | label(c, info); | |
| 382 | } | |
| 383 | if (!shown() && cams[0]) showCam(cams[0], true); | |
| 384 | } | |
| 385 | async function pollCameras() { | |
| 386 | try { | |
| 387 | const r = await fetch("/cameras", { cache: "no-store" }); | |
| 388 | if (r.ok) reconcile(await r.json()); | |
| 389 | } catch { /* the next poll tries again */ } | |
| 390 | setTimeout(pollCameras, 3000); | |
| 340 | 391 | } |
| 341 | const showCam = (c, on) => { $(`pane${c.n}`).classList.toggle("on", on); c.img.src = on ? fresh(c.url) : ""; }; | |
| 342 | 392 | setInterval(() => { |
| 343 | 393 | for (const c of cams) { |
| 344 | if (!$(`pane${c.n}`).classList.contains("on")) continue; | |
| 394 | if (!c.pane.classList.contains("on")) continue; | |
| 345 | 395 | let changed = false; |
| 346 | 396 | try { |
| 347 | 397 | ctx.drawImage(c.img, 0, 0, 32, 18); |
| @@ -349,25 +399,24 @@ | ||
| 349 | 399 | changed = sig !== c.sig; c.sig = sig; |
| 350 | 400 | } catch { /* nothing decodable yet */ } |
| 351 | 401 | c.unchanged = changed ? 0 : c.unchanged + 1; |
| 352 | c.live = changed; | |
| 402 | c.live = changed && c.online !== false; | |
| 353 | 403 | if (c.unchanged >= 15) { c.img.src = fresh(c.url); c.unchanged = 0; } |
| 354 | 404 | } |
| 355 | $("live").textContent = cams.filter((c) => $(`pane${c.n}`).classList.contains("on")).map((c) => (c.live ? "live" : "waiting")).join(" · "); | |
| 405 | $("live").textContent = cams.filter((c) => c.pane.classList.contains("on")).map((c) => (c.live ? "live" : c.online === false ? "offline" : "waiting")).join(" · "); | |
| 356 | 406 | }, 1000); |
| 357 | showCam(cams[0], true); | |
| 358 | // Camera 2 appears when its stream answers. | |
| 359 | fetch(cams[1].url.replace(/\/stream.*$/, "/state"), { cache: "no-store" }).then((r) => { if (r.ok) $("v2").hidden = false; }).catch(() => {}); | |
| 360 | const panes = { v1: "pane1", v2: "pane2", v3: "pane3" }; | |
| 361 | for (const [btn, pane] of Object.entries(panes)) { | |
| 362 | $(btn).onclick = () => { | |
| 363 | const on = !$(pane).classList.contains("on"); | |
| 364 | if (!on && document.querySelectorAll(".pane.on").length === 1) return; // keep one pane | |
| 365 | $(btn).classList.toggle("on", on); | |
| 366 | if (pane === "pane3") $(pane).classList.toggle("on", on); else showCam(cams[pane === "pane1" ? 0 : 1], on); | |
| 367 | try { localStorage.setItem("so101.panes", JSON.stringify([...document.querySelectorAll(".pane.on")].map((p) => p.id))); } catch {} | |
| 368 | viewer.recenter?.(); | |
| 369 | }; | |
| 407 | if (relayed) pollCameras(); | |
| 408 | else { | |
| 409 | reconcile([{ id: 1, name: "Cam 1", url: direct(1), online: null }]); | |
| 410 | // Straight to an arm server, camera 2 appears when its stream answers. | |
| 411 | fetch(direct(2).replace(/\/stream.*$/, "/state"), { cache: "no-store" }).then((r) => { if (r.ok) reconcile([{ id: 1, name: "Cam 1", url: direct(1), online: null }, { id: 2, name: "Cam 2", url: direct(2), online: null }]); }).catch(() => {}); | |
| 370 | 412 | } |
| 413 | $("v3").onclick = () => { | |
| 414 | const on = !$("pane3").classList.contains("on"); | |
| 415 | if (!on && shown() === 1) return; // keep one pane | |
| 416 | $("v3").classList.toggle("on", on); $("pane3").classList.toggle("on", on); keep(); viewer.recenter?.(); | |
| 417 | }; | |
| 418 | $("pane3").dataset.key = "pane3"; | |
| 419 | if (chosen && !chosen.has("pane3")) { $("pane3").classList.remove("on"); $("v3").classList.remove("on"); } | |
| 371 | 420 | |
| 372 | 421 | // ---------------------------------------------------------------- the stream: the page as a leader |
| 373 | 422 | // Opened while a slider, a jog button or a model joint is held; frames go at most every 33 ms; closed shortly after letting go. |
| @@ -739,7 +788,6 @@ | ||
| 739 | 788 | const app = $("app"); |
| 740 | 789 | const fullscreen = () => (document.fullscreenElement ? document.exitFullscreen() : app.requestFullscreen()); |
| 741 | 790 | if (app.requestFullscreen) $("fs").onclick = fullscreen; else $("fs").hidden = true; |
| 742 | for (const c of cams) c.img.onclick = () => { $("hud").hidden = !$("hud").hidden; }; | |
| 743 | 791 | addEventListener("keydown", (e) => { |
| 744 | 792 | if (e.target.closest("input, select, textarea, dialog")) return; |
| 745 | 793 | if (e.key === "f" || e.key === "F") fullscreen(); |
| @@ -945,14 +993,14 @@ | ||
| 945 | 993 | $("panel").hidden = false; |
| 946 | 994 | $("calSteps").textContent = [ |
| 947 | 995 | `export SO101_ARM=http://${location.host}`, |
| 948 | `export SO101_CAMERA=${cams[0].url}`, | |
| 996 | `export SO101_CAMERA=${cams[0] ? new URL(cams[0].url, location.href).href : "<the camera's stream URL>"}`, | |
| 949 | 997 | `just vision-calibrate # hold the printed board in front of the camera, tilt it`, |
| 950 | 998 | `# save poses from this page: home, drop, he1 … he8 (without gripper)`, |
| 951 | 999 | `just vision-handeye he1,he2,he3,he4,he5,he6,he7,he8`, |
| 952 | 1000 | `just vision-rehearse # sees the cube, plans the joints, moves nothing`, |
| 953 | 1001 | `just vision-pick`, |
| 954 | 1002 | ``, |
| 955 | `# camera 2, once it streams: SO101_CAMERA=${cams[1].url} and --publish-as camera2 / handeye2`, | |
| 1003 | `# a second camera: SO101_CAMERA=<its stream URL> and --publish-as camera2 / handeye2`, | |
| 956 | 1004 | ].join("\n"); |
| 957 | 1005 | const sheets = await api("/printables"); |
| 958 | 1006 | $("printables").innerHTML = Array.isArray(sheets) && sheets.length |
| @@ -968,7 +1016,7 @@ | ||
| 968 | 1016 | const heLine = (h) => h ? mark(true, `solved ${(h.saved ?? "").slice(0, 10)} · ${h.poses?.length ?? "?"} poses · mean ${h.residuals_mm ? (h.residuals_mm.reduce((a, b) => a + b, 0) / h.residuals_mm.length).toFixed(1) : "?"} mm`) : mark(false, "", "not yet"); |
| 969 | 1017 | $("calStatus").innerHTML = [ |
| 970 | 1018 | ["camera 1", camLine(cal.camera)], ["hand-eye 1", heLine(cal.handeye)], |
| 971 | ...($("v2").hidden ? [] : [["camera 2", camLine(cal.camera2)], ["hand-eye 2", heLine(cal.handeye2)]]), | |
| 1019 | ...(cams.length < 2 ? [] : [["camera 2", camLine(cal.camera2)], ["hand-eye 2", heLine(cal.handeye2)]]), | |
| 972 | 1020 | ["pose home", armOnly("home")], ["pose drop", armOnly("drop")], |
| 973 | 1021 | ["poses he1…", mark(he >= 6, `${he} saved`, `${he} of 6 to 8 saved (without the gripper)`)], |
| 974 | 1022 | ].map(([k, v]) => `<b>${k}</b><span>${v}</span>`).join(""); |
| @@ -1061,12 +1109,6 @@ | ||
| 1061 | 1109 | await loadPoses(); |
| 1062 | 1110 | modelInfo = await api("/model.json"); |
| 1063 | 1111 | if (modelInfo.ok) viewer.urdf = modelInfo.urdf; else { $("v3").hidden = true; $("pane3").classList.remove("on"); } |
| 1064 | try { | |
| 1065 | const saved = JSON.parse(localStorage.getItem("so101.panes") || "null"); | |
| 1066 | const usable = (pane) => pane === "pane1" || (pane === "pane2" && !$("v2").hidden) || (pane === "pane3" && modelInfo.ok); | |
| 1067 | const wanted = Array.isArray(saved) ? saved.filter(usable) : []; | |
| 1068 | if (wanted.length) for (const [btn, pane] of Object.entries(panes)) { const on = wanted.includes(pane); $(btn).classList.toggle("on", on); if (pane === "pane3") $(pane).classList.toggle("on", on); else showCam(cams[pane === "pane1" ? 0 : 1], on); } | |
| 1069 | } catch {} | |
| 1070 | 1112 | poll(); |
| 1071 | 1113 | if (relayed) pollLink(); |
| 1072 | 1114 | })(); |
new above
offline