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
This commit is contained in:
1 parent
3fc8c75c60
commit
0e5f79f8e4
9 files changed
+801
-205
No files matched your search
@@ -11,6 +11,7 @@ require_relative "lib/maptool/library"
|
||||
require_relative "lib/maptool/routing"
|
||||
require_relative "lib/maptool/gpsd_client"
|
||||
require_relative "lib/maptool/mqtt_client"
|
||||
require_relative "lib/maptool/live_store"
|
||||
require_relative "lib/maptool/livetrack_store"
|
||||
require_relative "lib/maptool/livetrack_client"
|
||||
require_relative "lib/maptool/kiwix"
|
||||
@@ -305,31 +306,20 @@ module Maptool
|
||||
end
|
||||
end
|
||||
|
||||
# Live vehicle data off the home MQTT broker (MQTT_URL), polled by the
|
||||
# frontend from /api/live like GNSS above: the car's position from its LTE
|
||||
# GNSS tracker, and ITS-G5 stations heard by its receiver (one decoded
|
||||
# header per station and second). A station drops out once it hasn't been
|
||||
# heard for ITS_STALE_AFTER; the car's last position stays, with its age
|
||||
# (a parked tracker reports rarely).
|
||||
# Live data off the home MQTT broker (MQTT_URL), polled by the frontend
|
||||
# from /api/live like GNSS above — the "Tracker" and "ITS-G5" overlays:
|
||||
# the LTE GNSS tracker's position and USB power (+ its trail, optionally
|
||||
# logged), and ITS-G5 stations heard by its receiver. See LiveStore.
|
||||
TRACKER_TOPIC = "mcu-tracker/location"
|
||||
TRACKER_POWER_TOPIC = "mcu-tracker/binary_sensor/usb_power/state"
|
||||
ITS_TOPIC = "mcu-its-g5/decoded"
|
||||
ITS_STALE_AFTER = 60 # seconds
|
||||
|
||||
LIVE = { vehicle: nil, its: {} }
|
||||
LIVE_MUTEX = Mutex.new
|
||||
LIVE_STORE = LiveStore.new(File.join(DATA_DIR, "live"))
|
||||
|
||||
def self.record_live(topic, payload, at: Time.now.to_f)
|
||||
data = JSON.parse(payload)
|
||||
return unless data.is_a?(Hash)
|
||||
|
||||
LIVE_MUTEX.synchronize do
|
||||
case topic
|
||||
when TRACKER_TOPIC
|
||||
LIVE[:vehicle] = data.merge("received_at" => at) if data["latitude"] && data["longitude"]
|
||||
when ITS_TOPIC
|
||||
LIVE[:its][data["id"]] = data.merge("received_at" => at) if data["id"] && data["lat"] && data["lon"]
|
||||
LIVE[:its].delete_if { |_, station| at - station["received_at"] > ITS_STALE_AFTER }
|
||||
end
|
||||
case topic
|
||||
when TRACKER_TOPIC then LIVE_STORE.record_tracker(JSON.parse(payload), at: at)
|
||||
when TRACKER_POWER_TOPIC then LIVE_STORE.record_usb_power(payload)
|
||||
when ITS_TOPIC then LIVE_STORE.record_its(JSON.parse(payload), at: at)
|
||||
end
|
||||
rescue JSON::ParserError
|
||||
nil
|
||||
@@ -338,7 +328,7 @@ module Maptool
|
||||
unless MQTT_URL.empty?
|
||||
Thread.new do
|
||||
MqttClient.new(url: MQTT_URL, username: ENV.fetch("MQTT_USERNAME", ""), password: ENV.fetch("MQTT_PASSWORD", ""),
|
||||
topics: [TRACKER_TOPIC, ITS_TOPIC]).each_message { |topic, payload| record_live(topic, payload) }
|
||||
topics: [TRACKER_TOPIC, TRACKER_POWER_TOPIC, ITS_TOPIC]).each_message { |topic, payload| record_live(topic, payload) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1037,12 +1027,13 @@ module Maptool
|
||||
|
||||
get "/api/live" do
|
||||
require_login!
|
||||
now = Time.now.to_f
|
||||
age = ->(item) { item.except("received_at").merge("age" => (now - item["received_at"]).round) }
|
||||
LIVE_MUTEX.synchronize do
|
||||
stations = LIVE[:its].values.select { |station| now - station["received_at"] <= ITS_STALE_AFTER }
|
||||
json(available: !MQTT_URL.empty?, vehicle: LIVE[:vehicle] && age.(LIVE[:vehicle]), its: stations.map(&age))
|
||||
end
|
||||
json LIVE_STORE.snapshot.merge(available: !MQTT_URL.empty?)
|
||||
end
|
||||
|
||||
# The tracker's name, icon, color, trail logging and trail length.
|
||||
put "/api/live/settings" do
|
||||
require_login!
|
||||
json LIVE_STORE.update_settings(params.slice("name", "icon", "color", "logging", "trail_hours"))
|
||||
end
|
||||
|
||||
get "/api/gnss/position" do
|
||||
|
||||
Reference in new issue
Block a user