Files
maptool/views/sim.erb
T

102 lines
5.6 KiB
ERB
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<title>GNSS simulation</title>
<link rel="icon" href="/icons/icon.svg" type="image/svg+xml">
<style>
:root { color-scheme: light dark; --muted: GrayText; --line: color-mix(in srgb, CanvasText 15%, transparent); --ok: #2e7d32; --bad: #c62828; }
body { margin: 0; padding: 16px; background: Canvas; color: CanvasText; font: 15px/1.4 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
header { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; margin-bottom: 16px; }
h1 { font-size: 1.2rem; margin: 0; }
h2 { font-size: 0.8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin: 0 0 8px; }
#cards { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(min(360px, 100%), 1fr)); align-items: start; }
section { border: 1px solid var(--line); border-radius: 8px; padding: 12px 16px; }
button, select { font-size: 1rem; padding: 10px 14px; border-radius: 8px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 0; }
dt { color: var(--muted); } dd { margin: 0; font-variant-numeric: tabular-nums; }
.on { color: var(--ok); font-weight: 600; } .bad { color: var(--bad); }
a { color: inherit; }
</style>
</head>
<body>
<header>
<h1>GNSS simulation</h1>
<span id="state">loading…</span>
<a href="/status.html">Server status</a> <a href="/can.html">CANopen diagnostics</a> <a href="/" target="maptool">The map</a>
</header>
<div id="cards">
<section>
<h2>Drive</h2>
<div class="actions">
<button type="button" id="route">Drive the trip's route</button>
<button type="button" id="random">From a random place</button>
<button type="button" id="stop">Stop</button>
</div>
<p id="hint" style="color: GrayText; font-size: 0.85rem"></p>
<label>Simulated time
<select id="speedup"></select>
</label>
<p style="color: GrayText; font-size: 0.85rem">Faster time: the car moves on sooner; a recorded trip keeps the real speeds (each fix carries the simulation's clock).</p>
</section>
<section>
<h2>Position</h2>
<dl id="position"></dl>
</section>
<section>
<h2>Trip</h2>
<dl id="trip"></dl>
</section>
</div>
<script>
// The simulation's controls (lib/maptool/gnss_sim.rb, /api/sim/gnss):
// the trip's route with detours, or random trips; its time sped up.
// The map (another tab) follows it with GNSS on. Polled every second.
const $ = (id) => document.getElementById(id);
const json = async (url, options) => {
const response = await fetch(url, { cache: "no-store", ...options });
const body = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(body.error || `HTTP ${response.status}`);
return body;
};
const rows = (el, pairs) => (el.innerHTML = pairs.map(([k, v]) => `<dt>${k}</dt><dd>${v ?? "—"}</dd>`).join(""));
const send = (method, body) => json("/api/sim/gnss", { method, headers: { "Content-Type": "application/json" }, body: body && JSON.stringify(body) }).catch((e) => alert(e.message)).then(refresh);
$("route").addEventListener("click", () => send("POST", { trip: true }));
$("random").addEventListener("click", () => send("POST", {}));
$("stop").addEventListener("click", () => send("DELETE"));
$("speedup").addEventListener("change", () => send("PUT", { speedup: Number($("speedup").value) }));
async function refresh() {
try {
const [sim, gnss, trip, log] = await Promise.all([
json("/api/sim/gnss"), json("/api/gnss/position"), json("/api/trip"), json("/api/trip/log"),
]);
$("state").innerHTML = sim.running ? `<span class="on">● running</span>${sim.speedup > 1 ? ` · ${sim.speedup}× time` : ""}` : sim.error ? `<span class="bad">stopped: ${sim.error}</span>` : "stopped";
if (!$("speedup").options.length) $("speedup").innerHTML = sim.speedups.map((n) => `<option value="${n}">${n}×${n === 1 ? " (real)" : ""}</option>`).join("");
$("speedup").value = String(sim.speedup);
$("route").disabled = !trip.route;
$("hint").textContent = trip.route ? `The trip's route: ${trip.route.properties.distance_km.toFixed(1)} km, with a detour now and then.` : "No trip route — plan one on the map to drive it.";
rows($("position"), gnss.available ? [
["Lat, lon", `${gnss.lat.toFixed(5)}, ${gnss.lon.toFixed(5)}`], ["Speed", gnss.speed != null ? `${Math.round(gnss.speed * 3.6)} km/h` : null],
["Course", gnss.track != null ? `${Math.round(gnss.track)}°` : null], ["Fix", gnss.mode === 3 ? "3D" : gnss.mode === 2 ? "2D" : gnss.available ? "yes" : null],
] : [["Fix", "none"]]);
rows($("trip"), [
["Stops", trip.points.length ? `${trip.points.length} (${trip.points.filter((p) => p.passed).length} passed)` : "none"],
["Recording", log.active ? `<span class="on">● ${log.folder}</span>` : "no — Start the trip in the map's Trip menu"],
["Driven", log.active ? `${(log.distance_m / 1000).toFixed(1)} km, ${log.points} points` : null],
]);
} catch (e) {
$("state").innerHTML = `<span class="bad">${e.message}</span>${/401/.test(e.message) ? ' — <a href="/login">log in</a>' : ""}`;
}
}
refresh();
setInterval(refresh, 1000);
</script>
</body>
</html>