135 lines
7.8 KiB
ERB
135 lines
7.8 KiB
ERB
<!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>
|
||
<%= erb :_debug_nav, layout: false %>
|
||
<header>
|
||
<h1>GNSS simulation</h1>
|
||
<span id="state">loading…</span>
|
||
</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>
|
||
<div class="actions" style="margin-top: 8px">
|
||
<select id="track" aria-label="A library track"><option value="">A library track…</option></select>
|
||
<button type="button" id="replay">Replay it</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>
|
||
<section id="outputs" hidden>
|
||
<h2>Relays</h2>
|
||
<div id="relays"></div>
|
||
<p style="color: GrayText; font-size: 0.85rem">can.json's "outputs" (simulated: no frames sent). A click switches one; the keypad's bindings with an "output" do too.</p>
|
||
</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"));
|
||
$("replay").addEventListener("click", () => $("track").value && send("POST", { track: $("track").value }));
|
||
// the library's tracks (a recorded one replays at its own speed; one with no times can't)
|
||
json("/api/library/tree").then((tree) => {
|
||
const tracks = [];
|
||
const walk = (node, path) => node.children.forEach((c) => (c.type === "folder" ? walk(c, c.path) : c.kinds?.includes("track") && tracks.push([c.path, [path, c.name].filter(Boolean).join(" / ")])));
|
||
walk(tree, "");
|
||
const esc = (s) => s.replace(/[&<>"]/g, (ch) => `&#${ch.charCodeAt(0)};`);
|
||
$("track").innerHTML += tracks.map(([key, label]) => `<option value="${key}">${esc(label)}</option>`).join("");
|
||
}).catch(() => {});
|
||
$("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>' : ""}`;
|
||
}
|
||
}
|
||
// The relay modules (GET /api/outputs): a button per channel, lit when on.
|
||
async function relays() {
|
||
const data = await json("/api/outputs").catch(() => null);
|
||
$("outputs").hidden = !data;
|
||
if (!data) return;
|
||
const esc = (s) => String(s).replace(/[&<>"]/g, (ch) => `&#${ch.charCodeAt(0)};`);
|
||
const by = (o) => data.bindings.filter((b) => b.output === o).map((b) => `${b.input} (${b.mode || "toggle"})`).join(", ");
|
||
$("relays").innerHTML = data.modules.map((m) => `<p>${esc(m.name)} · node ${m.node}${m.simulated ? " · simulated" : ""}</p><div class="actions">` +
|
||
m.channels.map((c) => `<button type="button" data-output="${esc(m.name)}/${c.n}" class="${c.on ? "on" : ""}" title="${esc(by(`${m.name}.${c.n}`))}">${c.on ? "●" : "○"} ${c.n}${c.label ? ` ${esc(c.label)}` : ""}</button>`).join("") + "</div>").join("");
|
||
}
|
||
$("relays").addEventListener("click", (e) => {
|
||
const b = e.target.closest("button[data-output]");
|
||
if (b) json(`/api/outputs/${b.dataset.output}`, { method: "POST" }).catch((err) => alert(err.message)).then(relays);
|
||
});
|
||
refresh();
|
||
relays();
|
||
setInterval(() => { refresh(); relays(); }, 1000);
|
||
</script>
|
||
</body>
|
||
</html>
|