diff --git a/app.rb b/app.rb
index 65d0a54..91ef4f3 100644
--- a/app.rb
+++ b/app.rb
@@ -1364,7 +1364,7 @@ module Maptool
# CANopen node (heartbeat state, last seen) and each configured signal.
get "/api/can" do
require_login!
- snapshot = CAN_STORE.snapshot
+ snapshot = CAN_STORE.snapshot(since: params[:since]&.to_i)
units = CAN_DECODER ? CAN_DECODER.units : {}
snapshot[:signals] = snapshot[:signals].to_h { |name, signal| [name, units[name] ? signal.merge(unit: units[name]) : signal] }
# simulated: the GNSS simulation (/sim.html) stands in for gnss.*
diff --git a/lib/maptool/can.rb b/lib/maptool/can.rb
index 4a0954a..696bc2f 100644
--- a/lib/maptool/can.rb
+++ b/lib/maptool/can.rb
@@ -137,6 +137,7 @@ module Maptool
# every frame id seen (count, rate, last data) and the decoded signals.
class Store
MAX_FRAME_IDS = 2048 # ponytail: stops counting new ids past this, enough for a bus
+ LOG_SIZE = 1000 # the last frames as they came, for /can.html's trace
# Signals starting with this aren't taken from the bus (the GNSS
# simulation's, put by #put, stand in for the node's meanwhile).
@@ -148,6 +149,8 @@ module Maptool
@nodes = {}
@interfaces = {}
@frames = {}
+ @log = []
+ @seq = 0
end
def interface(name, **state)
@@ -174,6 +177,8 @@ module Maptool
# EMCY: error code (0 = errors reset), error register, 5 bytes vendor-specific
n[:emcy] = { code: frame.data.unpack1("S<"), register: frame.data.getbyte(2), at: at } if role == :emcy && frame.data.bytesize >= 3
end
+ @log << { seq: @seq += 1, at: at, interface: interface, id: frame.id, extended: frame.extended, data: frame.data.unpack1("H*") }
+ @log.shift if @log.size > LOG_SIZE
key = "#{interface}:#{frame.extended ? format('%08X', frame.id) : format('%03X', frame.id)}"
f = @frames[key]
if f.nil? && @frames.size < MAX_FRAME_IDS
@@ -208,8 +213,12 @@ module Maptool
end
end
- def snapshot
- @mutex.synchronize { { interfaces: @interfaces.dup, nodes: @nodes.dup, signals: @signals.dup, frames: @frames.values.map(&:dup) } }
+ # log: the frames after seq `since` (none without it).
+ def snapshot(since: nil)
+ @mutex.synchronize do
+ snap = { interfaces: @interfaces.dup, nodes: @nodes.dup, signals: @signals.dup, frames: @frames.values.map(&:dup) }
+ since ? snap.merge(log: @log.select { |f| f[:seq] > since }) : snap
+ end
end
end
diff --git a/test/test_can.rb b/test/test_can.rb
index 89176f1..0da0658 100644
--- a/test/test_can.rb
+++ b/test/test_can.rb
@@ -66,6 +66,8 @@ class CanTest < Minitest::Test
tpdo = snapshot[:frames].find { |f| f[:id] == 0x181 }
assert_equal [:tpdo1, 1, 2, 0.5], tpdo.values_at(:role, :node, :count, :interval)
assert_equal({ code: 0x8130, register: 0x11, at: 3.0 }, snapshot[:nodes][5][:emcy])
+ assert_equal [3, 4], store.snapshot(since: 2)[:log].map { |f| f[:seq] }
+ assert_equal "3081" "11", store.snapshot(since: 3)[:log].first[:data]
end
def test_a_node_missing_3_heartbeats_is_silent_until_heard_again
diff --git a/views/_debug_nav.erb b/views/_debug_nav.erb
new file mode 100644
index 0000000..e75e2fa
--- /dev/null
+++ b/views/_debug_nav.erb
@@ -0,0 +1,14 @@
+<%# The debug pages' switcher, on top of each (status, CAN, GNSS simulation). %>
+
+
diff --git a/views/can.erb b/views/can.erb
index 4159d10..b53c068 100644
--- a/views/can.erb
+++ b/views/can.erb
@@ -4,7 +4,7 @@
-
CANopen diagnostics
+ CAN analyzer
+ <%= erb :_debug_nav, layout: false %>
- CANopen diagnostics
+ CAN analyzer
loading…
@@ -56,6 +66,11 @@
+ Trace
+ commandOD indexsubindexpayloadunused
+ · byte 0: ccs/scs t n e s · the frame filter applies
+
+
diff --git a/views/sim.erb b/views/sim.erb
index 6f0a771..8b87ff2 100644
--- a/views/sim.erb
+++ b/views/sim.erb
@@ -23,10 +23,10 @@
+ <%= erb :_debug_nav, layout: false %>
diff --git a/views/status.erb b/views/status.erb
index cde9025..5a755a9 100644
--- a/views/status.erb
+++ b/views/status.erb
@@ -27,12 +27,12 @@
+ <%= erb :_debug_nav, layout: false %>