-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
1615 lines (1304 loc) · 46.9 KB
/
main.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
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Author: Edwin Glaser
// -------- Only for private usage -----------
// Date Nov. 2022
// HomeWizard P1
// https://homewizard-energy-api.readthedocs.io/endpoints.html#basic-information-api
// Tested on HWE-P1
// Data Description
// smr_version The DSMR version of the smart meter
// meter_model The brand indification the smart meter
// wifi_ssid The Wi-Fi network that the meter is connected to
// wifi_strength The strength of the Wi-Fi signal in %
// total_power_import_t1_kwh The power usage meter reading for tariff 1 in kWh
// total_power_import_t2_kwh The power usage meter reading for tariff 2 in kWh
// total_power_export_t1_kwh The power feed-in meter reading for tariff 1 in kWh
// total_power_export_t2_kwh The power feed-in meter reading for tariff 2 in kWh
// active_power_w The total active usage in Watts
// active_power_l1_w The active usage for fase 1 in Watts
// active_power_l2_w The active usage for fase 2 in Watts
// active_power_l3_w The active usage for fase 3 in Watts
// total_gas_m3 The gas meter reading in m3
// gas_timestamp The most recent gas update time stamp structured as YYMMDDhhmmss
// active_liter_lpm Active water usage in liters per minute
// total_liter_m3 Total water usage in cubic meters since installation
// Credits o.a.:
// Linechart source based on: https://codepen.io/yahiaelbnna/pen/KKgLOro
// {ip}/api
// P1
//{"product_type":"HWE-P1","product_name":"P1 meter","serial":"3c39e72ea574","firmware_version":"3.02","api_version":"v1"}
// Socket
// {"product_type":"HWE-SKT","product_name":"Energy Socket","serial":"<serial>","firmware_version":"3.02","api_version":"v1"}
// Kwh meter
// {"product_type":"SDM230-wifi","product_name":"KWh meter","serial":"<serial>","firmware_version":"2.11","api_version":"v1"}
// Watermeter
// {"product_type":"HWE-WTR","product_name":"Watermeter","serial":"<serial>","firmware_version":"1.17","api_version":"v1"}
//{ip}/api/v1/data
// P1
// Energy socket:
// {"wifi_ssid":"xxxxx","wifi_strength":54,"total_power_import_t1_kwh":12.345,"total_power_export_t1_kwh":456.789,"active_power_w":0.987,"active_power_l1_w":0.901}
// Kwh meter:
// {"wifi_ssid":"xxxx","wifi_strength":98,"total_power_import_t1_kwh":12.345,"total_power_export_t1_kwh":3456.789,"active_power_w":6.543,"active_power_l1_w":5.432}
// Watermeter:
// {"wifi_ssid":"xxxxx ","wifi_strength":72,"total_liter_m3":21.012,"active_liter_lpm":0}
var ip = {};
// var ip_W1;
let c = {};
let h = 0;
let w = 0;
let Ctx = {}; // Electr Canvas
let CtxG = {}; // Gas Canvas
let dataX = [], // X-axis
dataXG = [],
dataL_Total = [], // Y-axis total
datal1 = [], // Phase 1
datal2 = [], // Phase 2
datal3 = []; // Phase 3
let dataGas = [], // Gas Every call
dataGasPoint = []; // Gas Every Unique Measurement
var un = 0; // Unit : Value per 30 pixels
let xs = 0;
let dataT = [];
let button_stop = true; // is stopped so start is showing
let tabE = {};
let tabG = {};
var zoomStatus = false;
var sliderPercentage = 1; // [factor = percentage/100]
var sliderPoints = 10;
let debug = false;
var MMU = []; // Minimum Maximum Unit 0 = elec 1 = gas
var tabIndexCanvas = 0; // canvas index
var tabIndeX_dataT = 0; // tab index of E or G
// var sliderPoints = 10;
const max_seconds = 60*60*24 //1500;
let e_start = {};
let e_stop = {};
let e_scan = {};
let _string = `<tr><th>Time (hh:mm:ss/dd-mm-yy)</th><th>Gas (m3)</th><th>DeltaSum(liter)</th><th>Delta (liter)</th><th>min.</th><th>Flow (liter/min )</th></tr> `; // Gas string accumulated
let min = 0,
max = 0;
let _s = 0;
let _e = 0;
let _index = 0;
let _length = 0;
var gVar = {
s: 0,
e: 0,
zoomStatus: false,
get zoom(){
if(s== undefined){
s = 0
}
if(e==undefined){
e = dataX.length||0;
}
if(zoomStatus == undefined){
zoomStatus = false
}
return {s,e, zoomStatus};
},
set zoom(o){
s = o.s;
e = o.e;
zoomStatus = o.zoomStatus;
}
}
//let _k = $('#TimeGasM3') ;
// let _k = object('#TimeGasM3') ;
var _k;
let iteration = 0;
function Init(){
var slider = document.getElementById("myRange");
var sliderP = document.getElementById("myPoints");
var sliderTableSwitch = document.getElementById("tableSwitch_GAS");
// var output = document.getElementById("demo");
// output.innerHTML = slider.value; // Display the default slider value
document.getElementById("pointsValue").innerHTML = sliderPoints;
// Update the current slider value (each time you drag the slider handle)
// sliderTableSwitch.oninput = function(){
// console.log(this);
// }
slider.oninput = function() {
sliderPercentage = Number( this.value / 100 );
let _se = set_s_e(sliderPoints, sliderPercentage);
_s = _se.s;
_e = _se.e;
// gVar.zoom = {s:_s, e:_e, zoomStatus:zoomStatus}
// if(!zoomStatus){
// _e = dataX.length;
// document.getElementById("rangeValue").innerHTML = dataX.length;
// }else{
document.getElementById("rangeValue").innerHTML = _e
document.getElementById("rangeValue_from").innerHTML = _s;
document.getElementById("pointsValue").innerHTML = sliderPoints;
// }
if(button_stop){
Ctx.clearRect(0, 0, w, h);
CtxG.clearRect(0,0,wg,hg);
drawLine()
};
}
sliderP.oninput = function() {
sliderPoints = Math.floor( Math.exp( Number( this.value ) ) );
let _se = set_s_e(sliderPoints, sliderPercentage);
_s = _se.s;
_e = _se.e;
// gVar.zoom = {s:_s, e:_e, zoomStatus:zoomStatus}
document.getElementById("rangeValue").innerHTML = _e
document.getElementById("rangeValue_from").innerHTML = _s;
document.getElementById("pointsValue").innerHTML = sliderPoints;
if(button_stop){
Ctx.clearRect(0, 0, w, h);
CtxG.clearRect(0,0,wg,hg);
drawLine()
};
}
_s = 0;
_e = 0;
zoomStatus = false;
gVar.zoom = {s:_s, e:_e, zoomStatus: zoomStatus};
if(e_start && e_start.style){
e_start.style.opacity = 1 };
if(e_stop && e_stop.style){
e_stop.style.opacity = 0 };
if(e_scan && e_scan.style){
e_scan.style.opacity = 1 };
iteration = 0;
dataX = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
dataL_Total = [0,100];
un = Math.round((Math.max(...dataL_Total)-Math.min(...dataL_Total))/10)
xs = (w-80)/dataX.length; // steps on horizontal axis
dataT = [];
// Initialise Diagram framwork
chart.setCtx(Ctx);
chart.chartLine()
chart.digram();
// Set Canvas focus to Gas and draw empty diagram
chart.setCtx(CtxG);
chart.chartLine();
chart.digram();
}
function set_s_e(sliderPoints, sliderPercentage){
// Set end point
let _e = Math.floor(sliderPercentage * dataX.length);
if(_e < 10){ _e = 10};
let _s = _e - sliderPoints;
if(_s < 0 || _s==undefined){_s = 0}
gVar.zoom = {s:_s, e:_e, zoomStatus:zoomStatus}
if(debug){console.log(_s, _e, sliderPoints, sliderPercentage)};
return {s:_s, e:_e}
}
function tableSwitch_GAS(){
let _checkBox = document.getElementById("tableSwitch_GAS").checked;
if(debug){console.log( _checkBox )};
let _container_GAS = document.getElementById("table_GAS");
if(_checkBox && _container_GAS){
// Show table GAS
_container_GAS.style.display = "block";
}else{
// Hide table GAS
_container_GAS.style.display = "none";
}
}
window.onload = function() {
e_start = document.getElementById('Start');
e_stop = document.getElementById('Stop');
e_scan = document.getElementById('Scan');
if(e_start){
e_start.style.opacity = 1 };
if(e_stop){
e_stop.style.opacity = 0 };
if(e_scan && e_scan.style){
e_scan.style.opacity = 1 };
c = document.querySelector("canvas[le]");
cg = document.querySelector("canvas[lg]");
tabE = document.querySelector("draw-canvas-data-set");
tabG = document.querySelector("draw-canvas-data-setG");
// Electr.
h = c.height;
w = c.width ;
// Gas
hg = cg.height;
wg = cg.width ;
// sliderPoints = Math.floor( ( c.width / 30 ) ) - 2;
// sliderPoints = sliderPoints || Math.floor( ( c.width / 30 ) ) - 2;
// sliderPoints = sliderPoints;
// Canvas Context
Ctx = c.getContext('2d');
CtxG = cg.getContext('2d');
cg.onmousemove = function(e){
document.getElementById('G').focus();
tabE.style.opacity = "0" // $('draw-canvas-data-set')
tabG.style.opacity = "0" // $('draw-canvas-data-setG')
mouseMove(e)
};
c.onmousemove = function(e){
tabE.style.opacity = "0" // $('draw-canvas-data-set')
tabG.style.opacity = "0" // $('draw-canvas-data-setG')
document.getElementById('E').focus();
mouseMove(e)
};
function mouseMove(e){
let _ID = e.currentTarget.attributes[1].nodeValue; // E G
tabIndexCanvas = e.currentTarget.attributes[4].nodeValue; // 1 2
_index = 0;
_s = gVar.zoom.s
_e = gVar.zoom.e;
zoomStatus = gVar.zoom.zoomStatus;
if (_e == -1 || isNaN(_e)){
_s = 0;
_e = dataX.length ;
if(debug){console.log('correction')};
}
gVar.zoom = {s:_s,e:_e, zoomStatus:zoomStatus};
// T has 4* the amount of data in
for(let data of dataT){
// for (let ix = _s; ix < (_e * 4); ix++) {
// const data = dataT[ix];
for (const [key, value] of Object.entries(data)) {
let dataG = value.split(","),
lx = Number(e.offsetX), // - 10,
ly = Number(e.offsetY), // - c.height + 25,
dx = Number(dataG[1]), // x axis
dy = Number(dataG[0]); // y axis
let _delta = Math.floor(Number(dataG[4])/ 3);
if (_delta < 10){ _delta = 10};
if ( ( lx > dx - _delta && lx < dx + _delta ) && ( ly > dy - _delta && ly < dy + _delta ) ){
if ( _ID == 'E' && dataG[3] != 'Gas'){
tabE.innerHTML = dataG[3] + ':' + dataG[2] // $('draw-canvas-data-set')
tabE.style.opacity = "1" // $('draw-canvas-data-set')
tabE.style.left = e.layerX + "px" // $('draw-canvas-data-set')
tabE.style.top = e.layerY + "px" // $('draw-canvas-data-set')
if(debug){ console.log(_index, Number(dataG[7])) };
tabIndeX_dataT = Number(dataG[7]);
}else if ( _ID == 'G' && dataG[3] == 'Gas'){
tabG.innerHTML = dataG[3] + ':' + dataG[2] //$('draw-canvas-data-setG')
tabG.style.opacity = "1" //$('draw-canvas-data-setG')
tabG.style.left = e.layerX + "px" //$('draw-canvas-data-setG')
tabG.style.top = e.layerY + "px" //$('draw-canvas-data-setG')
}
}
else{
// not found...
// zoomStatus = false;
}
lx = lx -1
dx = dx -1
}
_index++;
}
}
Init();
document.getElementById('IP_P1').value = '192.168.2.*';
};
async function wrapper(){
console.log('start');
await waitInterval( meter, 999);
// await waitInterval( meterW, 999);
console.log(iteration);
button_stop = true;
console.log('finish');
};
async function wrapperW(){
console.log('start W');
// await waitInterval( meter, 999);
await waitInterval( meterW, 999);
console.log(iteration);
button_stop = true;
console.log('finish W');
};
function toggleZoom(){
zoomStatus = gVar.zoom.zoomStatus;
zoomStatus = !zoomStatus;
zoomStatus = gVar.zoom.zoomStatus;
let _buttonZoom = document.getElementById("Zoom");
if(zoomStatus){
_buttonZoom.style.backgroundColor = "lightblue";
}else{
_buttonZoom.style.backgroundColor = "white";
}
if (gVar.zoom == undefined || gVar.zoom.s == undefined || gVar.zoom.e == 0){
if (sliderPercentage == undefined || sliderPercentage == 0){
sliderPercentage = 1
}
_e = Math.floor( dataX.length * sliderPercentage ) ;
if(_e < 10){ _e = 10};
// document.getElementById("rangeValue").innerHTML = _e;
_s = _e - sliderPoints; //10
if (_s< 0){
_s = 0;
}
gVar.zoom = {s: _s, e: _e, zoomStatus: zoomStatus};
}
if(button_stop){
Ctx.clearRect(0, 0, w, h);
CtxG.clearRect(0,0,wg,hg);
drawLine()};
}
function getMinMaxUn(AA){
min = 0,
max = 0;
let _A = [];
// zoomStatus = gVar.zoom.zoomStatus;
if(zoomStatus){
_s = gVar.zoom.s;
_e = gVar.zoom.e;
// // document.getElementById("rangeValue").innerHTML = _e;
}else{
_s = 0;
_e = dataX.length;
// // if(sliderPercentage == 1){
// // document.getElementById("rangeValue").innerHTML = _e
// // }
}
// document.getElementById("rangeValue").innerHTML = _e
// zoomStatus
if (zoomStatus && AA[0] != null ){
// _s = tabIndeX_dataT - 5;
// if (_s<0){_s=0}
// _e = tabIndeX_dataT + 5;
if (_e > AA[0].length){
_e = AA[0].length
_s = AA[0].length - 10
}
// if (_s==0){_e = tabIndeX_dataT + 10}
if (_s<0){_s=0 ; _e = _s + sliderPoints} //+10
if (_e > AA[0].length){
_e = AA[0].length;
}
_A[0] = []; _A[1] = []; _A[2] = []; _A[3] = [];
if (AA[0] != null && AA[0] != undefined && AA[0].length > 1){
_A[0] = AA[0].slice(_s,_e);
_A[1] = AA[1].slice(_s,_e);
_A[2] = AA[2].slice(_s,_e);
_A[3] = AA[3].slice(_s,_e);
}else{
_A = AA;
}
}else{
_s = 0;
if(AA[0]){
_e = AA[0].length }else{
// _e = -1
}
_A = AA;
}
// gVar.zoom = {s:_s, e:_e, zoomStatus:zoomStatus};
if (_A.length >= 1){
if (Array.isArray(_A[0])){
let maxArray = _A.map(a => Math.max.apply(null, a)); // Get max of each array
max = Math.max(...maxArray)
// Get max of that maxArray
let minArray = _A.map(a => Math.min.apply(null, a));
min = Math.min(...minArray)
}else{
max = Math.max(..._A)
min = Math.min(..._A)
}
}
if(min == max){
if ( (max-min) > 10){ //10
min = _A[0] * 0.999;
max = _A[0] * 1.001;
}else{
min = 0;
max = _A[0] * 1.01;//+ 10;
}
}
// Unit multiple of 1, 10, 2, 20, 5, 50, 100, 200, 500 etc
un = (max - min) / 10;
// un = un * 0.99// give a little more space
// Units of 100,150, 200 and such
if ( un >= 1000){
max = Math.ceil(max / 5000) * 5000;
min = Math.floor(min / 5000) * 5000;
un = (max - min) / 10;
// un = Math.round(un / 500 ) * 500;
// Units of 10, 15, 20 etc
}else
if ( un >= 100){
max = Math.ceil(max / 500) * 500;
min = Math.floor(min / 500) * 500;
un = (max - min) / 10;
// un = Math.round(un / 50 ) * 50;
// Units of 10, 15, 20 etc
}else
if ( un >= 10){
max = Math.ceil(max / 50) * 50;
min = Math.floor(min / 50) * 50;
un = (max - min) / 10;
// un = Math.round(un / 10) * 10;
// Units of 1 2 5
}else
if ( un >= 1){
max = Math.ceil( max / 10) * 10;
min = Math.floor( min / 10) * 10;
un = (max - min) / 10;
// un = (Math.round( un / 1) ) * 1;
// Units of 0.1 0.2 0.5
}else
if ( un >= 0){
max = Math.ceil( max * 1) / 1;
min = Math.floor( min * 1) / 1;
un = (max - min) / 10;
// un = (Math.round( un * 10) ) / 10;
}
if (un == 0){ un = 0.1 ; max = 10; min = 0}
if (debug){console.log(min,max,un)};
return [min,max,un]
}
function drawLine(){
// Electric
if (dataL_Total.length != 0){
if (zoomStatus){
_s = gVar.zoom.s;
_e = gVar.zoom.e;
if (sliderPercentage == 1){
_e = dataX.length;
_s = _e - sliderPoints;
if (_s < 0){ _s = 0}
//gVar.zoom = {s:_s,e:_e, zoomStatus: zoomStatus};
}
zoomStatus = gVar.zoom.zoomStatus;
}else{
_s = 0
_e = dataX.length
}
gVar.zoom = {s:_s,e:_e, zoomStatus: zoomStatus};
_length = _e - _s ;
let A = [ dataL_Total.slice(_s,_e), datal1.slice(_s,_e), datal2.slice(_s,_e), datal3.slice(_s,_e)]; // Combine all arrays
MMU[0] = getMinMaxUn(A);
min = MMU[0][0];
max = MMU[0][1];
un = MMU[0][2];
// ys = (w-40)/dataX.length;
// xs = (w-80)/dataX.length;
// if(zoomStatus){
xs = (w-80)/_length;
// }
// if(!zoomStatus){
// _s = 0 ;
// _e = dataX.length;
// }else{
// _s = gVar.zoom.s
// _e = gVar.zoom.e
// };
// let A = [ dataL_Total, datal1, datal2, datal3];
// MMU[0] = getMinMaxUn(A);
dataT = [];
chart.setCtx(Ctx);
chart.chartLine()
chart.digram()
chart.data();
chart.draw();
chart.pointes();
}
// Gas
if (dataGasPoint.length != 0){
let datag = [];
for (datagas of dataGasPoint){
datag.push(datagas[5]);
}
// if(!zoomStatus){
_s = 0 ;
_e = dataX.length;
// }else{
// _s = gVar.zoom.s
// _e = gVar.zoom.e
// };
//gVar.zoomG = {s:_s,e:_e, zoomStatus: zoomStatus};
MMU[1] = getMinMaxUn(datag);
// min = MMU[1][0];
MMU[1][0] = min = 0;
max = MMU[1][1];
un = MMU[1][2]; // 1/10 of the axis
xs = (w-80)/dataXG.length; // Every points distance on x-axis
chart.setCtx(CtxG);
chart.chartLine()
chart.digram()
chart.dataGas();
chart.drawG();
chart.pointesGas();
}
return true;
}
async function tryIP(_ip){
let url = 'http://' + _ip + '/api'; // '/api/v1/data';
// http://192.168.2.4/api {"product_type":"HWE-P1","product_name":"P1 meter",
const promise1 = new Promise( (resolve,reject) =>{
if(debug){ console.log(url) };
fetch(url)
.then( async function(response){
if (response.ok){
let _json = await response.json();
// _json.then
if (_json.product_type == "HWE-P1"){
ip.P1 = _ip;
if(debug){console.log(ip.P1)};
if(debug){console.log('Found HWE-P1', url, ip.P1)};
document.getElementById('IP_P1').value = ip.P1;
resolve
}
if (_json.product_type == "HWE-WTR"){
ip.WTR = _ip;
if(debug){console.log(ip.WTR)};
if(debug){console.log('Found HWE-WTR', url, ip.WTR)};
document.getElementById('IP_WTR').value = ip.WTR;
resolve
}
reject
}else{
// alert("HTTP-Error: " + response.status);
reject
}
// console.log(response);
return response.json()
})
.catch((err) => {
// console.log(err);
})
})
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100));
try {
await Promise.race([promise1, promise2]);
} catch (e) {
// time out or func failed
if(debug){console.log(url, 'failed')};
}
// ERR_CONNECTION_TIMED_OUT
}
function scanIP(){
// Currently only a scan on last .* part of ip4
ip.P1 = document.getElementById('IP_P1').value;
ip.WTR = document.getElementById('IP_WTR').value;
if (ip.P1 != '' && ip.P1.split('.')[3].includes('*')){
// Scan network on last
// console.log(ip)
let _ip = ip.P1;
// .1 up to .255 scan the ip range
for (var ii = 1; ii < 256; ii++) {
let _ipArray = _ip.split('.')
_ipArray[3] = ii;
_ip = _ipArray[0] + '.' + _ipArray[1] + '.' + _ipArray[2] + '.' + _ipArray[3];
if(debug){console.log(_ip)};
tryIP(_ip) ;
}
}
}
function setHoverText(){
// Turn off hover text
let _hover = document.getElementsByClassName('tooltip')[0];
if(_hover){
if(!button_stop){
_hover.style.display = 'none';
}else{
_hover.style.display = 'inline-block';
}
}
}
function fill_data_random(){
let _i = 600 ; // amount of data points to make
let _interval = 1 ; // Every int seconds timemark
set_s_e(sliderPoints,sliderPercentage);
document.getElementById("rangeValue").innerHTML = _i;
gVar.zoom = {s:1,e:_i, zoomStatus};
dataX = [];
for ( let iii = 1; iii < _i ; iii++ ){
dataX.push(iii); // Math.floor(i/_interval));
// dataXG.push(Math.floor(i/(_interval*5)));
let ran1 = Math.random();
let ran2 = Math.random();
let ran3 = Math.random();
let _l1 = Math.floor(ran1 * 400);
let _l2 = Math.floor(ran2 * 400);
let _l3 = Math.floor(ran3 * 400);
let _tot = _l1 + _l2 + _l3;
datal1.push(_l1);
datal2.push(_l2);
datal3.push(_l3);
dataL_Total.push(_tot);
}
// dataX = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]; // x-axis :)
dataXG = [1,2,3,4,5,6,7,8,9,10]; // x-axis :)
// dataL_Total = [1000,10010,1001,980,1023,899,455,1200,300,10,232,432,244,666]; // y-axis values Totals
// datal1 = [151,242,353,264,115,36,645,374,493,2,55,3,77,55];
// datal2 = [12,14,15,33,22,44,77,22,33,34,33,55,22,33,44];
// datal3 = [34,866,333,700,600,345,333,1111,233,1,2,3,4,5];
dataGas = [
['21:10:06/22-11-22', 3037.961, 76206],
['21:10:06/22-11-22', 3037.961, 76206],
['21:10:06/22-11-22', 3037.961, 76206],
['21:15:05/22-11-22', 3038.991, 76505],
['21:15:05/22-11-22', 3038.991, 76505],
['21:15:05/22-11-22', 3038.991, 76505],
['21:20:05/22-11-22', 3039.991, 76505],
['21:20:05/22-11-22', 3039.991, 76505]
];
dataGasPoint = [
["18:10:00/22-11-22",2000.101, 75005, 2, 5, 0.4],
["18:10:00/22-11-22",2000.108, 75305, 125, 5, 25],
["18:10:00/22-11-22",2000.150, 75605, 100, 5, 20],
["18:10:00/22-11-22",2000.200, 75905, 0.0, 5, 0],
["18:10:00/22-11-22",2000.202, 76205, 28, 5, 15],
["18:10:00/22-11-22",2000.205, 76505, 255, 5, 50],
["18:10:00/22-11-22",2001.206, 76805, 25, 5, 5],
["18:10:00/22-11-22",2001.209, 77105, 133, 5, 25]
];
}
function run(){
button_stop = false;
setHoverText();
ip.P1 = document.getElementById('IP_P1').value;
ip.WTR = document.getElementById('IP_WTR').value;
if (ip.WTR != '' && !ip.WTR.includes('*')){
wrapperW() ;
}
if (ip.P1 != '' && !ip.P1.includes('*')){
document.getElementById('test').innerHTML = '';
Init();
if(e_start){
e_start.style.opacity = 0 }; // hide Start
if(e_stop){
e_stop.style.opacity = 1 }; // Show Stop
if(e_scan && e_scan.style){
e_scan.style.opacity = 0 }; // hide Scan
dataX = [];
dataXG = [];
dataL_Total = [];
datal1 = [];
datal2 = [];
datal3 = [];
dataT = []; // All entries of all graphs
dataGas = [];
dataGasPoint = [];
iteration = 1;
wrapper() ;
}else{
document.getElementById('test').innerHTML = 'TESTDATA';
// fake numbers insert
fill_data_random();
iteration = 1;
Ctx.clearRect(0, 0, w, h);
CtxG.clearRect(0,0,wg,hg);
drawLine();
button_stop = true;
writeNumbers_E( {
'active_power_w': dataL_Total[dataL_Total.length-1],
'active_power_l1_w': datal1[dataL_Total.length-1],
'active_power_l2_w': datal2[dataL_Total.length-1],
'active_power_l3_w': datal3[dataL_Total.length-1],
'total_gas_m3': dataGasPoint[dataGasPoint.length-1],
'total_power_export_t1_kwh' : 1234,
'total_power_export_t2_kwh' : 0123,
'total_power_import_t1_kwh' : 987,
'total_power_import_t2_kwh' : 321
} )
}
}
function stop(){
button_stop = true;
setHoverText();
if(e_start){
e_start.style.opacity = 1 }; // Show Start
if(e_stop){
e_stop.style.opacity = 0 }; // Hide Stop
if(e_scan && e_scan.style){
e_scan.style.opacity = 1 }; // Show Scan
}
async function meter(i){
iteration = i;
let url = 'http://' + ip.P1 + '/api/v1/data';
let response = await fetch(url);
if (response.ok) { // if HTTP-status is 200-299
// get the response body (the method explained below)
let json = await response.json();
if (isNaN(json.active_power_w)) { json.active_power_w = 0};
if (isNaN(json.active_power_l1_w)){ json.active_power_l1_w = 0};
if (isNaN(json.active_power_l2_w)){ json.active_power_l2_w = 0};
if (isNaN(json.active_power_l3_w)){ json.active_power_l3_w = 0};
dataL_Total.push(json.active_power_w);
datal1.push(json.active_power_l1_w);
datal2.push(json.active_power_l2_w);
datal3.push(json.active_power_l3_w);
dataX.push(i);
// Write to Screen
// Details
// IP_P1
document.getElementById('IP_P1_001').innerHTML = ip.P1; // wifi_ssid : WifiSSID
document.getElementById('WifiSSID').innerHTML = json.wifi_ssid; // wifi_ssid : WifiSSID
document.getElementById('WifiStrength').innerHTML = json.wifi_strength; // wifi_strength :
document.getElementById('MM').innerHTML = json.meter_model; //meter_model
document.getElementById('SV').innerHTML = json.smr_version; //smr_version:
// Electric
writeNumbers_E(json);
if(!zoomStatus){
// Ctx.clearRect(0, 0, w, h);
// CtxG.clearRect(0,0,wg,hg);
// drawLine();
}else{
if(debug){console.log('pause')}
// if(zoomStatus){
// _e = Math.floor( dataX.length * sliderPercentage ) ;
// if(_e < 10){ _e = 10};
// // document.getElementById("rangeValue").innerHTML = _e;
// _s = _e - sliderPoints; //10
// if (_s< 0){
// _s = 0;
// }
// Ctx.clearRect(0, 0, w, h);
// CtxG.clearRect(0,0,wg,hg);
// gVar.zoom = {s:_s,e:_e, zoomStatus: zoomStatus};
// drawLine();
// }
}
Ctx.clearRect(0, 0, w, h);
CtxG.clearRect(0,0,wg,hg);
drawLine();
if( sliderPercentage == 1 ){
document.getElementById("rangeValue").innerHTML = dataX.length }
// Gas details
if(json.gas_timestamp){
// YYMMDDhhmmss
let _stamp = json.gas_timestamp.toString();
let hh = _stamp.substring(6,8) ;
let mm = _stamp.substring(8,10) ;
let ss = _stamp.substring(10,12);
let DD = _stamp.substring(4,6) ;
let MM = _stamp.substring(2,4) ;
let YY = _stamp.substring(0,2) ;
let _time = hh + ':' + mm + ':' + ss;
let _date = DD + '-' + MM + '-' + YY;
// hh:mm:ss/YY-MM-DD
_time = _time + '/' + _date;
// Timestamp in seconds as number
// let _timestamp = Number(ss) + Number(60*mm) + Number(3600*hh);
let
// _timestamp = (new Date(Number('20'+ YY),MM,DD,hh,mm,ss,00)).getTime();
_timestamp = (new Date('20'+ YY + '-' + MM + '-' + DD + 'T' + hh + ':' + mm + ':' + ss)).getTime();
if(debug){console.log( new Date(_timestamp) , _timestamp)};
let _deltaGas = 0;
let _min = 0;
let _usage = 0;
// When have a first entrie...we want unique entris afterwards
if (dataGasPoint.length != 0){
// Is this entry unique now?
if (dataGasPoint[dataGasPoint.length-1][2] != _timestamp || debug == true){
// console.log(_string);
if(debug){ console.log(hh,mm,ss,_time,_timestamp)};
let _startGas = dataGasPoint[0][1];
let _deltaSum = ( ( Math.floor( json.total_gas_m3 *1000) - Math.floor(_startGas * 1000) ) * 1000 ) / 1000;
// String to screen
// _string = _string + _time + ' : ' + json.total_gas_m3;
// _string = 'Time / Date Gas Delta l/min FLow l/min. <br>';
_string = _string + '<tr><th>' + _time + '</th>';
// Save this unique timestamped Gas result to internal table
// dataGasPoint.push([_time, json.total_gas_m3, _timestamp])
// When we have 2 or more entries saved...we can make some calculations
if ( dataGasPoint.length > 0){
_deltaGas = Math.floor( ( json.total_gas_m3*1000 - dataGasPoint[dataGasPoint.length-1][1]*1000 ) *1000 ) / 1000;