CAN: a node that misses 3 heartbeats is flagged silent (one journal line when it stops and when it's back, red in /can.html); /can.html tables as wide as their content, numeric headers aligned with their values

This commit is contained in:
randy committed 2026-11-30 18:30:21 +01:00
1 parent 5c023619ec
commit 500acf9fd5
3 files changed
+51 -7

No files matched your search

+30 -1
View File
@@ -117,6 +117,10 @@ module Maptool
class Store class Store
MAX_FRAME_IDS = 2048 # ponytail: stops counting new ids past this, enough for a bus MAX_FRAME_IDS = 2048 # ponytail: stops counting new ids past this, enough for a bus
# Signals starting with this aren't taken from the bus (the GNSS
# simulation's, put by #put, stand in for the node's meanwhile).
attr_accessor :override_prefix
def initialize def initialize
@mutex = Mutex.new @mutex = Mutex.new
@signals = {} @signals = {}
@@ -137,6 +141,13 @@ module Maptool
role, node = Can.canopen_role(frame.id, extended: frame.extended) role, node = Can.canopen_role(frame.id, extended: frame.extended)
if node if node
n = (@nodes[node] ||= {}) n = (@nodes[node] ||= {})
if n.delete(:silent)
warn "can: node #{node} back after #{(at - n[:seen_at]).round}s"
end
if role == :heartbeat
n[:heartbeat_interval] = at - n[:heartbeat_at] if n[:heartbeat_at]
n[:heartbeat_at] = at
end
n[:seen_at] = at n[:seen_at] = at
n[:state] = HEARTBEAT_STATES.fetch(frame.data.getbyte(0) & 0x7F, "unknown") if role == :heartbeat && frame.data.bytesize >= 1 n[:state] = HEARTBEAT_STATES.fetch(frame.data.getbyte(0) & 0x7F, "unknown") if role == :heartbeat && frame.data.bytesize >= 1
# EMCY: error code (0 = errors reset), error register, 5 bytes vendor-specific # EMCY: error code (0 = errors reset), error register, 5 bytes vendor-specific
@@ -154,7 +165,25 @@ module Maptool
end end
f.merge!(count: f[:count] + 1, data: frame.data.unpack1("H*"), at: at) f.merge!(count: f[:count] + 1, data: frame.data.unpack1("H*"), at: at)
end end
values.each { |name, value| @signals[name] = { value: value, at: at } } values.each { |name, value| @signals[name] = { value: value, at: at } unless @override_prefix && name.start_with?(@override_prefix) }
mark_silent(at)
end
end
def put(values, at: Time.now.to_f)
@mutex.synchronize { values.each { |name, value| @signals[name] = { value: value, at: at } } }
end
# A node with a heartbeat that missed 3 of them: silent, warned once
# (in the journal, so the time it stopped is kept). Checked as other
# frames come in — with the whole bus quiet the interface says so.
def mark_silent(at)
return if @checked_at && at - @checked_at < 1
@checked_at = at
@nodes.each do |node, n|
next if n[:silent] || !n[:heartbeat_interval] || at - n[:heartbeat_at] < 3 * n[:heartbeat_interval]
n[:silent] = true
warn "can: node #{node} silent, last heartbeat #{Time.at(n[:heartbeat_at]).strftime('%F %T')}"
end end
end end
+14 -1
View File
@@ -57,7 +57,7 @@ class CanTest < Minitest::Test
store.record("vcan0", Frame.new(0x181, "".b, false), { "gnss.lat" => 49.1 }, at: 2.0) store.record("vcan0", Frame.new(0x181, "".b, false), { "gnss.lat" => 49.1 }, at: 2.0)
snapshot = store.snapshot snapshot = store.snapshot
assert_equal({ frames: 2, last_frame_at: 2.0 }, snapshot[:interfaces]["vcan0"]) assert_equal({ frames: 2, last_frame_at: 2.0 }, snapshot[:interfaces]["vcan0"])
assert_equal({ seen_at: 1.0, state: "operational" }, snapshot[:nodes][5]) assert_equal [1.0, "operational"], snapshot[:nodes][5].values_at(:seen_at, :state)
assert_equal({ value: 49.1, at: 2.0 }, snapshot[:signals]["gnss.lat"]) assert_equal({ value: 49.1, at: 2.0 }, snapshot[:signals]["gnss.lat"])
store.record("vcan0", Frame.new(0x181, "".b, false), {}, at: 2.5) store.record("vcan0", Frame.new(0x181, "".b, false), {}, at: 2.5)
@@ -68,6 +68,19 @@ class CanTest < Minitest::Test
assert_equal({ code: 0x8130, register: 0x11, at: 3.0 }, snapshot[:nodes][5][:emcy]) assert_equal({ code: 0x8130, register: 0x11, at: 3.0 }, snapshot[:nodes][5][:emcy])
end end
def test_a_node_missing_3_heartbeats_is_silent_until_heard_again
store = Maptool::Can::Store.new
[0, 5, 10].each { |t| store.record("vcan0", Frame.new(0x702, "\x05".b, false), {}, at: t.to_f) }
_, err = capture_io { store.record("vcan0", Frame.new(0x701, "\x05".b, false), {}, at: 24.0) }
refute store.snapshot[:nodes][2][:silent]
_, err = capture_io { store.record("vcan0", Frame.new(0x701, "\x05".b, false), {}, at: 26.0) }
assert store.snapshot[:nodes][2][:silent]
assert_match(/node 2 silent/, err)
_, err = capture_io { store.record("vcan0", Frame.new(0x702, "\x05".b, false), {}, at: 30.0) }
refute store.snapshot[:nodes][2][:silent]
assert_match(/node 2 back after 20s/, err)
end
# Over a real SocketCAN interface when there is one: # Over a real SocketCAN interface when there is one:
# sudo ip link add dev vcan0 type vcan && sudo ip link set up vcan0 # sudo ip link add dev vcan0 type vcan && sudo ip link set up vcan0
def test_frames_go_over_vcan0 def test_frames_go_over_vcan0
+7 -5
View File
@@ -12,12 +12,12 @@
header { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; margin-bottom: 12px; } header { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; margin-bottom: 12px; }
h1 { font-size: 1.2rem; margin: 0; } h1 { font-size: 1.2rem; margin: 0; }
h2 { font-size: 0.8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin: 20px 0 6px; } h2 { font-size: 0.8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin: 20px 0 6px; }
#status { color: var(--muted); font-size: 0.85rem; } #status { color: var(--muted); font-size: 0.85rem; } #status.bad { color: var(--bad); }
.scroll { overflow-x: auto; } .scroll { overflow-x: auto; }
table { border-collapse: collapse; font-size: 0.85rem; min-width: 100%; } table { border-collapse: collapse; font-size: 0.85rem; }
th, td { padding: 6px 12px 6px 0; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; } th, td { padding: 6px 12px 6px 0; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; } th { color: var(--muted); font-weight: 500; }
td.num { font-variant-numeric: tabular-nums; text-align: right; } .num { font-variant-numeric: tabular-nums; text-align: right; }
code { font: 0.85rem ui-monospace, SFMono-Regular, Menlo, monospace; } code { font: 0.85rem ui-monospace, SFMono-Regular, Menlo, monospace; }
.ok { color: var(--ok); } .warn { color: var(--warn); } .bad { color: var(--bad); } .ok { color: var(--ok); } .warn { color: var(--warn); } .bad { color: var(--bad); }
.stale td { opacity: 0.5; } .stale td { opacity: 0.5; }
@@ -74,7 +74,7 @@
for (const f of data.frames) if (f.node != null) (roles[f.node] ||= new Set()).add(f.role); 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]) => { 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)}` : "—"; 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) ? "stale" : ""}"><td>${nodeName(id)}</td><td class="${STATE_CLASS[n.state] || ""}">${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>`; 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 ? "silent (heartbeat stopped)" : 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); }), "No CANopen node heard yet.", 5);
fill("signals", Object.entries(data.signals).sort().map(([name, s]) => fill("signals", Object.entries(data.signals).sort().map(([name, s]) =>
@@ -100,7 +100,9 @@
const date = Date.parse(response.headers.get("Date")); const date = Date.parse(response.headers.get("Date"));
if (date) clockOffset = Math.abs(date / 1000 - Date.now() / 1000) > 2 ? date / 1000 - Date.now() / 1000 : 0; if (date) clockOffset = Math.abs(date / 1000 - Date.now() / 1000) > 2 ? date / 1000 - Date.now() / 1000 : 0;
render(last = await response.json()); render(last = await response.json());
status.textContent = `updated ${new Date().toLocaleTimeString()}`; 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()}`;
} catch (e) { } catch (e) {
status.textContent = `can't reach the server: ${e.message}`; status.textContent = `can't reach the server: ${e.message}`;
} finally { } finally {