-
Notifications
You must be signed in to change notification settings - Fork 1
/
nomad.js
375 lines (328 loc) · 10.9 KB
/
nomad.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
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
/* tank.js - Controls tank data viewing system. */
// Global Variables
var lastUpdateDate = new Date();
//var base_url = 'http://data.sparkfun.com/output/';
var data_base_url = 'http://ec2-52-88-170-89.us-west-2.compute.amazonaws.com:8080/output/';
//var public_key = '0lgbR1rRQ9UgbE6LoLx1';
var data_public_key = 'mr8jMdveOGSrqoB4XdWQcVrjaWQ';
var maxAliveTimeoutMS = 300000;
// onload callback
function drawChart() {
console.log("Drawing charts");
shouldUpdate = false;
var dataPage;
var maxAge;
var sinceDate;
if ( document.getElementById("fiveMin").checked ) {
sinceDate = "-5minutes";
pointSize = 4;
} else if ( document.getElementById("fifteenMin").checked ) {
sinceDate = "-15minutes";
pointSize = 2;
} else if ( document.getElementById("oneHr").checked ) {
sinceDate = "-60minutes";
pointSize = 1;
} else if ( document.getElementById("oneDay").checked ) {
sinceDate = "-24hours";
} else if ( document.getElementById("oneWeek").checked ) {
sinceDate = "-7days";
} else if ( document.getElementById("oneMonth").checked ) {
sinceDate = "-30days";
} else if ( document.getElementById("allPages").checked ) {
sinceDate = "";
}
console.log("Getting data: "+data_base_url + data_public_key + '.json' + '?gte[timestamp]='+sinceDate);
// JSONP request
var jsonData = $.ajax({
url: data_base_url + data_public_key + '.json' + '?gte[timestamp]='+sinceDate,
jsonp: 'callback',
cache: true,
dataType: 'jsonp',
beforeSend: function() {
spinner(true);
enableRangeSelector(false);
},
complete: function() {
spinner(false);
enableRangeSelector(true);
}
}).done(function (results) {
// init data tables
dataTemp = new google.visualization.DataTable();
dataSalinity = new google.visualization.DataTable();
dataConductivity = new google.visualization.DataTable();
dataOxygen = new google.visualization.DataTable();
dataDepth = new google.visualization.DataTable();
dataTemp.addColumn('datetime', 'Time');
dataTemp.addColumn('number', 'Water Temperature');
dataSalinity.addColumn('datetime', 'Time');
dataSalinity.addColumn('number', 'Salinity');
dataConductivity.addColumn('datetime', 'Time');
dataConductivity.addColumn('number', 'Conductivity');
dataOxygen.addColumn('datetime', 'Time');
dataOxygen.addColumn('number', 'Dissolved Oxygen');
dataDepth.addColumn('datetime', 'Time');
dataDepth.addColumn('number', 'Depth');
var mostRecent;
$.each(results, function (i, row) {
if ( i == 0 ) {
mostRecent = new Date(row.timestamp);
}
dataTemp.addRow([
(new Date(row.timestamp)),
parseFloat(row.watertemp)
]);
dataSalinity.addRow([
(new Date(row.timestamp)),
parseFloat(row.salinity)
]);
dataConductivity.addRow([
(new Date(row.timestamp)),
parseFloat(row.conductivity)
]);
dataOxygen.addRow([
(new Date(row.timestamp)),
parseFloat(row.dissolvedo2)
]);
dataDepth.addRow([
(new Date(row.timestamp)),
parseFloat(row.depth)
]);
if ( i == 0 ) {
document.getElementById("waterTempLabel").innerHTML = row.watertemp;
document.getElementById("salinityLabel").innerHTML = row.salinity;
document.getElementById("conductivityLabel").innerHTML = row.conductivity;
document.getElementById("oxygenLabel").innerHTML = row.dissolvedo2;
document.getElementById("pHLabel").innerHTML = row.ph;
document.getElementById("currentLabel").innerHTML = row.current;
document.getElementById("voltageLabel").innerHTML = row.voltage;
document.getElementById("intTempLabel").innerHTML = row.internaltemp;
document.getElementById("depthLabel").innerHTML = row.depth;
lastUpdateDate = new Date(row.timestamp);
}
});
chartTemp = new google.visualization.LineChart($('#chartTemp').get(0));
chartSalinity = new google.visualization.LineChart($('#chartSalinity').get(0));
chartConductivity = new google.visualization.LineChart($('#chartConductivity').get(0));
chartOxygen = new google.visualization.LineChart($('#chartOxygen').get(0));
chartDepth = new google.visualization.LineChart($('#chartDepth').get(0));
drawCharts();
console.log("Enabling live updates");
shouldUpdate = true;
});
}
function drawCharts() {
if ( shouldUpdate ) {
console.log("Redrawing charts");
spinner(true);
chartTemp.draw(dataTemp, {
title: 'Water Temperature',
vAxis: {
'title': 'Temperature (deg C)'
},
'height': 300,
colors: ['#ff0000'],
pointSize: pointSize
});
chartSalinity.draw(dataSalinity, {
title: 'Salinity',
vAxis: {
'title': 'Salinity (g/kg)'
},
'height': 300,
colors: ['#00ff00'],
pointSize: pointSize
});
chartConductivity.draw(dataConductivity, {
title: 'Conductivity',
vAxis: {
'title': 'Conductivity (S/m)'
},
'height': 300,
colors: ['#0000ff'],
pointSize: pointSize
});
chartOxygen.draw(dataOxygen, {
title: 'Dissolved Oxygen',
vAxis: {
'title': 'Dissolved Oxygen (mL/L)'
},
'height': 300,
colors: ['#ff00ff'],
pointSize: pointSize
});
chartDepth.draw(dataDepth, {
title: 'Depth',
vAxis: {
'title': 'Depth'
},
'height': 300,
colors: ['#2222ff'],
pointSize: pointSize
});
spinner(false);
}
}
function updateChart() {
if ( shouldUpdate ) {
console.log("Update data");
// JSONP request
var jsonData = $.ajax({
url: data_base_url + data_public_key + '.json' + '.json?gte[timestamp]=-60seconds',
jsonp: 'callback',
cache: true,
dataType: 'jsonp',
beforeSend: function() {
spinner(true);
},
complete: function() {
spinner(false);
}
}).done(function (results) {
var mostRecent;
$.each(results, function (i, row) {
if ( i == 0 ) {
dataTemp.insertRows(0,[[
(new Date(row.timestamp)),
parseFloat(row.watertemp)
]]);
dataSalinity.insertRows(0,[[
(new Date(row.timestamp)),
parseFloat(row.salinity)
]]);
dataConductivity.insertRows(0,[[
(new Date(row.timestamp)),
parseFloat(row.conductivity)
]]);
dataOxygen.insertRows(0,[[
(new Date(row.timestamp)),
parseFloat(row.dissolvedo2)
]]);
dataDepth.insertRows(0,[[
(new Date(row.timestamp)),
parseFloat(row.depth)
]]);
document.getElementById("waterTempLabel").innerHTML = row.watertemp;
document.getElementById("salinityLabel").innerHTML = row.salinity;
document.getElementById("conductivityLabel").innerHTML = row.conductivity;
document.getElementById("oxygenLabel").innerHTML = row.dissolvedo2;
document.getElementById("pHLabel").innerHTML = row.ph;
document.getElementById("currentLabel").innerHTML = row.current;
document.getElementById("voltageLabel").innerHTML = row.voltage;
document.getElementById("intTempLabel").innerHTML = row.internaltemp;
document.getElementById("depthLabel").innerHTML = row.depth;
lastUpdateDate = new Date(row.timestamp);
}
});
});
drawCharts();
}
// recursive call to repeat this function
setTimeout(updateChart,10000);
}
function timerUpdate() {
/*var elapsed = Math.floor(Math.abs(new Date() - lastUpdateDate)/1000);
if ( elapsed < 60 ) {
document.getElementById("timeSinceUpdate").innerHTML = '<span class="label label-success"><i class="fa fa-clock-o fa-lg"></i> '+elapsed+' seconds ago</span>';
} else if ( elapsed < 3600 ) {
document.getElementById("timeSinceUpdate").innerHTML = '<span class="label label-danger"><i class="fa fa-clock-o fa-lg"></i> '+Math.floor(elapsed/60)+' minutes ago</span>';
} else if ( elapsed < 84600) {
document.getElementById("timeSinceUpdate").innerHTML = '<span class="label label-danger"><i class="fa fa-clock-o fa-lg"></i> '+Math.floor(elapsed/3600)+' hours ago</span>';
} else {
document.getElementById("timeSinceUpdate").innerHTML = '<span class="label label-danger"><i class="fa fa-clock-o fa-lg"></i> '+Math.floor(elapsed/86400)+' days ago</span>';
}
// recursive call to repeat this function
setTimeout(timerUpdate,1000);*/
}
var spinnerCount = 0;
function spinner(on) {
if ( on ) {
spinnerCount++;
} else {
spinnerCount--;
if ( spinner < 0 ) {
spinner = 0;
}
}
if ( spinnerCount > 0 ) {
document.getElementById("spinner").innerHTML = '<i class="fa fa-spinner fa-pulse fa-fw"></i>';
} else {
document.getElementById("spinner").innerHTML = '';
}
}
function cameraImageUpdate() {
document.getElementById("camera1Image").src = "http://nomadcam.catalinasearanch.com/camera1/latest.jpg#" + new Date().getTime();
document.getElementById("camera2Image").src = "http://nomadcam.catalinasearanch.com/camera2/latest.jpg#" + new Date().getTime();
document.getElementById("camera1Link").href = document.getElementById("camera1Image").src;
document.getElementById("camera2Link").href = document.getElementById("camera2Image").src;
if ( true ) {
document.getElementById("camAlive").style.color = 'YellowGreen';
} else {
document.getElementById("camAlive").style.color = 'red';
}
/*$.ajax({
url: "http://nomadcam.catalinasearanch.com/camera1/",
success: function(data){
var thumbnailHTML = '';
var count = 0;
var index = 0;
jQuery.fn.reverse = [].reverse;
$($(data).find("a:contains(.jpg)")).reverse().each(function(){
// will loop through
var image = $(this).attr("href");
if ( count == 0 ) {
count++;
return;
}
if ( index % 15 == 0 ) {
if ( count++ <= 8 ) {
thumbnailHTML += '<div class="col-sm-6 col-md-3 col-lg-3">';
thumbnailHTML += '<div class="thumbnail">';
thumbnailHTML += '<a href="http://nomadcam.catalinasearanch.com/camera1/'+image+'">';
thumbnailHTML += '<img src="http://nomadcam.catalinasearanch.com/camera1/'+image+'" class="img-responsive" />';
thumbnailHTML += '</a></div></div>';
}
}
index++;
});
document.getElementById("cameraThumbSpan").innerHTML = thumbnailHTML;
}
});*/
// recursive call to repeat this function
setTimeout(cameraImageUpdate,30000);
}
function fftPlotUpdate() {
console.log("Updating FFT plot image");
document.getElementById("fftPlot").src = "http://nomadcam.catalinasearanch.com/imu/latest.png#" + new Date().getTime();
// recursive call to repeat this function
setTimeout(fftPlotUpdate,5*60*1000);
}
// load chart lib
//google.charts.load('current', {'packages':['corechart']});
// init data tables
var dataTemp;
var dataSalinity;
var dataConductivity;
var dataOxygen;
var dataDepth;
var chartTemp;
var chartSalinity;
var chartConductivity;
var chartOxygen;
var chartDepth;
var shouldUpdate = false;
var pointSize = 0;
// grab IP on start
//updateAddress();
// Start timer update
timerUpdate();
// Start loading camera images
cameraImageUpdate();
// Start loading fft plots
fftPlotUpdate();
// disable range selector
//enableRangeSelector(false);
// call drawChart once google charts is loaded
//google.charts.setOnLoadCallback(drawChart);
// start update cycle
setTimeout(updateChart,10000);