File size: 12,053 Bytes
4eecd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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 };
})();