// Live drone footage panel. Advisory only: it shows what the model sees in the newest frame, whether the signal is fresh, whether it holds the wall, // how much is painted, and whether painting COULD be allowed. It sends nothing to a drone or a sprayer, and "painting allowed" is a software // indicator, not a safety interlock (the server says so in every state it returns, and this panel repeats it). // // The panel is built here, not in index.html, so it stays independent of the rest of the console's markup. (() => { "use strict"; const $ = (s, r = document) => r.querySelector(s); const el = (tag, attrs = {}, ...kids) => { const e = document.createElement(tag); for (const [k, v] of Object.entries(attrs)) { if (k === "class") e.className = v; else if (k === "text") e.textContent = v; else e.setAttribute(k, v); } kids.forEach((c) => e.append(c)); return e; }; let panel = null, timer = 0, lastAnalysed = -1, busy = false, hadSession = false; async function api(path, options = {}) { const res = await fetch(path, options); const type = res.headers.get("Content-Type") || ""; const body = type.includes("json") ? await res.json() : await res.arrayBuffer(); if (!res.ok) throw new Error((body && body.error) || `HTTP ${res.status}`); return body; } const say = (m, k = "error") => (window.toast ? window.toast(m, k) : console.warn(m)); const operatorName = () => ((document.querySelector("#operator") || {}).value || "").trim(); function build() { const status = el("span", { class: "live-pill off", id: "live-pill", text: "OFF" }); const source = el("input", { class: "field", id: "live-source", placeholder: "rtsp://192.168.1.20:8554/cam or http://192.168.1.20:8080/video", maxlength: "300", "aria-label": "Camera stream URL" }); const video = el("select", { class: "field", id: "live-video", "aria-label": "Or replay an uploaded video as the feed" }, el("option", { value: "", text: "…or replay an uploaded video" })); const connect = el("button", { class: "primary", id: "live-connect", text: "Connect" }); const stop = el("button", { class: "ghost", id: "live-stop", text: "Stop", hidden: "" }); const img = el("img", { id: "live-frame", alt: "Latest analysed frame from the live feed", hidden: "" }); const empty = el("div", { class: "live-empty", id: "live-empty", text: "Not connected. Connect a camera stream or replay an uploaded video." }); const chips = el("div", { class: "live-chips", id: "live-chips" }); const peopleList = el("ul", { class: "live-people", id: "live-people", "aria-live": "polite" }); const holds = el("ul", { class: "live-holds", id: "live-holds", "aria-live": "polite" }); const twinBox = el("input", { type: "checkbox", id: "live-twin", checked: "" }); const twinRow = el("label", { class: "live-twinrow small" }, twinBox, el("span", { text: "Also update this job's digital twin from the feed (when it has one)" })); const arm = el("button", { class: "ghost", id: "live-arm", text: "Arm painting", disabled: "" }); const armNote = el("span", { class: "small muted", id: "live-armnote" }); const note = el("p", { class: "small muted live-disc", id: "live-disc", text: "Advisory only. This panel sends nothing to a drone or a sprayer and is not a safety interlock: the drone's own failsafes and a human pilot stay in charge." }); panel = el("section", { class: "live-panel", id: "live-panel", hidden: "", "aria-label": "Live drone footage" }, el("div", { class: "live-head" }, el("h2", { class: "section", text: "Live drone footage" }), status), el("div", { class: "live-src" }, source, video, connect, stop), el("div", { class: "live-body" }, el("div", { class: "live-view" }, img, empty), el("div", { class: "live-side" }, twinRow, chips, peopleList, el("div", { class: "small muted", text: "Holds (why painting is not allowed right now)" }), holds, el("div", { class: "live-armrow" }, arm, armNote))), note); const anchor = $("#robot-bar") || $(".legend"); (anchor && anchor.parentNode ? anchor.parentNode : document.body).insertBefore(panel, anchor || null); const btn = el("button", { class: "ghost live-open", id: "live-open", "aria-pressed": "false", text: "Live drone feed" }); (($(".group.views") || $(".toolbar")) || document.body).append(btn); btn.addEventListener("click", toggle); connect.addEventListener("click", start); stop.addEventListener("click", stopFeed); arm.addEventListener("click", toggleArm); source.addEventListener("keydown", (e) => { if (e.key === "Enter") start(); }); } async function toggle() { const open = panel.hidden; panel.hidden = !open; $("#live-open").setAttribute("aria-pressed", open ? "true" : "false"); if (open) { loadVideos(); poll(); timer = setInterval(poll, 900); } else { clearInterval(timer); } } async function loadVideos() { try { const vids = await api("/api/videos"), sel = $("#live-video"); sel.length = 1; vids.forEach((v) => sel.append(el("option", { value: v.id, text: `${v.id} · ${v.modified}` }))); } catch { /* the list is a convenience */ } } async function start() { const src = $("#live-source").value.trim(), vid = $("#live-video").value; if (!src && !vid) { say("Enter a camera stream URL, or pick an uploaded video to replay."); return; } $("#live-connect").disabled = true; const body = vid && !src ? { video_id: vid } : { source: src }; const j = typeof job !== "undefined" ? job : null; if (j && $("#live-twin").checked) { try { if ((await api(`/api/jobs/${j.id}/twin`)).built !== false) body.twin_job = j.id; } catch { /* this job has no twin: the feed just runs without one */ } } try { render(await api("/api/live", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) })); hadSession = true; lastAnalysed = -1; } catch (e) { say(e.message || e); } finally { $("#live-connect").disabled = false; } } async function stopFeed() { try { await api("/api/live/stop", { method: "POST", headers: { "Content-Type": "application/json" }, body: "{}" }); } catch (e) { say(e.message || e); } poll(); } async function toggleArm() { const armed = $("#live-arm").dataset.armed === "1"; if (!armed && !operatorName()) { say("Enter your name in the Operator box first: arming is a person's decision."); const o = $("#operator"); if (o) o.focus(); return; } try { render(await api("/api/live/arm", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ armed: !armed, operator: operatorName() }) })); } catch (e) { say(e.message || e); } } async function poll() { if (busy || !panel || panel.hidden) return; busy = true; try { render(await api("/api/live")); } catch { /* server briefly busy: keep the last state */ } finally { busy = false; } } function chip(label, value, tone) { return el("span", { class: `live-chip ${tone || ""}` }, el("small", { text: label }), el("b", { text: value })); } // People are reported as the gate sees them: in frame only, with the detector that produced the boxes named. "Not // configured" and "unknown" are deliberately not "clear" - nobody has looked, or the detector did not answer. function peopleText(st) { const p = st.people; if (!p) return "—"; if (!p.configured) return "not configured"; if (p.state === "unknown") return "unknown"; if (p.state === "people") return `${p.count} in frame${p.in_zone ? ` · ${p.in_zone} in the paint zone` : ""}`; if (p.state === "leaving") return `${(p.leaving || []).length} leaving`; return "clear"; } function peopleTone(st) { const p = st.people || {}; if (p.in_zone) return "bad"; if (p.state === "people" || p.state === "unknown" || p.configured === false) return "warn"; return p.state === "clear" ? "ok" : ""; } function render(st) { const running = !!st.running; $("#live-stop").hidden = !running; $("#live-arm").disabled = !running; const pill = $("#live-pill"); const sig = (st.signal || {}).state; const label = !running ? "OFF" : sig === "live" ? "LIVE" : sig === "stale" ? "STALE" : "LOST"; pill.textContent = label; pill.className = `live-pill ${!running ? "off" : sig === "live" ? "ok" : sig === "stale" ? "warn" : "bad"}`; const chips = $("#live-chips"); chips.textContent = ""; if (running) { const age = (st.signal || {}).age_s; chips.append( chip("Signal", age == null ? "none" : `${age.toFixed(1)} s old`, sig === "live" ? "ok" : sig === "stale" ? "warn" : "bad"), chip("Wall", st.locked ? "locked" : "not locked", st.locked ? "ok" : "bad"), chip("Painted", st.painted_fraction == null ? "—" : `${Math.round(100 * st.painted_fraction)}%`, ""), chip("Complete", st.complete ? "yes" : `${st.complete_streak || 0}/${st.confirm_frames} frames`, st.complete ? "ok" : ""), chip("People", peopleText(st), peopleTone(st)), chip("Person model", st.people_detector || "none", st.people_detector && st.people_detector !== "none" ? "" : "warn"), chip("Latency", st.latency_s == null ? "—" : `${st.latency_s.toFixed(1)} s`, ""), chip("Frames", `${(st.frames || {}).analysed || 0} analysed, ${(st.frames || {}).dropped || 0} skipped`, "")); } const peopleBox = $("#live-people"); peopleBox.textContent = ""; const pp = st.people; if (running && pp) { if (!pp.configured) { peopleBox.append(el("li", { class: "muted", text: "Person detection is off: set PAINTVISION_PERSON_MODEL to a YOLO .onnx and restart the console." })); } else if (pp.state === "unknown") { peopleBox.append(el("li", { class: "warn", text: "Person detection is not answering: treat the frame as occupied." })); } else { (pp.people || []).forEach((p) => peopleBox.append(el("li", { class: p.in_zone ? "zone" : "" }, el("b", { text: `Person ${p.id}` }), el("span", { text: `${Math.round((p.score || 0) * 100)}% · ${p.in_zone ? "in the paint zone" : "clear of the paint zone"}` })))); (pp.leaving || []).forEach((p) => peopleBox.append(el("li", { class: "muted" }, el("b", { text: `Person ${p.id}` }), el("span", { text: `left ${p.missing_for_s.toFixed(1)} s ago` })))); if (!(pp.people || []).length && !(pp.leaving || []).length && pp.state === "clear") { peopleBox.append(el("li", { class: "muted", text: "No person in frame." })); } } } const holds = $("#live-holds"); holds.textContent = ""; if (running) { if (st.paint_allowed) holds.append(el("li", { class: "ok", text: "No holds: painting could be allowed (software indicator only)." })); (st.holds || []).forEach((h) => holds.append(el("li", { text: h }))); if (st.error) holds.append(el("li", { class: "bad", text: `Analysis error: ${st.error}` })); } const arm = $("#live-arm"); arm.dataset.armed = st.armed ? "1" : "0"; arm.textContent = st.armed ? "Disarm" : "Arm painting"; $("#live-armnote").textContent = st.armed ? `Armed by ${st.armed_by}; expires in ${st.arm_expires_in_s}s, and drops if the signal or the wall lock is lost.` : ""; const img = $("#live-frame"), empty = $("#live-empty"); const showImg = running && st.has_frame; img.hidden = !showImg; empty.hidden = showImg; empty.textContent = running ? "Waiting for the first analysed frame…" : "Not connected. Connect a camera stream or replay an uploaded video."; const n = (st.frames || {}).analysed || 0; if (showImg && n !== lastAnalysed) { lastAnalysed = n; img.src = `/api/live/frame.jpg?n=${n}`; } if (!running && hadSession) { hadSession = false; lastAnalysed = -1; } } function init() { build(); } if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init(); window.PVLive = { poll, render }; })();