node-switchbot 1.4.2-beta.1 → 1.4.2-beta.10
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
CHANGED
|
@@ -54,12 +54,17 @@ But some functionalities of this module were developed through trial and error.
|
|
|
54
54
|
- [`close()` method](#close-method)
|
|
55
55
|
- [`pause()` method](#pause-method)
|
|
56
56
|
- [`runToPos()` method](#runtopos-method)
|
|
57
|
+
- [`SwitchbotDeviceWoPlugMini` object](#switchbotdevicewoplugmini-object)
|
|
58
|
+
- [`turnOn()` method](#turnon-method)
|
|
59
|
+
- [`turnOff()` method](#turnoff-method)
|
|
60
|
+
- [`toggle()` method](#toggle-method)
|
|
57
61
|
- [Advertisement data](#advertisement-data)
|
|
58
62
|
- [Bot (WoHand)](#bot-wohand)
|
|
59
63
|
- [Meter (WoSensorTH)](#meter-wosensorth)
|
|
60
64
|
- [Curtain (WoCurtain)](#curtain-wocurtain)
|
|
61
65
|
- [Contact (WoContact)](#contact-wocontact)
|
|
62
66
|
- [Motion (WoMotion)](#motion-womotion)
|
|
67
|
+
- [PlugMini (WoPlugMini)](#plugmini-woplugmini)
|
|
63
68
|
- [Release Note](#release-note)
|
|
64
69
|
- [References](#references)
|
|
65
70
|
- [License](#license)
|
|
@@ -340,7 +345,7 @@ The `startScan()` method starts to scan advertising packets coming from devices.
|
|
|
340
345
|
|
|
341
346
|
| Property | Type | Required | Description |
|
|
342
347
|
| :------- | :----- | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
343
|
-
| `model` | String | Optional | `"H"`, `"T"` or `"
|
|
348
|
+
| `model` | String | Optional | `"H"`, `"T"`, `"c"`, `"g"` or `"j"`. If `"H"` is specified, this method will discover only Bots. If `"T"` is specified, this method will discover only Meters. If `"c"` is specified, this method will discover only Curtains. If `"g"` or `"j"` is specified, this method will discover only (US/JP) Plug Minis. |
|
|
344
349
|
| `id` | String | Optional | If this value is set, this method will discover only a device whose ID is as same as this value. The ID is identical to the MAC address. This value is case-insensitive, and colons are ignored. |
|
|
345
350
|
|
|
346
351
|
Whenever a packet is received, the callback function set to the [`onadvertisement`](#Switchbot-onadvertisement-event-handler) property of the [`Switchbot`](#Switchbot-object) object will be called. When a packet is received, a hash object representing the packet will be passed to the callback function.
|
|
@@ -835,6 +840,31 @@ switchbot
|
|
|
835
840
|
});
|
|
836
841
|
```
|
|
837
842
|
|
|
843
|
+
---
|
|
844
|
+
## `SwitchbotDeviceWoPlugMini` object
|
|
845
|
+
|
|
846
|
+
The `SwitchbotDeviceWoPlugMini ` object represents a PlugMini, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
|
|
847
|
+
|
|
848
|
+
Actually, the `SwitchbotDeviceWoPlugMini ` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
|
|
849
|
+
|
|
850
|
+
### `turnOn()` method
|
|
851
|
+
|
|
852
|
+
The `turnOn()` method sends a turn-on command to the PlugMini. This method returns a `Promise` object. A `boolean` value indicating whether the PlugMini is on (`true`), is passed to the `resolve()` method of the Promise.
|
|
853
|
+
|
|
854
|
+
If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
|
|
855
|
+
|
|
856
|
+
### `turnOff()` method
|
|
857
|
+
|
|
858
|
+
The `turnOff()` method sends a turn-off command to the PlugMini. This method returns a `Promise` object. A `boolean` value indicating whether the PlugMini is off (`false`), is passed to the `resolve()` method of the Promise.
|
|
859
|
+
|
|
860
|
+
If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
|
|
861
|
+
|
|
862
|
+
### `toggle()` method
|
|
863
|
+
|
|
864
|
+
The `toggle()` method sends a toggle command to the PlugMini, toggling between the on and off state. This method returns a `Promise` object. A `boolean` value indicating whether the PlugMini is on (`true`) or off (`false`), is passed to the `resolve()` method of the Promise.
|
|
865
|
+
|
|
866
|
+
If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
|
|
867
|
+
|
|
838
868
|
---
|
|
839
869
|
|
|
840
870
|
## Advertisement data
|
|
@@ -1023,6 +1053,42 @@ Structure of the `serviceData`:
|
|
|
1023
1053
|
| `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
|
|
1024
1054
|
| `lightLevel` | String | This value indicates the light level (`dark`, `bright`). |
|
|
1025
1055
|
|
|
1056
|
+
### PlugMini (WoPlugMini)
|
|
1057
|
+
|
|
1058
|
+
Example of the advertisement data:
|
|
1059
|
+
|
|
1060
|
+
```json
|
|
1061
|
+
{
|
|
1062
|
+
"id": "cd2409ea3e9441f87d4580e0380a62bf",
|
|
1063
|
+
"address": "60:55:f9:35:f6:a6",
|
|
1064
|
+
"rssi": -50,
|
|
1065
|
+
"serviceData": {
|
|
1066
|
+
"model": "j",
|
|
1067
|
+
"modelName": "WoPlugMini",
|
|
1068
|
+
"state": "off",
|
|
1069
|
+
"delay": false,
|
|
1070
|
+
"timer": false,
|
|
1071
|
+
"syncUtcTime": true,
|
|
1072
|
+
"wifiRssi": 48,
|
|
1073
|
+
"overload": false,
|
|
1074
|
+
"currentPower": 0
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
```
|
|
1078
|
+
|
|
1079
|
+
Structure of the `serviceData`:
|
|
1080
|
+
|
|
1081
|
+
| Property | Type | Description |
|
|
1082
|
+
| :------------ | :------ | :---------------------------------------------------------------------------------- |
|
|
1083
|
+
| `model` | String | This value is always `"j"` or `"g"`, which means "PlugMini" (JP or US). |
|
|
1084
|
+
| `modelName` | String | This value is always `"WoPlugMini"`, which means "PlugMini". |
|
|
1085
|
+
| `state ` | Boolean | This value indicates whether the plug mini is turned on (`true`) or not (`false`). |
|
|
1086
|
+
| `delay` | Boolean | Indicates whether a delay is present. |
|
|
1087
|
+
| `timer` | Boolean | Indicates whether a timer is present. |
|
|
1088
|
+
| `syncUtcTime` | boolean | Indicates whether the UTC time has been synchronized. |
|
|
1089
|
+
| `overload` | boolean | Indicates whether the Plug Mini is overloaded, more than 15A current overload. |
|
|
1090
|
+
| `currentPower`| Float | Current power consumption in Watts. |
|
|
1091
|
+
|
|
1026
1092
|
---
|
|
1027
1093
|
|
|
1028
1094
|
## References
|
|
@@ -83,41 +83,29 @@ class SwitchbotAdvertising {
|
|
|
83
83
|
let sd = null;
|
|
84
84
|
|
|
85
85
|
if (model === "H") {
|
|
86
|
-
|
|
87
|
-
sd = this._parseServiceDataForWoHand(buf, onlog);
|
|
86
|
+
sd = this._parseServiceDataForWoHand(buf, onlog);//WoHand
|
|
88
87
|
} else if (model === "T") {
|
|
89
|
-
|
|
90
|
-
sd = this._parseServiceDataForWoSensorTH(buf, onlog);
|
|
88
|
+
sd = this._parseServiceDataForWoSensorTH(buf, onlog);//WoSensorTH
|
|
91
89
|
} else if (model === "e") {
|
|
92
|
-
|
|
93
|
-
sd = this._parseServiceDataForWoHumi(buf, onlog);
|
|
90
|
+
sd = this._parseServiceDataForWoHumi(buf, onlog);//WoHumi
|
|
94
91
|
} else if (model === "s") {
|
|
95
|
-
|
|
96
|
-
sd = this._parseServiceDataForWoPresence(buf, onlog);
|
|
92
|
+
sd = this._parseServiceDataForWoPresence(buf, onlog);//WoPresence
|
|
97
93
|
} else if (model === "d") {
|
|
98
|
-
|
|
99
|
-
sd = this._parseServiceDataForWoContact(buf, onlog);
|
|
94
|
+
sd = this._parseServiceDataForWoContact(buf, onlog);//WoContact
|
|
100
95
|
} else if (model === "c") {
|
|
101
|
-
|
|
102
|
-
sd = this._parseServiceDataForWoCurtain(buf, onlog);
|
|
96
|
+
sd = this._parseServiceDataForWoCurtain(buf, onlog);// WoCurtain
|
|
103
97
|
} else if (model === "u") {
|
|
104
|
-
|
|
105
|
-
sd = this._parseServiceDataForWoColorBulb(buf, onlog);
|
|
98
|
+
sd = this._parseServiceDataForWoBulb(manufacturerData, onlog);// WoBulb
|
|
106
99
|
} else if (model === "g") {
|
|
107
|
-
// WoPlugMini (US)
|
|
108
|
-
sd = this._parseServiceDataForWoPlugMiniUS(manufacturerData, onlog);
|
|
100
|
+
sd = this._parseServiceDataForWoPlugMiniUS(manufacturerData, onlog); // WoPlugMini (US)
|
|
109
101
|
} else if (model === "j") {
|
|
110
|
-
|
|
111
|
-
sd = this._parseServiceDataForWoPlugMiniJP(manufacturerData, onlog);
|
|
102
|
+
sd = this._parseServiceDataForWoPlugMiniJP(manufacturerData, onlog);// WoPlugMini (JP)
|
|
112
103
|
} else if (model === "o") {
|
|
113
|
-
|
|
114
|
-
sd = this._parseServiceDataForWoSmartLock(buf, onlog);
|
|
104
|
+
sd = this._parseServiceDataForWoSmartLock(buf, onlog);// WoSmartLock
|
|
115
105
|
} else if (model === "i") {
|
|
116
|
-
|
|
117
|
-
sd = this._parseServiceDataForWoSensorTHPlus(buf, onlog);
|
|
106
|
+
sd = this._parseServiceDataForWoSensorTHPlus(buf, onlog);// WoMeterPlus
|
|
118
107
|
} else if (model === "r") {
|
|
119
|
-
|
|
120
|
-
sd = this._parseServiceDataForWoLEDStripLight(buf, onlog);
|
|
108
|
+
sd = this._parseServiceDataForWoStrip(buf, onlog);// WoStrip
|
|
121
109
|
} else {
|
|
122
110
|
if (onlog && typeof onlog === "function") {
|
|
123
111
|
onlog(
|
|
@@ -363,11 +351,11 @@ class SwitchbotAdvertising {
|
|
|
363
351
|
return data;
|
|
364
352
|
}
|
|
365
353
|
|
|
366
|
-
|
|
367
|
-
if (buf.length !==
|
|
354
|
+
_parseServiceDataForWoBulb(buf, onlog) {
|
|
355
|
+
if (buf.length !== 13) {
|
|
368
356
|
if (onlog && typeof onlog === "function") {
|
|
369
357
|
onlog(
|
|
370
|
-
`[
|
|
358
|
+
`[_parseServiceDataForWoBulb] Buffer length ${buf.length} !== 13!`
|
|
371
359
|
);
|
|
372
360
|
}
|
|
373
361
|
return null;
|
|
@@ -378,8 +366,8 @@ class SwitchbotAdvertising {
|
|
|
378
366
|
const byte12 = buf.readUInt8(12); // byte12: bit7: overload?
|
|
379
367
|
const byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
|
|
380
368
|
|
|
381
|
-
const state = byte9 ===
|
|
382
|
-
const lightLevel = byte9 &
|
|
369
|
+
const state = byte9 === 0x01 ? "off" : byte9 === 0x80 ? "on" : null;
|
|
370
|
+
const lightLevel = byte9 & 0b00000011;
|
|
383
371
|
const delay = !!(byte10 & 0b00000001);
|
|
384
372
|
const networkStatus = !!(byte10 & 0b00000001);
|
|
385
373
|
const statePreset = !!(byte10 & 0b00000010);
|
|
@@ -387,11 +375,15 @@ class SwitchbotAdvertising {
|
|
|
387
375
|
const wifiRssi = byte11;
|
|
388
376
|
const dynamicRate = !!(byte12 & 0b10000000);
|
|
389
377
|
const loopIndex = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
|
|
390
|
-
// TODO: voltage ???
|
|
391
378
|
|
|
392
379
|
const data = {
|
|
393
380
|
model: "u",
|
|
394
|
-
modelName: "
|
|
381
|
+
modelName: "WoBulb",
|
|
382
|
+
state: state,
|
|
383
|
+
};
|
|
384
|
+
/* const data = {
|
|
385
|
+
model: "u",
|
|
386
|
+
modelName: "WoBulb",
|
|
395
387
|
state: state,
|
|
396
388
|
lightLevel: lightLevel,
|
|
397
389
|
delay: delay,
|
|
@@ -401,7 +393,7 @@ class SwitchbotAdvertising {
|
|
|
401
393
|
wifiRssi: wifiRssi,
|
|
402
394
|
dynamicRate: dynamicRate,
|
|
403
395
|
loopIndex: loopIndex,
|
|
404
|
-
}
|
|
396
|
+
};*/
|
|
405
397
|
|
|
406
398
|
return data;
|
|
407
399
|
}
|
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
const SwitchbotDevice = require("./switchbot-device.js");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/
|
|
5
|
+
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md
|
|
6
6
|
*/
|
|
7
|
-
class
|
|
7
|
+
class SwitchbotDeviceWoBulb extends SwitchbotDevice {
|
|
8
8
|
/**
|
|
9
9
|
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
|
|
10
10
|
*/
|
|
11
11
|
readState() {
|
|
12
|
-
return this._operateBot([0x57, 0x0f,
|
|
12
|
+
return this._operateBot([0x57, 0x0f, 0x48, 0x01]);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* @private
|
|
17
17
|
*/
|
|
18
18
|
_setState(reqByteArray) {
|
|
19
|
-
const base = [0x57, 0x0f,
|
|
19
|
+
const base = [0x57, 0x0f, 0x47, 0x01];
|
|
20
20
|
return this._operateBot([].concat(base, reqByteArray));
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -78,4 +78,4 @@ class SwitchbotDeviceWoPlugMini extends SwitchbotDevice {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
module.exports =
|
|
81
|
+
module.exports = SwitchbotDeviceWoBulb;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const SwitchbotDevice = require("./switchbot-device.js");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md
|
|
6
|
+
*/
|
|
7
|
+
class SwitchbotDeviceWoStrip extends SwitchbotDevice {
|
|
8
|
+
/**
|
|
9
|
+
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
|
|
10
|
+
*/
|
|
11
|
+
readState() {
|
|
12
|
+
return this._operateBot([0x57, 0x0f, 0x4A, 0x01]);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
_setState(reqByteArray) {
|
|
19
|
+
const base = [0x57, 0x0f, 0x49, 0x01];
|
|
20
|
+
return this._operateBot([].concat(base, reqByteArray));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
|
|
25
|
+
*/
|
|
26
|
+
turnOn() {
|
|
27
|
+
return this._setState([0x01, 0x01]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
|
|
32
|
+
*/
|
|
33
|
+
turnOff() {
|
|
34
|
+
return this._setState([0x01, 0x02]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
|
|
39
|
+
*/
|
|
40
|
+
toggle() {
|
|
41
|
+
return this._setState([0x02, 0x03]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
_operateBot(bytes) {
|
|
48
|
+
const req_buf = Buffer.from(bytes);
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
this._command(req_buf)
|
|
51
|
+
.then((res_bytes) => {
|
|
52
|
+
const res_buf = Buffer.from(res_bytes);
|
|
53
|
+
if (res_buf.length === 2) {
|
|
54
|
+
let code = res_buf.readUInt8(1);
|
|
55
|
+
if (code === 0x00 || code === 0x80) {
|
|
56
|
+
const is_on = code === 0x80;
|
|
57
|
+
resolve(is_on);
|
|
58
|
+
} else {
|
|
59
|
+
reject(
|
|
60
|
+
new Error(
|
|
61
|
+
"The device returned an error: 0x" + res_buf.toString("hex")
|
|
62
|
+
)
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
reject(
|
|
67
|
+
new Error(
|
|
68
|
+
"Expecting a 2-byte response, got instead: 0x" +
|
|
69
|
+
res_buf.toString("hex")
|
|
70
|
+
)
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
.catch((error) => {
|
|
75
|
+
reject(error);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = SwitchbotDeviceWoStrip;
|
package/lib/switchbot.js
CHANGED
|
@@ -10,6 +10,8 @@ const SwitchbotDeviceWoContact = require("./switchbot-device-wocontact.js");
|
|
|
10
10
|
const SwitchbotDeviceWoSensorTH = require("./switchbot-device-wosensorth.js");
|
|
11
11
|
const SwitchbotDeviceWoHumi = require("./switchbot-device-wohumi.js");
|
|
12
12
|
const SwitchbotDeviceWoPlugMini = require("./switchbot-device-woplugmini.js");
|
|
13
|
+
const SwitchbotDeviceWoBulb = require("./switchbot-device-wobulb.js");
|
|
14
|
+
const SwitchbotDeviceWoStrip = require("./switchbot-device-wostrip.js");
|
|
13
15
|
|
|
14
16
|
class Switchbot {
|
|
15
17
|
/* ------------------------------------------------------------------
|
|
@@ -226,7 +228,7 @@ class Switchbot {
|
|
|
226
228
|
device = new SwitchbotDeviceWoCurtain(peripheral, this.noble);
|
|
227
229
|
break;
|
|
228
230
|
case "u":
|
|
229
|
-
device = new
|
|
231
|
+
device = new SwitchbotDeviceWoBulb(peripheral, this.noble);
|
|
230
232
|
break;
|
|
231
233
|
case "g":
|
|
232
234
|
case "j":
|
|
@@ -239,7 +241,7 @@ class Switchbot {
|
|
|
239
241
|
device = new SwitchbotDeviceWoSensorTHPlus(peripheral, this.noble);
|
|
240
242
|
break;
|
|
241
243
|
case "r":
|
|
242
|
-
device = new
|
|
244
|
+
device = new SwitchbotDeviceWoStrip(peripheral, this.noble);
|
|
243
245
|
break;
|
|
244
246
|
default: // 'resetting', 'unknown'
|
|
245
247
|
device = new SwitchbotDevice(peripheral, this.noble);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-switchbot",
|
|
3
|
-
"version": "1.4.2-beta.
|
|
4
|
-
"description": "The node-switchbot is a Node.js module which allows you to
|
|
3
|
+
"version": "1.4.2-beta.10",
|
|
4
|
+
"description": "The node-switchbot is a Node.js module which allows you to control your Switchbot Devices through Bluetooth (BLE).",
|
|
5
5
|
"main": "./lib/switchbot.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib"
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"Bluetooth smart",
|
|
26
26
|
"Bluetooth"
|
|
27
27
|
],
|
|
28
|
-
"homepage": "https://github.com/OpenWonderLabs",
|
|
28
|
+
"homepage": "https://github.com/OpenWonderLabs/node-switchbot",
|
|
29
29
|
"author": "OpenWonderLabs (https://github.com/OpenWonderLabs)",
|
|
30
30
|
"license": "MIT",
|
|
31
31
|
"repository": {
|