Skip to content

Commit 1b438d8

Browse files
v1.0.3 (#42)
# Changes - Fixed misspelling.
1 parent f92bb95 commit 1b438d8

10 files changed

+134
-59
lines changed

lib/switchbot-advertising.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,19 @@ class SwitchbotAdvertising {
9797
return null;
9898
}
9999
let address = peripheral.address || '';
100-
if(address === '')
101-
{
100+
if (address === '') {
102101
address = peripheral.advertisement.manufacturerData || '';
103-
if(address !== '')
104-
{
102+
if (address !== '') {
105103
const str = peripheral.advertisement.manufacturerData.toString('hex').slice(4);
106-
address = str.substr(0,2);
107-
for(var i = 2; i< str.length; i+=2)
108-
{
104+
address = str.substr(0, 2);
105+
for (var i = 2; i < str.length; i += 2) {
109106
address = address + ":" + str.substr(i, 2);
110107
}
111108
// console.log("address", typeof(address), address);
112109
}
113110
}
114-
else
115-
{
116-
address=address.replace(/-/g,':');
111+
else {
112+
address = address.replace(/-/g, ':');
117113
}
118114
let data = {
119115
id: peripheral.id,
@@ -175,6 +171,34 @@ class SwitchbotAdvertising {
175171
return data;
176172
}
177173

174+
_parseServiceDataForWoSensorMS(buf) {
175+
if (buf.length !== 6) {
176+
return null;
177+
}
178+
179+
let data = {
180+
model: 'M',
181+
modelName: 'WoSensorMS',
182+
battery: (byte2 & 0b01111111)
183+
};
184+
185+
return data;
186+
}
187+
188+
_parseServiceDataForWoSensorCS(buf) {
189+
if (buf.length !== 6) {
190+
return null;
191+
}
192+
193+
let data = {
194+
model: 'CS',
195+
modelName: 'WoSensorCS',
196+
battery: (byte2 & 0b01111111)
197+
};
198+
199+
return data;
200+
}
201+
178202
_parseServiceDataForWoCurtain(buf) {
179203
if (buf.length !== 5) {
180204
return null;

lib/switchbot-device-wocurtain.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Date: 2020-10-26
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
import SwitchbotDevice from './switchbot-device.js';
9+
const SwitchbotDevice = require('./switchbot-device.js');
1010

1111
class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
1212

@@ -106,4 +106,4 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
106106
}
107107
}
108108

109-
export default SwitchbotDeviceWoCurtain;
109+
module.exports = SwitchbotDeviceWoCurtain;

lib/switchbot-device-wohand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Date: 2020-02-10
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
import SwitchbotDevice from './switchbot-device.js';
9+
const SwitchbotDevice = require('./switchbot-device.js');
1010

1111
class SwitchbotDeviceWoHand extends SwitchbotDevice {
1212

@@ -103,4 +103,4 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
103103

104104
}
105105

106-
export default SwitchbotDeviceWoHand;
106+
module.exports = SwitchbotDeviceWoHand;

lib/switchbot-device-wosensorcs.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* ------------------------------------------------------------------
2+
* node-linking - switchbot-device-wosensorth.js
3+
*
4+
* Copyright (c) 2019, Futomi Hatano, All rights reserved.
5+
* Released under the MIT license
6+
* Date: 2019-11-16
7+
* ---------------------------------------------------------------- */
8+
'use strict';
9+
const SwitchbotDevice = require('./switchbot-device.js');
10+
11+
class SwitchbotDeviceWoSensorCS extends SwitchbotDevice {
12+
13+
14+
}
15+
16+
module.exports = SwitchbotDeviceWoSensorCS;

lib/switchbot-device-wosensorms.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* ------------------------------------------------------------------
2+
* node-linking - switchbot-device-wosensorth.js
3+
*
4+
* Copyright (c) 2019, Futomi Hatano, All rights reserved.
5+
* Released under the MIT license
6+
* Date: 2019-11-16
7+
* ---------------------------------------------------------------- */
8+
'use strict';
9+
const SwitchbotDevice = require('./switchbot-device.js');
10+
11+
class SwitchbotDeviceWoSensorMS extends SwitchbotDevice {
12+
13+
14+
}
15+
16+
module.exports = SwitchbotDeviceWoSensorMS;

lib/switchbot-device-wosensorth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Date: 2019-11-16
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
import SwitchbotDevice from './switchbot-device.js';
9+
const SwitchbotDevice = require('./switchbot-device.js');
1010

1111
class SwitchbotDeviceWoSensorTH extends SwitchbotDevice {
1212

1313

1414
}
1515

16-
export default SwitchbotDeviceWoSensorTH;
16+
module.exports = SwitchbotDeviceWoSensorTH;

lib/switchbot-device.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* Date: 2020-02-19
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
import { check, error as _error } from './parameter-checker.js';
10-
import { parse } from './switchbot-advertising.js';
9+
const parameterChecker = require('./parameter-checker.js');
10+
const switchbotAdvertising = require('./switchbot-advertising.js');
1111

1212
class SwitchbotDevice {
1313
/* ------------------------------------------------------------------
@@ -16,6 +16,7 @@ class SwitchbotDevice {
1616
* [Arguments]
1717
* - peripheral | Object | Required | The `peripheral` object of noble,
1818
* | | | which represents this device
19+
* - noble | Noble | Required | The Nobel object created by the noble module.
1920
* ---------------------------------------------------------------- */
2021
constructor(peripheral, noble) {
2122
this._peripheral = peripheral;
@@ -32,7 +33,7 @@ class SwitchbotDevice {
3233
this._COMMAND_TIMEOUT_MSEC = 3000;
3334

3435
// Save the device information
35-
let ad = parse(peripheral);
36+
let ad = switchbotAdvertising.parse(peripheral);
3637
this._id = ad.id;
3738
this._address = ad.address;
3839
this._model = ad.serviceData.model;
@@ -374,12 +375,12 @@ class SwitchbotDevice {
374375
setDeviceName(name) {
375376
return new Promise((resolve, reject) => {
376377
// Check the parameters
377-
let valid = check({ name: name }, {
378+
let valid = parameterChecker.check({ name: name }, {
378379
name: { required: true, type: 'string', minBytes: 1, maxBytes: 100 }
379380
});
380381

381382
if (!valid) {
382-
reject(new Error(_error.message));
383+
reject(new Error(parameterChecker.error.message));
383384
return;
384385
}
385386

@@ -494,4 +495,4 @@ class SwitchbotDevice {
494495

495496
}
496497

497-
export default SwitchbotDevice;
498+
module.exports = SwitchbotDevice;

lib/switchbot.js

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
* Date: 2019-11-18
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
import { check, error as _error } from './parameter-checker.js';
10-
import { parse } from './switchbot-advertising.js';
9+
const parameterChecker = require('./parameter-checker.js');
10+
const switchbotAdvertising = require('./switchbot-advertising.js');
1111

12-
import SwitchbotDevice from './switchbot-device.js';
13-
import SwitchbotDeviceWoHand from './switchbot-device-wohand.js';
14-
import SwitchbotDeviceWoSensorTH from './switchbot-device-wosensorth.js';
15-
import SwitchbotDeviceWoCurtain from './switchbot-device-wocurtain.js';
12+
const SwitchbotDevice = require('./switchbot-device.js');
13+
const SwitchbotDeviceWoHand = require('./switchbot-device-wohand.js');
14+
const SwitchbotDeviceWoCurtain = require('./switchbot-device-wocurtain.js');
15+
const SwitchbotDeviceWoSensorMS = require('./switchbot-device-wosensorms.js');
16+
const SwitchbotDeviceWoSensorCS = require('./switchbot-device-wosensorcs.js');
17+
const SwitchbotDeviceWoSensorTH = require('./switchbot-device-wosensorth.js');
1618

1719
class Switchbot {
1820
/* ------------------------------------------------------------------
@@ -40,7 +42,6 @@ class Switchbot {
4042
this.onadvertisement = null;
4143

4244
// Private properties
43-
this._initialized = false;
4445
this._scanning = false;
4546
this._DEFAULT_DISCOVERY_DURATION = 5000
4647
this._PRIMARY_SERVICE_UUID_LIST = ['cba20d00224d11e69fb80002a5d5c51b'];
@@ -55,11 +56,12 @@ class Switchbot {
5556
* - duration | Integer | Optional | Duration for discovery process (msec).
5657
* | | | The value must be in the range of 1 to 60000.
5758
* | | | The default value is 5000 (msec).
58-
* - model | String | Optional | "H", "T" or "c".
59-
* | | | If "H" is specified, this method will discover
60-
* | | | only Bots. If "T" is specified, this method
61-
* | | | will discover only Meters. If "c" is specified,
62-
* | | | this method will discover only Curtains.
59+
* - model | String | Optional | "H", "T", "M", "CS", or "c".
60+
* | | | If "H" is specified, this method will discover only Bots.
61+
* | | | If "T" is specified, this method will discover only Meters.
62+
* | | | If "M" is specified, this method will discover only Motion Sensors.
63+
* | | | If "CS" is specified, this method will discover only Contact Sensors.
64+
* | | | If "C" is specified, this method will discover only Curtains.
6365
* - id | String | Optional | If this value is set, this method willl discover
6466
* | | | only a device whose ID is as same as this value.
6567
* | | | The ID is identical to the MAC address.
@@ -79,15 +81,15 @@ class Switchbot {
7981
discover(params = {}) {
8082
let promise = new Promise((resolve, reject) => {
8183
// Check the parameters
82-
let valid = check(params, {
84+
let valid = parameterChecker.check(params, {
8385
duration: { required: false, type: 'integer', min: 1, max: 60000 },
84-
model: { required: false, type: 'string', enum: ['H', 'T', 'c'] },
86+
model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'c'] },
8587
id: { required: false, type: 'string', min: 12, max: 17 },
8688
quick: { required: false, type: 'boolean' }
8789
}, false);
8890

8991
if (!valid) {
90-
reject(new Error(_error.message));
92+
reject(new Error(parameterChecker.error.message));
9193
return;
9294
}
9395

@@ -181,17 +183,27 @@ class Switchbot {
181183
}
182184

183185
_getDeviceObject(peripheral, id, model) {
184-
let ad = parse(peripheral);
186+
let ad = switchbotAdvertising.parse(peripheral);
185187
if (this._filterAdvertising(ad, id, model)) {
186188
let device = null;
187-
if (ad.serviceData.model === 'H') {
188-
device = new SwitchbotDeviceWoHand(peripheral);
189-
} else if (ad.serviceData.model === 'T') {
190-
device = new SwitchbotDeviceWoSensorTH(peripheral);
191-
} else if (ad.serviceData.model === 'c') {
192-
device = new SwitchbotDeviceWoCurtain(peripheral);
193-
} else {
194-
device = new SwitchbotDevice(peripheral);
189+
switch (ad.serviceData.model) {
190+
case 'H':
191+
device = new SwitchbotDeviceWoHand(peripheral, this.noble);
192+
break;
193+
case 'T':
194+
device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble);
195+
break;
196+
case 'M':
197+
device = new SwitchbotDeviceWoSensorMS(peripheral, this.noble);
198+
break;
199+
case 'CS':
200+
device = new SwitchbotDeviceWoSensorCS(peripheral, this.noble);
201+
break;
202+
case 'c':
203+
device = new SwitchbotDeviceWoCurtain(peripheral, this.noble);
204+
break;
205+
default: // 'resetting', 'unknown'
206+
device = new SwitchbotDevice(peripheral, this.noble);
195207
}
196208
return device;
197209
} else {
@@ -222,15 +234,21 @@ class Switchbot {
222234
* - Start to monitor advertising packets coming from switchbot devices
223235
*
224236
* [Arguments]
225-
* - params | Object | Optional |
226-
* - model | String | Optional | "H", or "T" or "c".
237+
* - params | Object | Optional |
238+
* - model | String | Optional | "H", "T", "M", "CS", or "C".
227239
* | | | If "H" is specified, the `onadvertisement`
228240
* | | | event hander will be called only when advertising
229241
* | | | packets comes from Bots.
230242
* | | | If "T" is specified, the `onadvertisement`
231243
* | | | event hander will be called only when advertising
232244
* | | | packets comes from Meters.
233-
* | | | If "c" is specified, the `onadvertisement`
245+
* | | | If "M" is specified, the `onadvertisement`
246+
* | | | event hander will be called only when advertising
247+
* | | | packets comes from Motion Sensor.
248+
* | | | If "CS" is specified, the `onadvertisement`
249+
* | | | event hander will be called only when advertising
250+
* | | | packets comes from Contact Sensor.
251+
* | | | If "C" is specified, the `onadvertisement`
234252
* | | | event hander will be called only when advertising
235253
* | | | packets comes from Curtains.
236254
* - id | String | Optional | If this value is set, the `onadvertisement`
@@ -248,13 +266,13 @@ class Switchbot {
248266
startScan(params) {
249267
let promise = new Promise((resolve, reject) => {
250268
// Check the parameters
251-
let valid = check(params, {
252-
model: { required: false, type: 'string', enum: ['H', 'T', 'c'] },
269+
let valid = parameterChecker.check(params, {
270+
model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'c'] },
253271
id: { required: false, type: 'string', min: 12, max: 17 },
254272
}, false);
255273

256274
if (!valid) {
257-
reject(new Error(_error.message));
275+
reject(new Error(parameterChecker.error.message));
258276
return;
259277
}
260278

@@ -273,7 +291,7 @@ class Switchbot {
273291

274292
// Set an handler for the 'discover' event
275293
this.noble.on('discover', (peripheral) => {
276-
let ad = parse(peripheral);
294+
let ad = switchbotAdvertising.parse(peripheral);
277295
if (this._filterAdvertising(ad, p.id, p.model)) {
278296
if (this.onadvertisement && typeof (this.onadvertisement) === 'function') {
279297
this.onadvertisement(ad);
@@ -325,12 +343,12 @@ class Switchbot {
325343
wait(msec) {
326344
return new Promise((resolve, reject) => {
327345
// Check the parameters
328-
let valid = check({ msec: msec }, {
346+
let valid = parameterChecker.check({ msec: msec }, {
329347
msec: { required: true, type: 'integer', min: 0 }
330348
});
331349

332350
if (!valid) {
333-
reject(new Error(_error.message));
351+
reject(new Error(parameterChecker.error.message));
334352
return;
335353
}
336354
// Set a timer
@@ -340,4 +358,4 @@ class Switchbot {
340358

341359
}
342360

343-
export default Switchbot;
361+
module.exports = Switchbot;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)