forked from dingo35/SmartEVSE-3.5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
1192 lines (1073 loc) · 49.4 KB
/
app.js
File metadata and controls
1192 lines (1073 loc) · 49.4 KB
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
'use strict';
/* ========== DOM helpers ========== */
const $id = (id) => document.getElementById(id);
const $qs = (sel) => document.querySelector(sel);
const $qa = (sel) => document.querySelectorAll(sel);
function showEl(el) { if (el) el.style.display = ''; }
function hideEl(el) { if (el) el.style.display = 'none'; }
function showById(id) { showEl($id(id)); }
function hideById(id) { hideEl($id(id)); }
function showAll(sel) { $qa(sel).forEach(showEl); }
function hideAll(sel) { $qa(sel).forEach(hideEl); }
/* ========== State ========== */
const endpoint = document.location + 'settings';
let initiated = false;
let mqttEditMode = false;
let ocppEditMode = false;
let last_evse_state_id = 0;
/* ========== WebSocket Data Channel ========== */
var dataWs = null;
var dataWsReconnectTimer = null;
var dataWsReconnectAttempts = 0;
var wsConnected = false;
/* Cache of last known WS values for computing totals */
var wsCache = {
phase_L1: 0, phase_L2: 0, phase_L3: 0,
evmeter_L1: 0, evmeter_L2: 0, evmeter_L3: 0,
evmeter_power: 0, battery_current: 0
};
function updateConnStatus(connected) {
var el = $id('conn_status');
if (!el) return;
el.style.backgroundColor = connected ? '#1cc88a' : '#e74a3b';
el.title = connected ? 'Live (WebSocket)' : 'Polling (HTTP)';
}
/* Apply flat WS data fields to the DOM */
function applyWsData(d) {
if (d.state_id !== undefined || d.error_flags !== undefined)
updateStateDot(d.state_id, d.error_flags);
if (d.mode_id !== undefined) {
var modeNames = {0:'OFF', 1:'NORMAL', 2:'SOLAR', 3:'SMART', 4:'PAUSE'};
$qs('#mode').textContent = modeNames[d.mode_id] || 'N/A';
for (var x of [0, 1, 2, 3, 4]) {
$qs('#mode_' + x).classList.toggle('active', x === d.mode_id);
}
syncMobileNav(d.mode_id);
if (d.mode_id == 2) {
showAll('.with_solar');
hideById('override_current_box');
hideById('override_current_box2');
} else {
hideAll('.with_solar');
showById('override_current_box');
showById('override_current_box2');
}
}
if (d.charge_current !== undefined)
$id('charge_current').textContent = (d.charge_current / 10).toFixed(1) + " A";
if (d.temp !== undefined) {
var maxT = d.temp_max !== undefined ? d.temp_max : '';
if (maxT !== '') $id('temp').textContent = d.temp + " \u00B0C / " + maxT + " \u00B0C";
}
if (d.pwm !== undefined)
$id('dutycycle').textContent = (d.pwm * 100 / 1024).toFixed(0) + " %";
if (d.car_connected !== undefined)
$id('car_connected').textContent = d.car_connected ? "Yes" : "No";
if (d.override_current !== undefined)
$id('override_current').textContent = (d.override_current / 10).toFixed(1) + " A";
if (d.current_min !== undefined)
$id('current_min').textContent = (d.current_min / 10).toFixed(1) + " A";
if (d.current_max !== undefined)
$id('current_max').textContent = (d.current_max / 10).toFixed(1) + " A";
/* Phase currents - update cache and recompute totals */
var phaseChanged = false;
if (d.phase_L1 !== undefined) { wsCache.phase_L1 = d.phase_L1; phaseChanged = true; }
if (d.phase_L2 !== undefined) { wsCache.phase_L2 = d.phase_L2; phaseChanged = true; }
if (d.phase_L3 !== undefined) { wsCache.phase_L3 = d.phase_L3; phaseChanged = true; }
if (phaseChanged) {
$id('phase_1').textContent = (wsCache.phase_L1 / 10).toFixed(1) + " A";
$id('phase_2').textContent = (wsCache.phase_L2 / 10).toFixed(1) + " A";
$id('phase_3').textContent = (wsCache.phase_L3 / 10).toFixed(1) + " A";
$id('phase_total').textContent = ((wsCache.phase_L1 + wsCache.phase_L2 + wsCache.phase_L3) / 10).toFixed(1) + " A";
updatePhaseBars(wsCache.phase_L1, wsCache.phase_L2, wsCache.phase_L3);
}
var evChanged = false;
if (d.evmeter_L1 !== undefined) { wsCache.evmeter_L1 = d.evmeter_L1; evChanged = true; }
if (d.evmeter_L2 !== undefined) { wsCache.evmeter_L2 = d.evmeter_L2; evChanged = true; }
if (d.evmeter_L3 !== undefined) { wsCache.evmeter_L3 = d.evmeter_L3; evChanged = true; }
if (evChanged) {
$id('evmeter_currents_1').textContent = (wsCache.evmeter_L1 / 10).toFixed(1) + " A";
$id('evmeter_currents_2').textContent = (wsCache.evmeter_L2 / 10).toFixed(1) + " A";
$id('evmeter_currents_3').textContent = (wsCache.evmeter_L3 / 10).toFixed(1) + " A";
$id('evmeter_currents_total').textContent = ((wsCache.evmeter_L1 + wsCache.evmeter_L2 + wsCache.evmeter_L3) / 10).toFixed(1) + " A";
}
if (d.evmeter_power !== undefined) {
$id('evmeter_power').textContent = (d.evmeter_power / 1000).toFixed(1) + " kW";
wsCache.evmeter_power = d.evmeter_power;
}
if (d.evmeter_charged_wh !== undefined)
$id('evmeter_charged_kwh').textContent = (d.evmeter_charged_wh / 1000).toFixed(1) + " kWh";
/* Update power flow when any relevant value changes */
if (phaseChanged || d.evmeter_power !== undefined || d.battery_current !== undefined) {
var mt = wsCache.phase_L1 + wsCache.phase_L2 + wsCache.phase_L3;
updatePowerFlow(mt, wsCache.evmeter_power || 0, d.battery_current !== undefined ? d.battery_current : (wsCache.battery_current || 0));
}
if (d.battery_current !== undefined) {
wsCache.battery_current = d.battery_current;
$id('battery_current').textContent = (d.battery_current / 10).toFixed(1) + " A";
if (d.battery_current == 0) $id('battery_status').textContent = "Idle";
else $id('battery_status').textContent = d.battery_current < 0 ? "Discharging" : "Charging";
}
if (d.solar_stop_timer !== undefined && d.solar_stop_timer > 0) {
var stateEl = $id('state');
if (stateEl && stateEl.textContent.indexOf('Stopping') === -1)
stateEl.textContent += " (Stopping in " + d.solar_stop_timer + "s)";
}
if (d.loadbl !== undefined) {
if (d.loadbl > 1) {
showById('loadbl'); showById('loadbl_text');
$id('loadbl_node').textContent = "Slave Node " + (d.loadbl - 1);
hideById('contactor2');
} else if (d.loadbl == 1) {
showById('loadbl'); showById('loadbl_text');
$id('loadbl_node').textContent = "Master";
hideById('contactor2');
} else {
hideById('loadbl'); hideById('loadbl_text');
showById('contactor2');
}
}
}
function connectDataWs() {
if (dataWs && (dataWs.readyState === WebSocket.OPEN || dataWs.readyState === WebSocket.CONNECTING)) return;
var wsUrl = (window.location.protocol === 'https:' ? 'wss:' : 'ws:') + '//' + window.location.host + '/ws/data';
var socket = new WebSocket(wsUrl);
dataWs = socket;
socket.onopen = function() {
dataWsReconnectAttempts = 0;
wsConnected = true;
updateConnStatus(true);
socket.send(JSON.stringify({subscribe: ['state']}));
};
socket.onmessage = function(event) {
try {
var msg = JSON.parse(event.data);
if (msg.d) applyWsData(msg.d);
} catch(e) { /* ignore parse errors */ }
};
socket.onerror = function() {
if (socket.readyState !== WebSocket.CLOSED) socket.close();
};
socket.onclose = function() {
if (dataWs === socket) dataWs = null;
wsConnected = false;
updateConnStatus(false);
var delay = Math.min(1000 * Math.pow(2, dataWsReconnectAttempts), 10000) + Math.floor(Math.random() * 500);
dataWsReconnectAttempts++;
dataWsReconnectTimer = setTimeout(connectDataWs, delay);
/* Resume polling while WS is disconnected */
loadData();
};
}
/* Visibility-aware pause/resume for data WS (registered once) */
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
if (dataWs) dataWs.close();
} else if (!dataWs || dataWs.readyState !== WebSocket.OPEN) {
clearTimeout(dataWsReconnectTimer);
dataWsReconnectAttempts = 0;
connectDataWs();
}
});
/* ========== Theme (dark mode) ========== */
function getTheme() {
var stored = localStorage.getItem('evse_theme');
if (stored) return stored;
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
function applyTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
var btn = $id('theme_toggle');
if (btn) btn.textContent = theme === 'dark' ? '\u2600' : '\u263E'; /* sun or moon */
}
function toggleTheme() {
var current = document.documentElement.getAttribute('data-theme') || getTheme();
var next = current === 'dark' ? 'light' : 'dark';
localStorage.setItem('evse_theme', next);
applyTheme(next);
}
/* Apply saved theme immediately */
applyTheme(getTheme());
/* ========== UI helpers ========== */
var maxMainsAmps = 25; /* default, updated on loadData */
function updateStateDot(stateId, errorFlags) {
var dot = $id('state_dot');
if (!dot) return;
if (errorFlags && errorFlags > 0) { dot.style.backgroundColor = '#e74a3b'; return; }
/* stateId: 0=A(idle), 1=B(connected), 2=C(charging), 8=Activate, etc. */
if (stateId === 2 || stateId === 6 || stateId === 7) dot.style.backgroundColor = '#1cc88a'; /* charging - green */
else if (stateId === 1 || stateId === 5 || stateId === 9) dot.style.backgroundColor = '#36b9cc'; /* connected - blue */
else dot.style.backgroundColor = '#858796'; /* idle/off - gray */
}
function updatePhaseBars(l1, l2, l3, prefix) {
prefix = prefix || 'mains_bar';
var max = maxMainsAmps * 10; /* values are in 0.1A */
var vals = [l1, l2, l3];
var labels = ['L1', 'L2', 'L3'];
for (var i = 0; i < 3; i++) {
var pct = max > 0 ? Math.min(100, Math.abs(vals[i]) / max * 100) : 0;
var barEl = $id(prefix + '_' + labels[i]);
var valEl = $id(prefix + '_val_' + labels[i]);
if (barEl) barEl.style.width = pct.toFixed(1) + '%';
if (valEl) valEl.textContent = (vals[i] / 10).toFixed(1) + 'A';
}
}
function updateNodeOverview(nodes, maxCurrent) {
var container = $id('node_list');
if (!container || !nodes) return;
var html = '';
var totalCurrent = 0;
var activeCount = 0;
for (var i = 0; i < nodes.length; i++) {
var n = nodes[i];
if (n.state === 'Idle' && n.current === 0 && i > 0) continue; /* skip unused nodes */
activeCount++;
totalCurrent += n.current;
var pct = maxCurrent > 0 ? Math.min(100, n.current / maxCurrent * 100) : 0;
var color = n.state === 'Charging' ? '#1cc88a' : n.state === 'Request' ? '#f6c23e' : '#858796';
var label = i === 0 ? 'Master' : 'Node ' + i;
var badge = n.sched ? '<span style="font-size:.7rem;padding:1px 4px;border-radius:3px;background:' +
(n.sched === 'Active' ? '#1cc88a' : n.sched === 'Paused' ? '#f6c23e' : '#858796') +
';color:#fff;margin-left:4px;">' + n.sched + '</span>' : '';
html += '<div class="phase-bar-row" style="margin-bottom:4px;">' +
'<span class="phase-bar-label" style="width:60px;">' + label + '</span>' +
'<div class="phase-bar-track"><div style="height:100%;border-radius:5px;width:' +
pct.toFixed(1) + '%;background:' + color + ';transition:width .4s;min-width:2px;"></div></div>' +
'<span class="phase-bar-value">' + (n.current / 10).toFixed(1) + 'A' + badge + '</span></div>';
}
container.innerHTML = html;
/* Total bar */
var totalPct = maxCurrent > 0 ? Math.min(100, totalCurrent / (maxCurrent * activeCount) * 100) : 0;
var totalBar = $id('lb_total_bar');
var totalVal = $id('lb_total_val');
if (totalBar) totalBar.style.width = totalPct.toFixed(1) + '%';
if (totalVal) totalVal.textContent = (totalCurrent / 10).toFixed(1) + 'A';
}
function syncMobileNav(modeId) {
for (var x of [0, 1, 2, 3, 4]) {
var btn = $id('mnav_' + x);
if (btn) btn.classList.toggle('active', x === modeId);
}
}
/* ========== Power flow diagram ========== */
function updatePowerFlow(mainsTotal, evPower, batCurrent) {
/* mainsTotal in 0.1A, evPower in W, batCurrent in 0.1A */
var lineGH = $id('pf_line_gh');
var lineHE = $id('pf_line_he');
var lineBH = $id('pf_line_bh');
var gridVal = $id('pf_grid_val');
var evseVal = $id('pf_evse_val');
var batGroup = $id('pf_battery');
var batVal = $id('pf_bat_val');
if (!lineGH) return;
/* Grid -> Home flow */
var gridW = Math.abs(mainsTotal) * 23; /* rough W estimate at 230V */
if (gridVal) gridVal.textContent = (gridW / 1000).toFixed(1) + ' kW';
var sw = Math.max(2, Math.min(6, Math.abs(mainsTotal) / 100));
lineGH.style.strokeWidth = sw;
if (mainsTotal > 5) {
lineGH.setAttribute('class', 'pf-line flow-fwd'); /* importing */
} else if (mainsTotal < -5) {
lineGH.setAttribute('class', 'pf-line flow-rev'); /* exporting */
} else {
lineGH.setAttribute('class', 'pf-line flow-none');
}
/* Home -> EVSE flow */
if (evseVal) evseVal.textContent = (Math.abs(evPower) / 1000).toFixed(1) + ' kW';
var sw2 = Math.max(2, Math.min(6, Math.abs(evPower) / 2000));
lineHE.style.strokeWidth = sw2;
if (evPower > 50) {
lineHE.setAttribute('class', 'pf-line flow-fwd');
} else {
lineHE.setAttribute('class', 'pf-line flow-none');
}
/* Battery (optional) */
if (batCurrent !== undefined && batCurrent !== 0) {
if (batGroup) batGroup.style.display = '';
if (lineBH) lineBH.style.display = '';
if (batVal) batVal.textContent = (Math.abs(batCurrent) / 10).toFixed(1) + ' A';
if (lineBH) {
if (batCurrent > 0) {
lineBH.setAttribute('class', 'pf-line flow-fwd'); /* charging battery */
} else {
lineBH.setAttribute('class', 'pf-line flow-rev'); /* discharging */
}
}
}
}
/* ========== Cert visibility ========== */
function toggleCertVisibility() {
$id('mqtt_ca_cert_wrapper').style.display =
$id('mqtt_tls').checked ? '' : 'none';
}
/* ========== Data loading ========== */
function loadData() {
fetch(endpoint)
.then(function(r) { return r.json(); })
.then(function(data) {
if (!initiated) {
initiated = true;
var versionEl = $id('version');
versionEl.textContent = data.version;
versionEl.dataset.version = data.version;
sessionStorage.setItem("version", JSON.stringify(data.version));
$id('serialnr').textContent += data.serialnr;
sessionStorage.setItem("serialnr", JSON.stringify(data.serialnr));
var minCurrent = parseInt(data.settings.current_min.toFixed(1));
var maxCurrent = parseInt(data.settings.current_max.toFixed(1));
if (data.evse.loadbl < 2) {
var select = $id('mode_override_current');
select.add(new Option('no override', 0));
for (var x = minCurrent; x <= maxCurrent; x++) {
select.add(new Option(x + 'A', x));
}
}
$id('required_evccid').value = data.settings.required_evccid || "";
}
/* Mode display */
$qs('#mode').textContent = data.mode;
for (var x of [0, 1, 2, 3, 4]) {
$qs('#mode_' + x).classList.toggle('active', x === data.mode_id);
}
syncMobileNav(data.mode_id);
$id('dutycycle').textContent = (data.evse.pwm * 100 / 1024).toFixed(0) + " %";
if (data.mode_id == 2) { /* SOLAR MODE */
showAll('.with_solar');
hideById('override_current_box');
hideById('override_current_box2');
} else {
hideAll('.with_solar');
showById('override_current_box');
showById('override_current_box2');
}
if (data.ev_state) {
var full_soc = data.ev_state.full_soc;
var initial_soc = data.ev_state.initial_soc;
var computed_soc = data.ev_state.computed_soc;
var time_until_full = data.ev_state.time_until_full;
var energy_capacity = data.ev_state.energy_capacity;
var evccid = data.ev_state.evccid;
$id('computed_soc').innerHTML = computed_soc >= 0 ? computed_soc + " %" : "N/A";
$id('full_soc').innerHTML = full_soc >= 0 ? full_soc + " %" : "N/A";
$id('initial_soc').innerHTML = initial_soc >= 0 ? initial_soc + " %" : "N/A";
$id('energy_capacity').innerHTML = energy_capacity >= 0 ? (energy_capacity / 1000).toFixed(1) + " kWh" : "N/A";
$id('evccid').innerHTML = evccid || "N/A";
var fullAtEl = $id('full_at');
fullAtEl.textContent = time_until_full > 0
? new Date(+Date.now() + (time_until_full * 1000)).toLocaleString(undefined, { timeStyle: 'short', dateStyle: 'short' })
: 'N/A';
fullAtEl.title = time_until_full > 0 ? Math.round(time_until_full / 60) + ' min to go' : 'N/A';
}
if (data.mqtt) {
var mqttEl = $id('mqtt');
mqttEl.textContent = data.mqtt.status || 'N/A';
showEl(mqttEl);
showById('mqtt_config');
} else {
var mqttEl2 = $id('mqtt');
mqttEl2.textContent = '';
hideEl(mqttEl2);
hideAll('.config');
hideById('mqtt_config');
}
if (data.evse.loadbl > 1) {
showById('loadbl');
showById('loadbl_text');
$id('loadbl_node').textContent = "Slave Node " + (data.evse.loadbl - 1);
hideById('contactor2');
showById('mode_2');
showById('mode_3');
hideAll('.with_solar');
hideById('override_current_box');
hideById('override_current_box2');
$qa('#form_pwm input, #form_pwm button, #form_pwm select').forEach(function(el) { el.disabled = true; });
} else if (data.evse.loadbl == 1) {
showById('loadbl');
showById('loadbl_text');
$id('loadbl_node').textContent = "Master";
hideById('contactor2');
showById('mode_2');
showById('mode_3');
hideAll('.with_solar');
$qa('#form_pwm input, #form_pwm button, #form_pwm select').forEach(function(el) { el.disabled = false; });
} else {
hideById('loadbl');
hideById('loadbl_text');
showById('contactor2');
showById('mode_2');
showById('mode_3');
$qa('#form_pwm input, #form_pwm button, #form_pwm select').forEach(function(el) { el.disabled = false; });
}
if (data.evse.loadbl == 1) {
showAll('.with_scheduling');
$id('prio_strategy').value = data.settings.prio_strategy;
$id('rotation_interval').value = data.settings.rotation_interval;
$id('idle_timeout').value = data.settings.idle_timeout;
if (data.schedule) {
var states = data.schedule.state.join(', ');
$id('schedule_state').textContent = states;
$id('rotation_timer').textContent = data.schedule.rotation_timer + 's';
}
if (data.nodes) {
updateNodeOverview(data.nodes, data.settings.current_max);
}
} else {
hideAll('.with_scheduling');
}
$id('car_connected').textContent = data.car_connected ? "Yes" : "No";
$id('state').textContent = data.evse.state;
last_evse_state_id = data.evse.last_state_id;
updateStateDot(data.evse.state_id, data.evse.error_id);
$id('temp').textContent = data.evse.temp + " \u00B0C / " + data.evse.temp_max + " \u00B0C";
if (data.evse.error != "None") {
$id('error').textContent = data.evse.error;
showById('with_errors');
} else {
hideById('with_errors');
}
if (data.evse.rfid != "Not Installed") {
$id('rfid').textContent = data.evse.rfid;
} else {
hideById('show_rfid');
}
if (data.evse.solar_stop_timer > 0) {
$id('state').textContent += " (Stopping in " + data.evse.solar_stop_timer + "s)";
}
$id('current_min').textContent = data.settings.current_min.toFixed(1) + " A";
$id('current_max').textContent = data.settings.current_max.toFixed(1) + " A";
$id('override_current').textContent = (data.settings.override_current / 10).toFixed(1) + " A";
$id('enable_C2').textContent = data.settings.enable_C2;
if (data.settings.starttime) {
$id('starttime_date_time').textContent = new Date(data.settings.starttime * 1000).toLocaleDateString() + " " + new Date(data.settings.starttime * 1000).toLocaleTimeString();
} else {
$id('starttime_date_time').textContent = "none";
}
if (data.settings.stoptime) {
$id('stoptime_date_time').textContent = new Date(data.settings.stoptime * 1000).toLocaleDateString() + " " + new Date(data.settings.stoptime * 1000).toLocaleTimeString();
} else {
$id('stoptime_date_time').textContent = "none";
}
$id('repeat').textContent = data.settings.repeat == 1 ? "Daily" : "none";
$id('battery_current').textContent = (data.home_battery.current / 10).toFixed(1) + " A";
$id('phase_total').textContent = (data.phase_currents.TOTAL / 10).toFixed(1) + " A";
$id('phase_1').textContent = (data.phase_currents.L1 / 10).toFixed(1) + " A";
$id('phase_2').textContent = (data.phase_currents.L2 / 10).toFixed(1) + " A";
$id('phase_3').textContent = (data.phase_currents.L3 / 10).toFixed(1) + " A";
maxMainsAmps = data.settings.current_main || 25;
updatePhaseBars(data.phase_currents.L1, data.phase_currents.L2, data.phase_currents.L3);
updatePowerFlow(data.phase_currents.TOTAL, data.ev_meter.import_active_power, data.home_battery.current);
$id('evmeter_currents_total').textContent = (data.ev_meter.currents.TOTAL / 10).toFixed(1) + " A";
$id('evmeter_currents_1').textContent = (data.ev_meter.currents.L1 / 10).toFixed(1) + " A";
$id('evmeter_currents_2').textContent = (data.ev_meter.currents.L2 / 10).toFixed(1) + " A";
$id('evmeter_currents_3').textContent = (data.ev_meter.currents.L3 / 10).toFixed(1) + " A";
$id('charge_current').textContent = (data.settings.charge_current / 10).toFixed(1) + " A";
$id('phase_original_total').textContent = (data.phase_currents.original_data.TOTAL / 10).toFixed(1) + " A";
$id('phase_original_1').textContent = (data.phase_currents.original_data.L1 / 10).toFixed(1) + " A";
$id('phase_original_2').textContent = (data.phase_currents.original_data.L2 / 10).toFixed(1) + " A";
$id('phase_original_3').textContent = (data.phase_currents.original_data.L3 / 10).toFixed(1) + " A";
if (data.phase_currents.last_data_update > 0) {
$id('p1_data_time').textContent = new Date(data.phase_currents.last_data_update * 1000).toLocaleTimeString();
$id('p1_data_date').textContent = new Date(data.phase_currents.last_data_update * 1000).toLocaleDateString();
showById('with_p1_api_data_date');
showById('with_p1_api_data_time');
} else {
hideById('with_p1_api_data_date');
hideById('with_p1_api_data_time');
}
if (data.home_battery.last_update > 0) {
$id('battery_last_update_time').textContent = new Date(data.home_battery.last_update * 1000).toLocaleTimeString();
$id('battery_last_update_date').textContent = new Date(data.home_battery.last_update * 1000).toLocaleDateString();
showById('with_homebattery');
} else {
hideById('with_homebattery');
}
if (data.home_battery.current == 0) {
$id('battery_status').textContent = "Idle";
} else {
$id('battery_status').textContent = data.home_battery.current < 0 ? "Discharging" : "Charging";
}
if (data.settings.mains_meter === "Disabled") {
hideAll('.with_mainsmeter');
} else {
showAll('.with_mainsmeter');
}
if (data.ev_meter.description == "Disabled") {
$qa('[id=with_evmeter]').forEach(hideEl);
} else {
$qa('[id=with_evmeter]').forEach(showEl);
$id('evmeter_description').textContent = data.ev_meter.description;
$id('evmeter_power').textContent = (data.ev_meter.import_active_power / 1000).toFixed(1) + " kW";
$id('evmeter_total_kwh').textContent = (data.ev_meter.total_wh / 1000).toFixed(1) + " kWh";
$id('evmeter_charged_kwh').textContent = (data.ev_meter.charged_wh / 1000).toFixed(1) + " kWh";
}
$id('solar_start_current').value = data.settings.solar_start_current;
$id('solar_max_import_current').value = data.settings.solar_max_import;
$id('solar_stop_time').value = data.settings.solar_stop_time;
if (data.settings.modem == "Experiment" || data.settings.modem == "QCA7000") {
showAll('.with_modem');
} else {
hideAll('.with_modem');
}
if (data.mqtt && !mqttEditMode) {
$id('mqtt_host').value = data.mqtt.host;
$id('mqtt_port').value = data.mqtt.port;
$id('mqtt_username').value = data.mqtt.username;
$id('mqtt_password').value = data.mqtt.password;
$id('mqtt_topic_prefix').value = data.mqtt.topic_prefix;
$id('mqtt_tls').checked = data.mqtt.tls;
if (data.mqtt.change_only !== undefined)
$id('mqtt_change_only').checked = data.mqtt.change_only;
if (data.mqtt.heartbeat !== undefined)
$id('mqtt_heartbeat').value = data.mqtt.heartbeat;
$id('mqtt_ca_cert').value = data.mqtt.ca_cert || '';
toggleCertVisibility();
}
$id('lcdlock').checked = data.settings.lcdlock == 1;
if (data.settings.lock != 0) {
$id('cablelock').checked = data.settings.cablelock == 1;
} else {
hideById('cablelock');
hideEl($id('cablelock_label'));
}
if (data.ocpp) {
if (data.ocpp.mode == "Enabled") {
showById('ocpp_settings');
$id('enable_ocpp').checked = true;
} else {
hideById('ocpp_settings');
$id('enable_ocpp').checked = false;
}
if (data.ocpp.auto_auth == "Enabled") {
showById('ocpp_auto_auth_idtag_wrapper');
$id('ocpp_auto_auth').checked = true;
} else {
hideById('ocpp_auto_auth_idtag_wrapper');
$id('ocpp_auto_auth').checked = false;
}
if (!ocppEditMode) {
$id('ocpp_backend_url').value = data.ocpp.backend_url;
$id('ocpp_cb_id').value = data.ocpp.cb_id;
$id('ocpp_auth_key').value = data.ocpp.auth_key;
$id('ocpp_auto_auth_idtag').value = data.ocpp.auto_auth_idtag;
}
$id('ocpp_ws_status').textContent = data.ocpp.status;
} else {
hideById('ocpp_config_outer');
}
/* Only continue polling if WebSocket is not connected */
if (!wsConnected) setTimeout(loadData, 5000);
});
}
/* ========== Settings functions ========== */
function SolStartCurr() {
fetch("/settings?solar_start_current=" + $id('solar_start_current').value, { method: 'POST' });
}
function SolImportCurr() {
fetch("/settings?solar_max_import=" + $id('solar_max_import_current').value, { method: 'POST' });
}
function SolStopTime() {
fetch("/settings?stop_timer=" + $id('solar_stop_time').value, { method: 'POST' });
}
function setPrioStrategy() {
fetch("/settings?prio_strategy=" + $id('prio_strategy').value, { method: 'POST' });
}
function setRotationInterval() {
fetch("/settings?rotation_interval=" + $id('rotation_interval').value, { method: 'POST' });
}
function setIdleTimeout() {
fetch("/settings?idle_timeout=" + $id('idle_timeout').value, { method: 'POST' });
}
/* ========== Mode activation ========== */
function activate(mode) {
var starttime = $qs('input[name="starttime"]').value;
var stoptime = $qs('input[name="stoptime"]').value;
var repeat2 = +$qs('#daily_repeat').checked;
var params = new URLSearchParams({
mode: '' + mode,
starttime: starttime,
stoptime: stoptime,
repeat: '' + repeat2
});
if ([1, 2, 3].includes(mode)) {
var override_current = $qs('#mode_override_current').value;
params.append('override_current', '' + (override_current * 10));
}
fetch(endpoint + '?' + params, { method: 'POST' });
/* Immediate visual feedback */
$qs('#mode').textContent = $qs('#mode_' + mode).textContent;
for (var x of [0, 1, 2, 3, 4]) {
$qs('#mode_' + x).classList.toggle('active', x === mode);
}
}
/* ========== MQTT config ========== */
function toggleMqttEdit() {
mqttEditMode = !mqttEditMode;
$qa('.mqtt_settings').forEach(function(el) {
el.style.display = el.style.display === 'none' ? '' : 'none';
});
if (mqttEditMode) {
$id('edit_mqtt_button').textContent = "Close Settings";
fetch("/mqtt_ca_cert").then(function(r) { return r.text(); }).then(function(certData) {
$id('mqtt_ca_cert').value = certData;
});
} else {
$id('edit_mqtt_button').textContent = "Edit Settings";
}
}
function configureMqtt() {
var params = {
mqtt_update: 1,
mqtt_host: $id('mqtt_host').value,
mqtt_port: $id('mqtt_port').value,
mqtt_username: $id('mqtt_username').value,
mqtt_password: $id('mqtt_password').value,
mqtt_topic_prefix: $id('mqtt_topic_prefix').value,
mqtt_tls: $id('mqtt_tls').checked ? 1 : 0,
mqtt_ca_cert: $id('mqtt_ca_cert').value,
mqtt_change_only: $id('mqtt_change_only').checked ? 1 : 0,
mqtt_heartbeat: $id('mqtt_heartbeat').value
};
var query = Object.keys(params)
.map(function(k) { return k + "=" + encodeURIComponent(params[k]); })
.join("&");
fetch("/settings?" + query, { method: 'POST' });
alert('Settings applied');
toggleMqttEdit();
}
/* ========== Checkbox toggles ========== */
function toggleLCDlock() {
fetch("/settings?lcdlock=" + ($id('lcdlock').checked ? 1 : 0), { method: 'POST' });
}
function toggleCableLock() {
fetch("/settings?cablelock=" + ($id('cablelock').checked ? 1 : 0), { method: 'POST' });
}
function toggleEnableOcpp() {
fetch("/settings?ocpp_update=1&ocpp_mode=" + ($id('enable_ocpp').checked ? 1 : 0), { method: 'POST' });
}
function toggleEnableOcppAutoAuth() {
fetch("/settings?ocpp_update=1&ocpp_auto_auth=" + ($id('ocpp_auto_auth').checked ? 1 : 0), { method: 'POST' });
if ($id('ocpp_auto_auth').checked) {
loadData();
}
}
/* ========== OCPP config ========== */
function toggleOcppEdit() {
ocppEditMode = !ocppEditMode;
var fields = ['ocpp_backend_url', 'ocpp_cb_id', 'ocpp_auth_key', 'ocpp_auto_auth', 'ocpp_auto_auth_label', 'ocpp_auto_auth_idtag'];
if (ocppEditMode) {
$id('ocpp_save_btn').textContent = "Save";
fields.forEach(function(id) { var el = $id(id); if (el) el.disabled = false; });
} else {
configureOcpp();
$id('ocpp_save_btn').textContent = "Edit Settings";
fields.forEach(function(id) { var el = $id(id); if (el) el.disabled = true; });
}
}
function configureOcpp() {
var params = {
ocpp_update: 1,
ocpp_backend_url: $id('ocpp_backend_url').value,
ocpp_cb_id: $id('ocpp_cb_id').value,
ocpp_auth_key: $id('ocpp_auth_key').value,
ocpp_auto_auth_idtag: $id('ocpp_auto_auth_idtag').value
};
var query = Object.keys(params)
.map(function(k) { return k + "=" + encodeURIComponent(params[k]); })
.join("&");
fetch("/settings?" + query, { method: 'POST' });
}
/* ========== Actions ========== */
function reboot(event) {
event && event.preventDefault();
var httpStatus;
fetch("/reboot")
.then(function(response) {
httpStatus = response.status;
return response.text();
})
.then(function(message) {
document.body.innerHTML = '<div id="rebootMsg" class="alert alert-success" role="alert"></div>';
$qs('#rebootMsg').innerText = message;
if (httpStatus === 200) {
setInterval(function() { $qs('#rebootMsg').innerText += '.'; }, 500);
setInterval(function() { window.location.reload(); }, 5000);
} else {
$qs('#rebootMsg').innerHTML
+= '<br><br><a href="#" class="alert-link" onclick="window.location.reload()">Return to webinterface</a>';
}
})
.catch(function(error) {
document.body.innerHTML = '<div id="errorMsg" class="alert alert-danger" role="alert"></div>';
$qs('#errorMsg').innerText = 'Error: ' + error;
});
}
function gotoDoc(event) {
var version = $id('version').dataset.version || '';
var gitHub = 'https://github.com/dingo35/SmartEVSE-3.5/tree/';
var docPath = version.startsWith('v') ? version : 'master?tab=readme-ov-file';
window.location.href = gitHub + docPath + '#documentation';
event && event.preventDefault();
}
function postPWM(value) {
fetch("/settings?override_pwm=" + value, { method: 'POST' });
}
function postRequiredEVCCID() {
fetch("/settings?required_evccid=" + $id('required_evccid').value, { method: 'POST' });
}
/* ========== Diagnostic Telemetry Viewer ========== */
var diagWs = null;
var diagMaxRows = 100;
var diagStateNames = ['A','B','C','D','COMM_B','COMM_B_OK','COMM_C','COMM_C_OK','Activate','B1','C1','MODEM_REQ','MODEM_WAIT','MODEM_DONE','MODEM_DEN'];
var diagModeNames = ['NRM','SMT','SOL'];
var diagAccessNames = ['OFF','ON','PAUSE'];
var diagPrevState = -1;
function diagParseSnapshot(buf) {
var dv = new DataView(buf);
return {
ts: dv.getUint32(0, true), state: dv.getUint8(4), err: dv.getUint8(5),
delay: dv.getUint8(6), access: dv.getUint8(7), mode: dv.getUint8(8),
mL1: dv.getInt16(9, true), mL2: dv.getInt16(11, true), mL3: dv.getInt16(13, true),
chgA: dv.getUint16(23, true), solTmr: dv.getUint16(29, true),
temp: dv.getInt8(49), rssi: dv.getInt8(55)
};
}
function diagFormatRow(s) {
var sev = s.err > 0 ? ' sev-err' : (s.solTmr > 0 || s.delay > 0) ? ' sev-warn' : '';
var stName = diagStateNames[s.state] || s.state;
var stChanged = (diagPrevState !== -1 && s.state !== diagPrevState);
diagPrevState = s.state;
var m = Math.floor(s.ts / 60), sec = s.ts % 60;
return '<div class="diag-row' + sev + '"><span class="diag-ts">' + m + ':' + (sec < 10 ? '0' : '') + sec +
'</span><span class="diag-state">' + stName + (stChanged ? ' ←' : '') + '</span>' +
(diagAccessNames[s.access] || '?') + '/' + (diagModeNames[s.mode] || '?') +
' | ' + (s.chgA / 10).toFixed(1) + 'A | M:' +
(s.mL1 / 10).toFixed(0) + '/' + (s.mL2 / 10).toFixed(0) + '/' + (s.mL3 / 10).toFixed(0) +
' | ' + s.temp + '\u00B0C' +
(s.err > 0 ? ' | ERR:0x' + s.err.toString(16) : '') +
(s.solTmr > 0 ? ' | sol:' + s.solTmr + 's' : '') + '</div>';
}
function diagStart() {
var profile = $id('diag_profile').value;
fetch('/diag/start?profile=' + profile, { method: 'POST' }).then(function() {
diagConnectWs();
$id('diag_status_badge').className = 'diag-badge diag-badge-on';
$id('diag_status_badge').textContent = 'Capturing';
});
}
function diagStop() {
fetch('/diag/stop', { method: 'POST' }).then(function() {
$id('diag_status_badge').className = 'diag-badge diag-badge-off';
$id('diag_status_badge').textContent = 'Stopped';
});
if (diagWs) diagWs.close();
}
function diagConnectWs() {
if (diagWs && diagWs.readyState === WebSocket.OPEN) return;
var url = (window.location.protocol === 'https:' ? 'wss:' : 'ws:') + '//' + window.location.host + '/diag/stream';
var ws = new WebSocket(url);
diagWs = ws;
ws.binaryType = 'arraybuffer';
var rowCount = 0;
ws.onmessage = function(ev) {
if (!(ev.data instanceof ArrayBuffer) || ev.data.byteLength < 56) return;
var snap = diagParseSnapshot(ev.data);
var log = $id('diag_log');
if (!log) return;
log.insertAdjacentHTML('beforeend', diagFormatRow(snap));
rowCount++;
while (log.children.length > diagMaxRows) log.removeChild(log.firstChild);
log.scrollTop = log.scrollHeight;
var c = $id('diag_count');
if (c) c.textContent = rowCount + ' samples';
};
ws.onclose = function() { diagWs = null; };
ws.onerror = function() { if (ws.readyState !== WebSocket.CLOSED) ws.close(); };
}
/* Check diag status on load */
fetch('/diag/status').then(function(r) { return r.json(); }).then(function(d) {
if (d && d.profile && d.profile !== 'off') {
$id('diag_status_badge').className = 'diag-badge diag-badge-on';
$id('diag_status_badge').textContent = d.profile;
diagConnectWs();
}
}).catch(function() {});
/* ========== LCD WebSocket (IIFE) ========== */
(function() {
var LCD_SCREEN = $qs('#lcd .lcd-screen');
var LCD_BUTTON_CONTAINERS = $qa('#lcd .lcd-buttons, #lcd .lcd-display-buttons');
var LCD_ACTIVATE = $qs('#lcd .lcd-activate');
var PASSWORD_FIELD = $qs('#lcd-password');
var PASSWORD_FORM = $qs('#lcd-password-form');
var LOCK_STATUS = $qs('#lcd-lock-status');
var LOCK_HINT = $qs('#lcd-lock-hint');
var SESSION_STORAGE_PIN_KEY = 'lcdPinCode';
var LOCKED_PRESS_ALERT_THRESHOLD = 3;
var MIN_BUTTON_PRESS_MS = 300;
var WS_RECONNECT_BASE_MS = 1000;
var WS_RECONNECT_MAX_MS = 10000;
var WS_RECONNECT_JITTER_MS = 400;
var WS_INITIAL_RETRY_MS = 750;
var WS_INITIAL_RETRY_JITTER_MS = 250;
var WS_CONNECT_TIMEOUT_MS = 5000;
var WS_URL = (window.location.protocol === 'https:' ? 'wss:' : 'ws:') + '//' + window.location.host + '/ws/lcd';
var passwordVerified = false;
var blockedButtonPressCount = 0;
var lcdSocket = null;
var reconnectTimer = null;
var connectTimeoutTimer = null;
var reconnectAttempts = 0;
var hasConnectedOnce = false;
var wsPausedByVisibility = false;
var activeFrameUrl = null;
var activePointers = new Map();
function setActivateText(text) {
if (LCD_ACTIVATE) LCD_ACTIVATE.textContent = text;
}
function clearConnectTimeout() {
if (connectTimeoutTimer !== null) {
window.clearTimeout(connectTimeoutTimer);
connectTimeoutTimer = null;
}
}
function clearReconnectTimer() {
if (reconnectTimer !== null) {
window.clearTimeout(reconnectTimer);
reconnectTimer = null;
}
}
function shouldKeepWsActive() {
return !wsPausedByVisibility;
}
function requestImmediateReconnect() {
if (!shouldKeepWsActive()) return;
reconnectAttempts = 0;
clearReconnectTimer();
connectLCDWebSocket();
}
function disconnectLCDWebSocket() {
clearConnectTimeout();
clearReconnectTimer();
if (lcdSocket && (lcdSocket.readyState === WebSocket.OPEN || lcdSocket.readyState === WebSocket.CONNECTING)) {
lcdSocket.close();
}
}
function scheduleReconnect() {
if (!shouldKeepWsActive()) return;
if (reconnectTimer !== null) return;
var backoffMs = hasConnectedOnce
? Math.min(WS_RECONNECT_BASE_MS * (Math.pow(2, reconnectAttempts)), WS_RECONNECT_MAX_MS)
: WS_INITIAL_RETRY_MS;
var jitterMs = hasConnectedOnce
? Math.floor(Math.random() * WS_RECONNECT_JITTER_MS)
: Math.floor(Math.random() * WS_INITIAL_RETRY_JITTER_MS);
var reconnectDelayMs = backoffMs + jitterMs;
reconnectAttempts += 1;
setActivateText('Reconnecting in ' + Math.ceil(reconnectDelayMs / 1000) + 's...');
reconnectTimer = window.setTimeout(function() {
reconnectTimer = null;
connectLCDWebSocket();
}, reconnectDelayMs);
}
function activateLCD() {
if (!LCD_ACTIVATE) return;
LCD_ACTIVATE.remove();
LCD_ACTIVATE = null;
}
function getStoredPin() {
try { return sessionStorage.getItem(SESSION_STORAGE_PIN_KEY); }
catch (e) { return null; }
}
function storePin(pin) {
try { sessionStorage.setItem(SESSION_STORAGE_PIN_KEY, pin); }
catch (e) { /* ignore */ }
}
function clearStoredPin() {
try { sessionStorage.removeItem(SESSION_STORAGE_PIN_KEY); }
catch (e) { /* ignore */ }
}
function updateLockUi(isUnlocked) {
if (isUnlocked) {
LOCK_STATUS.textContent = 'Unlocked';
LOCK_STATUS.style.color = '#1cc88a';
LOCK_HINT.textContent = 'Buttons are unlocked. You can use Left, Middle and Right.';
} else {
LOCK_STATUS.textContent = 'Locked - Enter PIN';
LOCK_STATUS.style.color = '#e74a3b';
LOCK_HINT.textContent = 'LCD buttons are locked. Enter your PIN to unlock them.';
}
}
function connectLCDWebSocket() {
if (!shouldKeepWsActive()) return;
if (lcdSocket && (lcdSocket.readyState === WebSocket.OPEN || lcdSocket.readyState === WebSocket.CONNECTING)) return;
setActivateText('Connecting...');
var socket = new WebSocket(WS_URL);