-
Notifications
You must be signed in to change notification settings - Fork 3
/
AdScanerTrg2.html
848 lines (809 loc) · 30.3 KB
/
AdScanerTrg2.html
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
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AdScanerTrg v2.2</title>
</head>
<script type="text/javascript">
var cfg = {tmp_thr:0,tmp_hst:0,hm_thr:0,hm_hst:0,hm_thr:0,hm_hst:0,m_tim:0,flg:0};
var info = {temp:0,humi:0,lumi:0,mtimer:0,motion:0,jlight:0,flg:0};
var sw_version;
var bluetoothDevice;
var devServer;
var advCharacteristic;
var cfgCharacteristic;
var notify_on = 0;
var $ = function(id) { return document.getElementById(id);}
function clearLog() {
$("log").innerHTML = "";
}
function onButtonClick() {
$('butConnect').disabled = true;
return (bluetoothDevice ? Promise.resolve() : requestDevice())
.then(connectDeviceAndCacheCharacteristics)
.catch(error => {
log('Argh! ' + error);
$('butConnect').disabled = false;
});
}
function requestDevice() {
log('Requesting any Bluetooth Device...');
return navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
filters: [{namePrefix: 'BLE'}],
optionalServices: [0xffe0,0x1f10,0x180a]})
.then(device => {
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
});
}
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return new Uint8Array(bytes);
}
function bytesToHex(data) {
return new Uint8Array(data).reduce(function(memo, i) {
return memo + ("0" + i.toString(16)).slice(-2);
}, "");
}
function dump(ar, len) {
let s = '';
for(let i=0; i < len; i++) {
s += hex(ar[i],2);
}
return s;
}
function dump8(ar, len) {
let s = '';
for(let i=0; i < len; i++) {
s += hex(ar.getUint8(i),2);
}
return s;
}
function updateCfg() {
cfg.tmp_thr = Math.round(100.0 * parseFloat($("cfg_tmp_thr").value));
cfg.tmp_hst = Math.round(100.0 * parseFloat($("cfg_tmp_hst").value));
cfg.hm_thr = Math.round(100.0 * parseFloat($("cfg_hm_thr").value));
cfg.hm_hst = Math.round(100.0 * parseFloat($("cfg_hm_hst").value));
cfg.lm_thr = Math.round(parseFloat($("cfg_lm_thr").value));
cfg.lm_hst = Math.round(parseFloat($("cfg_lm_hst").value));
cfg.m_tim = Math.round(parseFloat($("cfg_m_tim").value));
cfg.flg = $("cfg_m_low").checked & 1;
}
function showConfig() {
$("cfg_tmp_thr").value = cfg.tmp_thr.toFixed(2);
$("cfg_tmp_hst").value = cfg.tmp_hst.toFixed(2);
$("cfg_hm_thr").value = cfg.hm_thr.toFixed(2);
$("cfg_hm_hst").value = cfg.hm_hst.toFixed(2);
$("cfg_lm_thr").value = cfg.lm_thr.toFixed(0);
$("cfg_lm_hst").value = cfg.lm_hst.toFixed(0);
$("cfg_m_tim").value = cfg.m_tim.toFixed(0);
$("cfg_m_low").checked = cfg.flg & 1;
}
function showInfo() {
$("cur_temp").innerHTML = "Current Temperature: "+((info.temp > 0.0)? "+":"")+info.temp.toFixed(2)+"C°, Trigger: "+ ((info.flg&8)?"On":"Off")+", OUT_TH: "+((info.flg&2)?"On":"Off");
$("cur_humi").innerHTML = "Current Humidity: "+info.humi.toFixed(2)+"%, Trigger: "+ ((info.flg&16)?"On":"Off") + ", OUT_TH: "+((info.flg&2)?"On":"Off");
$("cur_lumi").innerHTML = "Current Illumination: "+info.lumi.toFixed(0)+ ", Trigger: "+ ((info.flg&32)?"On":"Off")+ ", OUT_LM: "+((info.flg&4)?"On":"Off");
$("cur_mtim").innerHTML = "Current Timer: "+info.mtimer.toFixed(0);
$("cur_outs").innerHTML = "OUT_RS: "+((info.flg&1)?"On":"Off");
}
function getCfg() {
if(cfgCharacteristic) {
cfgCharacteristic.writeValue(new Uint8Array([0x55])).then(_ => {
log('Read Config');
/* cfgCharacteristic.writeValue(new Uint8Array([0x40])).then(_ => {
cfgCharacteristic.writeValue(new Uint8Array([0x41])).then(_ => {
log('Read Config...');
})
})*/
})
}
}
function setCfg() {
if(cfgCharacteristic) {
updateCfg();
cfgCharacteristic.writeValue(new Uint8Array([0x55,
cfg.tmp_thr&0xff, (cfg.tmp_thr>>8)&0xff,
cfg.tmp_hst&0xff, (cfg.tmp_hst>>8)&0xff,
cfg.hm_thr&0xff, (cfg.hm_thr>>8)&0xff,
cfg.hm_hst&0xff, (cfg.hm_hst>>8)&0xff,
cfg.lm_thr&0xff, (cfg.lm_thr>>8)&0xff,
cfg.lm_hst&0xff, (cfg.lm_hst>>8)&0xff,
cfg.m_tim&0xff, (cfg.m_tim>>8)&0xff,
cfg.flg&0xff])).then(_ => {
log('Set Config');
});
}
}
function getInfo() {
if(cfgCharacteristic)
cfgCharacteristic.writeValue(new Uint8Array([0x00]));
}
function readMAC(mid) {
if(cfgCharacteristic) {
cfgCharacteristic.writeValue(new Uint8Array([0x40+mid])).then(_ => {
log('Read MAC'+(mid+1));
});
}
}
function readBkey(mid) {
if(cfgCharacteristic) {
cfgCharacteristic.writeValue(new Uint8Array([0x18+mid])).then(_ => {
log('Read BindKey'+(mid+1));
});
}
}
function sendMAC(mid) {
if(cfgCharacteristic) {
let el = $("dev_mac"+(mid+1)).value;
let len = el.length;
if(len == 12) {
let mac = hexToBytes(el);
len = mac.length;
if(len == 6) {
cfgCharacteristic.writeValue(new Uint8Array([0x40+mid, mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]])).then(_ => {
log('Set MAC'+(mid+1)+': '+dump(mac,6));
});
} else
log("Must be 6 hex MAC digits!")
} else
log("Must be 6 hex MAC digits!")
return;
}
}
// 522a79d97580579dcc5854135d6c01b4
function sendBkey(mid) {
let bk = $("cbind_key"+(mid+1)).value;
if(bk.length == 32) {
let bkey = hexToBytes(bk);
if(bkey.length == 16) {
let blk = new Uint8Array(17);
blk.set(bkey,1);
blk[0] = 0x18 + mid;
log("Send bindkey"+(mid+1)+"...");
cfgCharacteristic.writeValue(blk).then(_ => {
log("Set new bindkey"+(mid+1)+": " + dump(blk.slice(1), 16));
$("cbind_key"+(mid+1)).value = dump(blk.slice(1), 16);
});
return;
}
}
log("BindKey"+(mid+1)+" must be 16 hex digits!")
}
var filter_mac = null;
function StartNotify() {
if(advCharacteristic) {
if(!notify_on) {
advCharacteristic.startNotifications().then(_ => {
$('butNotify').value = "Stop Scaner";
let el = $("fil_mac").value;
let len = el.length;
if(len == 12) {
let mac = hexToBytes(el);
len = mac.length;
if(len == 6) {
filter_mac = new Uint8Array(6);
filter_mac[0] = mac[5];
filter_mac[1] = mac[4];
filter_mac[2] = mac[3];
filter_mac[3] = mac[2];
filter_mac[4] = mac[1];
filter_mac[5] = mac[0];
} else
filter_mac = null;
} else
filter_mac = null;
notify_on = 1;
log('Notifications started');})
} else {
$('butNotify').value = "Start Scaner";
advCharacteristic.stopNotifications().then(_ => {
notify_on = 0;
log('Notifications stoped');})
}
}
}
function getSwVersion() {
return new Promise((resolve, reject) => {
devServer.getPrimaryService(0x180a).then(service => {
log("Found Device Information Service");
return service.getCharacteristic(0x2a28); // Software Revision String
}).then(characteristic => {
log("Get Software Revision String");
return characteristic.readValue();
}).then(value => {
let hw = new TextDecoder("utf-8").decode(value);
log('Softwaree Revision String: ' + hw);
return resolve(hw);
}).catch(error => {
log("No Software Revision String.");
console.log(error);
return resolve(null);
});
})
}
function connectDeviceAndCacheCharacteristics() {
if (bluetoothDevice.gatt.connected) {
$('butConnect').disabled = false;
return Promise.resolve();
}
log('Connecting to GATT Server...');
return bluetoothDevice.gatt.connect()
.then(server => {
devServer = server;
return getSwVersion()})
.then(value => {
if(value != null && value.length > 3 && value[0] == 'V' && value[2] == '.') sw_version = Math.round(parseFloat(value.slice(1)) * 10);
else sw_version = 0;
log('Getting ADV Service...');
return devServer.getPrimaryService(0x1f10);
})
.then(service => {
log('Getting ADV Characteristic...');
return service.getCharacteristic(0x1f11);
})
.then(characteristic => {
advCharacteristic = characteristic;
advCharacteristic.addEventListener('characteristicvaluechanged', advhandleChanged);
log('Getting CFG Service...');
return devServer.getPrimaryService(0xffe0);
})
.then(service => {
log('Getting CFG Characteristic...');
return service.getCharacteristic(0xffe1);
})
.then(characteristic => {
cfgCharacteristic = characteristic;
log('Start Notify CFG Characteristic...');
cfgCharacteristic.addEventListener('characteristicvaluechanged', cfghandleChanged);
return cfgCharacteristic.startNotifications();
})
.then(_ => {
log('Read Config...');
return cfgCharacteristic.writeValue(new Uint8Array([0x40]))})
.then(_ => {
return cfgCharacteristic.writeValue(new Uint8Array([0x41]))})
.then(_ => {
return cfgCharacteristic.writeValue(new Uint8Array([0x55]))})
.then(_ => {
return cfgCharacteristic.writeValue(new Uint8Array([0x00]))})
.then(_ => {
if(sw_version >= 20)
return cfgCharacteristic.writeValue(new Uint8Array([0x18]))
.then(_ => { return cfgCharacteristic.writeValue(new Uint8Array([0x19]))});
else return;
})
.then(_ => {
$('butSendMAC1').disabled = false;
$('butReadMAC1').disabled = false;
$('butSendMAC2').disabled = false;
$('butReadMAC2').disabled = false;
if(sw_version >= 20) {
$('butReadBkey1').disabled = false;
$('butReadBkey2').disabled = false;
$('butSendBkey1').disabled = false;
$('butSendBkey2').disabled = false; }
$('butSetCfg').disabled = false;
$('butGetCfg').disabled = false;
$('butNotify').disabled = false;
$('butConnect').value = 'Disconnect';
$('butConnect').onclick = function(){onResetButtonClick()};
$('butConnect').disabled = false;
log('BLE Device Connected');
})
.catch(error => {
log('Argh! ' + error);
$('butConnect').disabled = false;
});
}
function cfghandleChanged(event) {
let value = event.target.value.buffer ? event.target.value : new DataView(event.target.value);
let len = value.byteLength;
console.log("blk:"+dump8(value,len));
if(len == 0) return;
let blkid = value.getUint8(0);
if(blkid == 0x55 && len > 15) {
cfg.tmp_thr = value.getInt16(1, true) / 100.0; // temp threshold
cfg.tmp_hst = value.getInt16(3, true) / 100.0; // temp hysteresis
cfg.hm_thr = value.getInt16(5, true) / 100.0; // humi threshold
cfg.hm_hst = value.getInt16(7, true) / 100.0; // humi hysteresis
cfg.lm_thr = value.getInt16(9, true); // illu threshold
cfg.lm_hst = value.getInt16(11, true); // illu hysteresis
cfg.m_tim = value.getUint16(13, true);
cfg.flg = value.getUint8(15);
showConfig();
} else if(blkid == 0x00 && len > 11) {
info.temp = value.getInt16(1, true) / 100.0; // temp
info.humi = value.getInt16(3, true) / 100.0; // humi
info.lumi = value.getInt16(5, true); // light
info.mtimer = value.getUint16(7, true)
info.motion = value.getUint8(9);
info.jlight = value.getUint8(10);
info.flg = value.getUint8(11);
console.log("Info# Temp: "+info.temp.toFixed(2)+
", Humi: "+info.humi.toFixed(2)+
", Lumi: " +info.lumi.toFixed(0)+
", MTimer: " +info.mtimer+
", Motion: " +info.motion+
", JLight: "+info.jlight+
", Flg: 0x"+hex(info.flg,2)+
" # ReedSwitchOut: "+(info.flg&1)+
", TempHumiOut: "+((info.flg>>1)&1)+
", LightMotionOut: "+((info.flg>>2)&1)+
", TempEvent: "+((info.flg>>3)&1)+
", HumiEvent: "+((info.flg>>4)&1)+
", LightEvent: "+((info.flg>>5)&1));
showInfo();
} else if(((blkid == 0x18)||(blkid == 0x19)) && len >= 1) { // Get/set beacon bkey in EEP
let mid = blkid&1;
if(len > 16) {
let s = bytesToHex(value.buffer.slice(1),16);
log("Read bindkey"+(mid+1)+": "+ s);
//if($("cbind_key"+(mid+1)))
$("cbind_key"+(mid+1)).value = s;
} else {
if(len == 2 && value.getUint8(1) == 0xff)
log("No bindkey"+(mid+1)+" in EEP!");
else
log("Error read bindkey"+(mid+1)+" from EEP!");
// if($("cbind_key"+(mid+1)))
$("cbind_key"+(mid+1)).value = '?';
}
} else if(blkid == 0x40 && len > 6) {
$("dev_mac1").value = hex(value.getUint16(5,true),4) + hex(value.getUint16(3,true),4) + hex(value.getUint16(1,true),4);
} else if(blkid == 0x41 && len > 6) {
$("dev_mac2").value = hex(value.getUint16(5,true),4) + hex(value.getUint16(3,true),4) + hex(value.getUint16(1,true),4);
} else
log("blk?:"+dump8(value,len));
}
function advhandleChanged(event) {
let value = event.target.value.buffer ? event.target.value : new DataView(event.target.value);
if(value.byteLength > 11 && value.getUint8(8) == 0x16) {
// value.buffer: rssi, mac, adv msg
mac = new Uint8Array(value.buffer.slice(1,7));
if(filter_mac != null && !(filter_mac.every((v, i) => mac[i] == v))) {
return;
}
let s = hex(value.getUint16(5,true),4) + hex(value.getUint16(3,true),4) + hex(value.getUint16(1,true),4) + ', RSSI: ' + value.getInt8(0) + ',';
advDataView(s, value.getUint16(9,true), new DataView(event.target.value.buffer.slice(11)));
} // else log("Read?: "+dump8(value, value.byteLength));
}
function onResetButtonClick() {
if(bluetoothDevice && bluetoothDevice.gatt.connected) {
bluetoothDevice.gatt.disconnect();
log('BLE Device Disconnected');
}
bluetoothDevice = null;
advCharacteristic = null;
cfgCharacteristic = null;
devServer = null;
notify_on = 0;
$('butNotify').disabled = true;
$('butNotify').value = "Start Scaner";
$('butSendMAC1').disabled = true;
$('butReadMAC1').disabled = true;
$('butSendMAC2').disabled = true;
$('butReadMAC2').disabled = true;
$('butReadBkey1').disabled = true;
$('butReadBkey2').disabled = true;
$('butSendBkey1').disabled = true;
$('butSendBkey2').disabled = true;
$('butSetCfg').disabled = true;
$('butGetCfg').disabled = true;
$('butConnect').value = 'Connect';
$('butConnect').disabled = false;
$('butConnect').onclick = function(){onButtonClick()};
}
function onDisconnected() {
onResetButtonClick();
}
function log(data) {
var time = new Date().toLocaleTimeString();
var logString = time + ": " + data;
document.getElementById("log").innerHTML += logString + "<br>";
}
function hex(number, len) {
var str = (number.toString(16)).toUpperCase();
while (str.length < len) str = '0' + str;
return str;
}
const tabid2name = [
{ id: 0x01AA, name: "LYWSDCGQ"},
{ id: 0x045B, name: "LYWSD02"},
{ id: 0x055B, name: "LYWSD03MMC"},
{ id: 0x1203, name: "XMWSDJ04MMC"},
{ id: 0x04E1, name: "XMMF01JQD"},
{ id: 0x0098, name: "HHCCJCY01"},
{ id: 0x03BC, name: "GCLS002"},
{ id: 0x015D, name: "HHCCPOT002"},
{ id: 0x040A, name: "WX08ZM"},
{ id: 0x098B, name: "MCCGQ02HL"},
{ id: 0x0083, name: "YM-K1501"},
{ id: 0x0113, name: "YM-K1501EU"},
{ id: 0x045C, name: "V-SK152"},
{ id: 0x0863, name: "SJWS01LM"},
{ id: 0x07F6, name: "MJYD02YL"},
{ id: 0x03DD, name: "MUE4094RT"},
{ id: 0x0A8D, name: "RTCGQ02LM"},
{ id: 0x00DB, name: "MMC-T201-1"},
{ id: 0x0489, name: "M1S-T500"},
{ id: 0x0C3C, name: "CGC1"},
{ id: 0x0576, name: "CGD1"},
{ id: 0x066F, name: "CGDK2"},
{ id: 0x0347, name: "CGG1"},
{ id: 0x0B48, name: "CGG1-ENCRYPTED"},
{ id: 0x03D6, name: "CGH1"},
{ id: 0x0A83, name: "CGPR1"},
{ id: 0x06d3, name: "MHO-C303"},
{ id: 0x0387, name: "MHO-C401"},
{ id: 0x02DF, name: "JQJCY01YM"},
{ id: 0x0997, name: "JTYJGD03MI"},
{ id: 0x1568, name: "K9B-1BTN"},
{ id: 0x1569, name: "K9B-2BTN"},
{ id: 0x0DFD, name: "K9B-3BTN"},
{ id: 0x07BF, name: "YLAI003"},
{ id: 0x0153, name: "YLYK01YL"},
{ id: 0x068E, name: "YLYK01YL-FANCL"},
{ id: 0x04E6, name: "YLYK01YL-VENFAN"},
{ id: 0x03BF, name: "YLYB01YL-BHFRC"},
{ id: 0x03B6, name: "YLKG07YL/YLKG08YL"},
{ id: 0x069E, name: "ZNMS16LM"},
{ id: 0x069F, name: "ZNMS17LM"},
{ id: 0x04E9, name: "MJZNMSQ01YD"},
{ id: 0x098C, name: "XMZNMST02YD"}
];
function id2name(dev_id) {
let did = tabid2name.map(el => el.id);
let i = did.indexOf(dev_id);
if(i != -1) return tabid2name[i].name;
return "?";
}
const tabDoor2event = [
{ id: 0, name: "opened"},
{ id: 1, name: "closed"},
{ id: 2, name: "closing timeout"},
{ id: 3, name: "device reset"}
];
function Door2event(id) {
let did = tabDoor2event.map(el => el.id);
let i = did.indexOf(id);
if(i != -1) return tabDoor2event[i].name;
return "?";
}
const tabHaBle2name = [
{ id: 0, name: "PacketId", unit: "", factor: 1},
{ id: 1, name: "battery", unit: "%", factor: 1},
{ id: 2, name: "temperature", unit: "°C", factor: 0.01},
{ id: 3, name: "humidity", unit: "%", factor: 0.01},
{ id: 4, name: "pressure", unit: "hPa", factor: 0.01},
{ id: 5, name: "illuminance", unit: "lux", factor: 0.01},
{ id: 6, name: "weight", unit: "kg", factor: 0.01},
{ id: 7, name: "weight", unit: "kg", factor: 1},
{ id: 8, name: "dewpoint", unit: "°C", factor: 0.01},
{ id: 9, name: "count", unit: "", factor: 1},
{ id: 10, name: "energy", unit: "kWh", factor: 0.001},
{ id: 11, name: "power", unit: "W", factor: 0.01},
{ id: 12, name: "voltage", unit: "V", factor: 0.001},
{ id: 13, name: "pm2x5", unit: "kg/m3", factor: 1},
{ id: 14, name: "pm10", unit: "kg/m3", factor: 1},
{ id: 15, name: "boolean", unit: "", factor: 1},
{ id: 16, name: "switch", unit: "", factor: 1},
{ id: 17, name: "opened", unit: "", factor: 1}
];
const advDataView = (labelOfDataSource, key, b) => {
let si = '', h = '0123456789ABCDEF';
let buf = new Uint8Array(b.buffer);
buf.forEach((v) => { si += h[v >> 4] + h[v & 15]; });
if(key == 0xfe95) { // mi
if(b.byteLength >= 2) { // custom mi
let ctrl = b.getUint16(0, true);
let s = 'MiVer'+ (ctrl>>12);
let i = 2;
if((ctrl&0xf000) >= 0x2000 && b.byteLength >= i + 3) {
let dev_id = b.getUint16(i, true);
let frcntr = b.getUint8(i+2);
i += 3;
s += ', DevID: 0x'+ hex(dev_id, 4);
s += "-" + id2name(dev_id);
s += ', FnCnt: '+ frcntr;
s += ', CtrID: 0x'+ hex(ctrl, 4);
if(ctrl & 0x100)
s +=' Registered and bound';
else
s +=' Not bound';
if(ctrl & 0x200)
s +=', Request APP to register and bind';
switch((ctrl>>10)&3) {
case 0:
s +=', Old version certification';
break;
case 1:
s +=', Safety certification';
break;
case 2:
s +=', Standard certification';
break;
//case 3:
// s +=' reserved';
// break;
}
if(ctrl & 0x200)
s +=', Request APP to register and bind';
if((ctrl & 0x10) && b.byteLength >= i + 6) {
s += ', MAC: '+hex(buf[i+5],2)+hex(buf[i+4],2)+hex(buf[i+3],2)+hex(buf[i+2],2)+hex(buf[i+1],2)+hex(buf[i],2);
i += 6;
}
if((ctrl & 0x20) && b.byteLength >= i + 1) {
let cap = b.getUint8(i);
i += 1;
s +=', Capability: 0x'+ hex(cap,2);
switch((cap>>3) & 3) {
case 0:
s +=' - no binding';
break;
case 1:
s +=' - front binding';
break;
case 2:
s +=' - back binding';
break;
case 3:
s +=' - Combo';
break;
}
if(cap & 0x20 && b.byteLength >= i + 2) {
s +=', IO: 0x'+ hex(b.getUint16(i, true), 4);
i += 2;
}
}
if((ctrl & 0x40)) {
if(b.byteLength >= i + 4) {
if(ctrl & 8) {
s += ', Data encrypted';
} else {
let data_id = b.getUint16(i, true);
let data_len = b.getUint8(i+2);
i += 3;
if(b.byteLength >= i + data_len) switch(data_id) {
case 0x1006:
if(data_len == 2) {
let humi = b.getInt16(i, true) / 10.0;
s += ', Humi: '+humi+'%';
}
break;
case 0x1004:
if(data_len == 2) {
let temp = b.getInt16(i, true) / 10.0;
s += ', Temp: '+temp+'°C';
}
break;
case 0x100a:
if(data_len == 1) {
s += ', Bat: '+ b.getUint8(i) + '%';
if(b.byteLength >= i + 3 && b.getUint8(i+1) == 2) {
let Vbat = b.getUint16(i+2, true);
s += ', Vbat: '+ Vbat + ' mV';
i += 3;
}
}
break;
case 0x100d:
if(data_len == 4 && b.byteLength >= i + 3) {
let temp = b.getInt16(i, true) / 10.0;
let humi = b.getInt16(i+2, true) / 10.0;
s += ', Temp: '+temp+'°C Humi: '+humi+'%';
}
break;
case 0x1019:
if(data_len == 1) {
s += ', Door: '+ Door2event(b.getUint8(i));
}
break;
default:
s += ', DataID: 0x'+hex(data_id,4);
let data_value = 0;
if(data_len == 1)
s += ' Value: 0x'+hex(b.getUint8(i),2);
else if(data_len == 2)
s += ' Value: 0x'+hex(b.getUint16(i, true),4);
else if(data_len == 4)
s += ' Value: 0x'+hex(b.getUint32(i, true), 8);
else
s += ' DataLen: ' + data_len;
break;
}
i += data_len;
}
}
}
if(ctrl & 0x80)
s +=', Mesh Data';
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' ' + s);
}
}
} else if(key == 0x181a) {
if(b.byteLength == 15) { // pvvx
// 70 58 56 38 C1 A4 69 0A 60 09 25 0B 48 70 04
// 27 DB 64 38 C1 A4 22 08 10 0B 75 0B 51 FE 0F
let temp = b.getInt16(6, true) / 100.0;
let humi = b.getInt16(8, true) / 100.0;
let vbat = b.getUint16(10, true);
let bat = b.getUint8(12);
let cnt = b.getUint8(13);
let flg = b.getUint8(14);
let s = 'MAC: '+hex(buf[5],2)+hex(buf[4],2)+hex(buf[3],2)+hex(buf[2],2)+hex(buf[1],2)+hex(buf[0],2);
s += ', Bat: '+bat+'%, Vbat: '+vbat+' mV , Temp: '+temp+'°C, Humi: '+humi+'%, MsgId: '+cnt+', Flg: 0x'+hex(flg,2)+' rds('+(flg&1)+')/trg('+((flg>>1)&1)+')';
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' ' + s);}
else if(b.byteLength == 13) { // atc1441
// A4 C1 38 56 58 70 01 0A 18 48 0B 25 70
let temp = b.getInt16(6, false) / 10.0;
let humi = b.getUint8(8);
let bat = b.getUint8(9);
let vbat = b.getUint16(10, false);
let cnt = b.getUint8(12);
let s = 'MAC: '+hex(buf[0],2)+hex(buf[1],2)+hex(buf[2],2)+hex(buf[3],2)+hex(buf[4],2)+hex(buf[5],2);
s += ', Bat: '+bat+'%, Vbat: '+vbat+' mV, Temp: '+temp+'°C, Humi: '+humi+'%, MsgId: '+cnt;
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' ' + s);}
else if(b.byteLength == 11) { // pvvx encrypted
// 35 B746 94BF 96 90 EF1ACFA9
// 3D B14D 6358 E9 D9 163D67A4
// 53 EA40 87F6 66 C1 8822D997
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' Custom, Data encrypted');}
else if(b.byteLength == 8) { // atc encrypted
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' ATC, Data encrypted');}
else
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' Unknown');
} else if((key == 0xfdcd)||(key == 0xfff9)) { // Qingping / ClearGrass
// CGG1 "Goose": 0801 AE1610342D58 0104 1401 4601 0201 58
// CGPR1 0812 005E60342D58 0201 64 0F01 72 0904 08000000
// 0812 005E60342D58 0201 64 0F01 6E 0904 91140000
// 4812 005E60342D58 0804 01 911400 0F01 6F
// 4812 005E60342D58 1101 01 0F01 85
// 4812 005E60342D58 1101 00 0F01 8C
let msg_length = b.byteLength;
if(msg_length >= 11 && (b.getUint8(0)&0x3f) == 0x08) {
let device_id = b.getUint8(1);
let s = 'MAC: '+hex(buf[2],2)+hex(buf[3],2)+hex(buf[4],2)+hex(buf[5],2)+hex(buf[6],2)+hex(buf[7],2);
if(device_id == 0x01) s+=' CGG1 "Goose"';
else if (device_id == 0x07) s+=' CGG1';
else if (device_id == 0x09) s+=' CGP1W';
else if (device_id == 0x0C) s+=' CGD1';
else if (device_id == 0x12) s+=' CGPR1';
//if((b.getUint8(0)&0x40)!=0) s+=', Event';
let xdata_point = 10;
while (xdata_point < msg_length) {
let xdata_id = b.getUint8(xdata_point-2);
let xdata_size = b.getUint8(xdata_point-1);
if(xdata_point + xdata_size <= msg_length) {
if((xdata_id == 0x01)&&(xdata_size == 4)) {
let temp = b.getInt16(xdata_point, true) / 10.0;
let humi = b.getInt16(xdata_point+2, true) / 10.0;
s += ', Temp: '+temp+'°C, Humi: '+humi+'%';
} else if((xdata_id == 0x02)&&(xdata_size == 1)) {
s += ', Bat: '+b.getUint8(xdata_point)+'%';
} else if((xdata_id == 0x07)&&(xdata_size == 2)) {
s += ', Pressure: '+(b.getInt16(xdata_point, true) / 100.0);
} else if((xdata_id == 0x08)&&(xdata_size == 4)) {
s += ', Motion: '+ b.getUint8(xdata_point)+' & Light: '+(b.getUint16(xdata_point+1, true)+b.getUint8(xdata_point+3)*256);
} else if((xdata_id == 0x09)&&(xdata_size == 4)) {
s += ', Light: '+(b.getUint32(xdata_point, true));
} else if((xdata_id == 0x0F)&&(xdata_size == 1)) {
s += ', Count: '+b.getUint8(xdata_point);
} else if((xdata_id == 0x11)&&(xdata_size == 1)) {
if(b.getUint8(xdata_point) == 0) s += ', Light Off';
else s += ', Light On';
} else if (xdata_size == 1) {
s += ', id: 0x'+hex(xdata_id,2)+' - data: 0x'+hex(buf[xdata_point],2);
} else if (xdata_size == 2) {
s += ', id: 0x'+hex(xdata_id,2)+' - data: 0x'+hex(buf[xdata_point],2)+hex(buf[xdata_point+1],2);
} else if (xdata_size == 4) {
s += ', id: 0x'+hex(xdata_id,2)+' - data: 0x'+hex(buf[xdata_point],2)+hex(buf[xdata_point+1],2)+hex(buf[xdata_point+2],2)+hex(buf[xdata_point+3],2);
}
} else break;
xdata_point = xdata_point + xdata_size + 2;
}
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' ' + s);
}
} else if(key == 0x2aeb) { // Count24
if(b.byteLength > 2) {
let cnt = (b.getUint16(0, false)<<8) | b.getUint8(2);
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' Count 24 bits: ' + cnt);
}
} else if(key == 0x181e) { // HA BLE
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' HA-BLE, Data encrypted');
} else if(key == 0x181c) { // HA BLE
if(b.byteLength > 1) {
let s = ' HA-BLE';
let len = b.byteLength;
let i = 0;
while(i < len) {
let sl = b.getUint8(i)&31; // size
if(i+sl+1 > len) break;
let st = b.getUint8(i)>>5;
if(sl > 0) {
let dd = b.getUint8(i+1);
let value = 0;
let did = tabHaBle2name.map(el => el.id);
let e = did.indexOf(dd);
let f = 1;
if(e != -1) f = tabHaBle2name[e].factor;
if(st == 0) {
if(sl == 2)
value = b.getUint8(i+2);
else if(sl == 3)
value = b.getUint16(i+2, true);
else if(sl == 4)
value = b.getUint16(i+2, true) + (b.getUint8(i+4)<<16);
else if(sl == 5)
value = b.getUint32(i+2, true);
if(f != 1) value = (value*f).toFixed(3);
} else if(st == 1) {
if(sl == 2)
value = b.getInt8(i+2);
else if(sl == 3)
value = b.getInt16(i+2, true);
else if(sl == 4) {
value = b.getUnt16(i+2, true) + (b.getUint8(i+4)<<16);
if(value >= 0x800000)
value = 0x1000000 - value;
} else if(sl == 5)
value = b.getInt32(i+2, true);
if(f != 1) value = (value*f).toFixed(3);
} else
value = "?";
if(e != -1) {
s += ', '+tabHaBle2name[e].name+': '+value+tabHaBle2name[e].unit;
} else
s += ', '+st+'?: '+value;
}
i += sl+1;
}
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + s);
}
} else
log(` ${labelOfDataSource} Data: ` + hex(key,2) + ':' + si + ' ?');
};
</script>
<body>
<big><big>AdScanerTrg</big></big> <a href="https://github.com/pvvx/AdScanerTrg">ⓘ</a><br>
Web Experimental Features!<br>
#enable-experimental-web-platform-features (copy link:
<a href=chrome://flags/#enable-experimental-web-platform-features>Chrome</a>,
<a href=opera://flags/#enable-experimental-web-platform-features>Opera</a>,
<a href=edge://flags/#enable-experimental-web-platform-features>Edge</a>)<br><hr>
<input type="button" id="butConnect" onclick="onButtonClick()" value="Connect"><br><hr>
SensorDevice1 MAC: <input size="12" maxlength="12" type="text" id="dev_mac1" title="Must be 6 hex MAC digits!">
<input type="button" id="butReadMAC1" onclick="readMAC(0)" disabled="true" value="Read">
<input type="button" id="butSendMAC1" onclick="sendMAC(0)" disabled="true" value="Set"><br>
BindKey1:<br><input size="40" maxlength="32" title="Bind Key must be 16 hex digits" type="text" id="cbind_key1" value="?">
<input type="button" id="butReadBkey1" onclick="readBkey(0)" disabled="true" value="Read">
<input type="button" id="butSendBkey1" onclick="sendBkey(0)" disabled="true" value="Set"><br><br>
SensorDevice2 MAC: <input size="12" maxlength="12" type="text" id="dev_mac2" title="Must be 6 hex MAC digits!">
<input type="button" id="butReadMAC2" onclick="readMAC(1)" disabled="true" value="Read">
<input type="button" id="butSendMAC2" onclick="sendMAC(1)" disabled="true" value="Set"><br>
BindKey2:<br><input size="40" maxlength="32" title="Bind Key must be 16 hex digits" type="text" id="cbind_key2" value="?">
<input type="button" id="butReadBkey2" onclick="readBkey(1)" disabled="true" value="Read">
<input type="button" id="butSendBkey2" onclick="sendBkey(1)" disabled="true" value="Set"><br>
<hr><div id="cur_temp">Current Temperature: ?</div>
Temperature threshold: <input size="8" title="-40.00..80.00°C, default 21.00°C" id="cfg_tmp_thr" maxlength="6" value="21.00"> °<br>
Temperature hysteresis: <input size="8" title="Step 0.01°, default -0.55°, =0 off, if less than zero - activation on decrease, if more than zero - activation on excess" id="cfg_tmp_hst" maxlength="7" value="-0.55"> °<br><br>
<div id="cur_humi">Current Humidity: ?</div>
Humidity threshold: <input size="8" title="0..99.00%, default 50.00%" id="cfg_hm_thr" maxlength="6" value="50.00"> %<br>
Humidity hysteresis: <input size="8" title="Step 0.01%, default 0, =0 off, if less than zero - activation on decrease, if more than zero - activation on excess" id="cfg_hm_hst" maxlength="7" value="0.0"> %<br><br>
<div id="cur_lumi">Current Illumination: ?</div>
Illumination threshold: <input size="8" title="0..20000, default 500" id="cfg_lm_thr" maxlength="6" value="500"> <br>
Illumination hysteresis: <input size="8" title="Step 1, default 0, =0 off, if less than zero - activation on decrease, if more than zero - activation on excess" id="cfg_lm_hst" maxlength="7" value="0"><br><br>
<div id="cur_mtim">Current Timer: ?</div>
Motion Timer: <input size="8" title="0..65535 sec, Step 1 sec, default 0, =0 off, if more than zero - activation on excess" id="cfg_m_tim" maxlength="5" value="0"> sec<br>
Weak signal activation<input type="checkbox" id="cfg_m_low"><br><br>
<div id="cur_outs">OUT_RG ?</div><br>
<input type="button" id="butGetCfg" disabled="true" onclick="getCfg()" value="Get Config">
<input type="button" id="butSetCfg" disabled="true" onclick="setCfg()" value="Set Config"><br>
<!-- <input type="button" id="butGetInfo" disabled="true" onclick="getInfo()" value="Get Info"><br> -->
<hr>
<button type="button" onclick="clearLog();">Clear Log</button> <input type="button" id="butNotify" onclick="StartNotify()" disabled="true" value="Start Scaner">
Filter MAC: <input size="12" maxlength="12" type="text" id="fil_mac" title="Must be 6 hex MAC digits!">
<div id="log"></div>
</body>
</html>