-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathekayak.js
297 lines (259 loc) · 7.11 KB
/
ekayak.js
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
285
286
287
288
289
290
291
292
293
294
295
296
297
'use strict'
let loc = {
hostname: "www.bannister.id.au",
port:443,
useSSL:true
};
// location poptions https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
const locationOptions = {
enableHighAccuracy: true,
maximumAge: 10000, // milliseconds
timeout: 700 // milliseconds
};
var zoomLevel = 17;
// Create a client instance
let client = new Paho.MQTT.Client(loc.hostname, Number(loc.port), "clientId");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
let statusText = document.getElementById("status");
let locationText = document.getElementById("location")
let npositions = 0;
var map = L.map('map').fitWorld();
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 21,
attribution: '© OpenStreetMap'
}).addTo(map);
var breadCrumbLine = L.polyline([]).addTo(map);
var locationCircle = L.circle([0,0]).addTo(map);
// Create charts
const temperatureData = {
labels: [],
datasets: [
{
label: 'Internal Temp',
data: [],
borderColor: 'rgba(54, 162, 235, 1)',
backgroundColor: 'rgba(54, 162, 235, 0.2)',
yAxisID: 'y',
},
{
label: 'External Temp',
data: [],
borderColor: 'rgba(0, 99, 182, 1)',
backgroundColor: 'rgba(0, 99, 182, 0.2)',
yAxisID: 'y',
},
{
label: 'ESC Temp',
data: [],
borderColor: 'rgba(89, 150, 182, 1)',
backgroundColor: 'rgba(89, 99, 182, 0.2)',
yAxisID: 'y',
},
{
label: 'Humidity',
data: [],
borderColor: 'rgba(255, 99, 132, 1)',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
yAxisID: 'y1',
}
]
};
const locationData = {
labels: [],
datasets: [
{
label: 'Speed',
data: [],
borderColor: 'rgba(54, 162, 235, 1)',
backgroundColor: 'rgba(54, 162, 235, 0.2)',
yAxisID: 'y',
},
{
label: 'Heading',
data: [],
borderColor: 'rgba(255, 99, 132, 1)',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
yAxisID: 'y1',
}
]
};
const temperatureConfig = {
type: 'line',
data: temperatureData,
options: {
responsive: true,
interaction: {
mode: 'index',
intersect: false,
},
stacked: false,
plugins: {
title: {
display: true,
text: 'Temperature & humidity'
}
},
scales: {
y: {
type: 'linear',
display: true,
position: 'left',
title: {
display:true,
text: 'Temperature (deg)'
}
},
y1: {
type: 'linear',
display: true,
position: 'right',
title: {
display:true,
text: 'Relative humidity (%)'
},
// grid line settings
grid: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
},
},
}
},
};
const locationConfig = {
type: 'line',
data: locationData,
options: {
responsive: true,
interaction: {
mode: 'index',
intersect: false,
},
stacked: false,
plugins: {
title: {
display: true,
text: 'Location'
}
},
scales: {
y: {
type: 'linear',
display: true,
position: 'left',
title: {
display:true,
text: 'Speed (m/s)'
}
},
y1: {
type: 'linear',
display: true,
position: 'right',
title: {
display:true,
text: 'Heading (deg)'
},
// grid line settings
grid: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
},
},
}
},
};
const temperatureChart = new Chart('temperatureChart', temperatureConfig);
const locationChart = new Chart('locationChart', locationConfig);
// Chart update functions: https://www.chartjs.org/docs/latest/developers/updates.html
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset, i) => {
dataset.data.push(data[i]);
});
chart.update();
}
function removeData(chart) {
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
chart.update();
}
function locationSuccess(position) {
const c = position.coords;
locationText.textContent = `lat/long:${c.latitude}/${c.longitude} speed:${c.speed} heading:${c.heading} altitude:${c.altitude} accuracy:${c.accuracy} altaccuracy:${c.altitudeAccuracy} timestamp:${position.timestamp}`;
const latlng = [c.latitude, c.longitude];
if (npositions == 0) {
//map.flyTo(latlng, Number(zoomLevel), {animate:true}); // Flying takes too long and stops when the next location comes in
map.setView(latlng, zoomLevel);
} else {
map.panTo(latlng);
}
breadCrumbLine.addLatLng(latlng);
locationCircle.setLatLng(latlng);
locationCircle.setRadius(c.accuracy);
npositions = npositions + 1;
addData(locationChart, npositions, [c.speed, c.heading])
}
function locationError(error) {
locationText.textContent = `GEOLOCATION ERROR(${error.code}): ${error.message}`;
}
// setup geolocation
if ('geolocation' in navigator) {
const watchID = navigator.geolocation.watchPosition(locationSuccess, locationError, locationOptions);
} else {
locationText.textContent = "Geolocation not supported in this browser";
}
function setStatus(msg) {
statusText.textContent = msg;
console.log("Status changed:", msg);
}
function login() {
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
// For more options, see here: https://www.eclipse.org/paho/files/jsdoc/Paho.MQTT.Client.html
let options = {
timeout:3,
userName:username,
password:password,
onSuccess:onConnect,
onFailure:onConnectionLost,
useSSL:loc.useSSL,
//reconnect:true
}
setStatus("Connecting...");
client.connect(options);
}
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("ekayak");
// let message = new Paho.MQTT.Message("Hello");
// message.destinationName = "World";
// client.send(message);
setStatus("Connected");
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
setStatus(`Connection lost: ${responseObject.errorMessage}`);
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
let nmsg = 0;
function onMessageArrived(message) {
console.log("onMessageArrived:",message.payloadString);
console.log(message)
console.log("Destionation", message.destinationName);
console.log("QoS", message.qos);
console.log("retained", message.retained);
console.log("Duplicate", message.duplicate);
let out = document.getElementById("lastmessage");
out.textContent = message.payloadString;
let data = JSON.parse(message.payloadString);
addData(temperatureChart, nmsg, [data.internal_temp, data.external_temp, data.esc_temp, data.relative_humidity])
nmsg += 1;
}