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
@@ -108,6 +108,28 @@ class AppTest < Minitest::Test
|
||||
assert_equal 401, request.get("/api/gnss/position").status
|
||||
end
|
||||
|
||||
def test_live_requires_login
|
||||
assert_equal 401, request.get("/api/live").status
|
||||
end
|
||||
|
||||
# The car's last position stays (with its age); ITS stations expire.
|
||||
def test_live_keeps_the_vehicle_and_drops_stale_its_stations
|
||||
app = Maptool::App
|
||||
now = Time.now.to_f
|
||||
app.record_live(app::TRACKER_TOPIC, '{"latitude":48.1,"longitude":17.1,"speed":3}', at: now - 600)
|
||||
app.record_live(app::ITS_TOPIC, '{"id":"old","msg":"CAM","lat":48,"lon":17}', at: now - app::ITS_STALE_AFTER - 5)
|
||||
app.record_live(app::ITS_TOPIC, '{"id":"new","msg":"DENM","lat":48.2,"lon":17.2}', at: now)
|
||||
app.record_live(app::ITS_TOPIC, "not json")
|
||||
body = json_body(request.get("/api/live", **logged_in_env))
|
||||
assert_equal 48.1, body["vehicle"]["latitude"]
|
||||
assert_in_delta 600, body["vehicle"]["age"], 2
|
||||
refute body["vehicle"].key?("received_at")
|
||||
assert_equal ["new"], body["its"].map { |s| s["id"] }
|
||||
assert_equal false, body["available"] # no MQTT_URL in tests
|
||||
ensure
|
||||
app::LIVE_MUTEX.synchronize { app::LIVE.replace(vehicle: nil, its: {}) }
|
||||
end
|
||||
|
||||
# --- auth guard ---
|
||||
|
||||
def test_write_routes_require_login
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
require_relative "test_helper"
|
||||
require "socket"
|
||||
require_relative "../lib/maptool/mqtt_client"
|
||||
|
||||
class MqttClientTest < Minitest::Test
|
||||
# A one-connection fake broker: checks CONNECT/SUBSCRIBE, then sends a QoS 0
|
||||
# and a QoS 1 PUBLISH and records what the client sends back.
|
||||
def test_connects_subscribes_and_yields_messages
|
||||
server = TCPServer.new("127.0.0.1", 0)
|
||||
port = server.addr[1]
|
||||
seen = {}
|
||||
broker = Thread.new do
|
||||
conn = server.accept
|
||||
type, body = read_packet(conn)
|
||||
seen[:connect] = [type, body]
|
||||
conn.write("\x20\x02\x00\x00".b) # CONNACK, accepted
|
||||
type, body = read_packet(conn)
|
||||
seen[:subscribe] = [type, body]
|
||||
conn.write("\x90\x04\x00\x01\x00\x00".b) # SUBACK
|
||||
conn.write(publish("mcu-tracker/location", '{"latitude":48.1}', qos: 0))
|
||||
conn.write(publish("mcu-its-g5/decoded", '{"id":"a"}', qos: 1, packet_id: 7))
|
||||
seen[:puback] = read_packet(conn)
|
||||
sleep 0.2
|
||||
conn.close
|
||||
end
|
||||
|
||||
client = Maptool::MqttClient.new(url: "mqtt://127.0.0.1:#{port}", username: "maptool", password: "secret",
|
||||
topics: ["mcu-tracker/location", "mcu-its-g5/decoded"], client_id: "test")
|
||||
messages = []
|
||||
worker = Thread.new { client.each_message { |topic, payload| messages << [topic, payload] } }
|
||||
broker.join(5)
|
||||
sleep 0.1
|
||||
worker.kill
|
||||
|
||||
assert_equal [["mcu-tracker/location", '{"latitude":48.1}'], ["mcu-its-g5/decoded", '{"id":"a"}']], messages
|
||||
type, body = seen[:connect]
|
||||
assert_equal 1, type
|
||||
assert_includes body, "MQTT".b
|
||||
assert_equal 0xC2, body.getbyte(7) # clean session + username + password
|
||||
assert_includes body, "maptool".b
|
||||
assert_includes body, "secret".b
|
||||
type, body = seen[:subscribe]
|
||||
assert_equal 8, type
|
||||
assert_includes body, "mcu-its-g5/decoded".b
|
||||
assert_equal [4, "\x00\x07".b], seen[:puback] # PUBACK for packet 7
|
||||
ensure
|
||||
server&.close
|
||||
end
|
||||
|
||||
def test_rejects_other_url_schemes
|
||||
assert_raises(ArgumentError) { Maptool::MqttClient.new(url: "http://broker:1883", topics: []) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def read_packet(conn)
|
||||
header = conn.read(1).ord
|
||||
length = conn.read(1).ord # tests stay under 128 bytes
|
||||
[header >> 4, conn.read(length)]
|
||||
end
|
||||
|
||||
def publish(topic, payload, qos:, packet_id: nil)
|
||||
body = [topic.bytesize].pack("n") + topic.b + (qos.positive? ? [packet_id].pack("n") : "".b) + payload.b
|
||||
[0x30 | (qos << 1), body.bytesize].pack("CC") + body
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user