node-switchbot 1.1.3-beta.6 → 1.1.3-beta.9

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 CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. This project uses [Semantic Versioning](https://semver.org/)
4
4
 
5
+ ## [Beta - Version 1.2.0](https://github.com/OpenWonderLabs/node-switchbot/releases/tag/v1.1.2) (2021-11-13)
6
+
7
+ ### Changes
8
+
9
+ - Added support for SwitchBot "Contact" and "Motion"
10
+ - Housekeeping and update dependencies
11
+
5
12
  ## [Version 1.1.2](https://github.com/OpenWonderLabs/node-switchbot/releases/tag/v1.1.2) (2021-11-13)
6
13
 
7
14
  ### Changes
@@ -10,10 +10,10 @@ class SwitchbotAdvertising {
10
10
  * [Arguments]
11
11
  * - peripheral | Object | Required | A `Peripheral` object of noble
12
12
  *
13
- * [Returen value]
13
+ * [Return value]
14
14
  * - An object as follows:
15
15
  *
16
- * WoHand
16
+ * WoHand
17
17
  * {
18
18
  * id: 'c12e453e2008',
19
19
  * address: 'c1:2e:45:3e:20:08',
@@ -56,19 +56,17 @@ class SwitchbotAdvertising {
56
56
  * lightLevel: 1
57
57
  * }
58
58
  * }
59
- *
59
+ *
60
60
  * If the specified `Peripheral` does not represent any switchbot
61
61
  * device, this method will return `null`.
62
62
  * ---------------------------------------------------------------- */
63
- parse(peripheral) {
63
+ parse(peripheral, onlog) {
64
64
  let ad = peripheral.advertisement;
65
65
  if (!ad || !ad.serviceData) {
66
66
  return null;
67
67
  }
68
- if (ad.serviceData[0].uuid !== '0d00') {
69
- return null;
70
- }
71
- let buf = ad.serviceData[0].data;
68
+ let serviceData = ad.serviceData[0] || ad.serviceData;
69
+ let buf = serviceData.data;
72
70
  if (!buf || !Buffer.isBuffer(buf) || buf.length < 3) {
73
71
  return null;
74
72
  }
@@ -77,22 +75,28 @@ class SwitchbotAdvertising {
77
75
  let sd = null;
78
76
 
79
77
  if (model === 'H') { // WoHand
80
- sd = this._parseServiceDataForWoHand(buf);
78
+ sd = this._parseServiceDataForWoHand(buf, onlog);
81
79
  } else if (model === 'e') { // WoHumi
82
- sd = this._parseServiceDataForWoHumi(buf);
80
+ sd = this._parseServiceDataForWoHumi(buf, onlog);
83
81
  } else if (model === 'T') { // WoSensorTH
84
- sd = this._parseServiceDataForWoSensorTH(buf);
82
+ sd = this._parseServiceDataForWoSensorTH(buf, onlog);
85
83
  } else if (model === 'c') { // WoCurtain
86
- sd = this._parseServiceDataForWoCurtain(buf);
84
+ sd = this._parseServiceDataForWoCurtain(buf, onlog);
87
85
  } else if (model === 's') { // WoMotion
88
- sd = this._parseServiceDataForWoPresence(buf);
86
+ sd = this._parseServiceDataForWoPresence(buf, onlog);
89
87
  } else if (model === 'd') { // WoContact
90
- sd = this._parseServiceDataForWoContact(buf);
88
+ sd = this._parseServiceDataForWoContact(buf, onlog);
91
89
  } else {
90
+ if (onlog && typeof onlog === 'function') {
91
+ onlog(`[parseAdvertising.${peripheral.id}] return null, model "${model}" not available!`);
92
+ }
92
93
  return null;
93
94
  }
94
95
 
95
96
  if (!sd) {
97
+ if (onlog && typeof onlog === 'function') {
98
+ onlog(`[parseAdvertising.${peripheral.id}.${model}] return null, parsed serviceData empty!`);
99
+ }
96
100
  return null;
97
101
  }
98
102
  let address = peripheral.address || '';
@@ -116,11 +120,18 @@ class SwitchbotAdvertising {
116
120
  rssi: peripheral.rssi,
117
121
  serviceData: sd
118
122
  };
123
+
124
+ if (onlog && typeof onlog === 'function') {
125
+ onlog(`[parseAdvertising.${peripheral.id}.${model}] return ${JSON.stringify(data)}`);
126
+ }
119
127
  return data;
120
128
  }
121
129
 
122
- _parseServiceDataForWoHand(buf) {
130
+ _parseServiceDataForWoHand(buf, onlog) {
123
131
  if (buf.length !== 3) {
132
+ if (onlog && typeof onlog === 'function') {
133
+ onlog(`[_parseServiceDataForWoHand] Buffer length ${buf.length} !== 3!`);
134
+ }
124
135
  return null;
125
136
  }
126
137
  let byte1 = buf.readUInt8(1);
@@ -141,8 +152,11 @@ class SwitchbotAdvertising {
141
152
  return data;
142
153
  }
143
154
 
144
- _parseServiceDataForWoHumi(buf) {
155
+ _parseServiceDataForWoHumi(buf, onlog) {
145
156
  if (buf.length !== 8) {
157
+ if (onlog && typeof onlog === 'function') {
158
+ onlog(`[_parseServiceDataForWoHumi] Buffer length ${buf.length} !== 8!`);
159
+ }
146
160
  return null;
147
161
  }
148
162
  let byte1 = buf.readUInt8(1);
@@ -166,8 +180,11 @@ class SwitchbotAdvertising {
166
180
  return data;
167
181
  }
168
182
 
169
- _parseServiceDataForWoSensorTH(buf) {
183
+ _parseServiceDataForWoSensorTH(buf, onlog) {
170
184
  if (buf.length !== 6) {
185
+ if (onlog && typeof onlog === 'function') {
186
+ onlog(`[_parseServiceDataForWoSensorTH] Buffer length ${buf.length} !== 6!`);
187
+ }
171
188
  return null;
172
189
  }
173
190
  let byte2 = buf.readUInt8(2);
@@ -195,8 +212,11 @@ class SwitchbotAdvertising {
195
212
  return data;
196
213
  }
197
214
 
198
- _parseServiceDataForWoPresence(buf) {
215
+ _parseServiceDataForWoPresence(buf, onlog) {
199
216
  if (buf.length !== 6) {
217
+ if (onlog && typeof onlog === 'function') {
218
+ onlog(`[_parseServiceDataForWoPresence] Buffer length ${buf.length} !== 6!`);
219
+ }
200
220
  return null;
201
221
  }
202
222
  let byte1 = buf.readUInt8(1);
@@ -220,8 +240,11 @@ class SwitchbotAdvertising {
220
240
  return data;
221
241
  }
222
242
 
223
- _parseServiceDataForWoContact(buf) {
243
+ _parseServiceDataForWoContact(buf, onlog) {
224
244
  if (buf.length !== 9) {
245
+ if (onlog && typeof onlog === 'function') {
246
+ onlog(`[_parseServiceDataForWoContact] Buffer length ${buf.length} !== 9!`);
247
+ }
225
248
  return null;
226
249
  }
227
250
 
@@ -251,14 +274,18 @@ class SwitchbotAdvertising {
251
274
  return data;
252
275
  }
253
276
 
254
- _parseServiceDataForWoCurtain(buf) {
255
- if (buf.length !== 5) {
277
+ _parseServiceDataForWoCurtain(buf, onlog) {
278
+ if (buf.length !== 5 && buf.length !== 6) {
279
+ if (onlog && typeof onlog === 'function') {
280
+ onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 5 or 6!`);
281
+ }
256
282
  return null;
257
283
  }
258
284
  let byte1 = buf.readUInt8(1);
259
285
  let byte2 = buf.readUInt8(2);
260
286
  let byte3 = buf.readUInt8(3);
261
287
  let byte4 = buf.readUInt8(4);
288
+ // let byte5 = buf.readUInt8(5);
262
289
 
263
290
  let calibration = (byte1 & 0b01000000) ? true : false; // Whether the calibration is completed
264
291
  let battery = byte2 & 0b01111111; // %
@@ -10,7 +10,7 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
10
10
  * [Arguments]
11
11
  * - none
12
12
  *
13
- * [Returen value]
13
+ * [Return value]
14
14
  * - Promise object
15
15
  * Nothing will be passed to the `resolve()`.
16
16
  * ---------------------------------------------------------------- */
@@ -25,7 +25,7 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
25
25
  * [Arguments]
26
26
  * - none
27
27
  *
28
- * [Returen value]
28
+ * [Return value]
29
29
  * - Promise object
30
30
  * Nothing will be passed to the `resolve()`.
31
31
  * ---------------------------------------------------------------- */
@@ -40,7 +40,7 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
40
40
  * [Arguments]
41
41
  * - none
42
42
  *
43
- * [Returen value]
43
+ * [Return value]
44
44
  * - Promise object
45
45
  * Nothing will be passed to the `resolve()`.
46
46
  * ---------------------------------------------------------------- */
@@ -55,7 +55,7 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
55
55
  * [Arguments]
56
56
  * - percent | number | Required | the percentage of target position
57
57
  *
58
- * [Returen value]
58
+ * [Return value]
59
59
  * - Promise object
60
60
  * Nothing will be passed to the `resolve()`.
61
61
  * ---------------------------------------------------------------- */
@@ -10,7 +10,7 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
10
10
  * [Arguments]
11
11
  * - none
12
12
  *
13
- * [Returen value]
13
+ * [Return value]
14
14
  * - Promise object
15
15
  * Nothing will be passed to the `resolve()`.
16
16
  * ---------------------------------------------------------------- */
@@ -25,7 +25,7 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
25
25
  * [Arguments]
26
26
  * - none
27
27
  *
28
- * [Returen value]
28
+ * [Return value]
29
29
  * - Promise object
30
30
  * Nothing will be passed to the `resolve()`.
31
31
  * ---------------------------------------------------------------- */
@@ -40,7 +40,7 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
40
40
  * [Arguments]
41
41
  * - none
42
42
  *
43
- * [Returen value]
43
+ * [Return value]
44
44
  * - Promise object
45
45
  * Nothing will be passed to the `resolve()`.
46
46
  * ---------------------------------------------------------------- */
@@ -55,7 +55,7 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
55
55
  * [Arguments]
56
56
  * - none
57
57
  *
58
- * [Returen value]
58
+ * [Return value]
59
59
  * - Promise object
60
60
  * Nothing will be passed to the `resolve()`.
61
61
  * ---------------------------------------------------------------- */
@@ -70,7 +70,7 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
70
70
  * [Arguments]
71
71
  * - none
72
72
  *
73
- * [Returen value]
73
+ * [Return value]
74
74
  * - Promise object
75
75
  * Nothing will be passed to the `resolve()`.
76
76
  * ---------------------------------------------------------------- */
@@ -10,7 +10,7 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
10
10
  * [Arguments]
11
11
  * - none
12
12
  *
13
- * [Returen value]
13
+ * [Return value]
14
14
  * - Promise object
15
15
  * Nothing will be passed to the `resolve()`.
16
16
  * ---------------------------------------------------------------- */
@@ -25,7 +25,7 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
25
25
  * [Arguments]
26
26
  * - none
27
27
  *
28
- * [Returen value]
28
+ * [Return value]
29
29
  * - Promise object
30
30
  * Nothing will be passed to the `resolve()`.
31
31
  * ---------------------------------------------------------------- */
@@ -40,7 +40,7 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
40
40
  * [Arguments]
41
41
  * - none
42
42
  *
43
- * [Returen value]
43
+ * [Return value]
44
44
  * - Promise object
45
45
  * Nothing will be passed to the `resolve()`.
46
46
  * ---------------------------------------------------------------- */
@@ -55,7 +55,7 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
55
55
  * [Arguments]
56
56
  * - none
57
57
  *
58
- * [Returen value]
58
+ * [Return value]
59
59
  * - Promise object
60
60
  * Nothing will be passed to the `resolve()`.
61
61
  * ---------------------------------------------------------------- */
@@ -70,7 +70,7 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
70
70
  * [Arguments]
71
71
  * - none
72
72
  *
73
- * [Returen value]
73
+ * [Return value]
74
74
  * - Promise object
75
75
  * Nothing will be passed to the `resolve()`.
76
76
  * ---------------------------------------------------------------- */
@@ -5,7 +5,7 @@ const switchbotAdvertising = require('./switchbot-advertising.js');
5
5
  class SwitchbotDevice {
6
6
  /* ------------------------------------------------------------------
7
7
  * Constructor
8
- *
8
+ *
9
9
  * [Arguments]
10
10
  * - peripheral | Object | Required | The `peripheral` object of noble,
11
11
  * | | | which represents this device
@@ -83,7 +83,7 @@ class SwitchbotDevice {
83
83
  * [Arguments]
84
84
  * - none
85
85
  *
86
- * [Returen value]
86
+ * [Return value]
87
87
  * - Promise object
88
88
  * Nothing will be passed to the `resolve()`.
89
89
  * ---------------------------------------------------------------- */
@@ -285,7 +285,7 @@ class SwitchbotDevice {
285
285
  * [Arguments]
286
286
  * - none
287
287
  *
288
- * [Returen value]
288
+ * [Return value]
289
289
  * - Promise object
290
290
  * Nothing will be passed to the `resolve()`.
291
291
  * ---------------------------------------------------------------- */
@@ -329,7 +329,7 @@ class SwitchbotDevice {
329
329
  * [Arguments]
330
330
  * - none
331
331
  *
332
- * [Returen value]
332
+ * [Return value]
333
333
  * - Promise object
334
334
  * The device name will be passed to the `resolve()`.
335
335
  * ---------------------------------------------------------------- */
@@ -361,7 +361,7 @@ class SwitchbotDevice {
361
361
  * - name | String | Required | Device name. The bytes length of the name
362
362
  * | | | must be in the range of 1 to 20 bytes.
363
363
  *
364
- * [Returen value]
364
+ * [Return value]
365
365
  * - Promise object
366
366
  * Nothing will be passed to the `resolve()`.
367
367
  * ---------------------------------------------------------------- */
package/lib/switchbot.js CHANGED
@@ -13,7 +13,7 @@ const SwitchbotDeviceWoHumi = require('./switchbot-device-wohumi.js');
13
13
  class Switchbot {
14
14
  /* ------------------------------------------------------------------
15
15
  * Constructor
16
- *
16
+ *
17
17
  * [Arguments]
18
18
  * - params | Object | Optional |
19
19
  * - noble | Noble | Optional | The Nobel object created by the noble module.
@@ -30,15 +30,16 @@ class Switchbot {
30
30
  noble = require('@abandonware/noble');
31
31
  }
32
32
 
33
- // Plublic properties
33
+ // Public properties
34
34
  this.noble = noble;
35
35
  this.ondiscover = null;
36
36
  this.onadvertisement = null;
37
+ this.onlog = null;
37
38
 
38
39
  // Private properties
39
40
  this._scanning = false;
40
41
  this._DEFAULT_DISCOVERY_DURATION = 5000
41
- this._PRIMARY_SERVICE_UUID_LIST = ['cba20d00224d11e69fb80002a5d5c51b'];
42
+ this._PRIMARY_SERVICE_UUID_LIST = [];
42
43
  };
43
44
 
44
45
  /* ------------------------------------------------------------------
@@ -57,7 +58,7 @@ class Switchbot {
57
58
  * | | | If "s" is specified, this method will discover only Motion Sensors.
58
59
  * | | | If "d" is specified, this method will discover only Contact Sensors.
59
60
  * | | | If "c" is specified, this method will discover only Curtains.
60
- * - id | String | Optional | If this value is set, this method willl discover
61
+ * - id | String | Optional | If this value is set, this method will discover
61
62
  * | | | only a device whose ID is as same as this value.
62
63
  * | | | The ID is identical to the MAC address.
63
64
  * | | | This parameter is case-insensitive, and
@@ -68,7 +69,7 @@ class Switchbot {
68
69
  * | | | without waiting the specified duration.
69
70
  * | | | The default value is false.
70
71
  *
71
- * [Returen value]
72
+ * [Return value]
72
73
  * - Promise object
73
74
  * An array will be passed to the `resolve()`, which includes
74
75
  * `SwitchbotDevice` objects representing the found devices.
@@ -117,7 +118,7 @@ class Switchbot {
117
118
  resolve(device_list);
118
119
  };
119
120
 
120
- // Set an handler for the 'discover' event
121
+ // Set a handler for the 'discover' event
121
122
  this.noble.on('discover', (peripheral) => {
122
123
  let device = this._getDeviceObject(peripheral, p.id, p.model);
123
124
  if (!device) {
@@ -136,7 +137,7 @@ class Switchbot {
136
137
  }
137
138
  });
138
139
 
139
- // Start scaning
140
+ // Start scanning
140
141
  this.noble.startScanning(this._PRIMARY_SERVICE_UUID_LIST, false, (error) => {
141
142
  if (error) {
142
143
  reject(error);
@@ -178,7 +179,7 @@ class Switchbot {
178
179
  }
179
180
 
180
181
  _getDeviceObject(peripheral, id, model) {
181
- let ad = switchbotAdvertising.parse(peripheral);
182
+ let ad = switchbotAdvertising.parse(peripheral, this.onlog);
182
183
  if (this._filterAdvertising(ad, id, model)) {
183
184
  let device = null;
184
185
  switch (ad.serviceData.model) {
@@ -236,32 +237,32 @@ class Switchbot {
236
237
  * - params | Object | Optional |
237
238
  * - model | String | Optional | "H", "T", "e", "s", "d", or "c".
238
239
  * | | | If "H" is specified, the `onadvertisement`
239
- * | | | event hander will be called only when advertising
240
+ * | | | event handler will be called only when advertising
240
241
  * | | | packets comes from Bots.
241
242
  * | | | If "T" is specified, the `onadvertisement`
242
- * | | | event hander will be called only when advertising
243
+ * | | | event handler will be called only when advertising
243
244
  * | | | packets comes from Meters.
244
245
  * | | | If "e" is specified, the `onadvertisement`
245
- * | | | event hander will be called only when advertising
246
+ * | | | event handler will be called only when advertising
246
247
  * | | | packets comes from Humidifiers.
247
248
  * | | | If "s" is specified, the `onadvertisement`
248
- * | | | event hander will be called only when advertising
249
+ * | | | event handler will be called only when advertising
249
250
  * | | | packets comes from Motion Sensor.
250
251
  * | | | If "d" is specified, the `onadvertisement`
251
- * | | | event hander will be called only when advertising
252
+ * | | | event handler will be called only when advertising
252
253
  * | | | packets comes from Contact Sensor.
253
254
  * | | | If "c" is specified, the `onadvertisement`
254
- * | | | event hander will be called only when advertising
255
+ * | | | event handler will be called only when advertising
255
256
  * | | | packets comes from Curtains.
256
257
  * - id | String | Optional | If this value is set, the `onadvertisement`
257
- * | | | event hander will be called only when advertising
258
+ * | | | event handler will be called only when advertising
258
259
  * | | | packets comes from devices whose ID is as same as
259
260
  * | | | this value.
260
261
  * | | | The ID is identical to the MAC address.
261
262
  * | | | This parameter is case-insensitive, and
262
263
  * | | | colons are ignored.
263
264
  *
264
- * [Returen value]
265
+ * [Return value]
265
266
  * - Promise object
266
267
  * Nothing will be passed to the `resolve()`.
267
268
  * ---------------------------------------------------------------- */
@@ -291,9 +292,9 @@ class Switchbot {
291
292
  id: params.id || ''
292
293
  };
293
294
 
294
- // Set an handler for the 'discover' event
295
+ // Set a handler for the 'discover' event
295
296
  this.noble.on('discover', (peripheral) => {
296
- let ad = switchbotAdvertising.parse(peripheral);
297
+ let ad = switchbotAdvertising.parse(peripheral, this.onlog);
297
298
  if (this._filterAdvertising(ad, p.id, p.model)) {
298
299
  if (this.onadvertisement && typeof (this.onadvertisement) === 'function') {
299
300
  this.onadvertisement(ad);
@@ -301,7 +302,7 @@ class Switchbot {
301
302
  }
302
303
  });
303
304
 
304
- // Start scaning
305
+ // Start scanning
305
306
  this.noble.startScanning(this._PRIMARY_SERVICE_UUID_LIST, true, (error) => {
306
307
  if (error) {
307
308
  reject(error);
@@ -323,7 +324,7 @@ class Switchbot {
323
324
  * [Arguments]
324
325
  * - none
325
326
  *
326
- * [Returen value]
327
+ * [Return value]
327
328
  * - none
328
329
  * ---------------------------------------------------------------- */
329
330
  stopScan() {
@@ -338,7 +339,7 @@ class Switchbot {
338
339
  * [Arguments]
339
340
  * - msec | Integer | Required | Msec.
340
341
  *
341
- * [Returen value]
342
+ * [Return value]
342
343
  * - Promise object
343
344
  * Nothing will be passed to the `resolve()`.
344
345
  * ---------------------------------------------------------------- */
@@ -360,4 +361,4 @@ class Switchbot {
360
361
 
361
362
  }
362
363
 
363
- module.exports = Switchbot;
364
+ module.exports = Switchbot;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-switchbot",
3
- "version": "1.1.3-beta.6",
3
+ "version": "1.1.3-beta.9",
4
4
  "description": "The node-switchbot is a Node.js module which allows you to move your Switchbot (Bot)'s arm and Switchbot Curtain(Curtain), also monitor the temperature/humidity from SwitchBot Thermometer & Hygrometer (Meter).",
5
5
  "main": "./lib/switchbot.js",
6
6
  "files": [
@@ -37,6 +37,6 @@
37
37
  "@abandonware/noble": "^1.9.2-15"
38
38
  },
39
39
  "devDependencies": {
40
- "npm-check-updates": "^12.0.2"
40
+ "npm-check-updates": "^12.5.0"
41
41
  }
42
42
  }