-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
286 lines (221 loc) · 7.82 KB
/
index.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE html>
<!--
//
// Copyright (C) 2021 Marco Bergman <[email protected]>
//
// This Program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
-->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SignalK Tidal Dashboard</title>
<style>
* {font-family: Consolas, monospace}
#data tr:nth-child(odd) { background-color: #f2f2f2;}
#displayLabel td {vertical-align: top; font-weight: bold}
#displayValue td {text-align: center; vertical-align: middle; font-size: xx-large; font-weight: bold}
#displayUnit td {text-align: center; vertical-align: middle;}
</style>
</head>
<body style="">
<div id="data"></div>
<div id="gauge" style="display: inline-block; vertical-align:top;"></div>
<div id="display" style="display: inline-block; vertical-align:top;"></div>
<div id="log"></div>
<script>
// Defaults:
var myServerURL = location.protocol + "//" + location.host;
var vessel = "*";
var stations = {}
var names = {}
var lat = {}
var lon = {}
var ownLat = 0
var ownLon = 0
//
// Configure your signalk url:port below, like openplotter.myboat.local:3000 or 10.10.10.1:3000
/*
myServerURL = "http://192.168.178.3:3000";
*/
/*
var myServerURL = "https://cloud.signalk.org:443";
var vessel = "urn:mrn:imo:mmsi:244150473";
*/
var updatesPerSecond = 1; // refresh rate
// end user config
var displayDiv = document.getElementById('display');
var dataDiv = document.getElementById('data');
var logDiv = document.getElementById('log');
function formatTimestamp(timestamp) {
var date = new Date(timestamp)
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
hour = (hour < 10 ? "0" : "") + hour;
min = (min < 10 ? "0" : "") + min;
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min + ":" + sec;
}
function timeDiff (t) {
n = new Date();
t = new Date(n.getFullYear(), n.getMonth(), n.getDate(), t.substring(0,2), t.substring(3,5), 1)
return ((Math.round((t-n)/1000/600)/6 + 24)%24)
}
function formatHour (h) {
hours = Math.floor(h)
minutes = Math.round(60 * (h - hours))
return "+" + hours + ":" + (minutes<10 ? '0'+minutes : minutes)
}
function arrow (trend) {
if (trend) {
if (trend > 0.001) return '<img valign=center height="13px" src="up.png">'
if (trend < -0.001) return '<img valign=center height="13px" src="down.png">'
}
return ''
}
function printSignalkTable(signalkData) {
var result = "<table border=0><tr><td><b>Station<td width=100><b>Level (MSL)<td width=100 colspan=2><b>Trend/10min<td width=100 align='center'><b>Rel.astro<td><b>Next</tr>";
total = 0.0
divider = 0.0
timeOut = 300
for (var station in stations) {
stale = Date.now() - stations[station].timestamp > 1000 * timeOut ? true : false;
nextExtreme = stations[station].nextExtreme
diff = formatHour(timeDiff(nextExtreme.substring(3,8)))
relAstro = stations[station].waterlevel - stations[station].astro
result += "<tr>";
result += "<td><b>" + (stale ? "<strike>" : "") + station + "</td>";
result += "<td align='center'>" + (stale ? "<strike>" : "") + stations[station].waterlevel + "</td>";
result += "<td align='center'>" + arrow(stations[station].tidalTrend) + "</td>";
result += "<td align='center'>" + stations[station].tidalTrend + "</td>";
result += "<td align='center'>" + (relAstro > 0 ? "+" : "") + relAstro.toFixed(2) + "</td>";
result += "<td>" + diff + " " + nextExtreme + "</td>";
range = 60 * Math.sqrt (Math.pow(stations[station].latitude - ownLat, 2)
+ Math.pow(Math.cos (ownLat / 180 * Math.PI) * (stations[station].longitude - ownLon), 2))
weighingFactor = 1 / range
total += stations[station].waterlevel * weighingFactor
divider += weighingFactor
}
average = total / divider
result += "<tr><td><b>THIS SHIP "+formatTimestamp(Date.now()) + "</td>";
result += "<td align='center'><b><h1>" + average.toFixed(2) + "</td>";
result += "<td colspan=4></tr>"
result += "</table>";
dataDiv.innerHTML = result;
}
function httpGetAsync(theUrl, callback) {
// get a file through HTTP
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
// get the stations names, lats and longs
httpGetAsync(myServerURL + "/signalk/v1/api/aton/", onSchema);
function onSchema(responseText) {
// respond to the reception of a schema file
schema = JSON.parse(responseText);
Object.keys(schema).forEach(function(key) {
try {
position = {
latitude: schema[key].navigation.position.value['latitude'],
longitude: schema[key].navigation.position.value['longitude'],
nextExtreme: schema[key].environment.nextExtreme.value,
tidalTrend: schema[key].environment.tidalTrend.value,
waterlevel: schema[key].environment.depth.belowSurface.value,
astro: schema[key].environment.depth.astro.value }
stations[key] = position
} catch (error) {console.log(schema, error)}
})
printSignalkTable(signalkData);
//console.log(stations)
}
// set up the stream of signalk deltas
var wsURL = myServerURL.replace("http", "ws") + "/signalk/v1/stream?subscribe=none"
var ws = new WebSocket(wsURL);
// first subscribe for all vessels
var subscriptionMessage1 = JSON.stringify({"context": "vessels.self", "subscribe": [
{"path": "navigation.position"},
]});
var subscriptionMessage2 = JSON.stringify({"context": "aton.*", "subscribe": [
//{"path": "environment.depth.belowSurface"},
{"path": "*"},
]});
ws.onopen = function(event) {
ws.send(subscriptionMessage1)
ws.send(subscriptionMessage2)
}
ws.onclose = function(e) {
logDiv.innerHTML = "<H1>Connection closed</H1>"
console.log("ws close", e);
setTimeout(function() {
ws = new WebSocket(wsURL);
}, 10000);
}
var signalkData = {}; // holds the signalk data
var lastUpdate = Date.now();
ws.onmessage = function(event) {
// Process an incoming signalk update message
json = JSON.parse(event.data);
//logDiv.innerHTML = logDiv.innerHTML + JSON.stringify(json, null, 2);
for (var key in json) {
//logDiv.innerHTML = JSON.stringify(json[key][0], null, 2);
//logDiv.innerHTML = JSON.stringify(stations)
if (key == "context") {
target = json[key]
}
if (key == "updates" ) {
ts = Date.parse(json[key][0]['timestamp']);
station = target.split(".")[1]
// loop through all updated paths and store them in the various dicts
values = json[key][0]['values'];
for (tuple in values) {
path = values[tuple]['path'];
value = values[tuple]['value'];
if (path == "") {
names[target] = value["name"]
}
if (path == "environment.depth.belowSurface") {
if (stations[station]) {
stations[station].waterlevel = value
stations[station].timestamp = ts
}
}
if (path == "environment.depth.astro") {
if (stations[station]) {
stations[station].astro = value
}
}
if (path == "environment.nextExtreme") {
if (stations[station]) {
stations[station].nextExtreme = value
}
}
if (path == "environment.tidalTrend") {
if (stations[station]) {
stations[station].tidalTrend = value
}
}
if (path == "navigation.position") {
ownLat = value["latitude"]
ownLon = value["longitude"]
}
}
// present the updated dictionary to the screen at the specified refresh rate
elapsed = Date.now() - lastUpdate;
if (elapsed > 1000/updatesPerSecond) {
printSignalkTable(signalkData);
lastUpdate = Date.now();
}
}
}
}
</script>
</body></html>