-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·411 lines (368 loc) · 14.4 KB
/
script.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
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
var interval_id = "NULL";
var myChart;
var myChart1;
var myChart2;
var _start_datetime;
var _end_datetime;
function getData(start_datetime, end_datetime) {
_start_datetime = start_datetime;
_end_datetime = end_datetime;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
//console.log(this.responseText);
var data = JSON.parse(this.responseText);
if (data.length > 0) {
var lights_n_date = [];
var temp_n_date = [];
var temp2_n_date = [];
var humidities_n_date = [];
var mean_n_date = [];
var MaxTemp = -100;
var MinTemp = Infinity;
var HumidSum = 0;
var MaxHumid = -100;
var MinHumid = Infinity;
// var mean_total_n_date = [];
// var mean_total_input = 0;
// var mean_total_n_date2 = [];
// var mean_total_input2 = 0;
// var variance_total_input = 0;
// var variance_total_n_date = [];
// var variance_total_input2 = 0;
// var variance_total_n_date2 = [];
// var stddev_total_n_date = [];
// var stddev_total_input = 0;
// var stddev_total_n_date2 = [];
// var stddev_total_input2 = 0;
for (let i = 0; i < data.length; i++) {
lights_n_date.push({
x: (new Date(data[i][5])),
y: data[i][1]
});
temp_n_date.push({
x: (new Date(data[i][5])),
y: data[i][2]
});
temp2_n_date.push({
x: (new Date(data[i][5])),
y: data[i][3]
});
humidities_n_date.push({
x: (new Date(data[i][5])),
y: data[i][4]
});
var curr_avg_temp = (data[i][2] + data[i][3]) / 2;
mean_n_date.push({
x: (new Date(data[i][5])),
y: curr_avg_temp
});
if (MaxTemp < curr_avg_temp) {
MaxTemp = curr_avg_temp;
}
if (MinTemp > curr_avg_temp) {
MinTemp = curr_avg_temp
}
var curr_Humid = data[i][4];
HumidSum += curr_Humid;
if (MaxHumid < curr_Humid) {
MaxHumid = curr_Humid;
}
if (MinHumid > curr_Humid) {
MinHumid = curr_Humid
}
}
// Calculate mean total.
// for (let i = 0; i < data.length; i++) {
// mean_total_input += data[i][2];
// mean_total_input2 += data[i][3];
// }
// mean_total_input /= data.length;
// mean_total_input2 /= data.length;
var cells = document.querySelectorAll("#Stats_Table tbody td");
var averageTemp = mean_n_date.reduce((a, b) => a + b.y, 0) / mean_n_date.length;
cells[0].textContent = averageTemp.toFixed(2);
cells[1].textContent = MaxTemp.toFixed(2);
cells[2].textContent = MinTemp.toFixed(2);
cells[5].textContent = (HumidSum / data.length).toFixed(2);
cells[6].textContent = MaxHumid;
cells[7].textContent = MinHumid;
// Start and end points for total means, so a line gets drawn across the graph.
// mean_total_n_date.push({
// x: (new Date(data[0][5])),
// y: mean_total_input
// });
// mean_total_n_date.push({
// x: (new Date(data[data.length - 1][5])),
// y: mean_total_input
// });
// mean_total_n_date2.push({
// x: (new Date(data[0][5])),
// y: mean_total_input2
// });
// mean_total_n_date2.push({
// x: (new Date(data[data.length - 1][5])),
// y: mean_total_input2
// });
var Variance = 0;
// Calculating variance. Each difference, squared, then averaged.
for (let i = 0; i < data.length; i++) {
Variance += Math.pow(mean_n_date[i].y - averageTemp, 2);
}
Variance = Variance / data.length;
cells[3].textContent = Variance.toFixed(2);
// for (let i = 0; i < data.length; i++) {
// variance_total_input += Math.pow(data[i][2] - mean_total_input, 2);
// variance_total_input2 += Math.pow(data[i][3] - mean_total_input2, 2);
// }
// variance_total_input = variance_total_input / data.length;
// variance_total_input2 = variance_total_input2 / data.length;
// variance_total_n_date.push({
// x: (new Date(data[0][5])),
// y: variance_total_input
// });
// variance_total_n_date.push({
// x: (new Date(data[data.length - 1][5])),
// y: variance_total_input
// });
// variance_total_n_date2.push({
// x: (new Date(data[0][5])),
// y: variance_total_input2
// });
// variance_total_n_date2.push({
// x: (new Date(data[data.length - 1][5])),
// y: variance_total_input2
// });
// Calculating standard dev.
// Square root of the variance.
var StandardDeviation = Math.sqrt(Variance);
cells[4].textContent = StandardDeviation.toFixed(2);
// stddev_total_input = Math.sqrt(variance_total_input);
// stddev_total_input2 = Math.sqrt(variance_total_input2);
// stddev_total_n_date.push({
// x: (new Date(data[0][5])),
// y: stddev_total_input
// });
// stddev_total_n_date.push({
// x: (new Date(data[data.length - 1][5])),
// y: stddev_total_input
// });
// stddev_total_n_date2.push({
// x: (new Date(data[0][5])),
// y: stddev_total_input2
// });
// stddev_total_n_date2.push({
// x: (new Date(data[data.length - 1][5])),
// y: stddev_total_input2
// });
//var sum = myChart.data.datasets[0].data.reduce((a, b) => a + b.y, 0)
myChart.data.datasets[0].data = temp_n_date;
myChart.data.datasets[1].data = temp2_n_date;
myChart.data.datasets[2].data = mean_n_date;
// myChart.data.datasets[3].data = mean_total_n_date;
// myChart.data.datasets[4].data = mean_total_n_date2;
// myChart.data.datasets[5].data = variance_total_n_date;
// myChart.data.datasets[6].data = variance_total_n_date2;
// myChart.data.datasets[7].data = stddev_total_n_date;
// myChart.data.datasets[8].data = stddev_total_n_date2;
myChart.update();
myChart.resetZoom();
myChart1.data.datasets[0].data = humidities_n_date;
myChart1.update();
myChart1.resetZoom();
myChart2.data.datasets[0].data = lights_n_date;
myChart2.update();
myChart2.resetZoom();
}
}
}
// console.log(start_datetime, end_datetime)
xmlhttp.open("GET", "http://14.201.129.240:5000/logs/getData?start_datetime=" + start_datetime + "&end_datetime=" + end_datetime, true);
xmlhttp.send();
}
function main() {
var form = document.getElementById("main");
form.onsubmit = (e) => {
e.preventDefault();
start_date = document.getElementById("start_date").value;
start_time = document.getElementById("start_time").value;
end_date = document.getElementById("end_date").value;
end_time = document.getElementById("end_time").value;
end_date = end_date.length == 0 ? (new Date()).toLocaleDateString().split('/').join('-') : end_date;
start_date = start_date.length == 0 ? (new Date()).toLocaleDateString().split('/').join('-') : start_date;
end_time = end_time.length == 0 ? "23:59:59.999" : end_time;
start_time = start_time.length == 0 ? "00:00:00.000" : start_time;
end_time = end_time.length == 5 ? end_time + ":00" : end_time;
start_time = start_time.length == 5 ? start_time + ":00" : start_time;
getData(start_date + " " + start_time, end_date + " " + end_time);
}
var threshform = document.getElementById("form-thresh");
threshform.onsubmit = (e) => {
e.preventDefault();
var temp_threshold = document.getElementById("temp_threshold").value;
var humidity_threshold = document.getElementById("humidity_threshold").value;
var light_threshold = document.getElementById("light_threshold").value;
var thresh = temp_threshold + ";" + humidity_threshold + ";" + light_threshold;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://14.201.129.240:5000/setThreshold?threshold=" + thresh, true);
xmlhttp.send();
}
var ctx = document.getElementById('myTempChart').getContext('2d');
myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Temperature in C',
data: [],
borderColor: "#5f7394",
},
{
label: 'Temperature 2 in C',
data: [],
borderColor: "#5f7394"
},
{
label: 'Mean Temperature in C',
data: [],
borderColor: "#FF0000"
},
// {
// label: 'Mean Temperature (total) in C',
// data: [],
// borderColor: "#F000F0"
// },
// {
// label: 'Mean Temperature 2 (total) in C',
// data: [],
// borderColor: "#F000F0"
// },
// {
// label: 'Temperature Variance.',
// data: [],
// borderColor: "#5000FF"
// },
// {
// label: 'Temperature 2 Variance',
// data: [],
// borderColor: "#5000FF"
// },
// {
// label: 'Temperature Standard Dev.',
// data: [],
// borderColor: "#000000"
// },
// {
// label: 'Temperature 2 Standard Dev.',
// data: [],
// borderColor: "#000000"
// }
]
},
options: {
elements: {
point: {
radius: 0
}
},
responsive: true,
scales: {
xAxes: [{
type: 'time',
}],
},
pan: {
enabled: true,
mode: "x",
},
zoom: {
enabled: true,
drag: false,
mode: "x",
}
}
});
var ctx = document.getElementById('myHumidChart').getContext('2d');
myChart1 = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Humidity in %',
data: [],
borderColor: "#a77fba",
}]
},
options: {
elements: {
point: {
radius: 0
}
},
responsive: true,
scales: {
xAxes: [{
type: 'time',
}],
},
pan: {
enabled: true,
mode: "x",
},
zoom: {
enabled: true,
drag: false,
mode: "x"
}
}
});
var ctx = document.getElementById('myLightChart').getContext('2d');
myChart2 = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Light Intensity bw 0 and 1023',
data: [],
borderColor: "#50b56b"
}]
},
options: {
elements: {
point: {
radius: 0
}
},
responsive: true,
scales: {
xAxes: [{
type: 'time',
}],
},
pan: {
enabled: true,
mode: "x",
},
zoom: {
enabled: true,
drag: false,
mode: "x"
}
}
});
getData((new Date()).toLocaleDateString().split('/').join('-') + " " + "00:00:00.000", (new Date()).toLocaleDateString().split('/').join('-') + " " + new Date().toTimeString().substr(0, 8) + ".000");
document.getElementById("autorefresh-op-On").addEventListener("click", onAutoRefreshChange);
document.getElementById("autorefresh-op-Off").addEventListener("click", onAutoRefreshChange);
}
window.onload = main;
function onAutoRefreshChange() {
if (document.getElementById("autorefresh-op-On").checked) {
if (interval_id == "NULL") {
interval_id = setInterval(() => {
getData(_start_datetime, _end_datetime);
}, 3000);
}
}
else {
if (interval_id != "NULL") {
clearInterval(interval_id);
}
}
}