node-switchbot 1.6.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/lib/parameter-checker.js +10 -10
- package/lib/switchbot-advertising.js +161 -161
- package/lib/switchbot-device-wobulb.js +6 -6
- package/lib/switchbot-device-wocontact.js +1 -1
- package/lib/switchbot-device-wocurtain.js +3 -3
- package/lib/switchbot-device-wohand.js +3 -3
- package/lib/switchbot-device-wohumi.js +3 -3
- package/lib/switchbot-device-woplugmini.js +6 -6
- package/lib/switchbot-device-wopresence.js +1 -1
- package/lib/switchbot-device-wosensorth.js +1 -1
- package/lib/switchbot-device-wostrip.js +6 -6
- package/lib/switchbot-device.js +22 -22
- package/lib/switchbot.js +34 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -110,9 +110,9 @@ Monitoring the advertising packets, you can find your devices and know the lates
|
|
|
110
110
|
|
|
111
111
|
```JavaScript
|
|
112
112
|
// Load the node-switchbot and get a `Switchbot` constructor object
|
|
113
|
-
|
|
113
|
+
let Switchbot = require('node-switchbot');
|
|
114
114
|
// Create an `Switchbot` object
|
|
115
|
-
|
|
115
|
+
let switchbot = new Switchbot();
|
|
116
116
|
|
|
117
117
|
(async () => {
|
|
118
118
|
// Start to monitor advertisement packets
|
|
@@ -137,7 +137,7 @@ The [`startScan()`](#startscan-method) and [`wait()`](#Switchbot-wait-method) me
|
|
|
137
137
|
|
|
138
138
|
```javascript
|
|
139
139
|
// Load the node-switchbot and get a `Switchbot` constructor object
|
|
140
|
-
|
|
140
|
+
let Switchbot = require("node-switchbot");
|
|
141
141
|
// Create an `Switchbot` object
|
|
142
142
|
let switchbot = new Switchbot();
|
|
143
143
|
|
|
@@ -213,13 +213,13 @@ This sample discovers a Bot (WoHand), then put the Bot's arm down, finally put i
|
|
|
213
213
|
|
|
214
214
|
```javascript
|
|
215
215
|
// Load the node-switchbot and get a `Switchbot` constructor object
|
|
216
|
-
|
|
216
|
+
let Switchbot = require("node-switchbot");
|
|
217
217
|
// Create an `Switchbot` object
|
|
218
|
-
|
|
218
|
+
let switchbot = new Switchbot();
|
|
219
219
|
|
|
220
220
|
(async () => {
|
|
221
221
|
// Find a Bot (WoHand)
|
|
222
|
-
|
|
222
|
+
let bot_list = await switchbot.discover({ model: "H", quick: true });
|
|
223
223
|
if (bot_list.length === 0) {
|
|
224
224
|
throw new Error("No device was found.");
|
|
225
225
|
}
|
|
@@ -246,13 +246,13 @@ In this code, you can get a [`SwitchbotDeviceWoHand`](#SwitchbotDeviceWoHand-obj
|
|
|
246
246
|
In order to use the node-switchbot, you have to load the node-switchbot module as follows:
|
|
247
247
|
|
|
248
248
|
```JavaScript
|
|
249
|
-
|
|
249
|
+
let Switchbot = require('node-switchbot');
|
|
250
250
|
```
|
|
251
251
|
|
|
252
252
|
You can get an `Switchbot` constructor from the code above. Then you have to create an `Switchbot` object from the `Switchbot` constructor as follows:
|
|
253
253
|
|
|
254
254
|
```javascript
|
|
255
|
-
|
|
255
|
+
let switchbot = new Switchbot();
|
|
256
256
|
```
|
|
257
257
|
|
|
258
258
|
The `Switchbot` constructor takes an argument optionally. It must be a hash object containing the properties as follows:
|
|
@@ -267,11 +267,11 @@ The sample code below shows how to pass a `Nobel` object to the `Switchbot` cons
|
|
|
267
267
|
|
|
268
268
|
```JavaScript
|
|
269
269
|
// Create a Noble object
|
|
270
|
-
|
|
270
|
+
let noble = require('@abandonware/noble');
|
|
271
271
|
|
|
272
272
|
// Create a Switchbot object
|
|
273
|
-
|
|
274
|
-
|
|
273
|
+
let Switchbot = require('node-switchbot');
|
|
274
|
+
let switchbot = new Switchbot({'noble': noble});
|
|
275
275
|
```
|
|
276
276
|
|
|
277
277
|
In the code snippet above, the variable `switchbot` is an `Switchbot` object. The `Switchbot` object has a lot of methods as described in sections below.
|
package/lib/parameter-checker.js
CHANGED
|
@@ -37,7 +37,7 @@ class ParameterChecker {
|
|
|
37
37
|
* an `Error` object will be set to `this._error`.
|
|
38
38
|
*
|
|
39
39
|
* [Usage]
|
|
40
|
-
*
|
|
40
|
+
* let valid = parameterChecker.check(params, {
|
|
41
41
|
* level: {
|
|
42
42
|
* required: false,
|
|
43
43
|
* type: 'integer',
|
|
@@ -50,7 +50,7 @@ class ParameterChecker {
|
|
|
50
50
|
* }
|
|
51
51
|
* });
|
|
52
52
|
* if(!valid) {
|
|
53
|
-
*
|
|
53
|
+
* let e = parameterChecker.error.message;
|
|
54
54
|
* throw new Error(message);
|
|
55
55
|
* }
|
|
56
56
|
* ---------------------------------------------------------------- */
|
|
@@ -78,13 +78,13 @@ class ParameterChecker {
|
|
|
78
78
|
return false;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
let result = true;
|
|
82
|
+
let name_list = Object.keys(rules);
|
|
83
83
|
|
|
84
|
-
for (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
for (let i = 0; i < name_list.length; i++) {
|
|
85
|
+
let name = name_list[i];
|
|
86
|
+
let v = obj[name];
|
|
87
|
+
let rule = rules[name];
|
|
88
88
|
|
|
89
89
|
if (!rule) {
|
|
90
90
|
rule = {};
|
|
@@ -458,7 +458,7 @@ class ParameterChecker {
|
|
|
458
458
|
}
|
|
459
459
|
}
|
|
460
460
|
if (typeof rule.minBytes === "number") {
|
|
461
|
-
|
|
461
|
+
let blen = Buffer.from(value, "utf8").length;
|
|
462
462
|
if (blen < rule.minBytes) {
|
|
463
463
|
this._error = {
|
|
464
464
|
code: "LENGTH_UNDERFLOW",
|
|
@@ -475,7 +475,7 @@ class ParameterChecker {
|
|
|
475
475
|
}
|
|
476
476
|
}
|
|
477
477
|
if (typeof rule.maxBytes === "number") {
|
|
478
|
-
|
|
478
|
+
let blen = Buffer.from(value, "utf8").length;
|
|
479
479
|
if (blen > rule.maxBytes) {
|
|
480
480
|
this._error = {
|
|
481
481
|
code: "LENGTH_OVERFLOW",
|
|
@@ -61,16 +61,16 @@ class SwitchbotAdvertising {
|
|
|
61
61
|
* device, this method will return `null`.
|
|
62
62
|
* ---------------------------------------------------------------- */
|
|
63
63
|
parse(peripheral, onlog) {
|
|
64
|
-
|
|
64
|
+
let ad = peripheral.advertisement;
|
|
65
65
|
if (!ad || !ad.serviceData) {
|
|
66
66
|
return null;
|
|
67
67
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
let serviceData = ad.serviceData[0] || ad.serviceData;
|
|
69
|
+
let manufacturerData = ad.manufacturerData;
|
|
70
|
+
let buf = serviceData.data;
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
let bufIsInvalid = !buf || !Buffer.isBuffer(buf) || buf.length < 3;
|
|
73
|
+
let manufacturerDataIsInvalid =
|
|
74
74
|
!manufacturerData ||
|
|
75
75
|
!Buffer.isBuffer(manufacturerData) ||
|
|
76
76
|
manufacturerData.length < 3;
|
|
@@ -79,8 +79,8 @@ class SwitchbotAdvertising {
|
|
|
79
79
|
return null;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
let model = buf.slice(0, 1).toString("utf8");
|
|
83
|
+
let sd = null;
|
|
84
84
|
|
|
85
85
|
if (model === "H") {
|
|
86
86
|
sd = this._parseServiceDataForWoHand(buf, onlog);//WoHand
|
|
@@ -123,11 +123,11 @@ class SwitchbotAdvertising {
|
|
|
123
123
|
}
|
|
124
124
|
return null;
|
|
125
125
|
}
|
|
126
|
-
|
|
126
|
+
let address = peripheral.address || "";
|
|
127
127
|
if (address === "") {
|
|
128
128
|
address = peripheral.advertisement.manufacturerData || "";
|
|
129
129
|
if (address !== "") {
|
|
130
|
-
|
|
130
|
+
let str = peripheral.advertisement.manufacturerData
|
|
131
131
|
.toString("hex")
|
|
132
132
|
.slice(4, 16);
|
|
133
133
|
address = str.substr(0, 2);
|
|
@@ -139,7 +139,7 @@ class SwitchbotAdvertising {
|
|
|
139
139
|
} else {
|
|
140
140
|
address = address.replace(/-/g, ":");
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
let data = {
|
|
143
143
|
id: peripheral.id,
|
|
144
144
|
address: address,
|
|
145
145
|
rssi: peripheral.rssi,
|
|
@@ -165,14 +165,14 @@ class SwitchbotAdvertising {
|
|
|
165
165
|
}
|
|
166
166
|
return null;
|
|
167
167
|
}
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
let byte1 = buf.readUInt8(1);
|
|
169
|
+
let byte2 = buf.readUInt8(2);
|
|
170
170
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
171
|
+
let mode = byte1 & 0b10000000 ? true : false; // Whether the light switch Add-on is used or not
|
|
172
|
+
let state = byte1 & 0b01000000 ? true : false; // Whether the switch status is ON or OFF
|
|
173
|
+
let battery = byte2 & 0b01111111; // %
|
|
174
174
|
|
|
175
|
-
|
|
175
|
+
let data = {
|
|
176
176
|
model: "H",
|
|
177
177
|
modelName: "WoHand",
|
|
178
178
|
mode: mode,
|
|
@@ -192,17 +192,17 @@ class SwitchbotAdvertising {
|
|
|
192
192
|
}
|
|
193
193
|
return null;
|
|
194
194
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
195
|
+
let byte2 = buf.readUInt8(2);
|
|
196
|
+
let byte3 = buf.readUInt8(3);
|
|
197
|
+
let byte4 = buf.readUInt8(4);
|
|
198
|
+
let byte5 = buf.readUInt8(5);
|
|
199
|
+
|
|
200
|
+
let temp_sign = byte4 & 0b10000000 ? 1 : -1;
|
|
201
|
+
let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
|
|
202
|
+
let temp_f = (temp_c * 9 / 5) + 32;
|
|
203
203
|
temp_f = Math.round(temp_f * 10) / 10;
|
|
204
204
|
|
|
205
|
-
|
|
205
|
+
let data = {
|
|
206
206
|
model: "T",
|
|
207
207
|
modelName: "WoSensorTH",
|
|
208
208
|
temperature: {
|
|
@@ -226,15 +226,15 @@ class SwitchbotAdvertising {
|
|
|
226
226
|
}
|
|
227
227
|
return null;
|
|
228
228
|
}
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
let byte1 = buf.readUInt8(1);
|
|
230
|
+
let byte4 = buf.readUInt8(4);
|
|
231
231
|
|
|
232
232
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
233
|
+
let onState = byte1 & 0b10000000 ? true : false; // 1 - on
|
|
234
|
+
let autoMode = byte4 & 0b10000000 ? true : false; // 1 - auto
|
|
235
|
+
let percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3
|
|
236
236
|
|
|
237
|
-
|
|
237
|
+
let data = {
|
|
238
238
|
model: "e",
|
|
239
239
|
modelName: "WoHumi",
|
|
240
240
|
onState: onState,
|
|
@@ -255,20 +255,20 @@ class SwitchbotAdvertising {
|
|
|
255
255
|
return null;
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
258
|
+
let byte1 = buf.readUInt8(1);
|
|
259
|
+
let byte2 = buf.readUInt8(2);
|
|
260
|
+
let byte5 = buf.readUInt8(5);
|
|
261
261
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
262
|
+
let tested = byte1 & 0b10000000 ? true : false;
|
|
263
|
+
let movement = byte1 & 0b01000000 ? true : false;
|
|
264
|
+
let battery = byte2 & 0b01111111;
|
|
265
|
+
let led = (byte5 & 0b00100000) >> 5;
|
|
266
|
+
let iot = (byte5 & 0b00010000) >> 4;
|
|
267
|
+
let sense_distance = (byte5 & 0b00001100) >> 2;
|
|
268
|
+
let lightLevel = byte5 & 0b00000011;
|
|
269
|
+
let is_light = byte5 & 0b00000010 ? true : false;
|
|
270
270
|
|
|
271
|
-
|
|
271
|
+
let data = {
|
|
272
272
|
model: "s",
|
|
273
273
|
modelName: "WoMotion",
|
|
274
274
|
tested: tested,
|
|
@@ -295,21 +295,21 @@ class SwitchbotAdvertising {
|
|
|
295
295
|
return null;
|
|
296
296
|
}
|
|
297
297
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
298
|
+
let byte1 = buf.readUInt8(1);
|
|
299
|
+
let byte2 = buf.readUInt8(2);
|
|
300
|
+
let byte3 = buf.readUInt8(3);
|
|
301
|
+
let byte8 = buf.readUInt8(8);
|
|
302
|
+
|
|
303
|
+
let hallState = (byte3 >> 1) & 0b00000011;
|
|
304
|
+
let tested = byte1 & 0b10000000;
|
|
305
|
+
let movement = byte1 & 0b01000000 ? true : false; // 1 - Movement detected
|
|
306
|
+
let battery = byte2 & 0b01111111; // %
|
|
307
|
+
let contact_open = byte3 & 0b00000010 == 0b00000010;
|
|
308
|
+
let contact_timeout = byte3 & 0b00000100 == 0b00000100;
|
|
309
|
+
let lightLevel = byte3 & 0b00000001;
|
|
310
|
+
let button_count = byte8 & 0b00001111;
|
|
311
|
+
|
|
312
|
+
let data = {
|
|
313
313
|
model: "d",
|
|
314
314
|
modelName: "WoContact",
|
|
315
315
|
movement: movement,
|
|
@@ -339,19 +339,19 @@ class SwitchbotAdvertising {
|
|
|
339
339
|
}
|
|
340
340
|
return null;
|
|
341
341
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
342
|
+
let byte1 = buf.readUInt8(1);
|
|
343
|
+
let byte2 = buf.readUInt8(2);
|
|
344
|
+
let byte3 = buf.readUInt8(3);
|
|
345
|
+
let byte4 = buf.readUInt8(4);
|
|
346
|
+
|
|
347
|
+
let calibration = byte1 & 0b01000000 ? true : false; // Whether the calibration is completed
|
|
348
|
+
let battery = byte2 & 0b01111111; // %
|
|
349
|
+
let inMotion = byte3 & 0b10000000 ? true : false;
|
|
350
|
+
let currPosition = byte3 & 0b01111111; // current positon %
|
|
351
|
+
let lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10)
|
|
352
|
+
let deviceChain = byte4 & 0b00000111;
|
|
353
|
+
|
|
354
|
+
let data = {
|
|
355
355
|
model: "c",
|
|
356
356
|
modelName: "WoCurtain",
|
|
357
357
|
calibration: calibration,
|
|
@@ -374,31 +374,31 @@ class SwitchbotAdvertising {
|
|
|
374
374
|
}
|
|
375
375
|
return null;
|
|
376
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
|
-
|
|
377
|
+
let byte1 = buf.readUInt8(1);//power and light status
|
|
378
|
+
let byte2 = buf.readUInt8(2);//bulb brightness
|
|
379
|
+
let byte3 = buf.readUInt8(3);//bulb R
|
|
380
|
+
let byte4 = buf.readUInt8(4);//bulb G
|
|
381
|
+
let byte5 = buf.readUInt8(5);//bulb B
|
|
382
|
+
let byte6 = buf.readUInt8(6);//bulb temperature
|
|
383
|
+
let byte7 = buf.readUInt8(7);
|
|
384
|
+
let byte8 = buf.readUInt8(8);
|
|
385
|
+
let byte9 = buf.readUInt8(9);
|
|
386
|
+
let byte10 = buf.readUInt8(10);//bulb mode
|
|
387
|
+
|
|
388
|
+
let power = byte1;
|
|
389
|
+
let red = byte3;
|
|
390
|
+
let green = byte4;
|
|
391
|
+
let blue = byte5;
|
|
392
|
+
let color_temperature = byte6;
|
|
393
|
+
let state = byte7 & 0b01111111 ? true : false;
|
|
394
|
+
let brightness = byte7 & 0b01111111;
|
|
395
|
+
let delay = byte8 & 0b10000000;
|
|
396
|
+
let preset = byte8 & 0b00001000;
|
|
397
|
+
let color_mode = byte8 & 0b00000111;
|
|
398
|
+
let speed = byte9 & 0b01111111;
|
|
399
|
+
let loop_index = byte10 & 0b11111110;
|
|
400
|
+
|
|
401
|
+
let data = {
|
|
402
402
|
model: "u",
|
|
403
403
|
modelName: "WoBulb",
|
|
404
404
|
color_temperature: color_temperature,
|
|
@@ -427,22 +427,22 @@ class SwitchbotAdvertising {
|
|
|
427
427
|
}
|
|
428
428
|
return null;
|
|
429
429
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
430
|
+
let byte9 = buf.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
|
|
431
|
+
let byte10 = buf.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
|
|
432
|
+
let byte11 = buf.readUInt8(11); // byte11: wifi rssi
|
|
433
|
+
let byte12 = buf.readUInt8(12); // byte12: bit7: overload?
|
|
434
|
+
let byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
|
|
435
|
+
|
|
436
|
+
let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
|
|
437
|
+
let delay = !!(byte10 & 0b00000001);
|
|
438
|
+
let timer = !!(byte10 & 0b00000010);
|
|
439
|
+
let syncUtcTime = !!(byte10 & 0b00000100);
|
|
440
|
+
let wifiRssi = byte11;
|
|
441
|
+
let overload = !!(byte12 & 0b10000000);
|
|
442
|
+
let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
|
|
443
443
|
// TODO: voltage ???
|
|
444
444
|
|
|
445
|
-
|
|
445
|
+
let data = {
|
|
446
446
|
model: "g",
|
|
447
447
|
modelName: "WoPlugMini",
|
|
448
448
|
state: state,
|
|
@@ -466,22 +466,22 @@ class SwitchbotAdvertising {
|
|
|
466
466
|
}
|
|
467
467
|
return null;
|
|
468
468
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
469
|
+
let byte9 = buf.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
|
|
470
|
+
let byte10 = buf.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
|
|
471
|
+
let byte11 = buf.readUInt8(11); // byte11: wifi rssi
|
|
472
|
+
let byte12 = buf.readUInt8(12); // byte12: bit7: overload?
|
|
473
|
+
let byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
|
|
474
|
+
|
|
475
|
+
let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
|
|
476
|
+
let delay = !!(byte10 & 0b00000001);
|
|
477
|
+
let timer = !!(byte10 & 0b00000010);
|
|
478
|
+
let syncUtcTime = !!(byte10 & 0b00000100);
|
|
479
|
+
let wifiRssi = byte11;
|
|
480
|
+
let overload = !!(byte12 & 0b10000000);
|
|
481
|
+
let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
|
|
482
482
|
// TODO: voltage ???
|
|
483
483
|
|
|
484
|
-
|
|
484
|
+
let data = {
|
|
485
485
|
model: "j",
|
|
486
486
|
modelName: "WoPlugMini",
|
|
487
487
|
state: state,
|
|
@@ -505,13 +505,13 @@ class SwitchbotAdvertising {
|
|
|
505
505
|
}
|
|
506
506
|
return null;
|
|
507
507
|
}
|
|
508
|
-
|
|
509
|
-
|
|
508
|
+
let byte1 = buf.readUInt8(1);
|
|
509
|
+
let byte2 = buf.readUInt8(2);
|
|
510
510
|
|
|
511
|
-
|
|
512
|
-
|
|
511
|
+
let movement = (byte1 & 0b01000000) ? true : false; // 1 - Movement detected
|
|
512
|
+
let battery = byte2 & 0b01111111; // %
|
|
513
513
|
|
|
514
|
-
|
|
514
|
+
let data = {
|
|
515
515
|
model: "o",
|
|
516
516
|
modelName: "WoSmartLock",
|
|
517
517
|
battery: battery,
|
|
@@ -530,17 +530,17 @@ class SwitchbotAdvertising {
|
|
|
530
530
|
}
|
|
531
531
|
return null;
|
|
532
532
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
533
|
+
let byte2 = buf.readUInt8(2);
|
|
534
|
+
let byte3 = buf.readUInt8(3);
|
|
535
|
+
let byte4 = buf.readUInt8(4);
|
|
536
|
+
let byte5 = buf.readUInt8(5);
|
|
537
|
+
|
|
538
|
+
let temp_sign = byte4 & 0b10000000 ? 1 : -1;
|
|
539
|
+
let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
|
|
540
|
+
let temp_f = (temp_c * 9 / 5) + 32;
|
|
541
541
|
temp_f = Math.round(temp_f * 10) / 10;
|
|
542
542
|
|
|
543
|
-
|
|
543
|
+
let data = {
|
|
544
544
|
model: "i",
|
|
545
545
|
modelName: "WoSensorTHPlus",
|
|
546
546
|
temperature: {
|
|
@@ -565,28 +565,28 @@ class SwitchbotAdvertising {
|
|
|
565
565
|
return null;
|
|
566
566
|
}
|
|
567
567
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
568
|
+
let byte1 = buf.readUInt8(1);//power and light status
|
|
569
|
+
let byte2 = buf.readUInt8(2);//bulb brightness
|
|
570
|
+
let byte3 = buf.readUInt8(3);//bulb R
|
|
571
|
+
let byte4 = buf.readUInt8(4);//bulb G
|
|
572
|
+
let byte5 = buf.readUInt8(5);//bulb B
|
|
573
|
+
let byte7 = buf.readUInt8(7);
|
|
574
|
+
let byte8 = buf.readUInt8(8);
|
|
575
|
+
let byte9 = buf.readUInt8(9);
|
|
576
|
+
let byte10 = buf.readUInt8(10);
|
|
577
|
+
|
|
578
|
+
let state = byte7 & 0b10000000 ? true : false;
|
|
579
|
+
let brightness = byte7 & 0b01111111;
|
|
580
|
+
let red = byte3;
|
|
581
|
+
let green = byte4;
|
|
582
|
+
let blue = byte5;
|
|
583
|
+
let delay = byte8 & 0b10000000;
|
|
584
|
+
let preset = byte8 & 0b00001000;
|
|
585
|
+
let color_mode = byte8 & 0b00000111;
|
|
586
|
+
let speed = byte9 & 0b01111111;
|
|
587
|
+
let loop_index = byte10 & 0b11111110;
|
|
588
|
+
|
|
589
|
+
let data = {
|
|
590
590
|
model: "r",
|
|
591
591
|
modelName: "WoStrip",
|
|
592
592
|
state: state,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
let SwitchbotDevice = require("./switchbot-device.js");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md
|
|
@@ -16,7 +16,7 @@ class SwitchbotDeviceWoBulb extends SwitchbotDevice {
|
|
|
16
16
|
* @private
|
|
17
17
|
*/
|
|
18
18
|
_setState(reqByteArray) {
|
|
19
|
-
|
|
19
|
+
let base = [0x57, 0x0f, 0x47, 0x01];
|
|
20
20
|
return this._operateBot([].concat(base, reqByteArray));
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -149,15 +149,15 @@ class SwitchbotDeviceWoBulb extends SwitchbotDevice {
|
|
|
149
149
|
* @private
|
|
150
150
|
*/
|
|
151
151
|
_operateBot(bytes) {
|
|
152
|
-
|
|
152
|
+
let req_buf = Buffer.from(bytes);
|
|
153
153
|
return new Promise((resolve, reject) => {
|
|
154
154
|
this._command(req_buf)
|
|
155
155
|
.then((res_bytes) => {
|
|
156
|
-
|
|
156
|
+
let res_buf = Buffer.from(res_bytes);
|
|
157
157
|
if (res_buf.length === 2) {
|
|
158
|
-
|
|
158
|
+
let code = res_buf.readUInt8(1);
|
|
159
159
|
if (code === 0x00 || code === 0x80) {
|
|
160
|
-
|
|
160
|
+
let is_on = code === 0x80;
|
|
161
161
|
resolve(is_on);
|
|
162
162
|
} else {
|
|
163
163
|
reject(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
let SwitchbotDevice = require("./switchbot-device.js");
|
|
3
3
|
|
|
4
4
|
class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
|
|
5
5
|
/* ------------------------------------------------------------------
|
|
@@ -93,10 +93,10 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
|
|
|
93
93
|
|
|
94
94
|
_operateCurtain(bytes) {
|
|
95
95
|
return new Promise((resolve, reject) => {
|
|
96
|
-
|
|
96
|
+
let req_buf = Buffer.from(bytes);
|
|
97
97
|
this._command(req_buf)
|
|
98
98
|
.then((res_buf) => {
|
|
99
|
-
|
|
99
|
+
let code = res_buf.readUInt8(0);
|
|
100
100
|
if (res_buf.length === 3 && code === 0x01) {
|
|
101
101
|
resolve();
|
|
102
102
|
} else {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
let SwitchbotDevice = require("./switchbot-device.js");
|
|
3
3
|
|
|
4
4
|
class SwitchbotDeviceWoHand extends SwitchbotDevice {
|
|
5
5
|
/* ------------------------------------------------------------------
|
|
@@ -79,10 +79,10 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
|
|
|
79
79
|
|
|
80
80
|
_operateBot(bytes) {
|
|
81
81
|
return new Promise((resolve, reject) => {
|
|
82
|
-
|
|
82
|
+
let req_buf = Buffer.from(bytes);
|
|
83
83
|
this._command(req_buf)
|
|
84
84
|
.then((res_buf) => {
|
|
85
|
-
|
|
85
|
+
let code = res_buf.readUInt8(0);
|
|
86
86
|
if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
|
|
87
87
|
resolve();
|
|
88
88
|
} else {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
let SwitchbotDevice = require("./switchbot-device.js");
|
|
3
3
|
|
|
4
4
|
class SwitchbotDeviceWoHumi extends SwitchbotDevice {
|
|
5
5
|
/* ------------------------------------------------------------------
|
|
@@ -79,10 +79,10 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
|
|
|
79
79
|
|
|
80
80
|
_operateBot(bytes) {
|
|
81
81
|
return new Promise((resolve, reject) => {
|
|
82
|
-
|
|
82
|
+
let req_buf = Buffer.from(bytes);
|
|
83
83
|
this._command(req_buf)
|
|
84
84
|
.then((res_buf) => {
|
|
85
|
-
|
|
85
|
+
let code = res_buf.readUInt8(0);
|
|
86
86
|
if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
|
|
87
87
|
resolve();
|
|
88
88
|
} else {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
let SwitchbotDevice = require("./switchbot-device.js");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/plugmini.md
|
|
@@ -16,7 +16,7 @@ class SwitchbotDeviceWoPlugMini extends SwitchbotDevice {
|
|
|
16
16
|
* @private
|
|
17
17
|
*/
|
|
18
18
|
_setState(reqByteArray) {
|
|
19
|
-
|
|
19
|
+
let base = [0x57, 0x0f, 0x50, 0x01];
|
|
20
20
|
return this._operateBot([].concat(base, reqByteArray));
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -45,15 +45,15 @@ class SwitchbotDeviceWoPlugMini extends SwitchbotDevice {
|
|
|
45
45
|
* @private
|
|
46
46
|
*/
|
|
47
47
|
_operateBot(bytes) {
|
|
48
|
-
|
|
48
|
+
let req_buf = Buffer.from(bytes);
|
|
49
49
|
return new Promise((resolve, reject) => {
|
|
50
50
|
this._command(req_buf)
|
|
51
51
|
.then((res_bytes) => {
|
|
52
|
-
|
|
52
|
+
let res_buf = Buffer.from(res_bytes);
|
|
53
53
|
if (res_buf.length === 2) {
|
|
54
|
-
|
|
54
|
+
let code = res_buf.readUInt8(1);
|
|
55
55
|
if (code === 0x00 || code === 0x80) {
|
|
56
|
-
|
|
56
|
+
let is_on = code === 0x80;
|
|
57
57
|
resolve(is_on);
|
|
58
58
|
} else {
|
|
59
59
|
reject(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
let SwitchbotDevice = require("./switchbot-device.js");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md
|
|
@@ -16,7 +16,7 @@ class SwitchbotDeviceWoStrip extends SwitchbotDevice {
|
|
|
16
16
|
* @private
|
|
17
17
|
*/
|
|
18
18
|
_setState(reqByteArray) {
|
|
19
|
-
|
|
19
|
+
let base = [0x57, 0x0f, 0x49, 0x01];
|
|
20
20
|
return this._operateBot([].concat(base, reqByteArray));
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -143,15 +143,15 @@ class SwitchbotDeviceWoStrip extends SwitchbotDevice {
|
|
|
143
143
|
* @private
|
|
144
144
|
*/
|
|
145
145
|
_operateBot(bytes) {
|
|
146
|
-
|
|
146
|
+
let req_buf = Buffer.from(bytes);
|
|
147
147
|
return new Promise((resolve, reject) => {
|
|
148
148
|
this._command(req_buf)
|
|
149
149
|
.then((res_bytes) => {
|
|
150
|
-
|
|
150
|
+
let res_buf = Buffer.from(res_bytes);
|
|
151
151
|
if (res_buf.length === 2) {
|
|
152
|
-
|
|
152
|
+
let code = res_buf.readUInt8(1);
|
|
153
153
|
if (code === 0x00 || code === 0x80) {
|
|
154
|
-
|
|
154
|
+
let is_on = code === 0x80;
|
|
155
155
|
resolve(is_on);
|
|
156
156
|
} else {
|
|
157
157
|
reject(
|
package/lib/switchbot-device.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
let parameterChecker = require("./parameter-checker.js");
|
|
3
|
+
let switchbotAdvertising = require("./switchbot-advertising.js");
|
|
4
4
|
|
|
5
5
|
class SwitchbotDevice {
|
|
6
6
|
/* ------------------------------------------------------------------
|
|
@@ -26,7 +26,7 @@ class SwitchbotDevice {
|
|
|
26
26
|
this._COMMAND_TIMEOUT_MSEC = 3000;
|
|
27
27
|
|
|
28
28
|
// Save the device information
|
|
29
|
-
|
|
29
|
+
let ad = switchbotAdvertising.parse(peripheral);
|
|
30
30
|
this._id = ad.id;
|
|
31
31
|
this._address = ad.address;
|
|
32
32
|
this._model = ad.serviceData.model;
|
|
@@ -105,7 +105,7 @@ class SwitchbotDevice {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
// Check the connection state
|
|
108
|
-
|
|
108
|
+
let state = this.connectionState;
|
|
109
109
|
if (state === "connected") {
|
|
110
110
|
resolve();
|
|
111
111
|
return;
|
|
@@ -155,7 +155,7 @@ class SwitchbotDevice {
|
|
|
155
155
|
_getCharacteristics() {
|
|
156
156
|
return new Promise((resolve, reject) => {
|
|
157
157
|
// Set timeout timer
|
|
158
|
-
|
|
158
|
+
let timer = setTimeout(() => {
|
|
159
159
|
this._ondisconnect_internal = () => {};
|
|
160
160
|
timer = null;
|
|
161
161
|
reject(
|
|
@@ -179,20 +179,20 @@ class SwitchbotDevice {
|
|
|
179
179
|
|
|
180
180
|
// Discover services and characteristics
|
|
181
181
|
(async () => {
|
|
182
|
-
|
|
182
|
+
let service_list = await this._discoverServices();
|
|
183
183
|
if (!timer) {
|
|
184
184
|
throw new Error("");
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
|
|
187
|
+
let chars = {
|
|
188
188
|
write: null,
|
|
189
189
|
notify: null,
|
|
190
190
|
device: null,
|
|
191
191
|
};
|
|
192
192
|
|
|
193
|
-
for (
|
|
194
|
-
|
|
195
|
-
for (
|
|
193
|
+
for (let service of service_list) {
|
|
194
|
+
let char_list = await this._discoverCharacteristics(service);
|
|
195
|
+
for (let char of char_list) {
|
|
196
196
|
if (char.uuid === this._CHAR_UUID_WRITE) {
|
|
197
197
|
chars.write = char;
|
|
198
198
|
} else if (char.uuid === this._CHAR_UUID_NOTIFY) {
|
|
@@ -230,8 +230,8 @@ class SwitchbotDevice {
|
|
|
230
230
|
return;
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
-
|
|
234
|
-
for (
|
|
233
|
+
let service = null;
|
|
234
|
+
for (let s of service_list) {
|
|
235
235
|
if (s.uuid === this._SERV_UUID_PRIMARY) {
|
|
236
236
|
service = s;
|
|
237
237
|
break;
|
|
@@ -260,7 +260,7 @@ class SwitchbotDevice {
|
|
|
260
260
|
|
|
261
261
|
_subscribe() {
|
|
262
262
|
return new Promise((resolve, reject) => {
|
|
263
|
-
|
|
263
|
+
let char = this._chars.notify;
|
|
264
264
|
if (!char) {
|
|
265
265
|
reject(new Error("No notify characteristic was found."));
|
|
266
266
|
return;
|
|
@@ -280,7 +280,7 @@ class SwitchbotDevice {
|
|
|
280
280
|
|
|
281
281
|
_unsubscribe() {
|
|
282
282
|
return new Promise((resolve) => {
|
|
283
|
-
|
|
283
|
+
let char = this._chars.notify;
|
|
284
284
|
if (!char) {
|
|
285
285
|
resolve();
|
|
286
286
|
return;
|
|
@@ -307,7 +307,7 @@ class SwitchbotDevice {
|
|
|
307
307
|
return new Promise((resolve, reject) => {
|
|
308
308
|
this._was_connected_explicitly = false;
|
|
309
309
|
// Check the connection state
|
|
310
|
-
|
|
310
|
+
let state = this._peripheral.state;
|
|
311
311
|
if (state === "disconnected") {
|
|
312
312
|
resolve();
|
|
313
313
|
return;
|
|
@@ -351,7 +351,7 @@ class SwitchbotDevice {
|
|
|
351
351
|
* ---------------------------------------------------------------- */
|
|
352
352
|
getDeviceName() {
|
|
353
353
|
return new Promise((resolve, reject) => {
|
|
354
|
-
|
|
354
|
+
let name = "";
|
|
355
355
|
this._connect()
|
|
356
356
|
.then(() => {
|
|
357
357
|
if (!this._chars.device) {
|
|
@@ -392,7 +392,7 @@ class SwitchbotDevice {
|
|
|
392
392
|
setDeviceName(name) {
|
|
393
393
|
return new Promise((resolve, reject) => {
|
|
394
394
|
// Check the parameters
|
|
395
|
-
|
|
395
|
+
let valid = parameterChecker.check(
|
|
396
396
|
{ name: name },
|
|
397
397
|
{
|
|
398
398
|
name: { required: true, type: "string", minBytes: 1, maxBytes: 100 },
|
|
@@ -404,7 +404,7 @@ class SwitchbotDevice {
|
|
|
404
404
|
return;
|
|
405
405
|
}
|
|
406
406
|
|
|
407
|
-
|
|
407
|
+
let buf = Buffer.from(name, "utf8");
|
|
408
408
|
this._connect()
|
|
409
409
|
.then(() => {
|
|
410
410
|
if (!this._chars.device) {
|
|
@@ -439,7 +439,7 @@ class SwitchbotDevice {
|
|
|
439
439
|
return;
|
|
440
440
|
}
|
|
441
441
|
|
|
442
|
-
|
|
442
|
+
let res_buf = null;
|
|
443
443
|
|
|
444
444
|
this._connect()
|
|
445
445
|
.then(() => {
|
|
@@ -463,7 +463,7 @@ class SwitchbotDevice {
|
|
|
463
463
|
|
|
464
464
|
_waitCommandResponse() {
|
|
465
465
|
return new Promise((resolve, reject) => {
|
|
466
|
-
|
|
466
|
+
let timer = setTimeout(() => {
|
|
467
467
|
timer = null;
|
|
468
468
|
this._onnotify_internal = () => {};
|
|
469
469
|
reject(new Error("COMMAND_TIMEOUT"));
|
|
@@ -484,7 +484,7 @@ class SwitchbotDevice {
|
|
|
484
484
|
_read(char) {
|
|
485
485
|
return new Promise((resolve, reject) => {
|
|
486
486
|
// Set a timeout timer
|
|
487
|
-
|
|
487
|
+
let timer = setTimeout(() => {
|
|
488
488
|
reject("READ_TIMEOUT");
|
|
489
489
|
}, this._READ_TIMEOUT_MSEC);
|
|
490
490
|
|
|
@@ -507,7 +507,7 @@ class SwitchbotDevice {
|
|
|
507
507
|
_write(char, buf) {
|
|
508
508
|
return new Promise((resolve, reject) => {
|
|
509
509
|
// Set a timeout timer
|
|
510
|
-
|
|
510
|
+
let timer = setTimeout(() => {
|
|
511
511
|
reject("WRITE_TIMEOUT");
|
|
512
512
|
}, this._WRITE_TIMEOUT_MSEC);
|
|
513
513
|
|
package/lib/switchbot.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
let parameterChecker = require("./parameter-checker.js");
|
|
3
|
+
let switchbotAdvertising = require("./switchbot-advertising.js");
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
let SwitchbotDevice = require("./switchbot-device.js");
|
|
6
|
+
let SwitchbotDeviceWoHand = require("./switchbot-device-wohand.js");
|
|
7
|
+
let SwitchbotDeviceWoCurtain = require("./switchbot-device-wocurtain.js");
|
|
8
|
+
let SwitchbotDeviceWoPresence = require("./switchbot-device-wopresence.js");
|
|
9
|
+
let SwitchbotDeviceWoContact = require("./switchbot-device-wocontact.js");
|
|
10
|
+
let SwitchbotDeviceWoSensorTH = require("./switchbot-device-wosensorth.js");
|
|
11
|
+
let SwitchbotDeviceWoHumi = require("./switchbot-device-wohumi.js");
|
|
12
|
+
let SwitchbotDeviceWoPlugMini = require("./switchbot-device-woplugmini.js");
|
|
13
|
+
let SwitchbotDeviceWoBulb = require("./switchbot-device-wobulb.js");
|
|
14
|
+
let SwitchbotDeviceWoStrip = require("./switchbot-device-wostrip.js");
|
|
15
15
|
|
|
16
16
|
class Switchbot {
|
|
17
17
|
/* ------------------------------------------------------------------
|
|
@@ -26,7 +26,7 @@ class Switchbot {
|
|
|
26
26
|
* ---------------------------------------------------------------- */
|
|
27
27
|
constructor(params) {
|
|
28
28
|
// Check parameters
|
|
29
|
-
|
|
29
|
+
let noble = null;
|
|
30
30
|
if (params && params.noble) {
|
|
31
31
|
noble = params.noble;
|
|
32
32
|
} else {
|
|
@@ -83,9 +83,9 @@ class Switchbot {
|
|
|
83
83
|
* `SwitchbotDevice` objects representing the found devices.
|
|
84
84
|
* ---------------------------------------------------------------- */
|
|
85
85
|
discover(params = {}) {
|
|
86
|
-
|
|
86
|
+
let promise = new Promise((resolve, reject) => {
|
|
87
87
|
// Check the parameters
|
|
88
|
-
|
|
88
|
+
let valid = parameterChecker.check(
|
|
89
89
|
params,
|
|
90
90
|
{
|
|
91
91
|
duration: { required: false, type: "integer", min: 1, max: 60000 },
|
|
@@ -110,7 +110,7 @@ class Switchbot {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
// Determine the values of the parameters
|
|
113
|
-
|
|
113
|
+
let p = {
|
|
114
114
|
duration: params.duration || this._DEFAULT_DISCOVERY_DURATION,
|
|
115
115
|
model: params.model || "",
|
|
116
116
|
id: params.id || "",
|
|
@@ -120,16 +120,16 @@ class Switchbot {
|
|
|
120
120
|
// Initialize the noble object
|
|
121
121
|
this._init()
|
|
122
122
|
.then(() => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
123
|
+
let peripherals = {};
|
|
124
|
+
let timer = null;
|
|
125
|
+
let finishDiscovery = () => {
|
|
126
126
|
if (timer) {
|
|
127
127
|
clearTimeout(timer);
|
|
128
128
|
}
|
|
129
129
|
this.noble.removeAllListeners("discover");
|
|
130
130
|
this.noble.stopScanning();
|
|
131
|
-
|
|
132
|
-
for (
|
|
131
|
+
let device_list = [];
|
|
132
|
+
for (let addr in peripherals) {
|
|
133
133
|
device_list.push(peripherals[addr]);
|
|
134
134
|
}
|
|
135
135
|
resolve(device_list);
|
|
@@ -137,11 +137,11 @@ class Switchbot {
|
|
|
137
137
|
|
|
138
138
|
// Set a handler for the 'discover' event
|
|
139
139
|
this.noble.on("discover", (peripheral) => {
|
|
140
|
-
|
|
140
|
+
let device = this._getDeviceObject(peripheral, p.id, p.model);
|
|
141
141
|
if (!device) {
|
|
142
142
|
return;
|
|
143
143
|
}
|
|
144
|
-
|
|
144
|
+
let id = device.id;
|
|
145
145
|
peripherals[id] = device;
|
|
146
146
|
|
|
147
147
|
if (this.ondiscover && typeof this.ondiscover === "function") {
|
|
@@ -177,13 +177,13 @@ class Switchbot {
|
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
_init() {
|
|
180
|
-
|
|
180
|
+
let promise = new Promise((resolve, reject) => {
|
|
181
181
|
switch (this.noble.state) {
|
|
182
182
|
case "poweredOn":
|
|
183
183
|
resolve();
|
|
184
184
|
return;
|
|
185
185
|
case ("unsupported", "unauthorized", "poweredOff"):
|
|
186
|
-
|
|
186
|
+
let err = new Error(
|
|
187
187
|
"Failed to initialize the Noble object: " + this.noble.state
|
|
188
188
|
);
|
|
189
189
|
reject(err);
|
|
@@ -193,7 +193,7 @@ class Switchbot {
|
|
|
193
193
|
if (state === "poweredOn") {
|
|
194
194
|
resolve();
|
|
195
195
|
} else {
|
|
196
|
-
|
|
196
|
+
let err = new Error(
|
|
197
197
|
"Failed to initialize the Noble object: " + state
|
|
198
198
|
);
|
|
199
199
|
reject(err);
|
|
@@ -205,9 +205,9 @@ class Switchbot {
|
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
_getDeviceObject(peripheral, id, model) {
|
|
208
|
-
|
|
208
|
+
let ad = switchbotAdvertising.parse(peripheral, this.onlog);
|
|
209
209
|
if (this._filterAdvertising(ad, id, model)) {
|
|
210
|
-
|
|
210
|
+
let device = null;
|
|
211
211
|
switch (ad.serviceData.model) {
|
|
212
212
|
case "H":
|
|
213
213
|
device = new SwitchbotDeviceWoHand(peripheral, this.noble);
|
|
@@ -258,7 +258,7 @@ class Switchbot {
|
|
|
258
258
|
}
|
|
259
259
|
if (id) {
|
|
260
260
|
id = id.toLowerCase().replace(/\:/g, "");
|
|
261
|
-
|
|
261
|
+
let ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
262
262
|
if (ad_id !== id) {
|
|
263
263
|
return false;
|
|
264
264
|
}
|
|
@@ -324,9 +324,9 @@ class Switchbot {
|
|
|
324
324
|
* Nothing will be passed to the `resolve()`.
|
|
325
325
|
* ---------------------------------------------------------------- */
|
|
326
326
|
startScan(params) {
|
|
327
|
-
|
|
327
|
+
let promise = new Promise((resolve, reject) => {
|
|
328
328
|
// Check the parameters
|
|
329
|
-
|
|
329
|
+
let valid = parameterChecker.check(
|
|
330
330
|
params,
|
|
331
331
|
{
|
|
332
332
|
model: {
|
|
@@ -352,14 +352,14 @@ class Switchbot {
|
|
|
352
352
|
this._init()
|
|
353
353
|
.then(() => {
|
|
354
354
|
// Determine the values of the parameters
|
|
355
|
-
|
|
355
|
+
let p = {
|
|
356
356
|
model: params.model || "",
|
|
357
357
|
id: params.id || "",
|
|
358
358
|
};
|
|
359
359
|
|
|
360
360
|
// Set a handler for the 'discover' event
|
|
361
361
|
this.noble.on("discover", (peripheral) => {
|
|
362
|
-
|
|
362
|
+
let ad = switchbotAdvertising.parse(peripheral, this.onlog);
|
|
363
363
|
if (this._filterAdvertising(ad, p.id, p.model)) {
|
|
364
364
|
if (
|
|
365
365
|
this.onadvertisement &&
|
|
@@ -419,7 +419,7 @@ class Switchbot {
|
|
|
419
419
|
wait(msec) {
|
|
420
420
|
return new Promise((resolve, reject) => {
|
|
421
421
|
// Check the parameters
|
|
422
|
-
|
|
422
|
+
let valid = parameterChecker.check(
|
|
423
423
|
{ msec: msec },
|
|
424
424
|
{
|
|
425
425
|
msec: { required: true, type: "integer", min: 0 },
|
package/package.json
CHANGED