diff --git a/app.rb b/app.rb index e8f0aea..9b46aa6 100644 --- a/app.rb +++ b/app.rb @@ -1341,8 +1341,13 @@ module Maptool post "/api/sim/gnss" do require_login! - lon, lat = JSON.parse(request.body.read)["start"] - if lon.nil? + body = JSON.parse(request.body.read) + lon, lat = body["start"] + # {"trip": true}: along the shared trip's route (to try the navigation) + route = TRIP_STORE.get[:route] if body["trip"] + halt 422, json(error: "the trip has no route") if body["trip"] && !route + if route then nil + elsif lon.nil? codes = (PmtilesBuilder.offline_meta(DATA_DIR) || {}).fetch("sources", []).map { |source| source["code"] } bboxes = codes.filter_map { |code| PmtilesBuilder.country_bbox(code) } halt 422, json(error: "no offline countries to start in") if bboxes.empty? @@ -1351,7 +1356,7 @@ module Maptool end CAN_STORE.override_prefix = "gnss." - GNSS_SIM.start(lon, lat, bboxes: bboxes) do |fix| + GNSS_SIM.start(lon, lat, bboxes: bboxes, route: route) do |fix| CAN_STORE.put(fix.transform_keys { |key| "gnss.#{key}" }) App.gnss_update(fix) end diff --git a/docs/deployment.adoc b/docs/deployment.adoc index 07ded00..8ad4677 100644 --- a/docs/deployment.adoc +++ b/docs/deployment.adoc @@ -326,7 +326,7 @@ The node sends no course: `App.gnss_update` (where every fix lands, the node's and the GNSS simulation's) works it out from the fix ≥ 5 m back while moving ≥ 1.5 m/s and keeps the last one standing still, so `/api/gnss/position` has `track` and following in 3D turns heading-up. -The simulation (Dashboard settings, `POST`/`DELETE /api/sim/gnss`) drives +The simulation (Dashboard settings, `POST`/`DELETE /api/sim/gnss`; `{"trip": true}` drives the shared trip's route first, the dashboard's button does when there is one) drives random trips over the local Valhalla and stands in for the node's `gnss.*` meanwhile — on the car's server only for trying things out. While it runs, the dashboard takes the altitude from the terrain (no `gnss.alt` diff --git a/lib/maptool/gnss_sim.rb b/lib/maptool/gnss_sim.rb index 97d7ce2..a309668 100644 --- a/lib/maptool/gnss_sim.rb +++ b/lib/maptool/gnss_sim.rb @@ -38,14 +38,19 @@ module Maptool # Drives from [lon, lat] until stopped — or, with bboxes # ([[west, south, east, north], ...], the offline countries) instead, - # from a random place in one of them; each fix to the block: + # from a random place in one of them; with a route (the trip's /api/route + # Feature), that route from its start first, random trips after it; + # each fix to the block: # { lat:, lon:, speed: m/s, satellites:, hdop: }. - def start(lon = nil, lat = nil, bboxes: nil, &on_fix) + def start(lon = nil, lat = nil, bboxes: nil, route: nil, &on_fix) stop @error = nil @mutex.synchronize do @thread = Thread.new do - position, route = bboxes ? random_start(bboxes) : [[Float(lon), Float(lat)], nil] + position, route = if route then [route[:geometry][:coordinates].first, route] + elsif bboxes then random_start(bboxes) + else [[Float(lon), Float(lat)], nil] + end loop do position = drive(position, route, &on_fix) route = nil diff --git a/public/js/dashboard.js b/public/js/dashboard.js index d174201..f9a5376 100644 --- a/public/js/dashboard.js +++ b/public/js/dashboard.js @@ -176,6 +176,7 @@ export class DashboardControl { this._lastAt = 0; this._offFixes = 0; this._sentKm = undefined; // the new route drawn by the next fix + this._simLabel(); })); this._trace = []; // [lon, lat] fixes TRACE_STEP_M apart, for /api/road this._road = null; // its last answer @@ -340,7 +341,8 @@ export class DashboardControl { // driving from a random place in the offline countries, the GNSS control // turned on to follow it; on = true / false, null = just read the state. async _simulate(on) { - const options = on === true ? { method: "POST", headers: { "Content-Type": "application/json" }, body: "{}" } + // along the route when there is one (the shared trip's), else from a random place + const options = on === true ? { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(this._route ? { trip: true } : {}) } : on === false ? { method: "DELETE" } : {}; try { const response = await fetch("/api/sim/gnss", options); @@ -354,7 +356,11 @@ export class DashboardControl { this._simStatus.hidden = false; this._simStatus.textContent = `Simulation: ${e.message}`; } - this._simButton.textContent = this._simRunning ? "Stop the simulation" : "Simulate driving from the map center"; + this._simLabel(); + } + + _simLabel() { + if (this._simButton) this._simButton.textContent = this._simRunning ? "Stop the simulation" : this._route ? "Simulate driving the route" : "Simulate driving from a random place"; } _save() { diff --git a/test/test_gnss_sim.rb b/test/test_gnss_sim.rb index 027dd93..57f1ab9 100644 --- a/test/test_gnss_sim.rb +++ b/test/test_gnss_sim.rb @@ -22,6 +22,19 @@ class GnssSimTest < Minitest::Test assert_in_delta (1000 / (36 / 3.6) / 2 + 500 / (45 / 3.6)) / Maptool::GnssSim::TICK_S, fixes.size, 3 # a fix every TICK_S end + # Started with the trip's route: from its start along it, before any random trip. + def test_starts_along_a_given_route + fixes = Queue.new + sim = Maptool::GnssSim.new("http://valhalla", sleep: ->(_s) {}, router: ->(*) { raise Maptool::Routing::RouteError, "done" }) + route = { geometry: { coordinates: [[18.0, 49.0], [18.0, 49.001]] }, properties: { speed_limits: [[0.0, 50]] } } + sim.start(route: route) { |fix| fixes << fix } + first = fixes.pop + assert_in_delta 49.0, first[:lat], 0.0001 + assert_in_delta 18.0, first[:lon], 1e-9 + ensure + sim&.stop + end + # No road at the first random place: another one, inside the bbox. class FirstPlaceAtSea < Maptool::GnssSim attr_reader :tried