Nodes describe themselves: an SDO client (Can::Sdo) reads each esphome-canopen node's TPDOs and entities (type, name, unit, range) on its first heartbeat, cached by the TPDO layout hash, decoded with no hand-written signals — can.json keeps aliases (with unit conversion) and foreign devices; design doc (NPS planning, cockpit instruments, Unigrid band) with color tokens, the dashboard in cockpit colors; starting the simulation turns the GNSS follow on, the GNSS position an arrow by its course
CI / check (push) Successful in 23s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-30 19:51:03 +01:00
1 parent ce290f2162
commit e92f01db12
12 files changed
+713 -76

No files matched your search

+4 -8
View File
@@ -1,5 +1,5 @@
import { isLoggedIn } from "/js/auth.js";
import { onRouteChanged, requestReroute } from "/js/panels.js";
import { onRouteChanged, requestReroute, requestGnssFollow } from "/js/panels.js";
// The car's dashboard over the map, off the CAN signals (GET /api/can,
// names from data/can.json): one inclinometer — compass ring around a rear
@@ -251,17 +251,17 @@ export class DashboardControl {
}
// GNSS simulation on the server (POST/DELETE /api/sim/gnss): random
// driving from a random place in the offline countries, the map jumping
// there with the first fix; on = true / false, null = just read the state.
// 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: "{}" }
: on === false ? { method: "DELETE" } : {};
this._jumpToFix = on === true;
try {
const response = await fetch("/api/sim/gnss", options);
const state = await response.json();
if (!response.ok) throw new Error(state.error || `HTTP ${response.status}`);
this._simRunning = state.running;
if (on === true && state.running) requestGnssFollow(); // the map follows the simulated position
this._simStatus.hidden = !state.error;
this._simStatus.textContent = state.error ? `Simulation stopped: ${state.error}` : "";
} catch (e) {
@@ -305,10 +305,6 @@ export class DashboardControl {
// Date has 1 s resolution: +1 so a fresh value isn't "stale"
return [key, s && now - s.at < STALE_S + 1 ? s.value : null];
}));
if (this._jumpToFix && this._simRunning && this._values.lat != null) {
this._jumpToFix = false;
this._map.jumpTo({ center: [this._values.lon, this._values.lat], zoom: Math.max(this._map.getZoom(), 15) });
}
this._updateCourse(this._values);
this._navigate();
this._status.textContent = this._simRunning ? "Simulated GNSS" : data.available ? "" : "CAN is off on this server";
+22 -1
View File
@@ -1,9 +1,13 @@
import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs";
import { GNSS_ICON } from "/js/icons.js";
import { isLoggedIn } from "/js/auth.js";
import { onGnssFollow } from "/js/panels.js";
const POSITION_URL = "/api/gnss/position";
const POLL_INTERVAL_MS = 1500;
const JUMP_M = 1000; // farther than this from the marker: jump there, not glide (a new simulation)
// The position as an arrow, turned by the course once there is one.
const ARROW = `<svg viewBox="0 0 24 24" width="26" height="26"><path d="M12 2 L20 21 L12 16.5 L4 21 Z" fill="#2a9d8f" stroke="#fff" stroke-width="2" stroke-linejoin="round"/></svg>`;
// A sidebar icon that polls /api/gnss/position (the car's receiver, fed
// server-side into GNSS_LATEST in app.rb) and shows the fix as a
@@ -27,6 +31,11 @@ export function followPosition(map, lngLat, heading, speed, duration) {
});
}
// Meters between a MapLibre LngLat and [lon, lat] (equirectangular).
function distanceM({ lng, lat }, [lon2, lat2]) {
return Math.hypot((lon2 - lng) * Math.cos((lat * Math.PI) / 180), lat2 - lat) * 111320;
}
// The marker moved to lngLat the same way: a linear glide over duration ms.
function glideMarker(marker, lngLat, duration) {
cancelAnimationFrame(marker._glide);
@@ -71,6 +80,7 @@ export class GnssControl {
this._flyout = this._container.querySelector(".sidebar-flyout");
this._status = this._container.querySelector(".gnss-control__status");
this._icon.addEventListener("click", () => this._setEnabled(!this._enabled));
onGnssFollow(() => !this._enabled && !this._container.hidden && this._setEnabled(true));
// GET /api/gnss/position itself requires login too (app.rb) — hidden
// here rather than shown and failing on click, same reasoning as
@@ -128,6 +138,8 @@ export class GnssControl {
// flyTo (with its zoom swoop) only for the first fix, since that's the
// one jumping from wherever the map already was; easeTo for later
// updates, a plain follow rather than repeating that swoop every poll.
const far = this._marker && distanceM(this._marker.getLngLat(), lngLat) > JUMP_M;
if (far) this._removeMarker();
if (!this._marker) {
const el = document.createElement("div");
el.className = "gnss-marker";
@@ -135,12 +147,21 @@ export class GnssControl {
// style's sources/layers, so — unlike the radar/GPX layers — this
// survives map.setStyle() (switching Online/Offline) with no extra
// re-add handling needed.
this._marker = new Marker({ element: el }).setLngLat(lngLat).addTo(this._map);
this._marker = new Marker({ element: el, rotationAlignment: "map", pitchAlignment: "map" }).setLngLat(lngLat).addTo(this._map);
this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) });
} else {
glideMarker(this._marker, lngLat, POLL_INTERVAL_MS);
followPosition(this._map, lngLat, data.track, data.speed, POLL_INTERVAL_MS);
}
// an arrow turned by the course, once there is one (a dot until then)
if (Number.isFinite(data.track)) {
const el = this._marker.getElement();
if (!el.classList.contains("gnss-marker--arrow")) {
el.classList.add("gnss-marker--arrow");
el.innerHTML = ARROW;
}
this._marker.setRotation(data.track);
}
}
_removeMarker() {
+12
View File
@@ -135,6 +135,18 @@ export function onReroute(callback) {
document.addEventListener(REROUTE_EVENT, (event) => callback(event.detail));
}
// Something that moves the GNSS position (the simulation) wants the map
// to follow it: the GNSS control turns itself on.
const GNSS_FOLLOW_EVENT = "gnss-follow";
export function requestGnssFollow() {
document.dispatchEvent(new CustomEvent(GNSS_FOLLOW_EVENT));
}
export function onGnssFollow(callback) {
document.addEventListener(GNSS_FOLLOW_EVENT, callback);
}
export function onRouteChanged(callback) {
document.addEventListener(ROUTE_CHANGED_EVENT, (event) => callback(event.detail));
return currentRoute;