diff options
author | stuebinm | 2022-09-03 00:20:45 +0200 |
---|---|---|
committer | stuebinm | 2022-09-03 00:20:45 +0200 |
commit | 2b1a1888210caecbfc66b85b58ef9cd760a73800 (patch) | |
tree | d82f26cbe79be88682bd0e2e6aff6096b5d95e3b | |
parent | c3f30f688a14e88f9e203df5c93ae5138e080982 (diff) |
init onboard-unit
-rw-r--r-- | lib/Server/ControlRoom.hs | 21 | ||||
-rw-r--r-- | messages/de.msg | 3 | ||||
-rw-r--r-- | messages/en.msg | 5 | ||||
-rw-r--r-- | site/obu.hamlet | 92 |
4 files changed, 121 insertions, 0 deletions
diff --git a/lib/Server/ControlRoom.hs b/lib/Server/ControlRoom.hs index 9b61828..86f8deb 100644 --- a/lib/Server/ControlRoom.hs +++ b/lib/Server/ControlRoom.hs @@ -75,6 +75,8 @@ mkYesod "ControlRoom" [parseRoutes| /token/block/#Token TokenBlock GET /trips TripsViewR GET /trip/#TripID TripViewR GET +/obu OnboardUnitMenuR GET +/obu/#TripID/#Day OnboardUnitR GET |] emptyMarkup :: MarkupM a -> Bool @@ -308,6 +310,25 @@ getTokenBlock token = do redirect (TrainViewR runningTrip runningDay) Nothing -> notFound +getOnboardUnitMenuR :: Handler Html +getOnboardUnitMenuR = do + day <- liftIO getCurrentTime <&> utctDay + gtfs <- getYesod <&> getGtfs + let trips = tripsOnDay gtfs day + defaultLayout $ do + [whamlet| +<h1>_{MsgOBU} +<section> + _{MsgChooseTrain} + $forall Trip{..} <- trips + <hr> + <a href="@{OnboardUnitR tripTripID day}"> + #{tripTripID}: #{stationName (stopStation (V.head tripStops))} #{stopDeparture (V.head tripStops)} +|] + +getOnboardUnitR :: TripID -> Day -> Handler Html +getOnboardUnitR tripId day = + defaultLayout $(whamletFile "site/obu.hamlet") announceForm :: Day -> TripID -> Html -> MForm Handler (FormResult Announcement, Widget) announceForm day tripId = renderDivs $ Announcement diff --git a/messages/de.msg b/messages/de.msg index 213337d..2da81b8 100644 --- a/messages/de.msg +++ b/messages/de.msg @@ -19,3 +19,6 @@ NoTrainPing: keine empfangen raw: roh EstimatedDelay: Geschätzte Verspätung OnStationSequence idx: an Stationsindex #{idx} + +ChooseTrain: Fahrt auswählen +TokenFailed: konnte kein Token erhalten diff --git a/messages/en.msg b/messages/en.msg index 47bb66d..2a9c67a 100644 --- a/messages/en.msg +++ b/messages/en.msg @@ -24,3 +24,8 @@ NoTrainPing: none received raw: raw EstimatedDelay: Estimated Delay OnStationSequence idx@String: on station index #{idx} + +OBU: Onboard-Unit +ChooseTrain: Choose a Train +TokenFailed: Failed to acquire token +PermissionFailed: permission failed diff --git a/site/obu.hamlet b/site/obu.hamlet new file mode 100644 index 0000000..d96b96f --- /dev/null +++ b/site/obu.hamlet @@ -0,0 +1,92 @@ +<h1>_{MsgOBU} + +<section> + <h2>#{tripId} _{Msgon} #{day} + <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>Status + <p id="status">_{MsgNone} + + +<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; + + 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; + + fetch(`/api/train/ping`, { + method: "POST", + body: JSON.stringify({ + token: token, + lat: geoloc.coords.latitude, + long: geoloc.coords.longitude, + timestamp: (new Date()).toISOString() + }), + headers: {"Content-Type": "application/json"} + }).then((resp) => { + counter = counter + 1; + document.getElementById("status").innerText = `${counter}: sent`; + }).catch((error) => document.getElementById("status").innerText = error); + } + + async function geoError(error) { + document.getElementById("status").innerText = "error"; + alert(`_{MsgPermissionFailed}: \n${error.message}`); + console.log(error); + } + + async function main() { + let trip = await (await fetch("/api/trip/#{tripId}")).json(); + console.log("got trip info"); + + token = await (await fetch("/api/train/register/#{tripId}", { + 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; + + navigator.geolocation.watchPosition( + geoPing, + geoError, + {enableHighAccuracy: true} + ); + } + } + + main() |