From 500acf9fd5ea5b83c06d97e7db20676bf22333c7 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Mon, 30 Nov 2026 18:30:21 +0100 Subject: [PATCH] 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 --- lib/maptool/can.rb | 31 ++++++++++++++++++++++++++++++- test/test_can.rb | 15 ++++++++++++++- views/can.erb | 12 +++++++----- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/maptool/can.rb b/lib/maptool/can.rb index 27372d1..78aa078 100644 --- a/lib/maptool/can.rb +++ b/lib/maptool/can.rb @@ -117,6 +117,10 @@ module Maptool class Store 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 @mutex = Mutex.new @signals = {} @@ -137,6 +141,13 @@ module Maptool role, node = Can.canopen_role(frame.id, extended: frame.extended) if 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[: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 @@ -154,7 +165,25 @@ module Maptool end f.merge!(count: f[:count] + 1, data: frame.data.unpack1("H*"), at: at) 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 diff --git a/test/test_can.rb b/test/test_can.rb index b738a51..89176f1 100644 --- a/test/test_can.rb +++ b/test/test_can.rb @@ -57,7 +57,7 @@ class CanTest < Minitest::Test store.record("vcan0", Frame.new(0x181, "".b, false), { "gnss.lat" => 49.1 }, at: 2.0) snapshot = store.snapshot 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"]) 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]) 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: # sudo ip link add dev vcan0 type vcan && sudo ip link set up vcan0 def test_frames_go_over_vcan0 diff --git a/views/can.erb b/views/can.erb index 9bcefb7..8cb9faf 100644 --- a/views/can.erb +++ b/views/can.erb @@ -12,12 +12,12 @@ header { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; margin-bottom: 12px; } 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; } - #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; } - 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 { 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; } .ok { color: var(--ok); } .warn { color: var(--warn); } .bad { color: var(--bad); } .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); fill("nodes", Object.entries(data.nodes).sort((a, b) => a[0] - b[0]).map(([id, n]) => { const emcy = n.emcy ? `${hex(n.emcy.code, 4)} reg ${hex(n.emcy.register, 2)} ${n.emcy.code === 0 ? "(reset)" : ""} · ${age(n.emcy.at)}` : "—"; - return `${nodeName(id)}${esc(n.state || "no heartbeat")}${age(n.seen_at)}${emcy}${[...(roles[id] || [])].sort().map(esc).join(", ")}`; + return `${nodeName(id)}${n.silent ? "silent (heartbeat stopped)" : esc(n.state || "no heartbeat")}${age(n.seen_at)}${emcy}${[...(roles[id] || [])].sort().map(esc).join(", ")}`; }), "No CANopen node heard yet.", 5); fill("signals", Object.entries(data.signals).sort().map(([name, s]) => @@ -100,7 +100,9 @@ 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; 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) { status.textContent = `can't reach the server: ${e.message}`; } finally {