node-switchbot 1.6.0-beta.9 → 1.6.1-beta.0
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/switchbot-advertising.js +16 -6
- package/lib/switchbot-device-wobulb.js +5 -5
- package/lib/switchbot-device-wocontact.js +1 -1
- package/lib/switchbot-device-wocurtain.js +1 -1
- package/lib/switchbot-device-wohand.js +1 -1
- package/lib/switchbot-device-wohumi.js +1 -1
- package/lib/switchbot-device-woplugmini.js +5 -5
- package/lib/switchbot-device-wopresence.js +1 -1
- package/lib/switchbot-device-wosensorth.js +1 -1
- package/lib/switchbot-device-wostrip.js +5 -5
- package/lib/switchbot-device.js +2 -2
- package/lib/switchbot.js +12 -12
- 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.
|
|
@@ -69,8 +69,8 @@ class SwitchbotAdvertising {
|
|
|
69
69
|
let manufacturerData = ad.manufacturerData;
|
|
70
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;
|
|
@@ -127,7 +127,7 @@ class SwitchbotAdvertising {
|
|
|
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);
|
|
@@ -255,12 +255,10 @@ class SwitchbotAdvertising {
|
|
|
255
255
|
return null;
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
|
|
259
258
|
let byte1 = buf.readUInt8(1);
|
|
260
259
|
let byte2 = buf.readUInt8(2);
|
|
261
260
|
let byte5 = buf.readUInt8(5);
|
|
262
261
|
|
|
263
|
-
|
|
264
262
|
let tested = byte1 & 0b10000000 ? true : false;
|
|
265
263
|
let movement = byte1 & 0b01000000 ? true : false;
|
|
266
264
|
let battery = byte2 & 0b01111111;
|
|
@@ -350,7 +348,6 @@ class SwitchbotAdvertising {
|
|
|
350
348
|
let battery = byte2 & 0b01111111; // %
|
|
351
349
|
let inMotion = byte3 & 0b10000000 ? true : false;
|
|
352
350
|
let currPosition = byte3 & 0b01111111; // current positon %
|
|
353
|
-
//let currPosition = max(min(byte3 & 0b01111111, 100), 0) //byte3 & 0b01111111; // current positon %
|
|
354
351
|
let lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10)
|
|
355
352
|
let deviceChain = byte4 & 0b00000111;
|
|
356
353
|
|
|
@@ -388,6 +385,7 @@ class SwitchbotAdvertising {
|
|
|
388
385
|
let byte9 = buf.readUInt8(9);
|
|
389
386
|
let byte10 = buf.readUInt8(10);//bulb mode
|
|
390
387
|
|
|
388
|
+
let power = byte1;
|
|
391
389
|
let red = byte3;
|
|
392
390
|
let green = byte4;
|
|
393
391
|
let blue = byte5;
|
|
@@ -404,6 +402,7 @@ class SwitchbotAdvertising {
|
|
|
404
402
|
model: "u",
|
|
405
403
|
modelName: "WoBulb",
|
|
406
404
|
color_temperature: color_temperature,
|
|
405
|
+
power: power,
|
|
407
406
|
state: state,
|
|
408
407
|
red: red,
|
|
409
408
|
green: green,
|
|
@@ -566,6 +565,11 @@ class SwitchbotAdvertising {
|
|
|
566
565
|
return null;
|
|
567
566
|
}
|
|
568
567
|
|
|
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
|
|
569
573
|
let byte7 = buf.readUInt8(7);
|
|
570
574
|
let byte8 = buf.readUInt8(8);
|
|
571
575
|
let byte9 = buf.readUInt8(9);
|
|
@@ -573,6 +577,9 @@ class SwitchbotAdvertising {
|
|
|
573
577
|
|
|
574
578
|
let state = byte7 & 0b10000000 ? true : false;
|
|
575
579
|
let brightness = byte7 & 0b01111111;
|
|
580
|
+
let red = byte3;
|
|
581
|
+
let green = byte4;
|
|
582
|
+
let blue = byte5;
|
|
576
583
|
let delay = byte8 & 0b10000000;
|
|
577
584
|
let preset = byte8 & 0b00001000;
|
|
578
585
|
let color_mode = byte8 & 0b00000111;
|
|
@@ -584,6 +591,9 @@ class SwitchbotAdvertising {
|
|
|
584
591
|
modelName: "WoStrip",
|
|
585
592
|
state: state,
|
|
586
593
|
brightness: brightness,
|
|
594
|
+
red: red,
|
|
595
|
+
green: green,
|
|
596
|
+
blue: blue,
|
|
587
597
|
delay: delay,
|
|
588
598
|
preset: preset,
|
|
589
599
|
color_mode: color_mode,
|
|
@@ -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
|
/**
|
|
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
|
/* ------------------------------------------------------------------
|
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
|
/* ------------------------------------------------------------------
|
package/package.json
CHANGED