MOSS: a small, mostly 3D-printed litter-picking rover. Cleaner streets, one maker at a time.
Python
cockpit page: reloads itself when the server comes back after a restart (a window left open never stays blank or stale)
metrox674b02deaad6
diff --git a/benches/bench_moss_cockpit_cold.py b/benches/bench_moss_cockpit_cold.pyindex 2db04b8..3c31db4 100644--- a/benches/bench_moss_cockpit_cold.py+++ b/benches/bench_moss_cockpit_cold.py@@ -473,6 +473,9 @@ check("...and the demo log says the map was cleared",any("map cleared" in str(l) for l in (st_mc.get("log") or [])) or True, "no log in state; the sent channel is the proof")code_mx, ans_mx = cli.post("/cmd/map", {"action": "wipe"})check("demo /cmd/map with another action -> refused 400", code_mx == 400, f"{code_mx} {ans_mx}")+js_src = open(os.path.join(str(ROOT), "tools", "moss_cockpit", "app.js")).read()+check("the page reloads itself when the cockpit server comes back after a restart (offlineSince + location.reload in pollState)",+ "location.reload()" in js_src and "RELOAD_AFTER_AWAY_MS" in js_src and "S.offlineSince = Date.now()" in js_src, "reload-on-return missing in app.js")check("the page has the Clear map button", 'id="b-map-clear"' in open(os.path.join(str(ROOT), "tools", "moss_cockpit", "index.html")).read(), "b-map-clear missing in tools/moss_cockpit/index.html")demo.shutdown()diff --git a/tools/moss_cockpit/app.js b/tools/moss_cockpit/app.jsindex 816130a..2100b79 100644--- a/tools/moss_cockpit/app.js+++ b/tools/moss_cockpit/app.js@@ -20,6 +20,7 @@ var FETCH_TIMEOUT_MS = 2500;var TAP_SLOP_PX = 6; // a pointer that moved less than this is a tap, not a dragvar STALE_S = 3; // a topic older than this is shown as stalevar LIVE_S = 2; // telemetry, scan and path older than this are not drawn or read as live+var RELOAD_AFTER_AWAY_MS = 3000; // the server gone this long = restarted: reload the page when it is backvar EP_FULL_S = 300; // the bottom progress line is full after 5 min of episodevar RUN_FULL_S = 1800; // ... or after 30 min of run when no episode recordsvar CAM_RETRY_MS = 3000; // reconnect the MJPEG stream this long after it ended or failed, forever@@ -143,10 +144,15 @@ function pollState() {return fetchJSON("/state.json").then(function (d) {if (!d || d.__missing) throw new Error("no state");var wasOffline = !S.online && S.stateAt > 0;- S.state = d; S.online = true; S.stateAt = Date.now();+ // the cockpit server was away long enough to have been restarted (a deploy): the page+ // reloads itself, so a window left open never shows a stale or blank page; a short blip+ // only reconnects the camera stream+ if (wasOffline && S.offlineSince && Date.now() - S.offlineSince > RELOAD_AFTER_AWAY_MS) { location.reload(); return; }+ S.state = d; S.online = true; S.stateAt = Date.now(); S.offlineSince = 0;if (wasOffline) camRestart("Cockpit back. Reconnecting the stream."); // its stream died with itrender();}, function () {+ if (S.online || !S.offlineSince) S.offlineSince = Date.now();S.online = false;render();});