1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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()
|