node-switchbot 1.2.1-beta.9 → 1.4.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/CHANGELOG.md +48 -21
- package/LICENSE +1 -1
- package/README.md +389 -260
- package/lib/parameter-checker.js +271 -213
- package/lib/switchbot-advertising.js +280 -164
- package/lib/switchbot-device-wocontact.js +4 -7
- package/lib/switchbot-device-wocurtain.js +106 -91
- package/lib/switchbot-device-wohand.js +69 -65
- package/lib/switchbot-device-wohumi.js +69 -65
- package/lib/switchbot-device-woplugmini.js +81 -0
- package/lib/switchbot-device-wopresence.js +4 -7
- package/lib/switchbot-device-wosensorth.js +4 -7
- package/lib/switchbot-device.js +188 -149
- package/lib/switchbot.js +271 -233
- package/package.json +41 -41
package/lib/switchbot.js
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
const parameterChecker = require(
|
|
3
|
-
const switchbotAdvertising = require(
|
|
1
|
+
"use strict";
|
|
2
|
+
const parameterChecker = require("./parameter-checker.js");
|
|
3
|
+
const switchbotAdvertising = require("./switchbot-advertising.js");
|
|
4
4
|
|
|
5
|
-
const SwitchbotDevice = require(
|
|
6
|
-
const SwitchbotDeviceWoHand = require(
|
|
7
|
-
const SwitchbotDeviceWoCurtain = require(
|
|
8
|
-
const SwitchbotDeviceWoPresence = require(
|
|
9
|
-
const SwitchbotDeviceWoContact = require(
|
|
10
|
-
const SwitchbotDeviceWoSensorTH = require(
|
|
11
|
-
const SwitchbotDeviceWoHumi = require(
|
|
5
|
+
const SwitchbotDevice = require("./switchbot-device.js");
|
|
6
|
+
const SwitchbotDeviceWoHand = require("./switchbot-device-wohand.js");
|
|
7
|
+
const SwitchbotDeviceWoCurtain = require("./switchbot-device-wocurtain.js");
|
|
8
|
+
const SwitchbotDeviceWoPresence = require("./switchbot-device-wopresence.js");
|
|
9
|
+
const SwitchbotDeviceWoContact = require("./switchbot-device-wocontact.js");
|
|
10
|
+
const SwitchbotDeviceWoSensorTH = require("./switchbot-device-wosensorth.js");
|
|
11
|
+
const SwitchbotDeviceWoHumi = require("./switchbot-device-wohumi.js");
|
|
12
|
+
const SwitchbotDeviceWoPlugMini = require("./switchbot-device-woplugmini.js");
|
|
12
13
|
|
|
13
14
|
class Switchbot {
|
|
14
15
|
/* ------------------------------------------------------------------
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
* Constructor
|
|
17
|
+
*
|
|
18
|
+
* [Arguments]
|
|
19
|
+
* - params | Object | Optional |
|
|
20
|
+
* - noble | Noble | Optional | The Nobel object created by the noble module.
|
|
21
|
+
* | | | This parameter is optional.
|
|
22
|
+
* | | | If you don't specify this parameter, this
|
|
23
|
+
* | | | module automatically creates it.
|
|
24
|
+
* ---------------------------------------------------------------- */
|
|
24
25
|
constructor(params) {
|
|
25
26
|
// Check parameters
|
|
26
27
|
let noble = null;
|
|
27
28
|
if (params && params.noble) {
|
|
28
29
|
noble = params.noble;
|
|
29
30
|
} else {
|
|
30
|
-
noble = require(
|
|
31
|
+
noble = require("@abandonware/noble");
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
// Public properties
|
|
@@ -38,56 +39,64 @@ class Switchbot {
|
|
|
38
39
|
|
|
39
40
|
// Private properties
|
|
40
41
|
this._scanning = false;
|
|
41
|
-
this._DEFAULT_DISCOVERY_DURATION = 5000
|
|
42
|
+
this._DEFAULT_DISCOVERY_DURATION = 5000;
|
|
42
43
|
this._PRIMARY_SERVICE_UUID_LIST = [];
|
|
43
|
-
}
|
|
44
|
+
}
|
|
44
45
|
|
|
45
46
|
/* ------------------------------------------------------------------
|
|
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
|
-
|
|
47
|
+
* discover([params])
|
|
48
|
+
* - Discover switchbot devices
|
|
49
|
+
*
|
|
50
|
+
* [Arguments]
|
|
51
|
+
* - params | Object | Optional |
|
|
52
|
+
* - duration | Integer | Optional | Duration for discovery process (msec).
|
|
53
|
+
* | | | The value must be in the range of 1 to 60000.
|
|
54
|
+
* | | | The default value is 5000 (msec).
|
|
55
|
+
* - model | String | Optional | "H", "T", "e", "s", "d", "c", "u", "g", "o", "i", or "r".
|
|
56
|
+
* | | | If "H" is specified, this method will discover only Bots.
|
|
57
|
+
* | | | If "T" is specified, this method will discover only Meters.
|
|
58
|
+
* | | | If "e" is specified, this method will discover only Humidifiers.
|
|
59
|
+
* | | | If "s" is specified, this method will discover only Motion Sensors.
|
|
60
|
+
* | | | If "d" is specified, this method will discover only Contact Sensors.
|
|
61
|
+
* | | | If "c" is specified, this method will discover only Curtains.
|
|
62
|
+
* | | | If "u" is specified, this method will discover only Color Bulbs.
|
|
63
|
+
* | | | If "g" is specified, this method will discover only Plugs.
|
|
64
|
+
* | | | If "o" is specified, this method will discover only Locks.
|
|
65
|
+
* | | | If "i" is specified, this method will discover only Meter Pluses.
|
|
66
|
+
* | | | If "r" is specified, this method will discover only Locks.
|
|
67
|
+
* - id | String | Optional | If this value is set, this method will discover
|
|
68
|
+
* | | | only a device whose ID is as same as this value.
|
|
69
|
+
* | | | The ID is identical to the MAC address.
|
|
70
|
+
* | | | This parameter is case-insensitive, and
|
|
71
|
+
* | | | colons are ignored.
|
|
72
|
+
* - quick | Boolean | Optional | If this value is true, this method finishes
|
|
73
|
+
* | | | the discovery process when the first device
|
|
74
|
+
* | | | is found, then calls the resolve() function
|
|
75
|
+
* | | | without waiting the specified duration.
|
|
76
|
+
* | | | The default value is false.
|
|
77
|
+
*
|
|
78
|
+
* [Return value]
|
|
79
|
+
* - Promise object
|
|
80
|
+
* An array will be passed to the `resolve()`, which includes
|
|
81
|
+
* `SwitchbotDevice` objects representing the found devices.
|
|
82
|
+
* ---------------------------------------------------------------- */
|
|
82
83
|
discover(params = {}) {
|
|
83
84
|
let promise = new Promise((resolve, reject) => {
|
|
84
85
|
// Check the parameters
|
|
85
|
-
let valid = parameterChecker.check(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
let valid = parameterChecker.check(
|
|
87
|
+
params,
|
|
88
|
+
{
|
|
89
|
+
duration: { required: false, type: "integer", min: 1, max: 60000 },
|
|
90
|
+
model: {
|
|
91
|
+
required: false,
|
|
92
|
+
type: "string",
|
|
93
|
+
enum: ["H", "T", "e", "s", "d", "c", "u", "g", "j", "o", "i", "r"],
|
|
94
|
+
},
|
|
95
|
+
id: { required: false, type: "string", min: 12, max: 17 },
|
|
96
|
+
quick: { required: false, type: "boolean" },
|
|
97
|
+
},
|
|
98
|
+
false
|
|
99
|
+
);
|
|
91
100
|
|
|
92
101
|
if (!valid) {
|
|
93
102
|
reject(new Error(parameterChecker.error.message));
|
|
@@ -101,60 +110,66 @@ class Switchbot {
|
|
|
101
110
|
// Determine the values of the parameters
|
|
102
111
|
let p = {
|
|
103
112
|
duration: params.duration || this._DEFAULT_DISCOVERY_DURATION,
|
|
104
|
-
model: params.model ||
|
|
105
|
-
id: params.id ||
|
|
106
|
-
quick: params.quick ? true : false
|
|
113
|
+
model: params.model || "",
|
|
114
|
+
id: params.id || "",
|
|
115
|
+
quick: params.quick ? true : false,
|
|
107
116
|
};
|
|
108
117
|
|
|
109
118
|
// Initialize the noble object
|
|
110
|
-
this._init()
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
this._init()
|
|
120
|
+
.then(() => {
|
|
121
|
+
let peripherals = {};
|
|
122
|
+
let timer = null;
|
|
123
|
+
let finishDiscovery = () => {
|
|
124
|
+
if (timer) {
|
|
125
|
+
clearTimeout(timer);
|
|
126
|
+
}
|
|
127
|
+
this.noble.removeAllListeners("discover");
|
|
128
|
+
this.noble.stopScanning();
|
|
129
|
+
let device_list = [];
|
|
130
|
+
for (let addr in peripherals) {
|
|
131
|
+
device_list.push(peripherals[addr]);
|
|
132
|
+
}
|
|
133
|
+
resolve(device_list);
|
|
134
|
+
};
|
|
125
135
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
136
|
+
// Set a handler for the 'discover' event
|
|
137
|
+
this.noble.on("discover", (peripheral) => {
|
|
138
|
+
let device = this._getDeviceObject(peripheral, p.id, p.model);
|
|
139
|
+
if (!device) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
let id = device.id;
|
|
143
|
+
peripherals[id] = device;
|
|
134
144
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
145
|
+
if (this.ondiscover && typeof this.ondiscover === "function") {
|
|
146
|
+
this.ondiscover(device);
|
|
147
|
+
}
|
|
138
148
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
149
|
+
if (p.quick) {
|
|
150
|
+
finishDiscovery();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
144
154
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
155
|
+
// Start scanning
|
|
156
|
+
this.noble.startScanning(
|
|
157
|
+
this._PRIMARY_SERVICE_UUID_LIST,
|
|
158
|
+
false,
|
|
159
|
+
(error) => {
|
|
160
|
+
if (error) {
|
|
161
|
+
reject(error);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
timer = setTimeout(() => {
|
|
165
|
+
finishDiscovery();
|
|
166
|
+
}, p.duration);
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
})
|
|
170
|
+
.catch((error) => {
|
|
171
|
+
reject(error);
|
|
154
172
|
});
|
|
155
|
-
}).catch((error) => {
|
|
156
|
-
reject(error);
|
|
157
|
-
});
|
|
158
173
|
});
|
|
159
174
|
return promise;
|
|
160
175
|
}
|
|
@@ -162,19 +177,23 @@ class Switchbot {
|
|
|
162
177
|
_init() {
|
|
163
178
|
let promise = new Promise((resolve, reject) => {
|
|
164
179
|
switch (this.noble.state) {
|
|
165
|
-
case
|
|
180
|
+
case "poweredOn":
|
|
166
181
|
resolve();
|
|
167
182
|
return;
|
|
168
|
-
case
|
|
169
|
-
let err = new Error(
|
|
183
|
+
case ("unsupported", "unauthorized", "poweredOff"):
|
|
184
|
+
let err = new Error(
|
|
185
|
+
"Failed to initialize the Noble object: " + this.noble.state
|
|
186
|
+
);
|
|
170
187
|
reject(err);
|
|
171
188
|
return;
|
|
172
189
|
default: // 'resetting', 'unknown'
|
|
173
|
-
this.noble.once(
|
|
174
|
-
if (state ===
|
|
190
|
+
this.noble.once("stateChange", (state) => {
|
|
191
|
+
if (state === "poweredOn") {
|
|
175
192
|
resolve();
|
|
176
193
|
} else {
|
|
177
|
-
let err = new Error(
|
|
194
|
+
let err = new Error(
|
|
195
|
+
"Failed to initialize the Noble object: " + state
|
|
196
|
+
);
|
|
178
197
|
reject(err);
|
|
179
198
|
}
|
|
180
199
|
});
|
|
@@ -188,37 +207,38 @@ class Switchbot {
|
|
|
188
207
|
if (this._filterAdvertising(ad, id, model)) {
|
|
189
208
|
let device = null;
|
|
190
209
|
switch (ad.serviceData.model) {
|
|
191
|
-
case
|
|
210
|
+
case "H":
|
|
192
211
|
device = new SwitchbotDeviceWoHand(peripheral, this.noble);
|
|
193
212
|
break;
|
|
194
|
-
case
|
|
213
|
+
case "T":
|
|
195
214
|
device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble);
|
|
196
215
|
break;
|
|
197
|
-
case
|
|
216
|
+
case "e":
|
|
198
217
|
device = new SwitchbotDeviceWoHumi(peripheral, this.noble);
|
|
199
218
|
break;
|
|
200
|
-
case
|
|
219
|
+
case "s":
|
|
201
220
|
device = new SwitchbotDeviceWoPresence(peripheral, this.noble);
|
|
202
221
|
break;
|
|
203
|
-
case
|
|
222
|
+
case "d":
|
|
204
223
|
device = new SwitchbotDeviceWoContact(peripheral, this.noble);
|
|
205
224
|
break;
|
|
206
|
-
case
|
|
225
|
+
case "c":
|
|
207
226
|
device = new SwitchbotDeviceWoCurtain(peripheral, this.noble);
|
|
208
227
|
break;
|
|
209
|
-
case
|
|
228
|
+
case "u":
|
|
210
229
|
device = new SwitchbotDeviceWoColorBulb(peripheral, this.noble);
|
|
211
230
|
break;
|
|
212
|
-
case
|
|
231
|
+
case "g":
|
|
232
|
+
case "j":
|
|
213
233
|
device = new SwitchbotDeviceWoPlugMini(peripheral, this.noble);
|
|
214
234
|
break;
|
|
215
|
-
case
|
|
235
|
+
case "o":
|
|
216
236
|
device = new SwitchbotDeviceWoSmartLock(peripheral, this.noble);
|
|
217
237
|
break;
|
|
218
|
-
case
|
|
238
|
+
case "i":
|
|
219
239
|
device = new SwitchbotDeviceWoSensorTHPlus(peripheral, this.noble);
|
|
220
240
|
break;
|
|
221
|
-
case
|
|
241
|
+
case "r":
|
|
222
242
|
device = new SwitchbotDeviceWoLEDStripLight(peripheral, this.noble);
|
|
223
243
|
break;
|
|
224
244
|
default: // 'resetting', 'unknown'
|
|
@@ -235,8 +255,8 @@ class Switchbot {
|
|
|
235
255
|
return false;
|
|
236
256
|
}
|
|
237
257
|
if (id) {
|
|
238
|
-
id = id.toLowerCase().replace(/\:/g,
|
|
239
|
-
let ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g,
|
|
258
|
+
id = id.toLowerCase().replace(/\:/g, "");
|
|
259
|
+
let ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
240
260
|
if (ad_id !== id) {
|
|
241
261
|
return false;
|
|
242
262
|
}
|
|
@@ -250,64 +270,72 @@ class Switchbot {
|
|
|
250
270
|
}
|
|
251
271
|
|
|
252
272
|
/* ------------------------------------------------------------------
|
|
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
|
-
|
|
273
|
+
* startScan([params])
|
|
274
|
+
* - Start to monitor advertising packets coming from switchbot devices
|
|
275
|
+
*
|
|
276
|
+
* [Arguments]
|
|
277
|
+
* - params | Object | Optional |
|
|
278
|
+
* - model | String | Optional | "H", "T", "e", "s", "d", "c", "u", "g", "o", "i", or "r".
|
|
279
|
+
* | | | If "H" is specified, the `onadvertisement`
|
|
280
|
+
* | | | event handler will be called only when advertising
|
|
281
|
+
* | | | packets comes from Bots.
|
|
282
|
+
* | | | If "T" is specified, the `onadvertisement`
|
|
283
|
+
* | | | event handler will be called only when advertising
|
|
284
|
+
* | | | packets comes from Meters.
|
|
285
|
+
* | | | If "e" is specified, the `onadvertisement`
|
|
286
|
+
* | | | event handler will be called only when advertising
|
|
287
|
+
* | | | packets comes from Humidifiers.
|
|
288
|
+
* | | | If "s" is specified, the `onadvertisement`
|
|
289
|
+
* | | | event handler will be called only when advertising
|
|
290
|
+
* | | | packets comes from Motion Sensor.
|
|
291
|
+
* | | | If "d" is specified, the `onadvertisement`
|
|
292
|
+
* | | | event handler will be called only when advertising
|
|
293
|
+
* | | | packets comes from Contact Sensor.
|
|
294
|
+
* | | | If "c" is specified, the `onadvertisement`
|
|
295
|
+
* | | | event handler will be called only when advertising
|
|
296
|
+
* | | | packets comes from Curtains.
|
|
297
|
+
* | | | If "u" is specified, the `onadvertisement`
|
|
298
|
+
* | | | event handler will be called only when advertising
|
|
299
|
+
* | | | packets comes from Color Bulb.
|
|
300
|
+
* | | | If "g" is specified, the `onadvertisement`
|
|
301
|
+
* | | | event handler will be called only when advertising
|
|
302
|
+
* | | | packets comes from Plug Mini.
|
|
303
|
+
* | | | If "o" is specified, the `onadvertisement`
|
|
304
|
+
* | | | event handler will be called only when advertising
|
|
305
|
+
* | | | packets comes from Smart Lock.
|
|
306
|
+
* | | | If "i" is specified, the `onadvertisement`
|
|
307
|
+
* | | | event handler will be called only when advertising
|
|
308
|
+
* | | | packets comes from Meter Plus.
|
|
309
|
+
* | | | If "r" is specified, the `onadvertisement`
|
|
310
|
+
* | | | event handler will be called only when advertising
|
|
311
|
+
* | | | packets comes from LED Strip Light.
|
|
312
|
+
* - id | String | Optional | If this value is set, the `onadvertisement`
|
|
313
|
+
* | | | event handler will be called only when advertising
|
|
314
|
+
* | | | packets comes from devices whose ID is as same as
|
|
315
|
+
* | | | this value.
|
|
316
|
+
* | | | The ID is identical to the MAC address.
|
|
317
|
+
* | | | This parameter is case-insensitive, and
|
|
318
|
+
* | | | colons are ignored.
|
|
319
|
+
*
|
|
320
|
+
* [Return value]
|
|
321
|
+
* - Promise object
|
|
322
|
+
* Nothing will be passed to the `resolve()`.
|
|
323
|
+
* ---------------------------------------------------------------- */
|
|
304
324
|
startScan(params) {
|
|
305
325
|
let promise = new Promise((resolve, reject) => {
|
|
306
326
|
// Check the parameters
|
|
307
|
-
let valid = parameterChecker.check(
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
327
|
+
let valid = parameterChecker.check(
|
|
328
|
+
params,
|
|
329
|
+
{
|
|
330
|
+
model: {
|
|
331
|
+
required: false,
|
|
332
|
+
type: "string",
|
|
333
|
+
enum: ["H", "T", "e", "s", "d", "c", "u", "g", "j", "o", "i", "r"],
|
|
334
|
+
},
|
|
335
|
+
id: { required: false, type: "string", min: 12, max: 17 },
|
|
336
|
+
},
|
|
337
|
+
false
|
|
338
|
+
);
|
|
311
339
|
|
|
312
340
|
if (!valid) {
|
|
313
341
|
reject(new Error(parameterChecker.error.message));
|
|
@@ -319,71 +347,82 @@ class Switchbot {
|
|
|
319
347
|
}
|
|
320
348
|
|
|
321
349
|
// Initialize the noble object
|
|
322
|
-
this._init()
|
|
350
|
+
this._init()
|
|
351
|
+
.then(() => {
|
|
352
|
+
// Determine the values of the parameters
|
|
353
|
+
let p = {
|
|
354
|
+
model: params.model || "",
|
|
355
|
+
id: params.id || "",
|
|
356
|
+
};
|
|
323
357
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
if (this.onadvertisement && typeof (this.onadvertisement) === 'function') {
|
|
335
|
-
this.onadvertisement(ad);
|
|
358
|
+
// Set a handler for the 'discover' event
|
|
359
|
+
this.noble.on("discover", (peripheral) => {
|
|
360
|
+
let ad = switchbotAdvertising.parse(peripheral, this.onlog);
|
|
361
|
+
if (this._filterAdvertising(ad, p.id, p.model)) {
|
|
362
|
+
if (
|
|
363
|
+
this.onadvertisement &&
|
|
364
|
+
typeof this.onadvertisement === "function"
|
|
365
|
+
) {
|
|
366
|
+
this.onadvertisement(ad);
|
|
367
|
+
}
|
|
336
368
|
}
|
|
337
|
-
}
|
|
338
|
-
});
|
|
369
|
+
});
|
|
339
370
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
371
|
+
// Start scanning
|
|
372
|
+
this.noble.startScanning(
|
|
373
|
+
this._PRIMARY_SERVICE_UUID_LIST,
|
|
374
|
+
true,
|
|
375
|
+
(error) => {
|
|
376
|
+
if (error) {
|
|
377
|
+
reject(error);
|
|
378
|
+
} else {
|
|
379
|
+
resolve();
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
);
|
|
383
|
+
})
|
|
384
|
+
.catch((error) => {
|
|
385
|
+
reject(error);
|
|
347
386
|
});
|
|
348
|
-
}).catch((error) => {
|
|
349
|
-
reject(error);
|
|
350
|
-
});
|
|
351
387
|
});
|
|
352
388
|
return promise;
|
|
353
389
|
}
|
|
354
390
|
|
|
355
391
|
/* ------------------------------------------------------------------
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
392
|
+
* stopScan()
|
|
393
|
+
* - Stop to monitor advertising packets coming from switchbot devices
|
|
394
|
+
*
|
|
395
|
+
* [Arguments]
|
|
396
|
+
* - none
|
|
397
|
+
*
|
|
398
|
+
* [Return value]
|
|
399
|
+
* - none
|
|
400
|
+
* ---------------------------------------------------------------- */
|
|
365
401
|
stopScan() {
|
|
366
|
-
this.noble.removeAllListeners(
|
|
402
|
+
this.noble.removeAllListeners("discover");
|
|
367
403
|
this.noble.stopScanning();
|
|
368
404
|
}
|
|
369
405
|
|
|
370
406
|
/* ------------------------------------------------------------------
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
407
|
+
* wait(msec) {
|
|
408
|
+
* - Wait for the specified time (msec)
|
|
409
|
+
*
|
|
410
|
+
* [Arguments]
|
|
411
|
+
* - msec | Integer | Required | Msec.
|
|
412
|
+
*
|
|
413
|
+
* [Return value]
|
|
414
|
+
* - Promise object
|
|
415
|
+
* Nothing will be passed to the `resolve()`.
|
|
416
|
+
* ---------------------------------------------------------------- */
|
|
381
417
|
wait(msec) {
|
|
382
418
|
return new Promise((resolve, reject) => {
|
|
383
419
|
// Check the parameters
|
|
384
|
-
let valid = parameterChecker.check(
|
|
385
|
-
|
|
386
|
-
|
|
420
|
+
let valid = parameterChecker.check(
|
|
421
|
+
{ msec: msec },
|
|
422
|
+
{
|
|
423
|
+
msec: { required: true, type: "integer", min: 0 },
|
|
424
|
+
}
|
|
425
|
+
);
|
|
387
426
|
|
|
388
427
|
if (!valid) {
|
|
389
428
|
reject(new Error(parameterChecker.error.message));
|
|
@@ -393,7 +432,6 @@ class Switchbot {
|
|
|
393
432
|
setTimeout(resolve, msec);
|
|
394
433
|
});
|
|
395
434
|
}
|
|
396
|
-
|
|
397
435
|
}
|
|
398
436
|
|
|
399
437
|
module.exports = Switchbot;
|