This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.php
583 lines (484 loc) · 19.3 KB
/
plot.php
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php
require __DIR__ . '/vendor/autoload.php';
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
header('Content-Type: text/html; charset=ISO-8859-1');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
window.paceOptions = {
ajax: {
trackMethods: ['GET', 'POST', 'PUT', 'DELETE', 'REMOVE']
}
};
</script>
<script src='pace/pace.min.dynamic.js'></script>
<link href="pace/flat.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<script>
var options = null;
var names = null;
var chart = null;
var time = []; //last timestamp for data in each series
var refeshable = [];
var timer = null; //ajax get data timer
var lastupdate = null;
var idleTime = null;
var initiallength = 0;
var sock = null;
var reconnectalert = false;
var lastredraw = null;
var tinydb = [];
var tinydblock = false; //true if data calculation is in proccess, result i that data is paused, and qued.
var offset = 0;
Highcharts.setOptions({
global: {
timezoneOffset: -2 * 60
}
});
$(document).ajaxStart(function() { Pace.restart(); });
$(document).ready(function() {
var idleInterval = setInterval(function(){idleTime++;}, 1000);
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
$.ajax({ url: "select.php?type=selectgroup",
context: document.body,
success: function(data){
$('#selectgroup').html(data);
}});
$.ajax({ url: "select.php?type=selecttype",
context: document.body,
success: function(data){
$('#selecttype').html(data);
}});
$.ajax({ url: "submit.php",
context: document.body,
success: function(data){
$('#valgte').html(data);
$("[data-toggle='toggle']").bootstrapToggle('destroy');
$("[data-toggle='toggle']").bootstrapToggle();
}});
var last_valid_selection = null;
$('#reset').click(function() {
if (chart != null) {
reconnectalert = false;
$('#container').highcharts().destroy();
$('#stoplive').hide();
$('#info').html('Live data stopped. Press Get plot in order to restart it.');
clearInterval(timer);
sock.close();
}
chart = null;
$("#container").animate({
'width' : '0%',
'height': '0%'
});
$.ajax({ url: "submit.php?reset",
context: document.body,
success: function(data){
$('#selectdevices').html('');
$('#selectmes').html('');
$('#valgte').html('');
$("option:selected").prop("selected", false);
}});
});
$('#stoplive').click(function() {
$('#stoplive').hide();
$('#info').html('Live data stopped. Press Get plot in order to restart it.');
reconnectalert = false;
sock.close();
});
$('form').submit(function(event) {
var myForm = $(this);
var data = $(this).serializeArray();//serialize form inputs and pass them to php
$.post("submit.php",data,function(data){
$('#valgte').html(data);
$("[data-toggle='toggle']").bootstrapToggle('destroy');
$("[data-toggle='toggle']").bootstrapToggle();
});
$('#selectdevices').html('');
$('#selectmes').html('');
$("option:selected").prop("selected", false);
event.preventDefault();
});
$('#selectgroup').change(function(event) {
var myForm = $(this);
var data = $(this).serializeArray();//serialize form inputs and pass them to php
$.post("select.php?type=group",data,function(data){
$('#selectdevices').html(data);
});
if ($(this).val().length > 1) {
$(this).val(last_valid_selection);
} else {
last_valid_selection = $(this).val();
}
});
$('#selecttype').change(function(event) {
var myForm = $(this);
var data = $(this).serializeArray();//serialize form inputs and pass them to php
$.post("select.php?type=type",data,function(data){
$('#selectmes').html(data);
});
if ($(this).val().length > 1) {
$(this).val(last_valid_selection);
} else {
last_valid_selection = $(this).val();
}
});
});
var seriesOptions = [],
seriesCounter = 0,
updateCounter = 0;
function remove(what,id) {
$.ajax({ url: "submit.php?remove="+id+"&what="+what,
context: document.body,
success: function(data){
$('#valgte').html(data);
$("[data-toggle='toggle']").bootstrapToggle('destroy');
$("[data-toggle='toggle']").bootstrapToggle();
}});
}
function toggle(key,keyy) {
$.ajax({ url: "submit.php?key="+key+"&keyy="+keyy,
context: document.body,
success: function(data){
$('#valgte').html(data);
$("[data-toggle='toggle']").bootstrapToggle('destroy');
$("[data-toggle='toggle']").bootstrapToggle();
}});
}
function updatePlot(series) {
//updatetime = time;
//time = Date.now() / 1000 | 0; //unixtimestamp in secs
//debug
//updatetime = updatetime - 60;
var now = Date.now() / 1000 | 0
if (!document.hidden && (idleTime < 120 || (now-lastupdate) > 60)) {
lastupdate = Date.now() / 1000 | 0;
updateCounter = 0;
$.each(names, function (i, name) {
if (refeshable[i]) {
$.getJSON('https://it.pf.dk/fagprojekt/getdata.php?sID=' + name[0] + '&dID=' + name[1] + '&tID=' + name[2] + '&time='+ time[i] +'&callback=?', function (data) {
// var series = this.series[0];
//console.log('https://it.pf.dk/fagprojekt/getdata.php?sID=' + name[0] + '&dID=' + name[1] + '&tID=' + name[2] + '&time='+ updatetime +'&callback=?');
//console.log(name[4]+': '+JSON.stringify(data));
$.each(data, function(j,points) {
//console.log(name[4]+': '+JSON.stringify(data[j]));
//console.log(name[4]+' x: '+JSON.stringify(data[j][0]));
//console.log(name[4]+' y: '+JSON.stringify(data[j][1]));
series[i].addPoint([data[j][0],data[j][1]], false, true);
time[i] = data[j][0]/1000;
//console.log(name[4]+', x:'+points[i][0]+', y:'+points[i][1]);
})
updateCounter += 1;
if (updateCounter === names.length) {
chart.redraw();
}
});
}
});
} else console.log('Didnt update, window is hidden/reduced interval due to inactivity');
}
function getplot() {
var now = Date.now();
if (chart != null) {
reconnectalert = false;
$('#container').highcharts().destroy();
clearInterval(timer);
sock.close();
}
$('#stoplive').hide();
$('#info').html('Live streaming will start after plot has loaded.');
$("#container").animate({
'width' : '100%',
'height': '100%'
});
$("body").animate({"scrollTop": "0px"}, 1000);
$( "#matlab" ).html('');
seriesOptions = [];
seriesCounter = 0;
initiallength = 0;
options = null;
names = null;
twoload = [];
tdata = [];
cached = [];
tname = [];
//time = Date.now() / 1000 | 0; //unixtimestamp in secs
$.getJSON('getplot.php?&callback=?', function (data) {
options = data;
names = data[1];
//alert(JSON.stringify(options[0]));
$.each(names, function (i, name) {
$( "#matlab" ).append( '<a href=\"https://it.pf.dk/fagprojekt/getdata.php?sID=' + name[0] + '&dID=' + name[1] + '&tID=' + name[2] + '&type=MATLAB\">'+name[4]+'</a><br>');
twoload[i] = 0;
if ($.cookie("FC_Data_Plot_toggle").toString() == "0") {
$.getJSON('https://it.pf.dk/fagprojekt/getdata-cached.php?sID=' + name[0] + '&dID=' + name[1] + '&tID=' + name[2] + '&callback=?', function (data2) {
cached[i] = data2;
twoload[i] += 1;
if (twoload[i] === 2) {
addToPlot(tdata[i],cached[i],tname[i],i,seriesOptions)
}
seriesCounter += 1;
if (seriesCounter === names.length*2) {
chart = Highcharts.stockChart('container', options[0]);
var done = Date.now();
var loadtime = done-now;
console.log(initiallength+" datapoints loaded time: " + loadtime/1000 + " s");
}
});
}
$.getJSON('https://it.pf.dk/fagprojekt/getdata.php?sID=' + name[0] + '&dID=' + name[1] + '&tID=' + name[2] + '&callback=?', function (data) {
tdata[i] = data;
tname[i] = name;
twoload[i] += 1;
if (twoload[i] === 2 || $.cookie("FC_Data_Plot_toggle").toString() == "1") {
addToPlot(tdata[i],cached[i],tname[i],i,seriesOptions)
}
seriesCounter += 1;
if (seriesCounter === names.length*2 || $.cookie("FC_Data_Plot_toggle").toString() == "1" && seriesCounter === names.length) {
chart = Highcharts.stockChart('container', options[0]);
var done = Date.now();
var loadtime = done-now;
console.log(initiallength+" datapoints loaded time: " + loadtime/1000 + " s");
}
});
});
});
}
function addToPlot(data,cached,name,i,seriesOptions) {
refeshable[i] = true;
if (data == undefined) data = [];
if (cached == undefined) cached = [];
if( !$.isArray(data) || !data.length || data == "," || data == ",," || data == ",,," || data == "") {
var plotdata = cached;
refeshable[i] = false;
console.log(name[4] + " no new data, only cached used. Won't liveupdate this dataseries.");
}
else if( !$.isArray(cached) || !cached.length || cached == "," || cached == ",," || cached == "") {
var plotdata = data;
console.log(name[4] + " no cached data, only new data used");
} else {
var plotdata = cached.concat(data);
console.log(name[4] + " no problems");
}
if (plotdata == undefined) plotdata = [];
//console.log(name[4] + " " + plotdata);
/*
alert(name[4]+" data: "+data);
alert(name[4]+" cache: "+cached);
alert(name[4]+" total: "+plotdata);*/
seriesOptions[i] = {
name: name[4],
yAxis: name[3],
color: Highcharts.getOptions().colors[name[3]],
data: plotdata,
tooltip: {
valueSuffix: ' ' + name[5],
pointFormat: '{series.name}: <b>{point.y:.2f} '+name[5]+'</b><br/>'
}
};
initiallength+= plotdata.length;
if (plotdata.length != 0) time[i] = plotdata[plotdata.length-1][0]/1000;
else time[i] = Date.now();
}
var socketnames = [];
function startLiveUpdate(series) {
reconnectalert = true;
sock = new WebSocket("wss://it.pf.dk/fagprojekt/data");
lastredraw = Date.now();
sock.onopen = function(event) {
$('#stoplive').show();
console.log('Socket connected successfully');
socketnames = [];
tinydb = [];
$.each(names, function (i, name) {
if (refeshable[i]){
if (name[0] <=1){
if (socketnames.indexOf(name[6].toString()) == -1) socketnames.push(name[6].toString());
}
else if (name[0] == 2 || name[0] == 3) {
tinydb[i] = [];
$.each(name[6],function (j,nam){
//socketnames creation
if (socketnames.indexOf(nam.toString()) == -1) socketnames.push(nam.toString());
tinydb[i].push([nam.toString(),name[7],0,0]);
});
}
}
});
sock.send(JSON.stringify({
type: "name",
data: socketnames
}))
};
$('#info').html('Streaming data.');
sock.onmessage = function(event) {
var msg = JSON.parse(event.data)
//console.log(JSON.stringify(msg));
$.each(names, function (i, name) {
if (name[6].toString() == msg["device"].toString()) {
var x = msg["timestamp"];
var kind = name[7];
if (kind == "temp") kind = "temperature";
if (msg["state"][kind] != undefined) {
var val = parseFloat(msg["state"][kind]);
if (kind == "signal") var y = (val-255)/2.0;
else if (name[2] == 6) var y = val/100;
else if (name[2] == 1) var y = val/10;
else if (name[2] == 2) var y = val/1000;
else if (name[4] == 4) var y = (val-1)/10000;
else var y = val;
//console.log("x: "+x+" y: "+y+" kind: "+kind+" i: "+i+ " device: "+msg["device"].toString());
/* console.log(x);
console.log(y);*/
series[i].addPoint([x,y], false, false);
}
var done = Date.now();
offset = done-msg.timestamp;
}
});
//add data to tinydb
$.each(tinydb, function(i,td){
//console.log("Looking into tinydb no. "+i);
if (td != undefined) {
//console.log("adding mes to tinydb "+i);
$.each(td,function(q,tdb){
if(tdb[0] == msg["device"]) {
//console.log(msg);
$.each(msg["state"], function (j, mes) {
if (tdb[1] == j) {
var val = parseFloat(mes);
if (j == "signal") var y = (val-255)/2.0;
else if (j == "temp") var y = val/100;
else if (j == "voltage") var y = val/10;
else if (j == "current") var y = val/1000;
else if (j == "light") var y = (val-1)/10000;
else var y = val;
//if (y != 0) console.log(j+" "+y);
while(tinydblock) {}//Lås tinydb ved udtræk, respekter dette
tdb[2]+=y;
tdb[3]++;
}
});
}
});
}
});
if (Date.now()-lastredraw > 1000) {
lastredraw = Date.now();
tinydblock = true;// console.log("Locking...");
var toremove = [];
//console.log("start of script");
//console.log(tinydb);
$.each(names, function(i, name){
if (name[0] == 2 || name[0] == 3) {
var val = 0;
var n = 0;
//console.log(name[6]);
$.each(name[6], function(j,nam){
for (var q = 0;q<tinydb[i].length;q++) {
if (tinydb[i][q][0]==nam) {
//if (name[0] == 3) console.log(nam+" vals: "+tinydb[i][q][2]/tinydb[i][q][3]);
if (tinydb[i][q][3] != 0) {
val+=tinydb[i][q][2]/tinydb[i][q][3];
toremove.push([i,q]);
// tinydb[i][q][2]=0;
// tinydb[i][q][3]=0;
n++;
}
}
//console.log("val: "+val+" name[1]: "+name[1]);
}
});
//console.log("HEY");
//console.log("val: "+val+" len: "+name[1].length+" n: "+n+" name[0]: "+name[0]);
//console.log(name);
if (n >= name[1].length){
$.each(toremove,function(j,rem){
tinydb[rem[0]][rem[1]][2]=0;
tinydb[rem[0]][rem[1]][3]=0;
});
if (name[0] == 2 && n != 0) series[i].addPoint([lastredraw-offset,val/n], false, false);
else if (name[0] == 3 && n != 0) series[i].addPoint([lastredraw-offset,val], false, false);
}}
});
/*
$.each(tinydb, function(i,tdb){
tdb[2]=0;
tdb[3]=0;
});*/
tinydblock = false; //console.log("Unlocking...");
chart.redraw();
}
}
sock.onclose = function() {
if (reconnectalert) {
// if (confirm('Live-update connection lost, reconnect?')) {
startLiveUpdate(series);
//} else {
//$('#stoplive').hide();
// $('#info').html('Live data stopped. Press Create plot in order to restart it.');
// Do nothing!
// }
}
}
}
</script>
</head>
<body>
<div id=container></div>
<button id=reset>Reset</button> <button id=stoplive style="display:none">Stop liveupdate</button> <div id=info style=display:inline></div>
<div style="margin:0;width=100%">
<div style="float:left;width:50%">
<p><b>Tilføj dataserie(r) ud fra gruppe</b></p>
<form>
<select id=selectgroup name=selectgroup multiple size=10>
</select>
<select id=selectdevices name="selectdevices[]" multiple size=10>
</select>
<input type=submit value="Tilføj valgte">
</form>
</div>
<div style="float:right;width:50%">
<p><b>Tilføj dataserie(r) ud fra type</b></p>
<form>
<select id=selecttype name=selecttype multiple size=10>
</select>
<select id=selectmes name="selectmes[]" multiple size=10>
</select>
<input type=submit value="Tilføj valgte">
</form>
</div></div>
<form>
<select id=addmes name=addmes>
<?php //<option value=0>Måling</option> ?>
<option value=1>Signal</option>
<option value=2>Gennemsnit</option>
<option value=3 selected="">Sum</option>
</select>
Navn på dataserien: <input type=text name=name>
<input type=submit value="Tilføj">
</form>
<p><b>Valgte dataserier</b></p>
<div id=valgte></div>
<p><b>Matlab download (for de viste data i plottet)</b></p>
<div id=matlab></div>
</body>
</html>