diff options
author | stuebinm | 2024-05-08 23:34:43 +0200 |
---|---|---|
committer | stuebinm | 2024-05-09 01:31:26 +0200 |
commit | dc519ae889ab40fe1723cd601c3e79b73bdd2f51 (patch) | |
tree | 969bd8472ca40ebdd07eee46fc8c8506d1355f94 /site | |
parent | ad8a09cafa519a15a22cafbfd2fa289538edc73d (diff) |
restructure: split web frontend into several modules
Diffstat (limited to 'site')
-rw-r--r-- | site/obu.hamlet | 132 | ||||
-rw-r--r-- | site/tracker.hamlet | 147 |
2 files changed, 0 insertions, 279 deletions
diff --git a/site/obu.hamlet b/site/obu.hamlet deleted file mode 100644 index ed8017a..0000000 --- a/site/obu.hamlet +++ /dev/null @@ -1,132 +0,0 @@ -<h1>_{MsgOBU} - -<section> - <h2>#{ticketTripName} _{Msgon} #{ticketDay} - <strong>Token:</strong> <span id="token"> - -<section> - <h2>_{MsgLive} - <p><strong>Position: </strong><span id="lat"></span>, <span id="long"></span> - <p><strong>Accuracy: </strong><span id="acc"> - -<section> - <h2>_{MsgEstimated} - <p><strong>_{MsgDelay}</strong>: <span id="delay"> - <p><strong>_{MsgSequence}</strong>: <span id="sequence"> - -<section> - <h2>Status - <p id="status">_{MsgNone} - <p id>_{MsgError}: <span id="error"> - - -<script> - var token = null; - - let euclid = (a,b) => { - let x = a[0]-b[0]; - let y = a[1]-b[1]; - return x*x+y*y; - } - - let minimalDist = (point, list, proj, norm) => { - return list.reduce ( - (min, x) => { - let dist = norm(point, proj(x)); - return dist < min[0] ? [dist,x] : min - }, - [norm(point, proj(list[0])), list[0]] - )[1] - } - - let counter = 0; - let ws; - let id; - - async function geoError(error) { - document.getElementById("status").innerText = "error"; - alert(`_{MsgPermissionFailed}: \n${error.message}`); - console.log(error); - } - - async function wsError(error) { - // alert(`_{MsgWebsocketError}: \n${error.message === undefined ? error.reason : error.message}`); - console.log(error); - navigator.geolocation.clearWatch(id); - } - - async function wsClose(error) { - console.log(error); - document.getElementById("error").innerText = `websocket closed (reason: ${error.reason}). reconnecting …`; - navigator.geolocation.clearWatch(id); - setTimeout(openWebsocket, 1000); - } - - function wsMsg(msg) { - let json = JSON.parse(msg.data); - console.log(json); - document.getElementById("delay").innerText = - `${json.delay}s (${Math.floor(json.delay / 60)}min)`; - document.getElementById("sequence").innerText = json.sequence; - } - - - function initGeopos() { - document.getElementById("error").innerText = ""; - id = navigator.geolocation.watchPosition( - geoPing, - geoError, - {enableHighAccuracy: true} - ); - } - - - function openWebsocket () { - ws = new WebSocket((location.protocol == "http:" ? "ws" : "wss") + "://" + location.host + "/api/train/ping/ws"); - ws.onerror = wsError; - ws.onclose = wsClose; - ws.onmessage = wsMsg - ws.onopen = (event) => initGeopos(); - } - - async function geoPing(geoloc) { - console.log("got position update " + counter); - document.getElementById("lat").innerText = geoloc.coords.latitude; - document.getElementById("long").innerText = geoloc.coords.longitude; - document.getElementById("acc").innerText = geoloc.coords.accuracy; - - ws.send(JSON.stringify({ - token: token, - lat: geoloc.coords.latitude, - long: geoloc.coords.longitude, - timestamp: (new Date()).toISOString() - })); - counter += 1; - document.getElementById("status").innerText = `sent ${counter} pings`; - } - - - async function main() { - let trip = await (await fetch("/api/trip/#{ticketTripName}")).json(); - console.log("got trip info"); - - token = await (await fetch("/api/train/register/#{UUID.toText ticketId}", { - method: "POST", - body: JSON.stringify({agent: "onboard-unit"}), - headers: {"Content-Type": "application/json"} - })).json(); - - - if (token.error) { - alert("could not obtain token: \n" + token.msg); - document.getElementById("status").innerText = "_{MsgTokenFailed}"; - } else { - console.log("got token"); - - document.getElementById("token").innerText = token; - - openWebsocket(); - } - } - - main() diff --git a/site/tracker.hamlet b/site/tracker.hamlet deleted file mode 100644 index 2cc05e4..0000000 --- a/site/tracker.hamlet +++ /dev/null @@ -1,147 +0,0 @@ -<h1>_{MsgOBU} - -<section> - <h2>Tracker - <strong>Token:</strong> <span id="token"> -<section> - <h2>Status - <p id="status">_{MsgNone} - <p id>_{MsgError}: <span id="error"> -<section> - <h2>_{MsgLive} - <p><strong>Position: </strong><span id="lat"></span>, <span id="long"></span> - <p><strong>Accuracy: </strong><span id="acc"> -<section> - <h2>_{MsgEstimated} - <p><strong>_{MsgDelay}</strong>: <span id="delay"> - <p><strong>_{MsgSequence}</strong>: <span id="sequence"> - - -<script> - var token = null; - - let euclid = (a,b) => { - let x = a[0]-b[0]; - let y = a[1]-b[1]; - return x*x+y*y; - } - - let minimalDist = (point, list, proj, norm) => { - return list.reduce ( - (min, x) => { - let dist = norm(point, proj(x)); - return dist < min[0] ? [dist,x] : min - }, - [norm(point, proj(list[0])), list[0]] - )[1] - } - - let counter = 0; - let ws; - let id; - - function setStatus(msg) { - document.getElementById("status").innerText = msg - } - - async function geoError(error) { - setStatus("error"); - alert(`_{MsgPermissionFailed}: \n${error.message}`); - console.error(error); - main(); - } - - async function wsError(error) { - // alert(`_{MsgWebsocketError}: \n${error.message === undefined ? error.reason : error.message}`); - console.log(error); - navigator.geolocation.clearWatch(id); - } - - async function wsClose(error) { - console.log(error); - document.getElementById("error").innerText = `websocket closed (reason: ${error.reason}). reconnecting …`; - navigator.geolocation.clearWatch(id); - setTimeout(openWebsocket, 1000); - } - - function wsMsg(msg) { - let json = JSON.parse(msg.data); - console.log(json); - document.getElementById("delay").innerText = - `${json.delay}s (${Math.floor(json.delay / 60)}min)`; - document.getElementById("sequence").innerText = json.sequence; - } - - - function initGeopos() { - document.getElementById("error").innerText = ""; - id = navigator.geolocation.watchPosition( - geoPing, - geoError, - {enableHighAccuracy: true} - ); - } - - - function openWebsocket () { - ws = new WebSocket((location.protocol == "http:" ? "ws" : "wss") + "://" + location.host + "/api/tracker/ping/ws"); - ws.onerror = wsError; - ws.onclose = wsClose; - ws.onmessage = wsMsg; - ws.onopen = (event) => { - setStatus("connected"); - }; - } - - async function geoPing(geoloc) { - console.log("got position update " + counter); - document.getElementById("lat").innerText = geoloc.coords.latitude; - document.getElementById("long").innerText = geoloc.coords.longitude; - document.getElementById("acc").innerText = geoloc.coords.accuracy; - - if (ws !== undefined && ws.readyState == 1) { - ws.send(JSON.stringify({ - token: token, - geopos: [ geoloc.coords.latitude, geoloc.coords.longitude ], - timestamp: (new Date()).toISOString() - })); - counter += 1; - setStatus(`sent ${counter} pings`); - } else { - setStatus(`websocket readystate ${ws.readyState}`); - } - } - - - async function main() { - initGeopos(); - - let urlparams = new URLSearchParams(window.location.search); - - token = urlparams.get("token"); - - if (token === null) { - token = await (await fetch("/api/tracker/register/", { - method: "POST", - body: JSON.stringify({agent: "tracktrain-website"}), - headers: {"Content-Type": "application/json"} - })).json(); - - if (token.error) { - alert("could not obtain token: \n" + token.msg); - setStatus("_{MsgTokenFailed}"); - } else { - console.log("got token"); - window.location.search = `?token=${token}`; - } - } - - console.log(token) - - if (token !== null) { - document.getElementById("token").innerText = token; - openWebsocket(); - } - } - - main() |