-
Notifications
You must be signed in to change notification settings - Fork 1
/
read-rangetest.html
419 lines (326 loc) · 12.1 KB
/
read-rangetest.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Meshtastic rangetest.csv viewer (v2)</title>
<style>
form {
background-color:#eee;
padding:10px;
}
div#map {
display:none;
position:absolute;
top:100px;
left:0;
width:100%;
height:calc( 100% - 100px );
x-index:100;
}
form#filter {
display:none;
position:absolute;
top:0;
left:0;
width:100%;
height:100px;
x-index:100;
padding:2px;
}
</style>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js" type="text/javascript"></script>
</head>
<body>
<form id="upload" onsubmit="return false">
<h2>rangetest.csv viewer (v2)</h2>
<p>This page is for viewing a rangetest.csv file saved from a Meshtastic Node. Tested with the file saved from the Android App, not sure about other clients.
<p>It's just the "Export rangetest.csv" in the main menu. Haven't looked at the one from the Rangetest module.
<p>Plots the results on a Leaflet/OSM map. Should work globally.
<p>Once loaded, will be able to filter the data to narrow down the displayed data
<p>Note: the file is processed entirely in your browser, it is never shared or uploaded. Although OSM tileservers will see what squares you view. Press Ctrl-U to view source code of the page</p>
Select file: <input type=file id="input-file" accept=".csv">
<p>To load another file, just reload the page to return to this screen.
</form>
<form id="filter">
Day(s): <select name="days" id="selectDays" multiple size=5 onchange="updateMap()"></select>
Types(s): <select name="types" id="selectTypes" multiple size=5 onchange="updateMap()"></select>
Senders(s): <select name="senders" id="selectSenders" multiple size=5 onchange="updateMap()"></select>
(hold ctrl to select multiple)
</form>
<div id="map"></div>
<script>
//////////////////////////////////////////////////////////////////////////////////////
document.getElementById('input-file').addEventListener('change', event => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const text = e.target.result;
processCSV(text);
};
reader.readAsText(file);
});
//////////////////////////////////////////////////////////////////////////////////////
var nodes = {};
var map;
var bounds;
var baseMaps = {};
var overlayMaps = {};
let data;
let header;
let dayList = {};
let typeList = {};
let senderList = {};
function processCSV(text) {
//todo, allow to add data to existing array!
data = CSVToArray(text,',');
header = data.shift();
renderMap({});
}
function updateMap() {
let filters = {};
let select = document.getElementById('selectDays');
if (select.options.length > 0) {
filters['days'] = [];
for(let q=0;q<select.options.length;q++) {
if (select.options[q].selected)
filters['days'].push(select.options[q].value);
}
}
select = document.getElementById('selectTypes');
if (select.options.length > 0) {
filters['types'] = [];
for(let q=0;q<select.options.length;q++) {
if (select.options[q].selected)
filters['types'].push(select.options[q].value);
}
}
select = document.getElementById('selectSenders');
if (select.options.length > 0) {
filters['senders'] = [];
for(let q=0;q<select.options.length;q++) {
if (select.options[q].selected)
filters['senders'].push(select.options[q].value);
}
}
console.log(filters);
renderMap(filters);
}
///////////////////
function renderMap(filters) {
document.getElementById('map').style.display = 'inherit';
document.getElementById('filter').style.display = 'inherit';
if (map) {
overlayMaps['Remote Nodes'].clearLayers();
overlayMaps['Your Node Locations'].clearLayers();
overlayMaps['Contacts'].clearLayers();
} else {
var mapOptions = {
attributionControl:false //we add our own manually!
};
map = window.map = L.map('map', mapOptions).addControl(
L.control.attribution({ position: 'bottomright', prefix: ''}) );
setupBaseMaps();
overlayMaps['Remote Nodes'] = L.layerGroup().addTo(map);
overlayMaps['Your Node Locations'] = L.layerGroup().addTo(map);
overlayMaps['Contacts'] = L.layerGroup().addTo(map);
//should have alrady setup overlayMaps at this point too!
L.control.layers(baseMaps,overlayMaps).addTo(map);
}
///////////////////
let bounds = L.latLngBounds();
let done = 0;
let setup = 0;
if (Object.keys(dayList).length == 0)
setup = 1;
nodes = {}; //for now, clear this each time. later will want to deduplicate mutliple files
data.forEach(function(line,index) {
let values = {};
line.forEach((v,i) => values[header[i]] = v);
if (!values['from'])
return; //usually am empty row at the end!
///////////////////
let slat = parseFloat(values['sender lat']);
let slng = parseFloat(values['sender long']);
let rlat = parseFloat(values['rx lat']);
let rlng = parseFloat(values['rx long']);
///////////////////
let day = values['date'];
let type = values['payload'];
let sender = values['from']; //todo, convert to hex?
if (type.indexOf('<') != 0) {
if (type.indexOf('seq ') == 0) type = '<RANGETEST PING>';
else type = "Text Message";
}
if (values['sender name'] && values['sender name'].length > 1)
sender = values['sender name'];
if (setup) {
dayList[day] = (dayList[day])?(dayList[day]+1):1;
typeList[type] = (typeList[type])?(typeList[type]+1):1;
senderList[sender] = (senderList[sender])?(senderList[sender]+1):1;
} else if (filters) {
if (filters['days'] && filters['days'].length && filters['days'].indexOf(day) == -1) { return; }
if (filters['types'] && filters['types'].length && filters['types'].indexOf(type) == -1) { return; }
if (filters['senders'] && filters['senders'].length && filters['senders'].indexOf(sender) == -1) { return; }
}
///////////////////
//Sender Node
if (!isNaN(slat) && !isNaN(slng) && (slat!=0.0 || slng!=0.0)) { //either could be zero degrees! (but the module version gives 0,0 as location)
let key = values['from']+','+slat.toFixed(6)+','+slng.toFixed(6); //it could be mobile and moving!
if (nodes[key]) {
//marker already exists, doesnt need adding again!
if (values['sender name'] && !nodes[key]['sender name'])
nodes[key]['sender name'] = values['sender name'];
//todo update the title attached to the marker, now we know the name
} else {
nodes[key] = values; //??
L.circleMarker([slat,slng], {
radius: 6,
color: '#0000ff', //todo, pick from a colour pallet?
title:"Remote Node: " + values['sender name']+". At " + values['date'] + ' '+values['time']
}).addTo(overlayMaps['Remote Nodes']).bindPopup(showTitlePopup);
bounds.extend([slat,slng]);
done++;
}
} else {
//todo, plot a nearby point?
}
///////////////////
//Receiver/Self Node
if (!isNaN(rlat) && !isNaN(rlng) && (rlat!=0.0 || rlng!=0.0)) { //either could be zero degrees!
let key = 'self,'+slat.toFixed(6)+','+slng.toFixed(6); //even your node may be mobile
if (nodes[key]) {
//marker already exists, doesnt need adding again!
} else {
nodes[key] = values; //??
L.circleMarker([rlat,rlng], {
radius: 6,
color: '#ff0000',
title:"Self. At " + values['date'] + ' '+values['time'] //todo - doesnt work with circleMarker!
}).addTo(overlayMaps['Your Node Locations']).bindPopup(showTitlePopup);
bounds.extend([rlat,rlng]);
done++;
}
} else {
//hmm? doesnt seem to ever be empty in test data.
}
///////////////////
//line joining the two
if (!isNaN(slat) && !isNaN(slng) && (slat!=0.0 || slng!=0.0) && !isNaN(rlat) && !isNaN(rlng) && (rlat!=0.0 || rlng!=0.0)) {
L.polyline([[slat,slng],[rlat,rlng]], {
color: '#0000ff',
weight: parseInt(values['hop limit'])+1,
title: values['date'] + ' '+values['time']
}).addTo(overlayMaps['Contacts']).bindPopup(showTitlePopup);
}
///////////////////
});
///////////////////
if (done) {
map.fitBounds(bounds,{maxZoom:15});
}
if (setup) {
let select = document.getElementById('selectDays');
Object.keys(dayList).forEach(function(day) {
let opt = document.createElement('option');
opt.value = day
opt.innerHTML = day + ' ('+dayList[day]+' packets)';
select.appendChild(opt);
});
select = document.getElementById('selectTypes');
Object.keys(typeList).forEach(function(type) {
let opt = document.createElement('option');
opt.value = type
opt.text = type + ' ('+typeList[type]+' packets)';
select.appendChild(opt);
});
select = document.getElementById('selectSenders');
Object.keys(senderList).forEach(function(sender) {
let opt = document.createElement('option');
opt.value = sender
opt.text = sender + ' ('+senderList[sender]+' packets)';
select.appendChild(opt);
});
}
///////////////////
}
function setupBaseMaps() {
var osmAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
baseMaps['OpenStreetMap'] = new L.TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{mapLetter: 'o', minZoom: 3, maxZoom: 18, attribution: osmAttrib});
var topoAttribution = 'Data: © <a href="https://openstreetmap.org/copyright">OpenStreetMap</a>-Contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map Style: © (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>) <a href="https://opentopomap.org">OpenTopoMap</a> - [<a href="https://www.geograph.org/leaflet/otm-legend.php">Key</a>]';
baseMaps["OpenTopoMap"] = L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
{mapLetter: 'l', minZoom: 1, maxZoom: 17, detectRetina: false, attribution: topoAttribution});
map.addLayer(baseMaps['OpenStreetMap']);
}
function showTitlePopup(input) {
//input.options is the options from the original layer!
return input.options.title;
}
//////////////////////////////////////////////////////////////////////////////////////
//From: https://gist.github.com/rakeden/508ca124fabe97eba6d5734f2efcea32
function CSVToArray(strData, strDelimiter) {
// Check to see if the delimiter is defined. If not,
// then default to comma.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
// Create an array to hold our data. Give the array
// a default empty first row.
var arrData = [[]];
// Create an array to hold our individual pattern
// matching groups.
var arrMatches = null;
// Keep looping over the regular expression matches
// until we can no longer find a match.
while (arrMatches = objPattern.exec(strData)) {
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[1];
// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (
strMatchedDelimiter.length &&
strMatchedDelimiter !== strDelimiter
) {
// Since we have reached a new row of data,
// add an empty row to our data array.
arrData.push([]);
}
var strMatchedValue;
// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[2]) {
// We found a quoted value. When we capture
// this value, unescape any double quotes.
strMatchedValue = arrMatches[2].replace(
new RegExp("\"\"", "g"),
"\""
);
} else {
// We found a non-quoted value.
strMatchedValue = arrMatches[3];
}
// Now that we have our value string, let's add
// it to the data array.
arrData[arrData.length - 1].push(strMatchedValue);
}
// Return the parsed data.
return (arrData);
}
</script>
</body>
</html>