Shared trip: one itinerary per server (data/trip.json, GET/PUT /api/trip, routed server-side), the route panel edits it when logged in and polls other screens' changes in without moving the camera; the GNSS simulation skips trips under 1 km and starts elsewhere when it can't get on from a road island; the car's server updates itself from a topbar button (SELF_UPDATE=1: newer main with CI passed → bin/update-maptool.sh via maptool-update.path); Android Auto and the trip's next steps in TODO
CI / check (push) Successful in 22s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-30 19:19:37 +01:00
1 parent 5f4c7c6ee8
commit 17891c53b5
16 files changed
+513 -5

No files matched your search

+80 -1
View File
@@ -6,6 +6,12 @@ import { wirePreview } from "/js/preview.js";
import { elevationAt } from "/js/dem.js";
const ROUTE_URL = "/api/route";
// Logged in, the panel edits this server's shared trip (lib/maptool/trip.rb)
// instead of a route of its own: every change goes there (routed by the
// server), and other screens' changes come in by polling — the points,
// vehicle, avoids and route swapped in, the camera left where it is.
const TRIP_URL = "/api/trip";
const TRIP_POLL_MS = 2000;
const ISOCHRONE_URL = "/api/isochrone";
const REACH_SOURCE = "route-reach";
const PROFILE_SAMPLES = 400; // elevation lookups per route, at least 50 m apart
@@ -40,6 +46,8 @@ export class RoutePanelControl {
this._route = null; // the last /api/route Feature
this._request = 0; // so a slow older response can't overwrite a newer one
this._editing = null; // { key, id, name } of a saved route being edited
this._version = -1; // the shared trip's, -1: not loaded yet
this._pushing = 0; // PUTs in flight: polls wait for them
// Rerouting (the dashboard, off the route): the start and the via points
// already passed give way to the current position.
@@ -105,6 +113,74 @@ export class RoutePanelControl {
onlyOneSidePanelOpen(this._panel, () => this._setOpen(false));
sidebar.appendChild(this._container);
this._shared = isLoggedIn();
if (this._shared) {
this._pollTrip();
setInterval(() => this._pollTrip(), TRIP_POLL_MS);
}
}
// --- shared trip ------------------------------------------------------------
async _pollTrip() {
if (this._pushing) return;
try {
const response = await fetch(`${TRIP_URL}?version=${this._version}`, { cache: "no-store" });
if (response.status !== 200) return; // 204: unchanged
const trip = await response.json();
if (!this._pushing && trip.version > this._version) this._applyTrip(trip, true);
} catch {
// offline for a moment: the next poll
}
}
async _pushTrip() {
const request = ++this._request;
this._pushing++;
// the old route stays on the map (the driver's view) until the new one is in
if (this._points.length >= 2) this._summary.textContent = "Routing…";
try {
const response = await fetch(TRIP_URL, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
points: this._points.map(({ lngLat, name, nav }) => ({ lngLat, name, nav })),
vehicle: this._vehicleId,
avoid: this._avoid,
editing: this._editing,
}),
});
const trip = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(trip.error || `HTTP ${response.status}`);
if (request === this._request) this._applyTrip(trip, false);
} catch (err) {
if (request === this._request) this._showResult(null, `Not saved: ${err.message}`);
} finally {
this._pushing--;
}
}
// The server's trip into the panel: its route always, its points (and
// vehicle, avoids, the library route being edited) when they came from
// another screen.
_applyTrip(trip, fromElsewhere) {
this._version = trip.version;
if (fromElsewhere) {
this._points.forEach((p) => p.marker.remove());
this._points = [];
trip.points.forEach((p) => this._addPoint(p.lngLat, p.name, { quiet: true, nav: !!p.nav }));
this._editing = trip.editing;
this._avoid = { ...trip.avoid };
this._panel.querySelectorAll(".route-panel__avoid input").forEach((input) => (input.checked = !!this._avoid[input.name]));
if (trip.vehicle && trip.vehicle !== this._vehicleId) {
if (this._selectVehicle) this._selectVehicle(trip.vehicle);
else this._vehicleId = trip.vehicle;
}
this._renderPoints();
}
this._route = trip.route;
this._showResult(trip.route, trip.error ? `No route: ${trip.error}` : "");
}
_togglePanel(force) {
@@ -226,6 +302,7 @@ export class RoutePanelControl {
}
point.nav = !point.nav;
this._renderPoints();
if (this._shared) this._pushTrip(); // the server keeps the route, only the flag changes
});
button.classList.add("route-panel__nav");
button.classList.toggle("active", !!point.nav);
@@ -248,6 +325,7 @@ export class RoutePanelControl {
// --- route ----------------------------------------------------------------
async _fetchRoute() {
if (this._shared) return this._pushTrip();
const request = ++this._request;
this._route = null;
if (this._points.length < 2) {
@@ -599,7 +677,8 @@ export class RoutePanelControl {
};
try {
this._vehicleId = localStorage.getItem(VEHICLE_KEY);
// the shared trip's vehicle, when it came in before the vehicles did
this._vehicleId = this._version >= 0 && this._vehicleId ? this._vehicleId : localStorage.getItem(VEHICLE_KEY);
} catch {}
fill();
select.addEventListener("change", () => {