-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreports.html
455 lines (408 loc) · 14.1 KB
/
reports.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GPS Routes from ThingSpeak Channels</title>
<!-- Include Leaflet CSS -->
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<!-- Add Plotly library -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<!-- Add turf.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/6.3.0/turf.min.js"></script>
<!-- Add Font Awesome for icons -->
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<!-- Add some basic styling -->
<style>
body {
background-color: #f2f2f2;
color: #333;
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #1a237e;
color: #fff;
padding: 10px;
text-align: center;
position: fixed;
width: 100%;
top: 0;
z-index: 1000;
}
header h1 {
display: inline;
margin-right: 10px;
}
header i {
font-size: 24px;
}
#container {
border: 1px solid #ccc;
padding: 10px;
max-width: 1200px;
margin: 80px auto 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#map {
height: 400px;
margin-bottom: 20px;
}
#channelListContainer {
max-height: 300px;
overflow-y: auto;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.load-button {
cursor: pointer;
background-color: #1a237e;
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 3px;
}
.load-button:hover {
background-color: #0d47a1;
}
.highlighted {
background-color: #e3f2fd !important;
}
#output {
margin-top: 20px;
}
label {
margin-right: 10px;
}
button {
margin-right: 10px;
cursor: pointer;
padding: 8px 20px;
border: none;
background-color: #1a237e;
color: #fff;
border-radius: 3px;
}
button:hover {
background-color: #0d47a1;
}
</style>
</head>
<body>
<!-- Fixed header -->
<header>
<h1>Mobile Units</h1>
<i class="fas fa-chart-line"></i>
<div id="dateTime"></div>
</header>
<!-- Main container -->
<div id="container">
<!-- Create a div to hold the map -->
<div id="map"></div>
<!-- Create a scrollable box container for the channel list -->
<div id="channelListContainer">
<table id="channelTable">
<tr>
<th>Channel Name</th>
<th>Channel ID</th>
<th>Action</th>
<th>Last Seen</th>
</tr>
<tr>
<td>Mobile Airqo 1</td>
<td>2346960</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 1', 2346960)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 2</td>
<td>2346964</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 2', 2346964)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 3</td>
<td>2346967</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 3', 2346967)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 4</td>
<td>2346969</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 4', 2346969)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 5</td>
<td>2346963</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 5', 2346963)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 6</td>
<td>2346968</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 6', 2346968)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 7</td>
<td>2346974</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 7', 2346974)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 8</td>
<td>2346988</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 8', 2346988)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 09</td>
<td>2346962</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 09', 2346962)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 10</td>
<td>2422547</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 10', 2422547)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 11</td>
<td>2346993</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 11', 2346993)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 12</td>
<td>2346999</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 12', 2346999)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 13</td>
<td>2346986</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 13', 2346986)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 14</td>
<td>2347051</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 14', 2347051)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
<tr>
<td>Mobile Airqo 15</td>
<td>2347059</td>
<td><button class="load-button" onclick="loadCharts('Mobile Airqo 15', 2347059)">Load Charts</button></td>
<td id="lastSeenCell_${channelID}"></td>
</tr>
</table>
</div>
<!-- Create input field for the number of datasets -->
<label for="numDatasets">Number of Datasets:</label>
<input type="number" id="numDatasets" name="numDatasets" value="2" min="1">
<!-- Options for plotting -->
<div>
<input type="radio" id="plotMarker" name="plotOption" value="marker" checked>
<label for="plotMarker">Plot Single Marker</label>
<input type="radio" id="plotLine" name="plotOption" value="line">
<label for="plotLine">Trace Line</label>
</div>
<!-- Create buttons to plot all channels and close charts -->
<button onclick="plotAllChannels()">Plot All Channels</button>
<button onclick="closeCharts()">Close Charts</button>
<!-- Output section for displaying distances -->
<div id="output"></div>
<!-- Include Leaflet JavaScript -->
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script>
var currentPolylines = []; // Keep track of polylines for each channel
var currentMarkers = []; // Keep track of markers for each channel
// Function to fetch and plot GPS data for a specific channel
// Function to fetch and plot GPS data for a specific channel
// Function to fetch and plot GPS data for a specific channel
// Function to fetch and plot GPS data for a specific channel
// Function to fetch and plot GPS data for a specific channel
function fetchAndPlotData(deviceName, channelID, color, numDatasets, plotOption) {
var url = 'https://api.thingspeak.com/channels/' + channelID + '/feeds.json?results=' + numDatasets;
fetch(url)
.then(response => response.json())
.then(data => {
var timestamps = [];
var field1Data = [];
var field2Data = [];
var field7Data = [];
var totalDistance = 0; // Initialize total distance
data.feeds.forEach(feed => {
timestamps.push(feed.created_at);
field1Data.push(feed.field1);
field2Data.push(feed.field2);
field7Data.push(feed.field7);
// Plot points on the map
const lat = parseFloat(feed.field5);
const lng = parseFloat(feed.field6);
const marker = L.marker([lat, lng]).addTo(map);
marker.bindPopup(deviceName);
currentMarkers.push(marker);
});
var plotData;
if (plotOption === 'line') {
plotData = [{
lat: data.feeds.map(feed => parseFloat(feed.field5)),
lon: data.feeds.map(feed => parseFloat(feed.field6)),
type: 'scattermapbox',
mode: 'lines',
line: {
color: color,
},
name: 'Trace Line'
}];
} else {
plotData = [{
x: timestamps,
y: field1Data,
type: 'scatter',
name: 'Field 1'
}, {
x: timestamps,
y: field2Data,
type: 'scatter',
name: 'Field 2'
}, {
x: timestamps,
y: field7Data,
type: 'scatter',
name: 'Field 7'
}];
}
var layout = {
title: deviceName + ' Data',
xaxis: {
title: 'Timestamp'
},
yaxis: {
title: 'Value'
},
// Enable hover interactions
hovermode: 'closest',
// Add modebar buttons to display
displayModeBar: true
};
Plotly.newPlot('output', plotData, layout);
// Calculate distance and add it to total
for (let i = 1; i < data.feeds.length; i++) {
const from = [parseFloat(data.feeds[i - 1].field5), parseFloat(data.feeds[i - 1].field6)];
const to = [parseFloat(data.feeds[i].field5), parseFloat(data.feeds[i].field6)];
totalDistance += turf.distance(turf.point(from), turf.point(to), { units: 'kilometers' });
}
document.getElementById('output').innerHTML += '<p>Total distance traveled: ' + totalDistance.toFixed(2) + ' kilometers</p>';
})
.catch(error => console.error('Error fetching data for channel:', error));
}
// Function to load the charts for a specific channel
// Function to load the charts for a specific channel
function loadCharts(deviceName, channelID) {
// Clear previous output, polylines, and markers
document.getElementById('output').innerHTML = '';
currentPolylines.forEach(polyline => {
map.removeLayer(polyline);
});
currentPolylines = [];
currentMarkers.forEach(marker => {
map.removeLayer(marker);
});
currentMarkers = [];
// Remove highlighting from previously selected row
var table = document.getElementById('channelTable');
var rows = table.getElementsByTagName('tr');
for (var i = 1; i < rows.length; i++) {
rows[i].classList.remove('highlighted');
}
// Highlight the selected row
var selectedRow;
for (var i = 1; i < rows.length; i++) {
if (rows[i].getElementsByTagName('td')[1].innerText == channelID) {
selectedRow = rows[i];
break;
}
}
selectedRow.classList.add('highlighted');
var numDatasets = parseInt(document.getElementById('numDatasets').value);
var plotOption = document.querySelector('input[name="plotOption"]:checked').value;
fetchAndPlotData(deviceName, channelID, getRandomColor(), numDatasets, plotOption);
}
// Function to plot all channels
function plotAllChannels() {
var table = document.getElementById('channelTable');
var rows = table.getElementsByTagName('tr');
for (var i = 1; i < rows.length; i++) {
var deviceName = rows[i].getElementsByTagName('td')[0].innerText;
var channelID = rows[i].getElementsByTagName('td')[1].innerText;
loadCharts(deviceName, channelID);
}
}
// Function to close the charts
function closeCharts() {
// Clear previous output, polylines, and markers
document.getElementById('output').innerHTML = '';
currentPolylines.forEach(polyline => {
map.removeLayer(polyline);
});
currentPolylines = [];
currentMarkers.forEach(marker => {
map.removeLayer(marker);
});
currentMarkers = [];
}
// Generate random color
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Initialize Leaflet map
var map = L.map('map').setView([0, 0], 2); // set initial coordinates and zoom level
// Add OpenStreetMap street tile layer
var streetLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.opght">OpenSesseme</a> contributors'
}).addTo(map);
// Add OpenStreetMap satellite tile layer
var satelliteLayer = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: '© <a href="https://www.esri.com/">Esri</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>'
});
// Add Leaflet control for map view toggle
var toggleButton = L.control({ position: 'topright' });
toggleButton.onAdd = function (map) {
var div = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
div.innerHTML = '<button onclick="toggleMapView()">Toggle View</button>';
return div;
};
toggleButton.addTo(map);
// Bind markers to zoom events
map.on('zoomend', function() {
currentMarkers.forEach(marker => {
marker.setLatLng(marker.getLatLng());
});
});
setInterval(updateClock, 1000);
</script>
</body>
</html>