Repository navigation
Expand file tree
/
Copy pathsendToDatabase.js
More file actions
1109 lines (934 loc) · 39.9 KB
/
Copy pathsendToDatabase.js
File metadata and controls
1109 lines (934 loc) · 39.9 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
// ==UserScript==
// @name Calibration Capture - Send to Visualizer Database
// @namespace https://github.com/LeightonSolo/IsensixScripts
// @version 3.55
// @description Capture Isensix calibration and Live View data for the dashboard database.
// @author Leighton Solomon
// @match https://*/guardian/calibration/calsensor.php*
// @match https://*.isensix.com:*/guardian/calibration/calsensor.php*
// @match https://*.isensix.com:*/arms2/calibration/calsensor.php*
// @match https://*.isensix.com:*/arms/calsensor.php*
// @match https://*.isensix.com:*/guardian/calibration/calreport.php*
// @match https://*.isensix.com:*/arms2/calibration/calreport.php*
// @match https://*.isensix.com:*/arms2/calibration//calreport.php*
// @match https://*.isensix.com:*/arms2/calsetup.php*
// @match https://*.isensix.com:*/arms/calsetup.php*
// @match https://*/arms/calreport.php?cid=*
// @match https://*.isensix.com:*/guardian/iserep1.php*
// @match https://*.isensix.com:*/arms2/iserep1.php*
// @match https://*.isensix.com:*/arms/iserep1.php*
// @match https://*/arms2/calibration/calsensor.php*
// @match https://*.isensix.com:*/arms/admin/sensorcal.php
// @match https://*.isensix.com:*/arms/debug/debug_query.php*
// @match https://*/arms2/
// @match https://*/arms2/index.php*
// @match https://*/arms/
// @match https://*/arms/index.php*
// @match https://*/guardian/
// @match https://*/guardian/index.php*
// @downloadURL https://raw.githubusercontent.com/LeightonSolo/IsensixScripts/main/sendToDatabase.js
// @updateURL https://raw.githubusercontent.com/LeightonSolo/IsensixScripts/main/sendToDatabase.js
// @icon https://www.google.com/s2/favicons?sz=64&domain=isensix.com
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @connect flat-tree-380f.leightonsolo.workers.dev
// ==/UserScript==
const arms = document.querySelector(".headline2")?.textContent.trim() === "(Advanced Remote Monitoring System)";
let twoPointOne = false;
try {//determine if the server is Guardian 3.0
if(document.querySelector("#primary_nav_wrap > ul > li:nth-child(1) > a").title == "Guardian 2.1"){
twoPointOne = true;
console.log("2.1 Detected");
}
}
catch(err){}
let twoPointZero = false;
try {//determine if the server is Guardian 3.0
if(document.getElementsByClassName("ICO_DISCONNECT")[0].innerHTML == "Sign me off"){
twoPointZero = true;
console.log("2.0 Detected");
}
}
catch(err){}
let threePoint0 = false;
try {
if (document.querySelector("#isemainmenu > li:nth-child(1) > a")?.title === "Guardian 3.0") {
threePoint0 = true;
console.log("3.0 Detected");
}
}
catch(err){}
const TYPE_MAP = {
'RE': 'Temp-RE',
'RM': 'Temp-RM',
'SC': 'Temp-SC',
'TC': 'Temp-TC',
'HU': 'Humidity',
'DP': 'DiffPress',
'DP.25': 'DiffPress',
'DiffPressure': 'DiffPress',
'DiffPress .25': 'DiffPress',
//'MPM': 'MPM',
'BIN': 'Binary',
'O2': 'Oxygen',
//'CO2_A_20': 'CO2',
};
function normalizeType(raw) {
if (!raw) return null;
return TYPE_MAP[raw.trim()] ?? raw.trim();
}
(function () {
'use strict';
/* ─── Config ──────────────────────────────────────────── */
const WORKER_URL = 'https://flat-tree-380f.leightonsolo.workers.dev';
const API_KEY = 'U87iy7VynFYLJUDnfUYBJHnRKbRiQO3Z';
const COOLDOWN_MS = 1 * 60 * 1000; // 1 minutes per page type per server CHANGE TO 5 or more later?
/* ─── Shared utilities ────────────────────────────────── */
function getServer() {
const m = window.location.host.match(/:7(\d{3})/);
return m ? m[1] : null;
}
function showBanner(msg, color) {
const el = document.createElement('div');
el.textContent = msg;
el.style.cssText = `
position: fixed; top: 16px; right: 16px;
background: ${color}; color: white;
padding: 10px 16px; border-radius: 6px;
z-index: 99999; font-size: 14px;
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
font-family: system-ui, sans-serif;
`;
document.body.appendChild(el);
setTimeout(() => el.remove(), 4000);
}
function isCoolingDown(key) {
const last = GM_getValue(key, 0);
return Date.now() - last < COOLDOWN_MS;
}
function setCooldown(key) {
GM_setValue(key, Date.now());
}
function postSingle(data) {
GM_xmlhttpRequest({
method: 'POST',
url: `${WORKER_URL}/calibration`,
headers: { 'Content-Type': 'application/json', 'X-Api-Key': API_KEY },
data: JSON.stringify(data),
onload: (res) => {
if (res.status === 200) {
showBanner('✓ Calibration synced to dashboard', '#1a6e2e');
} else {
showBanner(`⚠ Capture failed (${res.status})`, '#8b1a1a');
console.error('Single post failed:', res.responseText);
}
},
onerror: () => showBanner('⚠ Network error', '#8b1a1a'),
});
}
function postBatch(sensors, onSuccess) {
GM_xmlhttpRequest({
method: 'POST',
url: `${WORKER_URL}/calibrations/batch`,
headers: { 'Content-Type': 'application/json', 'X-Api-Key': API_KEY },
data: JSON.stringify({ sensors }),
onload: (res) => {
if (res.status === 200) {
onSuccess(sensors.length);
} else {
showBanner(`⚠ Sync failed (${res.status})`, '#8b1a1a');
console.error(' Batch post failed:', res.responseText);
}
},
onerror: () => showBanner('⚠ Network error — sync failed', '#8b1a1a'),
});
}
/* ─── Page dispatcher ─────────────────────────────────── */
const path = window.location.pathname;
if (isLiveViewPath(path)) handleLiveView();
else if (isArmsCalibrationReport()) handleArmsCalibrationReport();
else if (path.includes('calsensor.php') || path.includes('sensorcal.php')) handleCalSensor();
else if (path.includes('calreport.php') || path.includes('calsetup.php')) handleCalReport();
else if (path.includes('iserep1.php')) handleIseRep();
else if (path.includes('debug_query.php')) handleArmsIseRep();
function isLiveViewPath(pathname) {
return /\/(?:arms2|arms|guardian)(?:\/+index\.php)?\/?$/i.test(pathname);
}
function isArmsCalibrationReport() {
return /\/arms\/calreport\.php$/i.test(path) && new URLSearchParams(window.location.search).has('cid');
}
function handleArmsCalibrationReport() {
const data = scrapeArmsCalibrationReport();
if (!data) {
showBanner('⚠ Could not read calibration report data', '#8b1a1a');
console.error('[sendToDatabase] Could not read required sensor ID or calibration time from ARMS report.');
return;
}
postSingle(data);
}
function scrapeArmsCalibrationReport() {
const server = getServer();
if (!server) return null;
const values = new Map();
for (const row of document.querySelectorAll('tr')) {
const label = row.querySelector('td.detailheader');
const value = row.querySelector('td.detailcell');
if (label && value) {
values.set(label.textContent.trim().toLowerCase(), value.textContent.trim());
}
}
const getValue = (label) => values.get(label.toLowerCase()) || null;
const sensor_id = getValue('Sensor ID');
const calibrated_at = parseToISO(getValue('Date Calibrated'));
if (!sensor_id || !calibrated_at) return null;
const serial_number = getValue('Sensor Serial No.');
const rawCertificate = getValue('Nist Certificate');
return {
sensor_id,
sensor_name: getValue('Sensor Name'),
zone: getValue('Zone'),
cp_address: getValue('Address'),
serial_number,
calibrated_by: getValue('Calibrated By'),
calibrated_at,
old_offset: parseFloatOrNull(getValue('Old Offset')),
new_offset: parseFloatOrNull(getValue('New Offset')),
cal_cert: !rawCertificate || rawCertificate.toLowerCase() === 'n/a' ? null : rawCertificate,
canned_msg: getValue('Comment'),
server,
};
}
/* ═══════════════════════════════════════════════════════
HANDLER 1 — calsensor.php (calibration confirmation)
Fires on Confirm button click. Single record, high fidelity.
Authoritative source for: calibrated_by, cp_address,
cal_cert, canned_msg, old_offset, new_offset, calibrated_at
════════════════════════════════════════════════════════ */
function handleCalSensor() {
if(path.includes('sensorcal.php')){
handleCalSensor_ARMS();
return;
}
const confirmBtn = document.getElementById('BTN_SAVE');
if (!confirmBtn) return;
const scrape = () => twoPointOne ? scrapeCalSensor_v21() : scrapeCalSensor();
const previewData = scrape();
if (previewData) {
console.info('[sendToDatabase] Calibration scrape preview (not submitted):', previewData);
} else {
console.warn('[sendToDatabase] Calibration scrape preview could not read sensor data.');
}
confirmBtn.addEventListener('click', () => {
const data = scrape();
if (!data) {
showBanner('⚠ Could not read sensor data', '#8b1a1a');
return;
}
postSingle(data);
});
}
function scrapeCalSensor() {
console.log("Scraping CalSensor for 2.0");
const server = getServer();
console.log("Server: ", server);
// Helper: find td text by th label
const getRow = (label) => {
for (const row of document.querySelectorAll('tr')) {
const th = row.querySelector('th');
if (th && th.textContent.trim() === label) {
return row.querySelector('td');
}
}
return null;
};
// Sensor ID cell (contains ID span and type <em>)
const sensorIdCell = getRow('Sensor ID');
const sensor_id = sensorIdCell?.querySelector('span')?.textContent?.trim() || document.querySelector('#calsen > table:nth-child(3) > tbody > tr:nth-child(1) > td')?.textContent?.trim() ||
null;
console.log("Sensor ID: ", sensor_id);
// Don't capture sensor_type here — iserep1 is authoritative for full type names
// Calibrated by from nav title — strip "Isensix " prefix
const userLink = document.querySelector('a[href="/guardian/v2prefs.php"]');
const calibrated_by = userLink?.getAttribute('title')?.trim() ?? null;
// Hidden inputs
const getHidden = (name) =>
document.querySelector(`input[name="${name}"]`)?.value?.trim() ?? null;
const new_offset = getHidden('offset');
const old_offset = getHidden('offsets[]');
const canned_msg = getHidden('comment');
// Cal cert — visible text in the certificate row
let cal_cert = null;
for (const row of document.querySelectorAll('tr')) {
const cells = row.querySelectorAll('td');
if (cells.length === 2 && cells[0].textContent.includes('Certificate')) {
const text = cells[1].textContent.trim();
cal_cert = text === 'n/a' ? null : text;
break;
}
}
// Calibrated at from visible table cell, convert to ISO
const calAtRaw = document.querySelector('td[name="ts[]"]')?.textContent?.trim() ?? null;
console.log("calAtRaw: ", calAtRaw);
//const calibrated_at = calAtRaw ? new Date(calAtRaw).toISOString() : null; before timezone fix
const parsedCalibratedAt = calAtRaw ? parseToISO(calAtRaw) : null;
const serverTimeText = document.querySelector('#uinfo')?.textContent ?? '';
const serverTimeRaw = serverTimeText.match(/Server Time:\s*(\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d{2}(?::\d{2})?)/i)?.[1] ?? null;
const calibrated_at = parsedCalibratedAt ?? (serverTimeRaw ? parseToISO(serverTimeRaw) : null);
console.log("calibrated_at: ", calibrated_at);
// Sensor name from onclick span
const sensor_name = document.querySelector('tr td span[onclick]')?.textContent?.trim() ?? null;
if (!sensor_id || !calibrated_at) return null;
return {
sensor_id,
sensor_name,
zone: getRow('Zone')?.textContent?.trim() ?? null,
serial_number: getRow('Sensor Serial')?.textContent?.trim() ?? null,
cp_address: getRow('CP Serial Number')?.textContent?.trim() ?? null,
old_offset: old_offset ? parseFloat(old_offset) : null,
new_offset: new_offset ? parseFloat(new_offset) : null,
cal_cert,
canned_msg,
calibrated_at,
calibrated_by,
server,
};
}
function scrapeCalSensor_v21() {
const server = getServer();
const getRow = (label) => {
for (const row of document.querySelectorAll('tr')) {
const th = row.querySelector('th');
if (th && th.textContent.trim() === label) {
return row.querySelector('td');
}
}
return null;
};
// Sensor ID is plain text in 2.1, no span wrapper
const sensor_id = getRow('Sensor ID')?.textContent?.trim() || null;
// Sensor name is plain text in 2.1, no onclick span
const sensor_name = getRow('Sensor Name')?.textContent?.trim() || null;
const calibrated_by = document
.querySelector('#primary_nav_wrap > ul > li.ise-ui-menu-item > a > span.isensix')
?.textContent?.trim() || null;
// Hidden inputs — same as original
const getHidden = (name) =>
document.querySelector(`input[name="${name}"]`)?.value?.trim() ?? null;
const new_offset = getHidden('offset');
const canned_msg = getHidden('comment');
// old_offset not available in 2.1 — omit
// calibrated_at not available from ts[] — use current time
//const calibrated_at = new Date().toISOString();
// With this — use current local time formatted without UTC conversion:
const now = new Date();
const calibrated_at = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}-${String(now.getDate()).padStart(2,'0')} ${String(now.getHours()).padStart(2,'0')}:${String(now.getMinutes()).padStart(2,'0')}`;
// Cal cert
let cal_cert = null;
for (const row of document.querySelectorAll('tr')) {
const cells = row.querySelectorAll('td');
if (cells.length === 2 && cells[0].textContent.includes('Certificate')) {
const text = cells[1].textContent.trim();
cal_cert = text === 'n/a' ? null : text;
break;
}
}
if (!sensor_id) return null;
return {
sensor_id,
sensor_name,
zone: getRow('Zone')?.textContent?.trim() ?? null,
serial_number: getRow('Sensor Serial')?.textContent?.trim() ?? null,
cp_address: getRow('CP Serial Number')?.textContent?.trim() ?? null,
new_offset: new_offset ? parseFloat(new_offset) : null,
old_offset: null,
cal_cert,
canned_msg,
calibrated_at,
calibrated_by,
server,
};
}
function handleCalSensor_ARMS() {
// ARMS uses a form submit — intercept it and check which button was clicked
const form = document.querySelector('form[name="calform"]');
if (!form) return;
let clickedButton = null;
// Track which submit button was clicked
form.querySelectorAll('input[type="submit"]').forEach(btn => {
btn.addEventListener('click', () => { clickedButton = btn.value; });
});
form.addEventListener('submit', (e) => {
// Only fire on Set Sensor Offset or Replace Sensor, not Cancel
if (!clickedButton ||
(!clickedButton.includes('Set Sensor Offset') &&
!clickedButton.includes('Replace Sensor'))) {
return;
}
const data = scrapeCalSensor_ARMS();
if (!data) {
showBanner('⚠ Could not read sensor data', '#8b1a1a');
return;
}
// Fire and forget — don't block the form submission
postSingle(data);
});
}
function scrapeCalSensor_ARMS() {
const server = getServer();
// Hidden inputs are the most reliable source for ARMS
const getHidden = (name) =>
document.querySelector(`input[name="${name}"]`)?.value?.trim() ?? null;
const sensor_id = getHidden('id');
const sensor_name = getHidden('sname');
const serial_number = getHidden('s_sn');
const new_offset = getHidden('newoffset');
const old_offset = getHidden('oldoffset');
const calibrated_by_raw = getHidden('uname');
// Clean up uname — ARMS stores it as "isensix tech." sometimes with trailing period
const calibrated_by = calibrated_by_raw?.replace(/\.$/, '').trim() || null;
// Zone and CP address from the sensor info table
// ARMS uses th.sinfoname / th.sinfodesc pairs
function getInfoRow(label) {
const headers = document.querySelectorAll('th.sinfoname');
for (const th of headers) {
if (th.textContent.trim() === label) {
return th.nextElementSibling?.textContent?.trim() ?? null;
}
}
return null;
}
const zone = getInfoRow('Zone Name');
const cp_address = getInfoRow('CP Address');
// Cal cert — selected option text from the NIST certificate dropdown
const certSelect = document.querySelector('select[name="nistCert"]');
const cal_cert = certSelect
? certSelect.options[certSelect.selectedIndex]?.text?.trim() || null
: null;
// Canned messages — collect text of checked checkboxes
const checkedMsgs = [];
document.querySelectorAll('input[type="checkbox"][name^="cmsg"]').forEach(cb => {
if (cb.checked) {
const label = cb.nextElementSibling?.textContent?.trim();
if (label) checkedMsgs.push(label);
}
});
const canned_msg = checkedMsgs.length ? checkedMsgs.join(' | ') : null;
// Comment field
const comment = document.querySelector('textarea[name="comment"]')?.value?.trim() || null;
// Combine canned messages and comment
const full_msg = [canned_msg, comment].filter(Boolean).join(' | ') || null;
// calibrated_at — use current time at click, formatted as local YYYY-MM-DD HH:MM
const now = new Date();
const calibrated_at = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}-${String(now.getDate()).padStart(2,'0')} ${String(now.getHours()).padStart(2,'0')}:${String(now.getMinutes()).padStart(2,'0')}`;
if (!sensor_id) return null;
return {
sensor_id,
sensor_name,
zone,
cp_address,
serial_number,
old_offset: old_offset ? parseFloat(old_offset) : null,
new_offset: new_offset ? parseFloat(new_offset) : null,
cal_cert,
canned_msg: full_msg,
calibrated_at,
calibrated_by,
server,
// sensor_type intentionally omitted — iserep1 is authoritative
};
}
/* ═══════════════════════════════════════════════════════
HANDLER 2 — calreport.php (calibration overview)
Fires on page load with cooldown. Batch upsert.
Good for: sensor inventory, zone, type, serial, offsets,
calibrated_at, calibrated_by. Opens every calibration cycle.
════════════════════════════════════════════════════════ */
function handleCalReport() {
const server = getServer();
const cooldownKey = `calreport_sync_${server}`;
if (isCoolingDown(cooldownKey)) {
console.log(`calreport cooldown active for server ${server}`);
return;
}
const sensors = (twoPointOne) ? scrapeCalReport_v21(server) : (twoPointZero) ? scrapeCalReport_v20(server) : scrapeCalReport(server);
if (!sensors.length) return;
postBatch(sensors, (count) => {
setCooldown(cooldownKey);
showBanner(`✓ Synced ${count} sensors data to dashboard`, '#1a6e2e');
});
}
function scrapeCalReport(server) {
console.log("scraping cal report for 3.0 server");
const rows = document.querySelectorAll('table#tuq1 tbody tr');
const sensors = [];
for (const row of rows) {
const tds = row.querySelectorAll('td');
if (tds.length < 9) continue;
const rawId = tds[2]?.textContent?.trim().replace('#', '');
if (!rawId || isNaN(rawId)) continue;
const calSpans = tds[6]?.querySelectorAll('span');
const rawCalibratedAt = calSpans?.[0]?.textContent?.trim();
const rawCalibratedBy = calSpans?.[1]?.textContent?.trim();
const calibrated_at = (!rawCalibratedAt || rawCalibratedAt === '-')
? null
: parseToISO(rawCalibratedAt);
const calibrated_by = (!rawCalibratedBy || rawCalibratedBy === '-')
? null
: rawCalibratedBy;
const certText = tds[10]?.textContent?.trim();
//console.log("Calibrated at: ", calibrated_at);
//console.log("Calibrated by: ", calibrated_by);
//console.log(certText);
sensors.push({
sensor_id: rawId,
zone: tds[1]?.querySelector('span')?.textContent?.trim() ?? null,
sensor_name: tds[3]?.querySelector('span')?.textContent?.trim() ?? null,
sensor_type: normalizeType(tds[4]?.querySelector('span')?.textContent?.trim() ?? null),
serial_number: tds[5]?.textContent?.trim() ?? null,
calibrated_at,
calibrated_by,
old_offset: parseFloatOrNull(tds[7]?.textContent),
new_offset: parseFloatOrNull(tds[8]?.textContent),
cal_cert: (!certText || certText === 'n/a') ? null : certText,
server,
});
}
return sensors;
}
function scrapeCalReport_v21(server) {
console.log("scraping cal report for 2.1 server");
const table = document.querySelector('table#tuq1')
|| document.querySelector('table.arms.p100');
if (!table) return [];
const rows = table.querySelectorAll('tbody tr');
const sensors = [];
for (const row of rows) {
const tds = row.querySelectorAll('td');
if (tds.length < 10) continue;
// Sensor ID from row id attribute (e.g. "r171" → "171")
const rawId = row.id?.replace(/^r/, '');
if (!rawId || isNaN(rawId)) continue;
// Calibrated at / by — plain text, "-" means null
const rawCalibratedAt = tds[6]?.textContent?.trim();
const rawCalibratedBy = tds[7]?.textContent?.trim();
const calibrated_at = (!rawCalibratedAt || rawCalibratedAt === '-')
? null
: parseToISO(rawCalibratedAt);
const calibrated_by = (!rawCalibratedBy || rawCalibratedBy === '-')
? null
: rawCalibratedBy;
//console.log("Calibrated at: ", calibrated_at);
//console.log("Calibrated by: ", calibrated_by);
// Cal cert — anchor text, skip "n/a"
const certText = tds[12]?.textContent?.trim();
sensors.push({
sensor_id: rawId,
zone: tds[1]?.textContent?.trim() || null,
sensor_name: tds[2]?.querySelector('a')?.textContent?.trim() || null,
sensor_type: normalizeType(tds[3]?.textContent?.trim() || null),
cp_address: tds[4]?.textContent?.trim() || null,
serial_number: tds[5]?.textContent?.trim() || null,
calibrated_at,
calibrated_by,
old_offset: parseFloatOrNull(tds[8]?.textContent),
new_offset: parseFloatOrNull(tds[9]?.textContent),
cal_cert: (!certText || certText === 'n/a') ? null : certText,
server,
});
}
return sensors;
}
function scrapeCalReport_v20(server) {
console.log("scraping cal report for 2.0 server");
const table = document.querySelector('table#tuq1')
|| document.querySelector('table.arms.p100');
if (!table) return [];
const rows = table.querySelectorAll('tbody tr');
const sensors = [];
for (const row of rows) {
const tds = row.querySelectorAll('td');
if (tds.length < 10) continue;
// Sensor ID from row id attribute (e.g. "r171" → "171")
const rawId = row.id?.replace(/^r/, '');
if (!rawId || isNaN(rawId)) continue;
// Calibrated at / by — plain text, "-" means null
const rawCalibratedAt = tds[5]?.querySelectorAll('span')[1]?.textContent?.trim();
const rawCalibratedBy = tds[6]?.textContent?.trim();
const calibrated_at = (!rawCalibratedAt || rawCalibratedAt === '-')
? null
: parseToISO(rawCalibratedAt);
const calibrated_by = (!rawCalibratedBy || rawCalibratedBy === '-')
? null
: rawCalibratedBy;
// Cal cert — anchor text, skip "n/a"
const certText = tds[11]?.textContent?.trim();
sensors.push({
sensor_id: rawId,
zone: tds[1]?.textContent?.trim() || null,
sensor_name: tds[2]?.textContent?.trim() || null,
sensor_type: normalizeType(tds[3]?.textContent?.trim() || null),
serial_number: tds[4]?.textContent?.trim() || null,
calibrated_at,
calibrated_by,
old_offset: parseFloatOrNull(tds[7]?.textContent),
new_offset: parseFloatOrNull(tds[8]?.textContent),
cal_cert: (!certText || certText === 'n/a') ? null : certText,
server,
});
}
return sensors;
}
/* ═══════════════════════════════════════════════════════
HANDLER 3 — iserep1.php (calibration query / status)
Fires on page load with cooldown. Batch upsert.
Authoritative source for: cp_address, access_point,
quality, status, sensor_type (full names), zone, serial.
Best data quality — techs should leave this open.
════════════════════════════════════════════════════════ */
function handleIseRep() {
const server = getServer();
const cooldownKey = `iserep_sync_${server}`;
if (isCoolingDown(cooldownKey)) {
console.log(`iserep cooldown active for server ${server}`);
return;
}
const sensors = scrapeIseRep(server);
if (!sensors.length) return;
//console.log(sensors);
postBatch(sensors, (count) => {
setCooldown(cooldownKey);
showBanner(`✓ Synced ${count} sensors status to dashboard`, '#1a6e2e');
});
}
function scrapeIseRep(server) {
// Columns: ID, CP_ADDRESS, SENSOR_NAME, SERIAL, OFFSET, AP, QUAL, STATUS, TYPE, ZONE, calibrated
const table = document.querySelector('table#tuq1')
|| document.querySelector('table.arms.p100');
if (!table) return [];
const rows = table.querySelectorAll('tbody tr');
const sensors = [];
for (const row of rows) {
const tds = row.querySelectorAll('td');
if (tds.length < 11) continue;
const rawId = tds[0]?.textContent?.trim();
if (!rawId || isNaN(rawId)) continue;
const rawCalibratedAt = tds[10]?.textContent?.trim();
const calibrated_at = (!rawCalibratedAt || rawCalibratedAt === '-')
? null
: parseToISO(rawCalibratedAt);
sensors.push({
sensor_id: rawId,
cp_address: tds[1]?.textContent?.trim() || null,
sensor_name: tds[2]?.textContent?.trim() || null,
serial_number:tds[3]?.textContent?.trim() || null,
new_offset: parseFloatOrNull(tds[4]?.textContent),
access_point: tds[5]?.textContent?.trim() || null,
quality: tds[6]?.textContent?.trim() || null,
status: tds[7]?.textContent?.trim() || null,
sensor_type: normalizeType(tds[8]?.textContent?.trim() || null),
zone: decodeHtmlEntities(tds[9]?.textContent?.trim()) || null,
calibrated_at,
// calibrated_by intentionally omitted — not available on this page
server,
});
}
console.log(sensors);
return sensors;
}
/* ─── Shared parsing helpers ──────────────────────────── */
function parseFloatOrNull(text) {
if (!text) return null;
const v = parseFloat(text.trim());
return isNaN(v) ? null : v;
}
function decodeHtmlEntities(text) {
if (!text) return null;
const el = document.createElement('textarea');
el.innerHTML = text;
return el.value;
}
function parseToISO(raw) {
if (!raw || raw.trim() === '-') return null;
const cleaned = raw.trim();
// ISO-ish from server: "2026-02-26 13:25:42" or "2026-02-26T13:25:42"
// Store as-is normalized to minute, NO UTC conversion
// These are server local times — converting to UTC would shift them incorrectly
if (/^\d{4}-\d{2}-\d{2}/.test(cleaned)) {
return cleaned.replace('T', ' ').slice(0, 16); // "2026-02-26 13:25"
}
const longDate = cleaned.match(
/^(?:[A-Za-z]+,\s*)?([A-Za-z]+)\s+(\d{1,2}),\s*(\d{4})\s+(\d{1,2}):(\d{2})(?::\d{2})?\s*(am|pm)$/i
);
if (longDate) {
const months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
const month = months.indexOf(longDate[1].toLowerCase());
if (month !== -1) {
let hour = parseInt(longDate[4], 10);
if (longDate[6].toLowerCase() === 'pm' && hour !== 12) hour += 12;
if (longDate[6].toLowerCase() === 'am' && hour === 12) hour = 0;
return `${longDate[3]}-${String(month + 1).padStart(2, '0')}-${String(parseInt(longDate[2], 10)).padStart(2, '0')} ${String(hour).padStart(2, '0')}:${longDate[5]}`;
}
}
// calreport format: "02/26/26 1:25 pm" or "04/03/26 11:33"
const m = cleaned.match(
/^(\d{1,2})\/(\d{1,2})\/(\d{2}|\d{4})\s+(\d{1,2}):(\d{2})(?::(\d{2}))?\s*(am|pm)?$/i
);
if (m) {
let [, month, day, year, hours, minutes, seconds, ampm] = m;
let h = parseInt(hours);
if (ampm) {
if (ampm.toLowerCase() === 'pm' && h !== 12) h += 12;
if (ampm.toLowerCase() === 'am' && h === 12) h = 0;
}
const fullYear = year.length === 2 ? 2000 + parseInt(year, 10) : parseInt(year, 10);
// Format as "YYYY-MM-DD HH:MM" without UTC conversion
const MM = String(parseInt(month)).padStart(2, '0');
const DD = String(parseInt(day)).padStart(2, '0');
const HH = String(h).padStart(2, '0');
const mm = String(parseInt(minutes)).padStart(2, '0');
return `${fullYear}-${MM}-${DD} ${HH}:${mm}`;
}
// Last resort
const d = new Date(cleaned);
return isNaN(d) ? null : d.toISOString().replace('T', ' ').slice(0, 16);
}
function handleArmsIseRep() {
const server = getServer();
const cooldownKey = `arms_iserep_sync_${server}`;
// Table only exists after form submit — if not present, nothing to do
const table = document.querySelector('table[border="1"][width="100%"]');
if (!table) return;
if (isCoolingDown(cooldownKey)) {
console.log(`ARMS iserep cooldown active for server ${server}`);
return;
}
const sensors = scrapeArmsIseRep(server, table);
if (!sensors.length) return;
postBatch(sensors, (count) => {
setCooldown(cooldownKey);
showBanner(`✓ Synced ${count} sensors status to dashboard (ARMS) `, '#1a6e2e');
});
}
function scrapeArmsIseRep(server, table) {
const rows = table.querySelectorAll('tbody tr');
const sensors = [];
for (const row of rows) {
const tds = row.querySelectorAll('td');
if (tds.length < 11) continue;
const rawId = tds[0]?.textContent?.trim();
if (!rawId || isNaN(rawId)) continue;
// Blank cells in ARMS are which becomes a non-breaking space
const getText = (td) => {
const t = td?.textContent?.trim().replace(/\u00a0/g, '').trim();
return t || null;
};
const rawCalibratedAt = getText(tds[10]);
const calibrated_at = rawCalibratedAt ? parseToISO(rawCalibratedAt) : null;
// Quality cell may be blank for disabled sensors — treat as null
const rawQual = getText(tds[6]);
const quality = rawQual || null;
sensors.push({
sensor_id: rawId,
cp_address: getText(tds[1]),
sensor_name: getText(tds[2]),
serial_number:getText(tds[3]),
new_offset: parseFloatOrNull(tds[4]?.textContent),
access_point: getText(tds[5]),
quality,
status: getText(tds[7]),
sensor_type: normalizeType(getText(tds[8])),
zone: getText(tds[9]),
calibrated_at,
server,
});
}
return sensors;
}
/* ═══════════════════════════════════════════════════════
HANDLER 4 — Live View
Captures the latest visible sensor health/status and radio
quality without submitting temperature readings.
════════════════════════════════════════════════════════ */
function handleLiveView() {
const server = getServer();
if (!server) return;
const cooldownKey = `liveview_sync_${server}`;
if (isCoolingDown(cooldownKey)) {
console.log(`Live View cooldown active for server ${server}`);
return;
}
const sensors = scrapeLiveView(server);
if (!sensors.length) return;
postBatch(sensors, (count) => {
setCooldown(cooldownKey);
showBanner(`✓ Synced ${count} Live View sensor statuses to dashboard`, '#1a6e2e');
});
}
function scrapeLiveView(server) {
if (threePoint0) return scrapeLiveViewGuardian3(server);
if (arms) return scrapeLiveViewArms(server);
return scrapeLiveViewGuardian2(server);
}
function scrapeLiveViewGuardian3(server) {
const sensors = [];
for (const sensorCard of document.querySelectorAll('.sensordata .lvsensor')) {
const nameElement = sensorCard.querySelector('.slnk, .slnk-title');
if (isExcludedLiveViewSensor(nameElement?.textContent)) continue;
const sensorId = getLiveViewSensorId(nameElement);
if (!sensorId) continue;
const box = sensorCard.closest('.senbox') || sensorCard;
const zone = findPreviousZoneName(box);
sensors.push(buildLiveViewRecord({
sensorId,
sensorName: nameElement?.textContent,
sensorCard,
reading: sensorCard.querySelector('.u'),
statusContainer: box,
zone,
server,
}));
}
return sensors;
}
function scrapeLiveViewGuardian2(server) {
const sensors = [];
for (const sensorCard of document.querySelectorAll('td.senbox')) {
const nameElement = sensorCard.querySelector('.slnk');
if (isExcludedLiveViewSensor(nameElement?.textContent)) continue;
const sensorId = getLiveViewSensorId(nameElement);
if (!sensorId) continue;
const zoneTable = sensorCard.closest('table.zone');
const zone = zoneTable?.querySelector('.zonename a')?.textContent
|| zoneTable?.querySelector('a[title*="show only"]')?.textContent
|| zoneTable?.querySelector('.zonename')?.textContent;
const readCell = sensorCard.querySelector('.sencell');
sensors.push(buildLiveViewRecord({
sensorId,
sensorName: nameElement?.textContent,
sensorCard,
reading: readCell?.querySelector('.read, .val'),
statusContainer: readCell || sensorCard,
zone,
server,
}));
}
return sensors;
}
function scrapeLiveViewArms(server) {
const sensors = [];
for (const nameElement of document.querySelectorAll('a.noul[href*="sensordetail.php"]')) {
if (isExcludedLiveViewSensor(nameElement.textContent)) continue;
const sensorId = nameElement.href.match(/[?&]id=(\d+)/i)?.[1];
if (!sensorId) continue;
const sensorRow = nameElement.closest('tr');
const readingCell = document.getElementById(`o_${sensorId}`);
if (!sensorRow || !readingCell) continue;
sensors.push(buildLiveViewRecord({
sensorId,
sensorName: nameElement.textContent,
sensorCard: sensorRow,
reading: readingCell,
statusContainer: readingCell,
server,
}));
}
return sensors;
}
function buildLiveViewRecord({