CAN actions: can.json's "actions" ({name, node, index, subindex, data: 1–4 bytes hex}) are SDO expedited downloads, a button beside their node on /can.html (confirm first, the result in the status line) — POST /api/can/actions/:n (login, 409 with no config, 404 unknown, 502 on an SDO error), GET /api/can lists them; first one: Level IMU on node 0x21 (0x2022:1 ← 01)

This commit is contained in:
randy committed 2026-12-03 01:29:12 +01:00
1 parent 0a5548cd8f
commit 97d1bda09c
6 files changed
+77 -7

No files matched your search

+22 -5
View File
@@ -47,7 +47,7 @@
<div class="scroll"><table id="interfaces"><thead><tr><th>Interface</th><th>State</th><th class="num">Frames</th><th>Last frame</th><th>Error</th></tr></thead><tbody></tbody></table></div>
<h2>Nodes</h2>
<div class="scroll"><table id="nodes"><thead><tr><th>Node</th><th>State</th><th>Last seen</th><th>Last EMCY</th><th>Sends</th></tr></thead><tbody></tbody></table></div>
<div class="scroll"><table id="nodes"><thead><tr><th>Node</th><th>State</th><th>Last seen</th><th>Last EMCY</th><th>Sends</th><th>Actions</th></tr></thead><tbody></tbody></table></div>
<h2>Signals</h2>
<div class="scroll"><table id="signals"><thead><tr><th>Signal</th><th class="num">Value</th><th>Unit</th><th>Updated</th></tr></thead><tbody></tbody></table></div>
@@ -97,6 +97,22 @@
document.querySelector(`#${id} tbody`).innerHTML = rows.length ? rows.join("") : `<tr><td class="empty" colspan="${cols}">${empty}</td></tr>`;
}
// can.json's "actions" beside their node: one SDO write each (POST /api/can/actions/<n>).
let notice = null; // an action's outcome, shown in the status for 10 s
const actionsHtml = (actions, node) => (actions || []).map((a, n) => a.node === node ? `<button type="button" data-action="${n}">${esc(a.name)}</button>` : "").join(" ");
document.getElementById("nodes").addEventListener("click", async (e) => {
const button = e.target.closest("button[data-action]");
if (!button || !confirm(`${button.textContent} on node ${last.actions[button.dataset.action].node}?`)) return;
button.disabled = true;
try {
const response = await fetch(`/api/can/actions/${button.dataset.action}`, { method: "POST" });
const body = await response.json().catch(() => ({}));
notice = { text: `${button.textContent}: ${response.ok ? "done" : body.error || `HTTP ${response.status}`}`, bad: !response.ok, until: Date.now() + 10000 };
} finally {
button.disabled = false;
}
});
const STATE_CLASS = { operational: "ok", "pre-operational": "warn", stopped: "bad", "boot-up": "warn" };
function render(data) {
@@ -108,8 +124,8 @@
for (const f of data.frames) if (f.node != null) (roles[f.node] ||= new Set()).add(f.role);
fill("nodes", Object.entries(data.nodes).sort((a, b) => a[0] - b[0]).map(([id, n]) => {
const emcy = n.emcy ? `<code>${hex(n.emcy.code, 4)}</code> reg <code>${hex(n.emcy.register, 2)}</code> ${n.emcy.code === 0 ? "(reset)" : ""} · ${age(n.emcy.at)}` : "—";
return `<tr class="${stale(n.seen_at) && !n.silent ? "stale" : ""}"><td>${nodeName(id)}</td><td class="${n.silent ? "bad" : STATE_CLASS[n.state] || ""}"${n.silent ? ' title="3 heartbeats missed"' : ""}>${n.silent ? "silent" : esc(n.state || "no heartbeat")}</td><td>${age(n.seen_at)}</td><td class="${n.emcy?.code ? "bad" : ""}">${emcy}</td><td>${[...(roles[id] || [])].sort().map(esc).join(", ")}</td></tr>`;
}), "No CANopen node heard yet.", 5);
return `<tr class="${stale(n.seen_at) && !n.silent ? "stale" : ""}"><td>${nodeName(id)}</td><td class="${n.silent ? "bad" : STATE_CLASS[n.state] || ""}"${n.silent ? ' title="3 heartbeats missed"' : ""}>${n.silent ? "silent" : esc(n.state || "no heartbeat")}</td><td>${age(n.seen_at)}</td><td class="${n.emcy?.code ? "bad" : ""}">${emcy}</td><td>${[...(roles[id] || [])].sort().map(esc).join(", ")}</td><td>${actionsHtml(data.actions, Number(id))}</td></tr>`;
}), "No CANopen node heard yet.", 6);
fill("signals", Object.entries(data.signals).sort().map(([name, s]) =>
`<tr class="${stale(s.at) ? "stale" : ""}"><td>${esc(name)}</td><td class="num">${typeof s.value === "number" ? +s.value.toFixed(6) : esc(s.value)}</td><td>${esc(s.unit)}</td><td>${age(s.at)}</td></tr>`),
@@ -136,8 +152,9 @@
if (date) clockOffset = Math.abs(date / 1000 - Date.now() / 1000) > 2 ? date / 1000 - Date.now() / 1000 : 0;
render(last = await response.json());
const silent = Object.keys(last.nodes).filter((id) => last.nodes[id].silent);
status.className = silent.length ? "bad" : "";
status.textContent = (silent.length ? `⚠ node ${silent.join(", ")} silent · ` : "") + `updated ${new Date().toLocaleTimeString()}`;
const shown = notice && notice.until > Date.now() ? notice : null;
status.className = silent.length || shown?.bad ? "bad" : "";
status.textContent = (shown ? `${shown.text} · ` : "") + (silent.length ? `⚠ node ${silent.join(", ")} silent · ` : "") + `updated ${new Date().toLocaleTimeString()}`;
} catch (e) {
status.textContent = `can't reach the server: ${e.message}`;
} finally {