CANopen diagnostics and map data over HTTPS: /can.html (interfaces, nodes with heartbeat state and last EMCY, decoded signals, every COB-ID with role, count, rate and last data); signals can mark an invalid raw value (esphome-canopen's unknown) and NaN gives no value; the CAN reader never dies and a broken can.json only turns CAN off; the GNSS node mcu-stm32-xiao-c5-2 (0x21) and host networking for the car's server in docs; bin/pull-maps.sh pulls map data from GET /api/sync/maps(/file) with SYNC_TOKEN instead of rsync over SSH, resuming cut-off downloads
CI / check (push) Successful in 42s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-29 12:47:23 +01:00
1 parent a3e5fefaf1
commit 6a0d6194fe
9 files changed
+365 -43

No files matched your search

+44 -2
View File
@@ -221,6 +221,22 @@ module Maptool
SYNC_SERVER_URL = ENV.fetch("SYNC_SERVER_URL", "")
SYNC_CLIENT = SYNC_TOKEN.empty? || SYNC_SERVER_URL.empty? ? nil : Sync.new(server: SYNC_SERVER_URL, token: SYNC_TOKEN, library: LIBRARY, state_path: File.join(DATA_DIR, "sync_state.json"))
SYNC_MUTEX = Mutex.new
# Map data the server hands its clients (GET /api/sync/maps, pulled by
# bin/pull-maps.sh — one way, the server builds it): these files and
# everything under these directories of DATA_DIR.
MAP_SYNC_FILES = %w[offline.pmtiles offline.pmtiles.meta.json terrain.pmtiles terrain.pmtiles.meta.json offline_regions.json].freeze
MAP_SYNC_DIRS = %w[routing places hiking_trails kiwix nps].freeze
# { "relative/path" => absolute path } of the map data there is now;
# hidden and temporary files (.rsync-partial, *.part, *.tmp) left out,
# and routing/service.sh (each server copies its own in at boot).
def self.map_sync_files
paths = MAP_SYNC_FILES.map { |f| File.join(DATA_DIR, f) } +
MAP_SYNC_DIRS.flat_map { |d| Dir.glob(File.join(DATA_DIR, d, "**", "*")) }
paths.select { |p| File.file?(p) }
.to_h { |p| [p.delete_prefix("#{DATA_DIR}/"), p] }
.reject { |rel, _| rel.split("/").any? { |part| part.start_with?(".") } || rel.match?(/\.(part|tmp)\z|[\t\n]/) || rel == "routing/service.sh" }
end
helpers do
def logged_in?
@@ -358,9 +374,16 @@ module Maptool
# counts from the frame that brings lat or lon.
CAN_CONFIG_PATH = File.join(DATA_DIR, "can.json")
CAN_STORE = Can::Store.new
CAN_CONFIG = File.exist?(CAN_CONFIG_PATH) ? JSON.parse(File.read(CAN_CONFIG_PATH)) : nil
# A broken can.json turns CAN off (logged) instead of the whole server.
CAN_CONFIG, CAN_DECODER = begin
config = JSON.parse(File.read(CAN_CONFIG_PATH)) if File.exist?(CAN_CONFIG_PATH)
[config, config && Can::Decoder.new(config.fetch("signals", []))]
rescue StandardError => e
warn "can: #{CAN_CONFIG_PATH}: #{e.message} — CAN off"
[nil, nil]
end
if CAN_CONFIG && !test?
decoder = Can::Decoder.new(CAN_CONFIG.fetch("signals", []))
decoder = CAN_DECODER
CAN_CONFIG.fetch("interfaces", []).each do |interface|
Thread.new do
Can.listen(interface, decoder: decoder, store: CAN_STORE, start_nodes: CAN_CONFIG["nmt_start"]) do |values|
@@ -414,6 +437,11 @@ module Maptool
erb :symbols, layout: false
end
# CANopen diagnostics (polls GET /api/can, which needs the login).
get "/can.html" do
erb :can, layout: false
end
# A share to the installed app normally never reaches the server: the
# service worker (public/sw.js) takes the POST, parks the files and
# opens the app. Only lands here if no worker is running yet — back to
@@ -866,6 +894,20 @@ module Maptool
halt 400, json(error: "bad source path")
end
# Map data for bin/pull-maps.sh: one "size<TAB>mtime<TAB>path" line per
# file, then each file by its path (Range requests resume a cut-off one).
get "/api/sync/maps" do
require_sync_token!
content_type :text
App.map_sync_files.sort.map { |rel, path| [File.size(path), File.mtime(path).to_i, rel].join("\t") }.join("\n") + "\n"
end
get "/api/sync/maps/file" do
require_sync_token!
path = App.map_sync_files[params[:path].to_s] or halt 404
send_file path
end
# The Sync button (a client only): pull, then push, now.
post "/api/sync/run" do
require_login!