Tracker and ITS-G5 as separate overlays, Overlays grouped by category: the tracker gets a name/icon/color, charging from its USB power topic, a floating info card (per-device opacity), a trail of the last 1–24 h and optional position logging that survives restarts (LiveStore); hiking trails keep their main checkbox as an all-countries toggle
CI / check (push) Successful in 20s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-19 11:57:36 +01:00
1 parent 3fc8c75c60
commit 0e5f79f8e4
9 files changed
+801 -205

No files matched your search

+144
View File
@@ -0,0 +1,144 @@
require "json"
require "fileutils"
module Maptool
# Live data off the home MQTT broker (app.rb wires the topics in): the GNSS
# tracker's position, its USB power state and the trail behind it, and the
# ITS-G5 stations its receiver hears. Thread-safe — the MQTT thread writes,
# requests read.
#
# The trail is kept in memory for TRAIL_KEEP; with logging on (a setting)
# every trail point is also appended to tracker.jsonl, which is read back
# on start, so the trail survives a restart/redeploy. The tracker's
# settings (name, icon, color, logging, trail_hours) live in settings.json
# next to it — the same for every device viewing the map.
class LiveStore
ITS_STALE_AFTER = 60 # seconds
TRAIL_KEEP = 24 * 3600 # seconds
# A trail point is only added once the tracker has moved this far, or
# this long has passed — a parked tracker repeating its position doesn't
# grow it.
TRAIL_MIN_METERS = 10
TRAIL_MIN_SECONDS = 300
DEFAULT_SETTINGS = {
"name" => "Tracker", "icon" => "automobiles", "color" => "#1a1a1a", "logging" => false, "trail_hours" => 1,
}.freeze
TRAIL_HOURS = [0, 1, 2, 4, 8, 24].freeze
def initialize(dir)
@dir = dir
@mutex = Mutex.new
@tracker = nil
@usb_power = nil
@its = {}
@settings = DEFAULT_SETTINGS.merge(read_json(settings_path) || {})
@trail = load_trail
end
def record_tracker(data, at: Time.now.to_f)
return unless data.is_a?(Hash) && data["latitude"] && data["longitude"]
@mutex.synchronize do
@tracker = data.merge("received_at" => at)
add_trail_point([at.round, data["longitude"].to_f.round(6), data["latitude"].to_f.round(6)])
end
end
# ESPHome's binary sensor state: "ON" / "OFF".
def record_usb_power(payload)
@mutex.synchronize { @usb_power = payload.to_s.strip.upcase == "ON" }
end
def record_its(data, at: Time.now.to_f)
return unless data.is_a?(Hash) && data["id"] && data["lat"] && data["lon"]
@mutex.synchronize do
@its[data["id"]] = data.merge("received_at" => at)
@its.delete_if { |_, station| at - station["received_at"] > ITS_STALE_AFTER }
end
end
# What /api/live returns: the tracker (with its age, settings, USB power
# and the trail of the last trail_hours) and the fresh ITS-G5 stations.
def snapshot(now: Time.now.to_f)
@mutex.synchronize do
tracker = @tracker && @tracker.except("received_at").merge(
"age" => (now - @tracker["received_at"]).round,
"usb_power" => @usb_power,
"trail" => @trail.select { |t, _, _| now - t <= @settings["trail_hours"] * 3600 }.map { |_, lon, lat| [lon, lat] },
)
stations = @its.values.select { |s| now - s["received_at"] <= ITS_STALE_AFTER }
.map { |s| s.except("received_at").merge("age" => (now - s["received_at"]).round) }
{ tracker: tracker, settings: @settings.dup, its: stations }
end
end
def settings
@mutex.synchronize { @settings.dup }
end
# Only known keys, sanitized; returns the new settings.
def update_settings(changes)
@mutex.synchronize do
changes = changes.to_h.transform_keys(&:to_s)
@settings["name"] = changes["name"].to_s.strip[0, 40] if changes.key?("name") && !changes["name"].to_s.strip.empty?
@settings["icon"] = changes["icon"].to_s[0, 60] if changes.key?("icon")
@settings["color"] = changes["color"] if changes["color"].to_s.match?(/\A#\h{6}\z/)
@settings["logging"] = [true, "true", "1", "on"].include?(changes["logging"]) if changes.key?("logging")
hours = changes["trail_hours"].to_s
@settings["trail_hours"] = hours.to_i if TRAIL_HOURS.include?(hours.to_i) && hours.match?(/\A\d+\z/)
FileUtils.mkdir_p(@dir)
File.write(settings_path, JSON.generate(@settings))
@settings.dup
end
end
private
def add_trail_point(point)
last = @trail.last
return if last && point[0] - last[0] < TRAIL_MIN_SECONDS && meters(last, point) < TRAIL_MIN_METERS
@trail << point
@trail.shift while @trail.first && point[0] - @trail.first[0] > TRAIL_KEEP
append_log(point) if @settings["logging"]
end
def append_log(point)
FileUtils.mkdir_p(@dir)
File.open(log_path, "a") { |f| f.puts(JSON.generate(point)) }
rescue SystemCallError => e
warn "[live] could not log the trail: #{e.message}"
end
# The last TRAIL_KEEP of the log, if there is one.
def load_trail
return [] unless File.exist?(log_path)
cutoff = Time.now.to_f - TRAIL_KEEP
File.foreach(log_path).filter_map do |line|
point = JSON.parse(line)
point if point.is_a?(Array) && point.size == 3 && point[0] >= cutoff
rescue JSON::ParserError
nil
end
end
# Equirectangular approximation; plenty for "has it moved 10 m".
def meters(a, b)
_, lon1, lat1 = a
_, lon2, lat2 = b
x = (lon2 - lon1) * Math.cos((lat1 + lat2) * Math::PI / 360)
Math.sqrt((x**2) + ((lat2 - lat1)**2)) * 111_320
end
def read_json(path)
JSON.parse(File.read(path)) if File.exist?(path)
rescue JSON::ParserError
nil
end
def settings_path = File.join(@dir, "settings.json")
def log_path = File.join(@dir, "tracker.jsonl")
end
end