Vehicle & ITS-G5 overlay: a stdlib MQTT subscriber (MQTT_URL/USERNAME/PASSWORD) keeps the car's LTE tracker position and the ITS-G5 stations heard by mcu-its-g5 (decoded header, stations expire after 60 s), served by /api/live and drawn as a car badge plus stations colored by type with DENM in red; Meshtastic noted in TODO
This commit is contained in:
1 parent
b381ca289f
commit
02c2ef05f8
12 files changed
+608
-12
No files matched your search
@@ -10,6 +10,7 @@ require_relative "lib/maptool/gpx_store"
|
||||
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/livetrack_store"
|
||||
require_relative "lib/maptool/livetrack_client"
|
||||
require_relative "lib/maptool/kiwix"
|
||||
@@ -158,6 +159,9 @@ module Maptool
|
||||
KIWIX_URL = ENV.fetch("KIWIX_URL", "")
|
||||
# The valhalla container (docker-compose.yaml), reached server side only.
|
||||
VALHALLA_URL = ENV.fetch("VALHALLA_URL", "")
|
||||
# The home MQTT broker the car's ESPHome nodes publish to (empty = off):
|
||||
# mqtt:// or mqtts://, with a read-only user. See LIVE below.
|
||||
MQTT_URL = ENV.fetch("MQTT_URL", "")
|
||||
Routing.install_service_script(DATA_DIR)
|
||||
|
||||
# Login delegates to the Gitea instance the user already has an account
|
||||
@@ -301,6 +305,43 @@ 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).
|
||||
TRACKER_TOPIC = "mcu-tracker/location"
|
||||
ITS_TOPIC = "mcu-its-g5/decoded"
|
||||
ITS_STALE_AFTER = 60 # seconds
|
||||
|
||||
LIVE = { vehicle: nil, its: {} }
|
||||
LIVE_MUTEX = Mutex.new
|
||||
|
||||
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
|
||||
end
|
||||
rescue JSON::ParserError
|
||||
nil
|
||||
end
|
||||
|
||||
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) }
|
||||
end
|
||||
end
|
||||
|
||||
get "/" do
|
||||
erb :index, locals: { logged_in: logged_in?, user: session[:user], revision: REVISION }
|
||||
end
|
||||
@@ -970,6 +1011,16 @@ module Maptool
|
||||
json(type: "FeatureCollection", features: features)
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
get "/api/gnss/position" do
|
||||
require_login!
|
||||
GNSS_LATEST_MUTEX.synchronize do
|
||||
|
||||
Reference in new issue
Block a user