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
This commit is contained in:
1 parent
5f4c7c6ee8
commit
17891c53b5
16 files changed
+513
-5
No files matched your search
@@ -276,6 +276,20 @@ html, body {
|
||||
background: #2a9d8f;
|
||||
}
|
||||
|
||||
/* A newer version for the car's server (public/js/update.js). */
|
||||
.topbar__update {
|
||||
height: 32px;
|
||||
padding: 0 10px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: #2a9d8f;
|
||||
color: #fff;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.topbar__update:disabled { opacity: 0.7; cursor: progress; }
|
||||
|
||||
.topbar__auth {
|
||||
margin-left: auto;
|
||||
display: flex;
|
||||
|
||||
@@ -23,6 +23,7 @@ import { getPosition } from "/vendor/suncalc/suncalc.js";
|
||||
import { notifySidePanelOpen, onlyOneSidePanelOpen, setupSidePanelModes } from "/js/panels.js";
|
||||
import { isLoggedIn } from "/js/auth.js";
|
||||
import { DebugControl } from "/js/debug.js";
|
||||
import { setupUpdate } from "/js/update.js";
|
||||
import { patchOnlineStyle, patternImage, PATTERN_IDS, NightMap } from "/js/map_themes.js";
|
||||
import { offerSharedFiles } from "/js/share_import.js";
|
||||
import HomeButton from "/vendor/npmap-home-button/home-button.esm.js";
|
||||
@@ -555,6 +556,7 @@ gnssControl.mount(sidebar);
|
||||
new LiveTrackPanelControl(map).mount(sidebar);
|
||||
|
||||
if (isLoggedIn()) new DebugControl().mount(sidebar);
|
||||
if (isLoggedIn()) setupUpdate(document.querySelector(".topbar__update"));
|
||||
setupSidePanelModes();
|
||||
|
||||
// The sidebar already has a "Base map" icon (opens the same panel) —
|
||||
|
||||
+80
-1
@@ -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", () => {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
// The car's server updating itself (GET/POST /api/update, SELF_UPDATE=1 in
|
||||
// its .env): a topbar button shows up when main has a newer version that
|
||||
// passed CI; a click installs it (bin/update-maptool.sh on the host) and the
|
||||
// page reloads once the server answers with the new revision.
|
||||
const CHECK_MS = 10 * 60 * 1000;
|
||||
const RESTART_POLL_MS = 5000;
|
||||
|
||||
export function setupUpdate(button) {
|
||||
let latest = null;
|
||||
|
||||
async function check() {
|
||||
try {
|
||||
const state = await (await fetch("/api/update", { cache: "no-store" })).json();
|
||||
if (!state.enabled) return;
|
||||
latest = state.latest;
|
||||
button.hidden = !state.available;
|
||||
button.textContent = `⟳ ${state.latest}`;
|
||||
button.title = `A new version (${state.latest}, now ${state.current}) — click to update; the app restarts for about a minute`;
|
||||
if (state.status?.state === "failed") button.title += `\nThe last update failed: ${state.status.message}`;
|
||||
setTimeout(check, CHECK_MS);
|
||||
} catch {
|
||||
setTimeout(check, CHECK_MS);
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForRestart(revision) {
|
||||
try {
|
||||
const health = await (await fetch("/api/health", { cache: "no-store" })).json();
|
||||
if (health.revision !== revision) return window.location.reload();
|
||||
} catch {
|
||||
// restarting: not answering for a while
|
||||
}
|
||||
setTimeout(() => waitForRestart(revision), RESTART_POLL_MS);
|
||||
}
|
||||
|
||||
button.addEventListener("click", async () => {
|
||||
if (!window.confirm(`Update to ${latest}? The app restarts — about a minute without it.`)) return;
|
||||
button.disabled = true;
|
||||
button.textContent = "Updating…";
|
||||
const response = await fetch("/api/update", { method: "POST" });
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
window.alert(data.error || `Update failed (${response.status})`);
|
||||
button.disabled = false;
|
||||
return check();
|
||||
}
|
||||
const revision = (await (await fetch("/api/health")).json()).revision;
|
||||
waitForRestart(revision);
|
||||
});
|
||||
|
||||
check();
|
||||
}
|
||||
Reference in new issue
Block a user