-
Notifications
You must be signed in to change notification settings - Fork 24
/
switchbot-advertising.js
634 lines (571 loc) · 18.5 KB
/
switchbot-advertising.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
"use strict";
class SwitchbotAdvertising {
constructor() {}
/* ------------------------------------------------------------------
* parse(peripheral)
* - Parse advertising packets coming from switchbot devices
*
* [Arguments]
* - peripheral | Object | Required | A `Peripheral` object of noble
*
* [Return value]
* - An object as follows:
*
* WoHand
* {
* id: 'c12e453e2008',
* address: 'c1:2e:45:3e:20:08',
* rssi: -43,
* serviceData: {
* model: 'H',
* modelName: 'WoHand',
* mode: false,
* state: false,
* battery: 95
* }
* }
*
* WoSensorTH
* {
* id: 'cb4eb903c96d',
* address: 'cb:4e:b9:03:c9:6d',
* rssi: -54,
* serviceData: {
* model: 'T',
* modelName: 'WoSensorTH',
* temperature: { c: 26.2, f: 79.2 },
* fahrenheit: false,
* humidity: 45,
* battery: 100
* }
* }
*
* WoCurtain
* {
* id: 'ec58c5d00111',
* address: 'ec:58:c5:d0:01:11',
* rssi: -39,
* serviceData: {
* model: 'c',
* modelName: 'WoCurtain',
* calibration: true,
* battery: 91,
* position: 1,
* lightLevel: 1
* }
* }
*
* If the specified `Peripheral` does not represent any switchbot
* device, this method will return `null`.
* ---------------------------------------------------------------- */
parse(peripheral, onlog) {
let ad = peripheral.advertisement;
if (!ad || !ad.serviceData) {
return null;
}
let serviceData = ad.serviceData[0] || ad.serviceData;
let manufacturerData = ad.manufacturerData;
let buf = serviceData.data;
let bufIsInvalid = !buf || !Buffer.isBuffer(buf) || buf.length < 3;
let manufacturerDataIsInvalid =
!manufacturerData ||
!Buffer.isBuffer(manufacturerData) ||
manufacturerData.length < 3;
if (bufIsInvalid || manufacturerDataIsInvalid) {
return null;
}
let model = buf.slice(0, 1).toString("utf8");
let sd = null;
if (model === "H") {
sd = this._parseServiceDataForWoHand(buf, onlog);//WoHand
} else if (model === "T") {
sd = this._parseServiceDataForWoSensorTH(buf, onlog);//WoSensorTH
} else if (model === "e") {
sd = this._parseServiceDataForWoHumi(buf, onlog);//WoHumi
} else if (model === "s") {
sd = this._parseServiceDataForWoPresence(buf, onlog);//WoPresence
} else if (model === "d") {
sd = this._parseServiceDataForWoContact(buf, onlog);//WoContact
} else if (model === "c") {
sd = this._parseServiceDataForWoCurtain(buf, onlog);// WoCurtain
} else if (model === "u") {
sd = this._parseServiceDataForWoBulb(manufacturerData, onlog);// WoBulb
} else if (model === "g") {
sd = this._parseServiceDataForWoPlugMiniUS(manufacturerData, onlog); // WoPlugMini (US)
} else if (model === "j") {
sd = this._parseServiceDataForWoPlugMiniJP(manufacturerData, onlog);// WoPlugMini (JP)
} else if (model === "o") {
sd = this._parseServiceDataForWoSmartLock(manufacturerData, onlog);// WoSmartLock
} else if (model === "i") {
sd = this._parseServiceDataForWoSensorTHPlus(buf, onlog);// WoMeterPlus
} else if (model === "r") {
sd = this._parseServiceDataForWoStrip(buf, onlog);// WoStrip
} else {
if (onlog && typeof onlog === "function") {
onlog(
`[parseAdvertising.${peripheral.id}] return null, model "${model}" not available!`
);
}
return null;
}
if (!sd) {
if (onlog && typeof onlog === "function") {
onlog(
`[parseAdvertising.${peripheral.id}.${model}] return null, parsed serviceData empty!`
);
}
return null;
}
let address = peripheral.address || "";
if (address === "") {
address = peripheral.advertisement.manufacturerData || "";
if (address !== "") {
let str = peripheral.advertisement.manufacturerData
.toString("hex")
.slice(4, 16);
address = str.substr(0, 2);
for (var i = 2; i < str.length; i += 2) {
address = address + ":" + str.substr(i, 2);
}
// console.log("address", typeof(address), address);
}
} else {
address = address.replace(/-/g, ":");
}
let data = {
id: peripheral.id,
address: address,
rssi: peripheral.rssi,
serviceData: sd,
};
if (onlog && typeof onlog === "function") {
onlog(
`[parseAdvertising.${peripheral.id}.${model}] return ${JSON.stringify(
data
)}`
);
}
return data;
}
_parseServiceDataForWoHand(buf, onlog) {
if (buf.length !== 3) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoHand] Buffer length ${buf.length} !== 3!`
);
}
return null;
}
let byte1 = buf.readUInt8(1);
let byte2 = buf.readUInt8(2);
let mode = byte1 & 0b10000000 ? true : false; // Whether the light switch Add-on is used or not
let state = byte1 & 0b01000000 ? true : false; // Whether the switch status is ON or OFF
let battery = byte2 & 0b01111111; // %
let data = {
model: "H",
modelName: "WoHand",
mode: mode,
state: state,
battery: battery,
};
return data;
}
_parseServiceDataForWoSensorTH(buf, onlog) {
if (buf.length !== 6) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoSensorTH] Buffer length ${buf.length} !== 6!`
);
}
return null;
}
let byte2 = buf.readUInt8(2);
let byte3 = buf.readUInt8(3);
let byte4 = buf.readUInt8(4);
let byte5 = buf.readUInt8(5);
let temp_sign = byte4 & 0b10000000 ? 1 : -1;
let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
let temp_f = (temp_c * 9 / 5) + 32;
temp_f = Math.round(temp_f * 10) / 10;
let data = {
model: "T",
modelName: "WoSensorTH",
temperature: {
c: temp_c,
f: temp_f,
},
fahrenheit: byte5 & 0b10000000 ? true : false,
humidity: byte5 & 0b01111111,
battery: byte2 & 0b01111111,
};
return data;
}
_parseServiceDataForWoHumi(buf, onlog) {
if (buf.length !== 8) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoHumi] Buffer length ${buf.length} !== 8!`
);
}
return null;
}
let byte1 = buf.readUInt8(1);
let byte4 = buf.readUInt8(4);
let onState = byte1 & 0b10000000 ? true : false; // 1 - on
let autoMode = byte4 & 0b10000000 ? true : false; // 1 - auto
let percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3
let data = {
model: "e",
modelName: "WoHumi",
onState: onState,
autoMode: autoMode,
percentage: autoMode ? 0 : percentage,
};
return data;
}
_parseServiceDataForWoPresence(buf, onlog) {
if (buf.length !== 6) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoPresence] Buffer length ${buf.length} !== 6!`
);
}
return null;
}
let byte1 = buf.readUInt8(1);
let byte2 = buf.readUInt8(2);
let byte5 = buf.readUInt8(5);
let tested = byte1 & 0b10000000 ? true : false;
let movement = byte1 & 0b01000000 ? true : false;
let battery = byte2 & 0b01111111;
let led = (byte5 & 0b00100000) >> 5;
let iot = (byte5 & 0b00010000) >> 4;
let sense_distance = (byte5 & 0b00001100) >> 2;
let lightLevel = byte5 & 0b00000011;
let is_light = byte5 & 0b00000010 ? true : false;
let data = {
model: "s",
modelName: "WoMotion",
tested: tested,
movement: movement,
battery: battery,
led: led,
iot: iot,
sense_distance: sense_distance,
lightLevel:
lightLevel == 1 ? "dark" : lightLevel == 2 ? "bright" : "unknown",
is_light: is_light,
};
return data;
}
_parseServiceDataForWoContact(buf, onlog) {
if (buf.length !== 9) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoContact] Buffer length ${buf.length} !== 9!`
);
}
return null;
}
let byte1 = buf.readUInt8(1);
let byte2 = buf.readUInt8(2);
let byte3 = buf.readUInt8(3);
let byte8 = buf.readUInt8(8);
let hallState = (byte3 >> 1) & 0b00000011;
let tested = byte1 & 0b10000000;
let movement = byte1 & 0b01000000 ? true : false; // 1 - Movement detected
let battery = byte2 & 0b01111111; // %
let contact_open = byte3 & 0b00000010 == 0b00000010;
let contact_timeout = byte3 & 0b00000100 == 0b00000100;
let lightLevel = byte3 & 0b00000001;
let button_count = byte8 & 0b00001111;
let data = {
model: "d",
modelName: "WoContact",
movement: movement,
tested: tested,
battery: battery,
contact_open: contact_open,
contact_timeout: contact_timeout,
lightLevel: lightLevel == 0 ? "dark" : "bright",
button_count: button_count,
doorState:
hallState == 0
? "close"
: hallState == 1
? "open"
: "timeout no closed",
};
return data;
}
_parseServiceDataForWoCurtain(buf, onlog) {
if (buf.length !== 5 && buf.length !== 6) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 5 or 6!`
);
}
return null;
}
let byte1 = buf.readUInt8(1);
let byte2 = buf.readUInt8(2);
let byte3 = buf.readUInt8(3);
let byte4 = buf.readUInt8(4);
let calibration = byte1 & 0b01000000 ? true : false; // Whether the calibration is completed
let battery = byte2 & 0b01111111; // %
let inMotion = byte3 & 0b10000000 ? true : false;
let currPosition = byte3 & 0b01111111; // current positon %
let lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10)
let deviceChain = byte4 & 0b00000111;
let data = {
model: "c",
modelName: "WoCurtain",
calibration: calibration,
battery: battery,
inMotion: inMotion,
position: currPosition,
lightLevel: lightLevel,
deviceChain: deviceChain,
};
return data;
}
_parseServiceDataForWoBulb(manufacturerData, onlog) {
if (manufacturerData.length !== 13) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoBulb] Buffer length ${manufacturerData.length} !== 13!`
);
}
return null;
}
let byte1 = manufacturerData.readUInt8(1);//power and light status
let byte2 = manufacturerData.readUInt8(2);//bulb brightness
let byte3 = manufacturerData.readUInt8(3);//bulb R
let byte4 = manufacturerData.readUInt8(4);//bulb G
let byte5 = manufacturerData.readUInt8(5);//bulb B
let byte6 = manufacturerData.readUInt8(6);//bulb temperature
let byte7 = manufacturerData.readUInt8(7);
let byte8 = manufacturerData.readUInt8(8);
let byte9 = manufacturerData.readUInt8(9);
let byte10 = manufacturerData.readUInt8(10);//bulb mode
let power = byte1;
let red = byte3;
let green = byte4;
let blue = byte5;
let color_temperature = byte6;
let state = byte7 & 0b01111111 ? true : false;
let brightness = byte7 & 0b01111111;
let delay = byte8 & 0b10000000;
let preset = byte8 & 0b00001000;
let color_mode = byte8 & 0b00000111;
let speed = byte9 & 0b01111111;
let loop_index = byte10 & 0b11111110;
let data = {
model: "u",
modelName: "WoBulb",
color_temperature: color_temperature,
power: power,
state: state,
red: red,
green: green,
blue: blue,
brightness: brightness,
delay: delay,
preset: preset,
color_mode: color_mode,
speed: speed,
loop_index: loop_index,
};
return data;
}
_parseServiceDataForWoPlugMiniUS(manufacturerData, onlog) {
if (manufacturerData.length !== 14) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoPlugMiniUS] Buffer length ${manufacturerData.length} should be 14`
);
}
return null;
}
let byte9 = manufacturerData.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
let byte10 = manufacturerData.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
let byte11 = manufacturerData.readUInt8(11); // byte11: wifi rssi
let byte12 = manufacturerData.readUInt8(12); // byte12: bit7: overload?
let byte13 = manufacturerData.readUInt8(13); // byte12[bit0~6] + byte13: current power value
let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
let delay = !!(byte10 & 0b00000001);
let timer = !!(byte10 & 0b00000010);
let syncUtcTime = !!(byte10 & 0b00000100);
let wifiRssi = byte11;
let overload = !!(byte12 & 0b10000000);
let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
// TODO: voltage ???
let data = {
model: "g",
modelName: "WoPlugMini",
state: state,
delay: delay,
timer: timer,
syncUtcTime: syncUtcTime,
wifiRssi: wifiRssi,
overload: overload,
currentPower: currentPower,
};
return data;
}
_parseServiceDataForWoPlugMiniJP(manufacturerData, onlog) {
if (manufacturerData.length !== 14) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoPlugMiniJP] Buffer length ${manufacturerData.length} should be 14`
);
}
return null;
}
let byte9 = manufacturerData.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
let byte10 = manufacturerData.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
let byte11 = manufacturerData.readUInt8(11); // byte11: wifi rssi
let byte12 = manufacturerData.readUInt8(12); // byte12: bit7: overload?
let byte13 = manufacturerData.readUInt8(13); // byte12[bit0~6] + byte13: current power value
let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
let delay = !!(byte10 & 0b00000001);
let timer = !!(byte10 & 0b00000010);
let syncUtcTime = !!(byte10 & 0b00000100);
let wifiRssi = byte11;
let overload = !!(byte12 & 0b10000000);
let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
// TODO: voltage ???
let data = {
model: "j",
modelName: "WoPlugMini",
state: state,
delay: delay,
timer: timer,
syncUtcTime: syncUtcTime,
wifiRssi: wifiRssi,
overload: overload,
currentPower: currentPower,
};
return data;
}
_parseServiceDataForWoSmartLock(manufacturerData, onlog) {
if (manufacturerData.length !== 6) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoSmartLock] Buffer length ${manufacturerData.length} !== 6!`
);
}
return null;
}
let byte2 = manufacturerData.readUInt8(2);
let byte7 = manufacturerData.readUInt8(7);
let byte8 = manufacturerData.readUInt8(8);
let LockStatus = {
LOCKED: 0b0000000,
UNLOCKED: 0b0010000,
LOCKING: 0b0100000,
UNLOCKING: 0b0110000,
LOCKING_STOP: 0b1000000,
UNLOCKING_STOP: 0b1010000,
NOT_FULLY_LOCKED: 0b1100000, //Only EU lock type
}
let battery = byte2 & 0b01111111; // %
let calibration = byte7 & 0b10000000 ? true : false;
let status = LockStatus(byte7 & 0b01110000);
let update_from_secondary_lock = byte7 & 0b00001000 ? true : false;
let door_open = byte7 & 0b00000100 ? true : false;
let double_lock_mode = byte8 & 0b10000000 ? true : false;
let unclosed_alarm = byte8 & 0b00100000 ? true : false;
let unlocked_alarm = byte8 & 0b00010000 ? true : false;
let auto_lock_paused = byte8 & 0b00000010 ? true : false;
let data = {
model: "o",
modelName: "WoSmartLock",
battery: battery,
calibration: calibration,
status: status,
update_from_secondary_lock: update_from_secondary_lock,
door_open: door_open,
double_lock_mode: double_lock_mode,
unclosed_alarm: unclosed_alarm,
unlocked_alarm: unlocked_alarm,
auto_lock_paused: auto_lock_paused,
};
return data;
}
_parseServiceDataForWoSensorTHPlus(buf, onlog) {
if (buf.length !== 6) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoSensorTHPlus] Buffer length ${buf.length} !== 6!`
);
}
return null;
}
let byte2 = buf.readUInt8(2);
let byte3 = buf.readUInt8(3);
let byte4 = buf.readUInt8(4);
let byte5 = buf.readUInt8(5);
let temp_sign = byte4 & 0b10000000 ? 1 : -1;
let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
let temp_f = (temp_c * 9 / 5) + 32;
temp_f = Math.round(temp_f * 10) / 10;
let data = {
model: "i",
modelName: "WoSensorTHPlus",
temperature: {
c: temp_c,
f: temp_f,
},
fahrenheit: byte5 & 0b10000000 ? true : false,
humidity: byte5 & 0b01111111,
battery: byte2 & 0b01111111,
};
return data;
}
_parseServiceDataForWoStrip(buf, onlog) {
if (buf.length !== 18) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoStrip] Buffer length ${buf.length} !== 18!`
);
}
return null;
}
let byte1 = buf.readUInt8(1);//power and light status
let byte2 = buf.readUInt8(2);//bulb brightness
let byte3 = buf.readUInt8(3);//bulb R
let byte4 = buf.readUInt8(4);//bulb G
let byte5 = buf.readUInt8(5);//bulb B
let byte7 = buf.readUInt8(7);
let byte8 = buf.readUInt8(8);
let byte9 = buf.readUInt8(9);
let byte10 = buf.readUInt8(10);
let state = byte7 & 0b10000000 ? true : false;
let brightness = byte7 & 0b01111111;
let red = byte3;
let green = byte4;
let blue = byte5;
let delay = byte8 & 0b10000000;
let preset = byte8 & 0b00001000;
let color_mode = byte8 & 0b00000111;
let speed = byte9 & 0b01111111;
let loop_index = byte10 & 0b11111110;
let data = {
model: "r",
modelName: "WoStrip",
state: state,
brightness: brightness,
red: red,
green: green,
blue: blue,
delay: delay,
preset: preset,
color_mode: color_mode,
speed: speed,
loop_index: loop_index,
};
return data;
}
}
module.exports = new SwitchbotAdvertising();