CAN bus recorded on the server into ASAM MDF4: can-recorder (Rust, can-recorder/) reads every interface's SocketCAN frames with the kernel's timestamp and writes a finished .mf4 every 5 min (data/can_rec/<UTC start>.mf4, .part then renamed: a power cut loses at most the last segment), its own minimal MDF 4.10 writer in the CANedge CAN_DataFrame layout (mdf4-rs's lacks the bus-event flags and components: asammdf couldn't DBC-decode it) — checked in asammdf 8.8.35 against candump, every frame, < 0.002 ms, DBC decoding works; lib/maptool/can_recorder.rb starts and stops it, GET/POST/DELETE /api/can/recording, the files by GET /api/can/recordings/<name>; /trace.html's 'Recording on the server (MF4)'; built in its own Dockerfile stage (rust:1.100-slim-trixie)
This commit is contained in:
1 parent
712c6bf7ad
commit
60d7c825fc
13 files changed
+980
No files matched your search
@@ -0,0 +1,72 @@
|
||||
require "fileutils"
|
||||
|
||||
module Maptool
|
||||
module Can
|
||||
# Recording the bus on the server: can-recorder (can-recorder/, Rust)
|
||||
# writes every interface's frames into ASAM MDF4 files, one per segment
|
||||
# (`<dir>/<UTC start>.mf4`, asammdf with a DBC, CSS Electronics' tools);
|
||||
# this starts it, stops it (SIGTERM: the segment being recorded is
|
||||
# written) and lists what it wrote.
|
||||
class Recorder
|
||||
NAME = /\A\d{8}-\d{6}\.mf4\z/
|
||||
STOP_WAIT = 10 # s for the last segment to be written
|
||||
|
||||
attr_reader :dir, :error, :started_at
|
||||
|
||||
def initialize(dir, interfaces, binary: ENV.fetch("CAN_RECORDER", "can-recorder"), segment: 300)
|
||||
@dir = dir
|
||||
@interfaces = interfaces
|
||||
@binary = binary
|
||||
@segment = segment
|
||||
@mutex = Mutex.new
|
||||
end
|
||||
|
||||
def running?
|
||||
@waiter&.alive? || false
|
||||
end
|
||||
|
||||
def start
|
||||
@mutex.synchronize do
|
||||
return if running?
|
||||
|
||||
FileUtils.mkdir_p(@dir)
|
||||
pid = Process.spawn(@binary, "--segment", @segment.to_s, @dir, *@interfaces)
|
||||
@waiter = Process.detach(pid)
|
||||
@pid = pid
|
||||
@started_at = Time.now.to_f
|
||||
@error = nil
|
||||
rescue SystemCallError => e
|
||||
@error = "#{@binary}: #{e.message}"
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
@mutex.synchronize do
|
||||
return unless running?
|
||||
|
||||
Process.kill("TERM", @pid)
|
||||
@waiter.join(STOP_WAIT) or Process.kill("KILL", @pid)
|
||||
status = @waiter.value
|
||||
@error = "#{@binary} exited with #{status.exitstatus}" unless status.success?
|
||||
@started_at = nil
|
||||
end
|
||||
end
|
||||
|
||||
# The newest first.
|
||||
def files
|
||||
return [] unless Dir.exist?(@dir)
|
||||
|
||||
Dir.children(@dir).grep(NAME).sort.reverse.map do |name|
|
||||
stat = File.stat(File.join(@dir, name))
|
||||
{ name: name, size: stat.size, at: stat.mtime.to_f }
|
||||
end
|
||||
end
|
||||
|
||||
def path(name)
|
||||
path = File.join(@dir, name)
|
||||
path if NAME.match?(name) && File.file?(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user