-
Notifications
You must be signed in to change notification settings - Fork 2
/
dagelijks.htm
705 lines (673 loc) · 25.1 KB
/
dagelijks.htm
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
<!doctype html>
<html lang="en">
<head>
<script>
const debugip="192.168.0.90"; //change this to the esp32 ip for remote running of this page
</script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="data:;base64,iVBORw0KGgo="><!--prevent favicon requests-->
<link rel="preload" as="font" href="https://fonts.googleapis.com/css?family=Roboto" crossorigin="anonymous"> <!-- force loading of font - see https://stackoverflow.com/questions/2756575/drawing-text-to-canvas-with-font-face-does-not-work-at-the-first-time/61809834#61809834 and https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content and https://stackoverflow.com/questions/45321043/preload-css-file-not-supported-on-firefox-and-safari-mac -->
<title>slimme meter dagoverzicht</title>
<meta name="description" content="smartMeterLogger-esp32">
<script>!function(a,b){"function"==typeof define&&define.amd?define([],b):"undefined"!=typeof module&&module.exports?module.exports=b():a.ReconnectingWebSocket=b()}(this,function(){function a(b,c,d){function l(a,b){var c=document.createEvent("CustomEvent");return c.initCustomEvent(a,!1,!1,b),c}var e={debug:!1,automaticOpen:!0,reconnectInterval:1e3,maxReconnectInterval:3e4,reconnectDecay:1.5,timeoutInterval:2e3};d||(d={});for(var f in e)this[f]="undefined"!=typeof d[f]?d[f]:e[f];this.url=b,this.reconnectAttempts=0,this.readyState=WebSocket.CONNECTING,this.protocol=null;var h,g=this,i=!1,j=!1,k=document.createElement("div");k.addEventListener("open",function(a){g.onopen(a)}),k.addEventListener("close",function(a){g.onclose(a)}),k.addEventListener("connecting",function(a){g.onconnecting(a)}),k.addEventListener("message",function(a){g.onmessage(a)}),k.addEventListener("error",function(a){g.onerror(a)}),this.addEventListener=k.addEventListener.bind(k),this.removeEventListener=k.removeEventListener.bind(k),this.dispatchEvent=k.dispatchEvent.bind(k),this.open=function(b){h=new WebSocket(g.url,c||[]),b||k.dispatchEvent(l("connecting")),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","attempt-connect",g.url);var d=h,e=setTimeout(function(){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","connection-timeout",g.url),j=!0,d.close(),j=!1},g.timeoutInterval);h.onopen=function(){clearTimeout(e),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onopen",g.url),g.protocol=h.protocol,g.readyState=WebSocket.OPEN,g.reconnectAttempts=0;var d=l("open");d.isReconnect=b,b=!1,k.dispatchEvent(d)},h.onclose=function(c){if(clearTimeout(e),h=null,i)g.readyState=WebSocket.CLOSED,k.dispatchEvent(l("close"));else{g.readyState=WebSocket.CONNECTING;var d=l("connecting");d.code=c.code,d.reason=c.reason,d.wasClean=c.wasClean,k.dispatchEvent(d),b||j||((g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onclose",g.url),k.dispatchEvent(l("close")));var e=g.reconnectInterval*Math.pow(g.reconnectDecay,g.reconnectAttempts);setTimeout(function(){g.reconnectAttempts++,g.open(!0)},e>g.maxReconnectInterval?g.maxReconnectInterval:e)}},h.onmessage=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onmessage",g.url,b.data);var c=l("message");c.data=b.data,k.dispatchEvent(c)},h.onerror=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onerror",g.url,b),k.dispatchEvent(l("error"))}},1==this.automaticOpen&&this.open(!1),this.send=function(b){if(h)return(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","send",g.url,b),h.send(b);throw"INVALID_STATE_ERR : Pausing to reconnect websocket"},this.close=function(a,b){"undefined"==typeof a&&(a=1e3),i=!0,h&&h.close(a,b)},this.refresh=function(){h&&h.close()}}return a.prototype.onopen=function(){},a.prototype.onclose=function(){},a.prototype.onconnecting=function(){},a.prototype.onmessage=function(){},a.prototype.onerror=function(){},a.debugAll=!1,a.CONNECTING=WebSocket.CONNECTING,a.OPEN=WebSocket.OPEN,a.CLOSING=WebSocket.CLOSING,a.CLOSED=WebSocket.CLOSED,a});</script>
<style>
*{box-sizing:border-box;}
html,body{
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:0;
padding:0;
background:black;
color:antiquewhite;
text-align:center;
font-family:'Roboto',sans-serif;
}
a{
text-decoration:none;
color:yellow;
}
h2{
margin:5px;
}
label{
width:50%;
padding:5px;
text-align:right;
}
select{
text-align:left;
font:inherit;
height:fit-content;
margin:5px;
border:solid 1px grey;
border-radius:5px;
background:inherit;
}
select:disabled{
color:white;
background:inherit;
}
.noselect{
-webkit-touch-callout:none; /* iOS Safari */
-webkit-user-select:none; /* Safari */
-khtml-user-select:none; /* Konqueror HTML */
-moz-user-select:none; /* Firefox */
-ms-user-select:none; /* Internet Explorer/Edge */
user-select:none; /* Non-prefixed version,currently
supported by Chrome and Opera */
}
#graphContainer{
margin:5px auto 2px auto;
border:solid 1px antiquewhite;
position:relative;
overflow:hidden;
border-radius:5px;
}
#dayGraph,#compareGraph,#background{
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
}
#background{
opacity:0.6;
}
#compareGraph{
opacity:0.5;
}
#dayGraph{
opacity:0.6;
}
#currentdate{
display:none;
}
#datepicker{
position:absolute;
top:10px;
bottom:10px;
left:10px;
right:10px;
font-size:22px;
padding:10px;
border:solid 1px white;
border-radius:5px;
background:black;
display:none;
overflow-y:auto;
}
#feedbackmsg{
position:absolute;
margin:auto;
background:black;
font-size:22px;
color:antiquewhite;
top:5px;
left:100px;
right:100px;
top:20px;
padding:10px;
border:solid 1px white;
border-radius:5px;
display:none;
}
#scale{
display:flex;
margin:auto;
}
.navbutton,.rangebutton{
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
text-transform:none;
padding:0px;
margin:0;
cursor:pointer;
text-align:center;
position:relative;
width:25%;
font-size:20px;
font-family:inherit;
border:solid 1px grey;
border-radius:5px;
background:lightgrey;
vertical-align:middle;
}
.navbutton:active,.rangebutton:active {
transform:scale(0.95);
}
#currentuse{
//line-height:120px;
display:none;
font-size:115px;
white-space:nowrap;
}
#header{
width:1440px;
margin:auto;
font-size:22px;
display:flex;
padding:4px 0 0 0;
}
#date,#compare{
margin:0;
white-space:nowrap;
width:50%;
}
#comparetitle {
margin:0;
}
#yearchoice,#monthchoice,#daychoice{
margin:5px;
display:flex;
}
@media only screen and (max-width:600px) {
#current,#scale,#graphContainer,#dayGraph,#compareGraph,#background{
display:none;
}
#currentdate{display:block;}
#currentuse{display:block;}
#header,#current{display:none;}
#footerbox,#compare{display:none;}
}
@media only screen and (min-width:600px) {
#graphContainer,#dayGraph,#compareGraph,#background {
width:600px;
height:208px;
}
#header,#scale{width:600px;}
}
@media only screen and (min-width:800px) {
#graphContainer,#dayGraph,#compareGraph,#background{
width:800px;
height:277px;
}
#header,#scale{width:800px;}
}
@media only screen and (min-width:1024px) {
#graphContainer,#dayGraph,#compareGraph,#background{
width:1024px;
height:355px;
}
#header,#scale{width:1024px;}
}
@media only screen and (min-width:1280px) {
#graphContainer,#dayGraph,#compareGraph,#background{
width:1280px;
height:444px;
}
#header,#scale{width:1280px;}
}
@media only screen and (min-width:1440px) {
#graphContainer,#dayGraph,#compareGraph,#background{
width:1440px;
height:500px;
}
#header,#scale{width:1440px;}
}
.footer{
margin:5px 10px;
display:inline-table;
white-space:nowrap;
}
#smallunit{
font-size:35px;
line-height:10px;
}
.currenttext{
font-size:20px;
margin:0;
padding:0;
}
.navbutton{
vertical-align:middle;
}
.navicon{
vertical-align:middle;
margin:4px;
}
.nowrap{
white-space:nowrap;
}
.comparechoice{
color:lightgrey;
}
#stopcompare{
color:lightgrey;
}
.comparechoice:hover,#stopcompare:hover{
cursor:pointer;
}
</style>
</head>
<body class="noselect">
<div id="header">
<p id="date"> </p><p id="compare"><span class="comparechoice" id="datechoice">vergelijk</span></p>
</div>
<div id="graphContainer">
<canvas id="background" width="1440" height="500"></canvas>
<canvas id="compareGraph" width="1440" height="500"></canvas>
<canvas id="dayGraph" width="1440" height="500"></canvas>
<p id="feedbackmsg"></p>
<p id="datepicker"></p>
</div>
<div id="scale">
<button type="button" id="prevbutton" class="navbutton"><svg class="navicon" xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"/></svg></button>
<input type="button" class="rangebutton" value="500 W" onmouseup="setRange(500)">
<input type="button" class="rangebutton" value="1000 W" onmouseup="setRange(1000)">
<input type="button" id="startRange" class="rangebutton" value="2000 W" onmouseup="setRange(2000)">
<input type="button" class="rangebutton" value="4000 W" onmouseup="setRange(4000)">
<button type="button" id="nextbutton" class="navbutton"><svg class="navicon" xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"/></svg></button>
</div>
<p id="currentdate"> </p>
<div id="currentuse"></div>
<div id="footerbox"></div>
<script>
const http_protocol = window.location.protocol !== 'https:' ? 'http://' : 'https://';
const http_host = http_protocol + (window.location.hostname ? window.location.hostname : debugip);
const ws_protocol = window.location.protocol !== 'https:' ? 'ws://' : 'wss://';
const ws_host = ws_protocol + (window.location.hostname ? window.location.hostname : debugip) + "/events";
var currentRange = 2000; //in Watts
var lowestUse = 10000000;
var hightime;
var highestUse = 0;
var lowtime;
var savedData;
var savedCompareData;
var reboots;
var currentDay;
var compareDay;
const compareColor = "yellow";
function ready(callbackFunction){
if(document.readyState != 'loading')
callbackFunction(event);
else
document.addEventListener("DOMContentLoaded", callbackFunction);
}
function logFileUrl(date) {
const datum = new Date(date);
return http_host + '/' + datum.getFullYear() + '/' + (datum.getMonth() + 1) + '/' + datum.getDate() + '.log';
}
function currentDayEqualsToday() {
const today = new Date();
const current = new Date(currentDay);
return (current.getFullYear() == today.getFullYear() && current.getMonth() == today.getMonth() && current.getDate() == today.getDate());
}
function showLoadingFileMsg(date) {
const options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour12: false};
const datestr = new Date(date).toLocaleDateString('nl-NL', options);
const feedbackmsg = document.getElementById('feedbackmsg');
feedbackmsg.innerHTML = 'data van ' + datestr + ' ophalen';
feedbackmsg.style.display = 'block';
}
function updateDate(tm){
const options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour12: false};
document.getElementById('date').innerHTML = tm.toLocaleDateString('nl-NL', options);
}
function updateFooter(){
const options = {hour: 'numeric', minute: '2-digit', hour12: false};
var html = reboots ? '<span class="footer">' + reboots + ' reboot(s)</span>' : '';
html += '<span class="footer">laagste verbruik: ' + lowestUse + ' W om ' + lowtime.toLocaleTimeString('nl-NL', options) + '</span>';
html += '<span class="footer">hoogste verbruik: ' + highestUse + ' W om ' + hightime.toLocaleTimeString('nl-NL', options) + '</span>';
document.getElementById('footerbox').innerHTML = html;
}
function map(x,in_min,in_max,out_min,out_max){
return(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
function setRange(newrange){
currentRange = newrange;
drawBackground();
drawGraph(savedData, false);
drawGraph(savedCompareData, true);
}
function getData(date, callBack, toCompareCanvas) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
switch (this.status) {
case 200 :
if (!toCompareCanvas) {
savedData = this.responseText;
currentDay = date;
}
else {
savedCompareData = this.responseText;
compareDay = date;
}
callBack(this.responseText, toCompareCanvas);
break;
case 404 : {
const options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour12: false};
document.getElementById('feedbackmsg').innerHTML = 'geen data aanwezig van <span class="nowrap">' + new Date(date).toLocaleDateString('nl-NL', options) + '</span>';
}
break;
default : console.log("unhandled xhr status code " + this.status);
}
}
}
xhttp.open('GET', logFileUrl(date), true);
xhttp.send();
}
function showAvailableMonths(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
switch (this.status) {
case 200 : {
var html = '<p id="monthchoice"><label for="months">maand</label>';
html += '<select name="months" id="months">';
this.responseText.split('\n').forEach(function(item){
if (item) html += '<option value="' + item + '">' + item.substring(item.lastIndexOf('/') + 1, item.length) + '</option>';
});
html += '<option selected value=""></option>';
html += '</select></p>';
//save the selected index
const savedYear = document.getElementById('years').selectedIndex;
document.getElementById('datepicker').innerHTML += html;
//restore the saved index
var years = document.getElementById('years');
years.selectedIndex = savedYear;
years.setAttribute('disabled', true)
document.getElementById('months').focus();
}
break;
case 404 :
console.log("404");
break;
default : console.log("unhandled xhr status code " + this.status);
}
}
}
xhttp.open('GET', http_host + '/maanden?jaar=' + document.getElementById('years').value, true);
xhttp.send();
}
function showAvailableDays(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
switch (this.status) {
case 200 : {
var html = '<p id="daychoice"><label for="days">dag</label>';
html += '<select name="days" id="days">';
this.responseText.split('\n').forEach(function(item){
if (item) html += '<option value="' + item.toLowerCase() + '">' + item.substring(item.lastIndexOf('/') + 1, item.indexOf('.')) + '</option>';
});
html += '<option selected value=""></option>';
html += '</select></p>';
//save the index
const savedYear = document.getElementById('years').selectedIndex;
const savedMonth = document.getElementById('months').selectedIndex;
document.getElementById('datepicker').innerHTML += html;
//restore the saved index
document.getElementById('years').selectedIndex = savedYear;
document.getElementById('months').selectedIndex = savedMonth;
document.getElementById('months').setAttribute('disabled', true);
document.getElementById('days').focus();
}
break;
case 404 :
console.log('404');
break;
default : console.log("unhandled xhr status code " + this.status);
}
}
}
xhttp.open('GET', http_host + '/dagen?maand=/' + document.getElementById('years').value + "/" + document.getElementById('months').value, true);
xhttp.send();
}
function getCompareData(url) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
switch (this.status) {
case 200 :
const year = parseInt(url.substring(1, 5));
const month = parseInt(url.substring(6, url.lastIndexOf('/'))) - 1
const mday = parseInt(url.substring(url.lastIndexOf('/') + 1, url.length - 4));
const date = new Date(year, month, mday);
const options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour12: false};
const datechoice = document.getElementById('datechoice');
datechoice.innerHTML = date.toLocaleDateString('nl-NL', options);
datechoice.style.color = compareColor;
savedCompareData = this.responseText;
drawGraph(this.responseText, true);
break;
case 404 :
console.log('404');
break
default: console.log('unhandled xhr status: ' + this.status);
}
document.getElementById('datepicker').style.display = 'none';
}
}
xhttp.open('GET', http_host + url);
xhttp.send();
}
ready(event => {
const oneday = 86400 * 1000;
var ws = new ReconnectingWebSocket(ws_host);
ws.onmessage = function(e){
if (e.data.startsWith('current\n')) {
const use = e.data.substring(e.data.indexOf('\n') + 1).split('\n');
document.getElementById('currentuse').innerHTML = '<p class="currenttext">huidig verbruik</p>' + use[0] + '<span id="smallunit"> w</span>';
}
else if (e.data.startsWith('electric_saved\n')) {
if (currentDayEqualsToday()) {
const item = e.data.substring(e.data.indexOf('\n'));
savedData += item;
drawGraphItem(item, false);
}
}
}
ws.onopen = function(){
console.log('[WebSocket] Connected to ' + ws_host);
if (currentDayEqualsToday()) {
const today = new Date(currentDay);
setTimeout(getData(today, drawGraph, false), 100);
}
}
ws.onclose = function(event){
console.log('[WebSocket] Connection died, code=' + event.code + ' reason=' + event.reason);
}
ws.onerror = function(error){
console.log('[WebSocket] ' + ws_host + ' Error: ' + error.message);
}
document.getElementById('prevbutton').addEventListener('mouseup', function() {
const previousday = new Date(currentDay).getTime() - oneday;
showLoadingFileMsg(previousday);
getData(previousday, drawGraph);
});
document.getElementById('nextbutton').addEventListener('mouseup', function() {
const nextday = new Date(currentDay).getTime() + oneday;
showLoadingFileMsg(nextday);
getData(nextday, drawGraph);
});
document.getElementById('feedbackmsg').addEventListener('mouseup', function() {
this.style.display = 'none';
});
// https://usefulangle.com/post/138/pure-javascript-event-handler-dynamic-element
document.querySelector('#datepicker').addEventListener('change', function(e) {
switch (e.target.id) {
case 'years' :
showAvailableMonths();
break;
case 'months' :
showAvailableDays();
break;
case 'days' :
getCompareData("/" + years.value + "/" + months.value + "/" + days.value);
break;
default : console.log('unhandled change on id ' + e.target.id);
}
});
document.querySelector('#datepicker').addEventListener('mouseup', function(e) {
switch (e.target.id) {
case 'stopcompare' :
this.style.display = 'none';
document.getElementById('feedbackmsg').style.display = 'none';
const datechoice = document.getElementById('datechoice');
datechoice.innerHTML = 'vergelijk';
datechoice.style.color = 'lightgrey';
const canvas = document.getElementById('compareGraph');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
savedCompareData = '';
break;
default : console.log('unhandled mouseup on id ' + e.target.id);
}
});
document.getElementById('datechoice').addEventListener('mouseup', function() {
document.getElementById('feedbackmsg').style.display = 'none';
var datepicker = document.getElementById('datepicker');
datepicker.innerHTML = '';
datepicker.style.display = (datepicker.style.display == 'block') ? 'none' : 'block';
if (datepicker.style.display == 'none') return;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
switch (this.status) {
case 200 : {
var html = '<p id="comparetitle">kies een datum of <span id="stopcompare">stop vergelijken</span></p><p id="yearchoice"><label for="years">jaar</label>';
html += '<select name="years" id="years">';
this.responseText.split('\n').forEach(function(item){
if (item) html += '<option value="' + item + '">' + item + '</option>';
});
html += '<option selected value=""></option>';
html += '</select></p>';
datepicker.innerHTML = html;
document.getElementById('years').focus();
}
break;
case 404 :
console.log('404');
break;
default : console.log('unhandled http status code ' + this.status);
}
}
}
xhttp.open('GET', http_host + '/jaren', true);
xhttp.send();
});
document.getElementById('currentdate').innerHTML = new Date().toLocaleDateString('nl-NL', {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour12: false});
drawBackground();
currentDay = new Date();
document.getElementById('startRange').focus();
document.addEventListener('visibilitychange', e=>{
switch(document.visibilityState) {
case 'visible' : {
ws.open();
if (currentDayEqualsToday())
getData(new Date(currentDay), drawGraph, false);
}
break;
case 'hidden' : {
ws.close();
}
default : {}
}
});
//window.addEventListener("unload", ws.close());
//window.addEventListener("focus", ws.open());
})
function drawBackground() {
const canvas = document.getElementById('background');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = "20px 'Roboto',sans-serif";
ctx.setLineDash([5,5,5]);
ctx.lineWidth = .7;
ctx.strokeStyle='white';
ctx.fillStyle = 'yellow';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const step = canvas.height/10;
for(vgrid = canvas.height - step; vgrid >= 0 + step; vgrid -= step){
ctx.beginPath();
ctx.moveTo(0, vgrid);
ctx.lineTo(1440, vgrid);
ctx.stroke();
const wattage = map(vgrid, canvas.height, 0, 0, currentRange).toString();
ctx.fillText(wattage, 60, vgrid);
ctx.fillText(wattage, 1440 >> 1, vgrid);
ctx.fillText(wattage, 1440 - 60, vgrid);
}
ctx.fillStyle = 'lightblue'
ctx.strokeStyle = 'lightblue';
for(h = 1;h < 24; h++){
const xpos = map(h,0,24,0,1440);
ctx.beginPath();
ctx.lineWidth = .7;
ctx.moveTo(xpos, 0);
ctx.lineTo(xpos, canvas.height);
ctx.stroke();
ctx.fillText(h, xpos, 25);
ctx.fillText(h, xpos, canvas.height - 25);
}
}
function drawGraphItem(item, toCompareCanvas){
if (!item) return;
const itemdata = item.split(' ');
const tm = new Date(parseInt(itemdata[0]) * 1000);
const currentUse = parseInt(itemdata[1]);
if (currentUse < lowestUse) {
lowestUse = currentUse;
lowtime = tm;
}
else if (currentUse > highestUse) {
highestUse = currentUse;
hightime = tm;
}
const secondstoday = tm.getSeconds() + tm.getMinutes() * 60 + tm.getHours() * 3600;
const xpos = map(secondstoday - 30, 0, 86400, 0, 1440);
const canvas = document.getElementById(toCompareCanvas ? 'compareGraph' : 'dayGraph');
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.lineWidth = 1;
ctx.strokeStyle = toCompareCanvas ? compareColor : 'white';
ctx.moveTo(xpos, canvas.height);
const ypos = map(currentUse, 0, currentRange, canvas.height, 0);
ctx.lineTo(xpos, ypos);
ctx.stroke();
}
function drawGraph(rawdata, toCompareCanvas){
if (!rawdata) return;
const canvas = document.getElementById(toCompareCanvas ? 'compareGraph' : 'dayGraph');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.lineWidth = 2;
ctx.setLineDash([]);
//raw (file) data always starts with a '#' which if it is the first item in the first line of the file can be discarded - it is the lastreboottime
if (rawdata.startsWith("#")) rawdata = rawdata.substring(rawdata.indexOf('\n') + 1);
//extract the date
if (!toCompareCanvas) {
const date = parseInt(rawdata.substring(0,rawdata.indexOf(' ')));
updateDate(new Date(date * 1000));
}
rawdata.split('\n').forEach(function(item){
if (!toCompareCanvas && item.startsWith('#')) {
const tm = new Date(parseInt(item.substring(1, item.indexOf(" "))) * 1000);
const reboottime = tm.getSeconds() + tm.getMinutes() * 60 + tm.getHours() * 3600;
const xpos = map(reboottime, 0, 86400, 0, 1440);
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "red";
ctx.moveTo(xpos, 0);
ctx.lineTo(xpos, canvas.height);
reboots++;
ctx.stroke();
} else
drawGraphItem(item, toCompareCanvas);
});
if (!toCompareCanvas) updateFooter();
lowestUse = 1000000;
highestUse = 0;
reboots = 0;
document.getElementById("feedbackmsg").style.display = 'none';
};
</script>
</body>
</html>