From 7f25fabcdd0772f7a73be558365f5f3a87900333 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 14:52:47 -0500 Subject: [PATCH 01/12] update es6 --- lib/parameter-checker.js | 2 +- lib/switchbot-advertising.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parameter-checker.js b/lib/parameter-checker.js index 84a7a984..a1bbd257 100644 --- a/lib/parameter-checker.js +++ b/lib/parameter-checker.js @@ -468,4 +468,4 @@ class ParameterChecker { } } -module.exports = new ParameterChecker(); +export default new ParameterChecker(); diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index b8e8241b..2f97a21a 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -202,4 +202,4 @@ class SwitchbotAdvertising { } } -module.exports = new SwitchbotAdvertising(); \ No newline at end of file +export default new SwitchbotAdvertising(); \ No newline at end of file From d2786ff59aa7006cf9bbe3946875878ef02fe75b Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 15:08:06 -0500 Subject: [PATCH 02/12] Update package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index b55c8331..a55cde30 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "type": "git", "url": "https://github.com/futomi/node-switchbot.git" }, - "readmeFilename": "README.md", + "readmeFilename": "README.md", + "type": "module", "dependencies": { "@abandonware/noble": ">=1.9.2-14" } From a9c3838da91afaf713d8e58f8af0cb3afc8ce475 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 15:10:58 -0500 Subject: [PATCH 03/12] Update package.json --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a55cde30..5aeb3bb9 100644 --- a/package.json +++ b/package.json @@ -32,9 +32,8 @@ "type": "git", "url": "https://github.com/futomi/node-switchbot.git" }, - "readmeFilename": "README.md", - "type": "module", + "type": "module", "dependencies": { "@abandonware/noble": ">=1.9.2-14" } -} +} \ No newline at end of file From 702221362357c5a264faab84abae0f439c747295 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 15:15:58 -0500 Subject: [PATCH 04/12] remove strict --- lib/parameter-checker.js | 2 -- lib/switchbot-advertising.js | 2 -- lib/switchbot-device-wocurtain.js | 1 - lib/switchbot-device-wohand.js | 1 - lib/switchbot-device-wosensorth.js | 1 - lib/switchbot-device.js | 1 - lib/switchbot.js | 1 - 7 files changed, 9 deletions(-) diff --git a/lib/parameter-checker.js b/lib/parameter-checker.js index a1bbd257..5da129bb 100644 --- a/lib/parameter-checker.js +++ b/lib/parameter-checker.js @@ -5,8 +5,6 @@ * Released under the MIT license * Date: 2019-11-16 * ---------------------------------------------------------------- */ -'use strict'; - class ParameterChecker { constructor() { this._error = null; diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 2f97a21a..fa48bd4c 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -5,8 +5,6 @@ * Released under the MIT license * Date: 2020-02-11 * ---------------------------------------------------------------- */ -'use strict'; - class SwitchbotAdvertising { constructor() { } diff --git a/lib/switchbot-device-wocurtain.js b/lib/switchbot-device-wocurtain.js index 97deafaf..f7174119 100644 --- a/lib/switchbot-device-wocurtain.js +++ b/lib/switchbot-device-wocurtain.js @@ -5,7 +5,6 @@ * Released under the MIT license * Date: 2020-10-26 * ---------------------------------------------------------------- */ -'use strict'; import SwitchbotDevice from './switchbot-device.js'; class SwitchbotDeviceWoCurtain extends SwitchbotDevice { diff --git a/lib/switchbot-device-wohand.js b/lib/switchbot-device-wohand.js index a4178086..b65225fd 100644 --- a/lib/switchbot-device-wohand.js +++ b/lib/switchbot-device-wohand.js @@ -5,7 +5,6 @@ * Released under the MIT license * Date: 2020-02-10 * ---------------------------------------------------------------- */ -'use strict'; import SwitchbotDevice from './switchbot-device.js'; class SwitchbotDeviceWoHand extends SwitchbotDevice { diff --git a/lib/switchbot-device-wosensorth.js b/lib/switchbot-device-wosensorth.js index 9e5ed710..84c293c6 100644 --- a/lib/switchbot-device-wosensorth.js +++ b/lib/switchbot-device-wosensorth.js @@ -5,7 +5,6 @@ * Released under the MIT license * Date: 2019-11-16 * ---------------------------------------------------------------- */ -'use strict'; import SwitchbotDevice from './switchbot-device.js'; class SwitchbotDeviceWoSensorTH extends SwitchbotDevice { diff --git a/lib/switchbot-device.js b/lib/switchbot-device.js index da8ca62f..90f7eb9d 100644 --- a/lib/switchbot-device.js +++ b/lib/switchbot-device.js @@ -5,7 +5,6 @@ * Released under the MIT license * Date: 2020-02-19 * ---------------------------------------------------------------- */ -'use strict'; import { check, error as _error } from './parameter-checker.js'; import { parse } from './switchbot-advertising.js'; diff --git a/lib/switchbot.js b/lib/switchbot.js index 0a1c8b2b..62ae6d5e 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -5,7 +5,6 @@ * Released under the MIT license * Date: 2019-11-18 * ---------------------------------------------------------------- */ -'use strict'; import { check, error as _error } from './parameter-checker.js'; import { parse } from './switchbot-advertising.js'; From da0a601bac03028f0f47bf6f00e303e129874380 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 15:31:50 -0500 Subject: [PATCH 05/12] Revert "remove strict" This reverts commit 702221362357c5a264faab84abae0f439c747295. --- lib/parameter-checker.js | 2 ++ lib/switchbot-advertising.js | 2 ++ lib/switchbot-device-wocurtain.js | 1 + lib/switchbot-device-wohand.js | 1 + lib/switchbot-device-wosensorth.js | 1 + lib/switchbot-device.js | 1 + lib/switchbot.js | 1 + 7 files changed, 9 insertions(+) diff --git a/lib/parameter-checker.js b/lib/parameter-checker.js index 5da129bb..a1bbd257 100644 --- a/lib/parameter-checker.js +++ b/lib/parameter-checker.js @@ -5,6 +5,8 @@ * Released under the MIT license * Date: 2019-11-16 * ---------------------------------------------------------------- */ +'use strict'; + class ParameterChecker { constructor() { this._error = null; diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index fa48bd4c..2f97a21a 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -5,6 +5,8 @@ * Released under the MIT license * Date: 2020-02-11 * ---------------------------------------------------------------- */ +'use strict'; + class SwitchbotAdvertising { constructor() { } diff --git a/lib/switchbot-device-wocurtain.js b/lib/switchbot-device-wocurtain.js index f7174119..97deafaf 100644 --- a/lib/switchbot-device-wocurtain.js +++ b/lib/switchbot-device-wocurtain.js @@ -5,6 +5,7 @@ * Released under the MIT license * Date: 2020-10-26 * ---------------------------------------------------------------- */ +'use strict'; import SwitchbotDevice from './switchbot-device.js'; class SwitchbotDeviceWoCurtain extends SwitchbotDevice { diff --git a/lib/switchbot-device-wohand.js b/lib/switchbot-device-wohand.js index b65225fd..a4178086 100644 --- a/lib/switchbot-device-wohand.js +++ b/lib/switchbot-device-wohand.js @@ -5,6 +5,7 @@ * Released under the MIT license * Date: 2020-02-10 * ---------------------------------------------------------------- */ +'use strict'; import SwitchbotDevice from './switchbot-device.js'; class SwitchbotDeviceWoHand extends SwitchbotDevice { diff --git a/lib/switchbot-device-wosensorth.js b/lib/switchbot-device-wosensorth.js index 84c293c6..9e5ed710 100644 --- a/lib/switchbot-device-wosensorth.js +++ b/lib/switchbot-device-wosensorth.js @@ -5,6 +5,7 @@ * Released under the MIT license * Date: 2019-11-16 * ---------------------------------------------------------------- */ +'use strict'; import SwitchbotDevice from './switchbot-device.js'; class SwitchbotDeviceWoSensorTH extends SwitchbotDevice { diff --git a/lib/switchbot-device.js b/lib/switchbot-device.js index 90f7eb9d..da8ca62f 100644 --- a/lib/switchbot-device.js +++ b/lib/switchbot-device.js @@ -5,6 +5,7 @@ * Released under the MIT license * Date: 2020-02-19 * ---------------------------------------------------------------- */ +'use strict'; import { check, error as _error } from './parameter-checker.js'; import { parse } from './switchbot-advertising.js'; diff --git a/lib/switchbot.js b/lib/switchbot.js index 62ae6d5e..0a1c8b2b 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -5,6 +5,7 @@ * Released under the MIT license * Date: 2019-11-18 * ---------------------------------------------------------------- */ +'use strict'; import { check, error as _error } from './parameter-checker.js'; import { parse } from './switchbot-advertising.js'; From 31dc6cb602931134efe89f582b64a8d1a98f864f Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 15:31:55 -0500 Subject: [PATCH 06/12] Revert "Update package.json" This reverts commit a9c3838da91afaf713d8e58f8af0cb3afc8ce475. --- package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5aeb3bb9..a55cde30 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,9 @@ "type": "git", "url": "https://github.com/futomi/node-switchbot.git" }, - "type": "module", + "readmeFilename": "README.md", + "type": "module", "dependencies": { "@abandonware/noble": ">=1.9.2-14" } -} \ No newline at end of file +} From e43e84a085bcfb7bbe9ea573e0f17a5a8f8edfbd Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 15:32:05 -0500 Subject: [PATCH 07/12] Revert "Update package.json" This reverts commit d2786ff59aa7006cf9bbe3946875878ef02fe75b. --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index a55cde30..b55c8331 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,7 @@ "type": "git", "url": "https://github.com/futomi/node-switchbot.git" }, - "readmeFilename": "README.md", - "type": "module", + "readmeFilename": "README.md", "dependencies": { "@abandonware/noble": ">=1.9.2-14" } From 022ae664e42d8cbcaf99b12677ea171806d36d62 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 16:03:54 -0500 Subject: [PATCH 08/12] revert es6 --- lib/parameter-checker.js | 2 +- lib/switchbot-advertising.js | 18 ++++------- lib/switchbot-device-wocurtain.js | 2 +- lib/switchbot-device-wohand.js | 4 +-- lib/switchbot-device-wosensorth.js | 4 +-- lib/switchbot-device.js | 13 ++++---- lib/switchbot.js | 52 +++++++++++++----------------- 7 files changed, 42 insertions(+), 53 deletions(-) diff --git a/lib/parameter-checker.js b/lib/parameter-checker.js index a1bbd257..84a7a984 100644 --- a/lib/parameter-checker.js +++ b/lib/parameter-checker.js @@ -468,4 +468,4 @@ class ParameterChecker { } } -export default new ParameterChecker(); +module.exports = new ParameterChecker(); diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 2f97a21a..ea542d4d 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -97,23 +97,19 @@ class SwitchbotAdvertising { return null; } let address = peripheral.address || ''; - if(address === '') - { + if (address === '') { address = peripheral.advertisement.manufacturerData || ''; - if(address !== '') - { + if (address !== '') { const str = peripheral.advertisement.manufacturerData.toString('hex').slice(4); - address = str.substr(0,2); - for(var i = 2; i< str.length; i+=2) - { + address = str.substr(0, 2); + for (var i = 2; i < str.length; i += 2) { address = address + ":" + str.substr(i, 2); } // console.log("address", typeof(address), address); } } - else - { - address=address.replace(/-/g,':'); + else { + address = address.replace(/-/g, ':'); } let data = { id: peripheral.id, @@ -202,4 +198,4 @@ class SwitchbotAdvertising { } } -export default new SwitchbotAdvertising(); \ No newline at end of file +module.exports = new SwitchbotAdvertising(); \ No newline at end of file diff --git a/lib/switchbot-device-wocurtain.js b/lib/switchbot-device-wocurtain.js index 97deafaf..84c8d4c9 100644 --- a/lib/switchbot-device-wocurtain.js +++ b/lib/switchbot-device-wocurtain.js @@ -6,7 +6,7 @@ * Date: 2020-10-26 * ---------------------------------------------------------------- */ 'use strict'; -import SwitchbotDevice from './switchbot-device.js'; +const SwitchbotDevice = require('./switchbot-device.js'); class SwitchbotDeviceWoCurtain extends SwitchbotDevice { diff --git a/lib/switchbot-device-wohand.js b/lib/switchbot-device-wohand.js index a4178086..7c7ce654 100644 --- a/lib/switchbot-device-wohand.js +++ b/lib/switchbot-device-wohand.js @@ -6,7 +6,7 @@ * Date: 2020-02-10 * ---------------------------------------------------------------- */ 'use strict'; -import SwitchbotDevice from './switchbot-device.js'; +const SwitchbotDevice = require('./switchbot-device.js'); class SwitchbotDeviceWoHand extends SwitchbotDevice { @@ -103,4 +103,4 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice { } -export default SwitchbotDeviceWoHand; \ No newline at end of file +module.exports = SwitchbotDeviceWoHand; \ No newline at end of file diff --git a/lib/switchbot-device-wosensorth.js b/lib/switchbot-device-wosensorth.js index 9e5ed710..3b9225d7 100644 --- a/lib/switchbot-device-wosensorth.js +++ b/lib/switchbot-device-wosensorth.js @@ -6,11 +6,11 @@ * Date: 2019-11-16 * ---------------------------------------------------------------- */ 'use strict'; -import SwitchbotDevice from './switchbot-device.js'; +const SwitchbotDevice = require('./switchbot-device.js'); class SwitchbotDeviceWoSensorTH extends SwitchbotDevice { } -export default SwitchbotDeviceWoSensorTH; \ No newline at end of file +module.exports = SwitchbotDeviceWoSensorTH; \ No newline at end of file diff --git a/lib/switchbot-device.js b/lib/switchbot-device.js index da8ca62f..ca2ba5ca 100644 --- a/lib/switchbot-device.js +++ b/lib/switchbot-device.js @@ -6,8 +6,8 @@ * Date: 2020-02-19 * ---------------------------------------------------------------- */ 'use strict'; -import { check, error as _error } from './parameter-checker.js'; -import { parse } from './switchbot-advertising.js'; +const parameterChecker = require('./parameter-checker.js'); +const switchbotAdvertising = require('./switchbot-advertising.js'); class SwitchbotDevice { /* ------------------------------------------------------------------ @@ -16,6 +16,7 @@ class SwitchbotDevice { * [Arguments] * - peripheral | Object | Required | The `peripheral` object of noble, * | | | which represents this device + * - noble | Noble | Required | The Nobel object created by the noble module. * ---------------------------------------------------------------- */ constructor(peripheral, noble) { this._peripheral = peripheral; @@ -32,7 +33,7 @@ class SwitchbotDevice { this._COMMAND_TIMEOUT_MSEC = 3000; // Save the device information - let ad = parse(peripheral); + let ad = switchbotAdvertising.parse(peripheral); this._id = ad.id; this._address = ad.address; this._model = ad.serviceData.model; @@ -374,12 +375,12 @@ class SwitchbotDevice { setDeviceName(name) { return new Promise((resolve, reject) => { // Check the parameters - let valid = check({ name: name }, { + let valid = parameterChecker.check({ name: name }, { name: { required: true, type: 'string', minBytes: 1, maxBytes: 100 } }); if (!valid) { - reject(new Error(_error.message)); + reject(new Error(parameterChecker.error.message)); return; } @@ -494,4 +495,4 @@ class SwitchbotDevice { } -export default SwitchbotDevice; \ No newline at end of file +module.exports = SwitchbotDevice; \ No newline at end of file diff --git a/lib/switchbot.js b/lib/switchbot.js index 0a1c8b2b..19d616d2 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -6,13 +6,12 @@ * Date: 2019-11-18 * ---------------------------------------------------------------- */ 'use strict'; -import { check, error as _error } from './parameter-checker.js'; -import { parse } from './switchbot-advertising.js'; +const parameterChecker = require('./parameter-checker.js'); +const switchbotAdvertising = require('./switchbot-advertising.js'); -import SwitchbotDevice from './switchbot-device.js'; -import SwitchbotDeviceWoHand from './switchbot-device-wohand.js'; -import SwitchbotDeviceWoSensorTH from './switchbot-device-wosensorth.js'; -import SwitchbotDeviceWoCurtain from './switchbot-device-wocurtain.js'; +const SwitchbotDevice = require('./switchbot-device.js'); +const SwitchbotDeviceWoHand = require('./switchbot-device-wohand.js'); +const SwitchbotDeviceWoSensorTH = require('./switchbot-device-wosensorth.js'); class Switchbot { /* ------------------------------------------------------------------ @@ -40,7 +39,6 @@ class Switchbot { this.onadvertisement = null; // Private properties - this._initialized = false; this._scanning = false; this._DEFAULT_DISCOVERY_DURATION = 5000 this._PRIMARY_SERVICE_UUID_LIST = ['cba20d00224d11e69fb80002a5d5c51b']; @@ -55,11 +53,10 @@ class Switchbot { * - duration | Integer | Optional | Duration for discovery process (msec). * | | | The value must be in the range of 1 to 60000. * | | | The default value is 5000 (msec). - * - model | String | Optional | "H", "T" or "c". + * - model | String | Optional | "H" or "T". * | | | 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. + * | | | will discover only Meters. * - id | String | Optional | If this value is set, this method willl discover * | | | only a device whose ID is as same as this value. * | | | The ID is identical to the MAC address. @@ -79,15 +76,15 @@ class Switchbot { discover(params = {}) { let promise = new Promise((resolve, reject) => { // Check the parameters - let valid = check(params, { + let valid = parameterChecker.check(params, { duration: { required: false, type: 'integer', min: 1, max: 60000 }, - model: { required: false, type: 'string', enum: ['H', 'T', 'c'] }, + model: { required: false, type: 'string', enum: ['H', 'T'] }, id: { required: false, type: 'string', min: 12, max: 17 }, quick: { required: false, type: 'boolean' } }, false); if (!valid) { - reject(new Error(_error.message)); + reject(new Error(parameterChecker.error.message)); return; } @@ -181,17 +178,15 @@ class Switchbot { } _getDeviceObject(peripheral, id, model) { - let ad = parse(peripheral); + let ad = switchbotAdvertising.parse(peripheral); if (this._filterAdvertising(ad, id, model)) { let device = null; if (ad.serviceData.model === 'H') { - device = new SwitchbotDeviceWoHand(peripheral); + device = new SwitchbotDeviceWoHand(peripheral, this.noble); } else if (ad.serviceData.model === 'T') { - device = new SwitchbotDeviceWoSensorTH(peripheral); - } else if (ad.serviceData.model === 'c') { - device = new SwitchbotDeviceWoCurtain(peripheral); + device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble); } else { - device = new SwitchbotDevice(peripheral); + device = new SwitchbotDevice(peripheral, this.noble); } return device; } else { @@ -223,16 +218,13 @@ class Switchbot { * * [Arguments] * - params | Object | Optional | - * - model | String | Optional | "H", or "T" or "c". + * - model | String | Optional | "H" or "T". * | | | If "H" is specified, the `onadvertisement` * | | | event hander will be called only when advertising * | | | packets comes from Bots. * | | | If "T" is specified, the `onadvertisement` * | | | event hander will be called only when advertising * | | | packets comes from Meters. - * | | | If "c" is specified, the `onadvertisement` - * | | | event hander will be called only when advertising - * | | | packets comes from Curtains. * - id | String | Optional | If this value is set, the `onadvertisement` * | | | event hander will be called only when advertising * | | | packets comes from devices whose ID is as same as @@ -248,13 +240,13 @@ class Switchbot { startScan(params) { let promise = new Promise((resolve, reject) => { // Check the parameters - let valid = check(params, { - model: { required: false, type: 'string', enum: ['H', 'T', 'c'] }, + let valid = parameterChecker.check(params, { + model: { required: false, type: 'string', enum: ['H', 'T'] }, id: { required: false, type: 'string', min: 12, max: 17 }, }, false); if (!valid) { - reject(new Error(_error.message)); + reject(new Error(parameterChecker.error.message)); return; } @@ -273,7 +265,7 @@ class Switchbot { // Set an handler for the 'discover' event this.noble.on('discover', (peripheral) => { - let ad = parse(peripheral); + let ad = switchbotAdvertising.parse(peripheral); if (this._filterAdvertising(ad, p.id, p.model)) { if (this.onadvertisement && typeof (this.onadvertisement) === 'function') { this.onadvertisement(ad); @@ -325,12 +317,12 @@ class Switchbot { wait(msec) { return new Promise((resolve, reject) => { // Check the parameters - let valid = check({ msec: msec }, { + let valid = parameterChecker.check({ msec: msec }, { msec: { required: true, type: 'integer', min: 0 } }); if (!valid) { - reject(new Error(_error.message)); + reject(new Error(parameterChecker.error.message)); return; } // Set a timer @@ -340,4 +332,4 @@ class Switchbot { } -export default Switchbot; +module.exports = Switchbot; \ No newline at end of file From 4caa72b3b0300928d194eddf052d9e1356a73bff Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 16:33:23 -0500 Subject: [PATCH 09/12] add motion and contact sensors --- lib/switchbot-advertising.js | 28 +++++++++++++++++++++++ lib/switchbot-device-wocurtain.js | 2 +- lib/switchbot-device-wosensorcs.js | 16 +++++++++++++ lib/switchbot-device-wosensorms.js | 16 +++++++++++++ lib/switchbot.js | 36 +++++++++++++++++++++++------- 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 lib/switchbot-device-wosensorcs.js create mode 100644 lib/switchbot-device-wosensorms.js diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index ea542d4d..2d7e5784 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -171,6 +171,34 @@ class SwitchbotAdvertising { return data; } + _parseServiceDataForWoSensorMS(buf) { + if (buf.length !== 6) { + return null; + } + + let data = { + model: 'M', + modelName: 'WoSensorMS', + battery: (byte2 & 0b01111111) + }; + + return data; + } + + _parseServiceDataForWoSensorCS(buf) { + if (buf.length !== 6) { + return null; + } + + let data = { + model: 'CS', + modelName: 'WoSensorCS', + battery: (byte2 & 0b01111111) + }; + + return data; + } + _parseServiceDataForWoCurtain(buf) { if (buf.length !== 5) { return null; diff --git a/lib/switchbot-device-wocurtain.js b/lib/switchbot-device-wocurtain.js index 84c8d4c9..293f0037 100644 --- a/lib/switchbot-device-wocurtain.js +++ b/lib/switchbot-device-wocurtain.js @@ -106,4 +106,4 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice { } } -export default SwitchbotDeviceWoCurtain; \ No newline at end of file +module.exports = SwitchbotDeviceWoCurtain; \ No newline at end of file diff --git a/lib/switchbot-device-wosensorcs.js b/lib/switchbot-device-wosensorcs.js new file mode 100644 index 00000000..cd1916be --- /dev/null +++ b/lib/switchbot-device-wosensorcs.js @@ -0,0 +1,16 @@ +/* ------------------------------------------------------------------ +* node-linking - switchbot-device-wosensorth.js +* +* Copyright (c) 2019, Futomi Hatano, All rights reserved. +* Released under the MIT license +* Date: 2019-11-16 +* ---------------------------------------------------------------- */ +'use strict'; +const SwitchbotDevice = require('./switchbot-device.js'); + +class SwitchbotDeviceWoSensorCS extends SwitchbotDevice { + + +} + +module.exports = SwitchbotDeviceWoSensorCS; \ No newline at end of file diff --git a/lib/switchbot-device-wosensorms.js b/lib/switchbot-device-wosensorms.js new file mode 100644 index 00000000..af2e94a7 --- /dev/null +++ b/lib/switchbot-device-wosensorms.js @@ -0,0 +1,16 @@ +/* ------------------------------------------------------------------ +* node-linking - switchbot-device-wosensorth.js +* +* Copyright (c) 2019, Futomi Hatano, All rights reserved. +* Released under the MIT license +* Date: 2019-11-16 +* ---------------------------------------------------------------- */ +'use strict'; +const SwitchbotDevice = require('./switchbot-device.js'); + +class SwitchbotDeviceWoSensorMS extends SwitchbotDevice { + + +} + +module.exports = SwitchbotDeviceWoSensorMS; \ No newline at end of file diff --git a/lib/switchbot.js b/lib/switchbot.js index 19d616d2..68cc7e65 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -11,6 +11,9 @@ const switchbotAdvertising = require('./switchbot-advertising.js'); const SwitchbotDevice = require('./switchbot-device.js'); const SwitchbotDeviceWoHand = require('./switchbot-device-wohand.js'); +const SwitchbotDeviceWoCurtain = require('./switchbot-device-wocurtain.js'); +const SwitchbotDeviceWoSensorMS = require('./switchbot-device-wosensorms.js'); +const SwitchbotDeviceWoSensorCS = require('./switchbot-device-wosensorcs.js'); const SwitchbotDeviceWoSensorTH = require('./switchbot-device-wosensorth.js'); class Switchbot { @@ -53,10 +56,12 @@ class Switchbot { * - duration | Integer | Optional | Duration for discovery process (msec). * | | | The value must be in the range of 1 to 60000. * | | | The default value is 5000 (msec). - * - model | String | Optional | "H" or "T". - * | | | If "H" is specified, this method will discover - * | | | only Bots. If "T" is specified, this method - * | | | will discover only Meters. + * - model | String | Optional | "H", "T", "M", "CS", or "C". + * | | | If "H" is specified, this method will discover only Bots. + * | | | If "T" is specified, this method will discover only Meters. + * | | | If "M" is specified, this method will discover only Motion Sensors. + * | | | If "CS" is specified, this method will discover only Contact Sensors. + * | | | If "C" is specified, this method will discover only Curtains. * - id | String | Optional | If this value is set, this method willl discover * | | | only a device whose ID is as same as this value. * | | | The ID is identical to the MAC address. @@ -78,7 +83,7 @@ class Switchbot { // Check the parameters let valid = parameterChecker.check(params, { duration: { required: false, type: 'integer', min: 1, max: 60000 }, - model: { required: false, type: 'string', enum: ['H', 'T'] }, + model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'C'] }, id: { required: false, type: 'string', min: 12, max: 17 }, quick: { required: false, type: 'boolean' } }, false); @@ -185,6 +190,12 @@ class Switchbot { device = new SwitchbotDeviceWoHand(peripheral, this.noble); } else if (ad.serviceData.model === 'T') { device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble); + } else if (ad.serviceData.model === 'C') { + device = new SwitchbotDeviceWoCurtain(peripheral, this.noble) + } else if (ad.serviceData.model === 'M') { + device = new SwitchbotDeviceWoSensorMS(peripheral, this.noble); + } else if (ad.serviceData.model === 'CS') { + device = new SwitchbotDeviceWoSensorCS(peripheral, this.noble); } else { device = new SwitchbotDevice(peripheral, this.noble); } @@ -217,14 +228,23 @@ class Switchbot { * - Start to monitor advertising packets coming from switchbot devices * * [Arguments] - * - params | Object | Optional | - * - model | String | Optional | "H" or "T". + * - params | Object | Optional | + * - model | String | Optional | "H", "T", "M", "CS", or "C". * | | | If "H" is specified, the `onadvertisement` * | | | event hander will be called only when advertising * | | | packets comes from Bots. * | | | If "T" is specified, the `onadvertisement` * | | | event hander will be called only when advertising * | | | packets comes from Meters. + * | | | If "M" is specified, the `onadvertisement` + * | | | event hander will be called only when advertising + * | | | packets comes from Motion Sensor. + * | | | If "CS" is specified, the `onadvertisement` + * | | | event hander will be called only when advertising + * | | | packets comes from Contact Sensor. + * | | | If "C" is specified, the `onadvertisement` + * | | | event hander will be called only when advertising + * | | | packets comes from Curtains. * - id | String | Optional | If this value is set, the `onadvertisement` * | | | event hander will be called only when advertising * | | | packets comes from devices whose ID is as same as @@ -241,7 +261,7 @@ class Switchbot { let promise = new Promise((resolve, reject) => { // Check the parameters let valid = parameterChecker.check(params, { - model: { required: false, type: 'string', enum: ['H', 'T'] }, + model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'C'] }, id: { required: false, type: 'string', min: 12, max: 17 }, }, false); From 61fe50da943f85342d49951676cb9d510374cac3 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 17:12:26 -0500 Subject: [PATCH 10/12] Update switchbot.js --- lib/switchbot.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/switchbot.js b/lib/switchbot.js index 68cc7e65..f1de9a2d 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -56,7 +56,7 @@ class Switchbot { * - duration | Integer | Optional | Duration for discovery process (msec). * | | | The value must be in the range of 1 to 60000. * | | | The default value is 5000 (msec). - * - model | String | Optional | "H", "T", "M", "CS", or "C". + * - model | String | Optional | "H", "T", "M", "CS", or "c". * | | | If "H" is specified, this method will discover only Bots. * | | | If "T" is specified, this method will discover only Meters. * | | | If "M" is specified, this method will discover only Motion Sensors. @@ -83,7 +83,7 @@ class Switchbot { // Check the parameters let valid = parameterChecker.check(params, { duration: { required: false, type: 'integer', min: 1, max: 60000 }, - model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'C'] }, + model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'c'] }, id: { required: false, type: 'string', min: 12, max: 17 }, quick: { required: false, type: 'boolean' } }, false); @@ -186,18 +186,24 @@ class Switchbot { let ad = switchbotAdvertising.parse(peripheral); if (this._filterAdvertising(ad, id, model)) { let device = null; - if (ad.serviceData.model === 'H') { - device = new SwitchbotDeviceWoHand(peripheral, this.noble); - } else if (ad.serviceData.model === 'T') { - device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble); - } else if (ad.serviceData.model === 'C') { - device = new SwitchbotDeviceWoCurtain(peripheral, this.noble) - } else if (ad.serviceData.model === 'M') { - device = new SwitchbotDeviceWoSensorMS(peripheral, this.noble); - } else if (ad.serviceData.model === 'CS') { - device = new SwitchbotDeviceWoSensorCS(peripheral, this.noble); - } else { - device = new SwitchbotDevice(peripheral, this.noble); + switch (ad.serviceData.model) { + case 'H': + device = new SwitchbotDeviceWoHand(peripheral, this.noble); + break; + case 'T': + device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble); + break; + case 'M': + device = new SwitchbotDeviceWoHand(peripheral, this.noble); + break; + case 'CS': + device = new SwitchbotDeviceWoHand(peripheral, this.noble); + break; + case 'c': + device = new SwitchbotDeviceWoHand(peripheral, this.noble); + break; + default: // 'resetting', 'unknown' + device = new SwitchbotDevice(peripheral, this.noble); } return device; } else { @@ -261,7 +267,7 @@ class Switchbot { let promise = new Promise((resolve, reject) => { // Check the parameters let valid = parameterChecker.check(params, { - model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'C'] }, + model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'c'] }, id: { required: false, type: 'string', min: 12, max: 17 }, }, false); From 7ea5b7a9823e6d48d2fea9441f3f8bef8e61fec5 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Thu, 29 Jul 2021 17:33:03 -0500 Subject: [PATCH 11/12] Update switchbot.js --- lib/switchbot.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/switchbot.js b/lib/switchbot.js index f1de9a2d..d99f9951 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -194,13 +194,13 @@ class Switchbot { device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble); break; case 'M': - device = new SwitchbotDeviceWoHand(peripheral, this.noble); + device = new SwitchbotDeviceWoSensorMS(peripheral, this.noble); break; case 'CS': - device = new SwitchbotDeviceWoHand(peripheral, this.noble); + device = new SwitchbotDeviceWoSensorCS(peripheral, this.noble); break; case 'c': - device = new SwitchbotDeviceWoHand(peripheral, this.noble); + device = new SwitchbotDeviceWoCurtain(peripheral, this.noble); break; default: // 'resetting', 'unknown' device = new SwitchbotDevice(peripheral, this.noble); From b401123a414b6db97e482c340dfece3795f61e27 Mon Sep 17 00:00:00 2001 From: Donavan Becker Date: Fri, 30 Jul 2021 17:17:36 -0500 Subject: [PATCH 12/12] v1.0.3 # Changes - Fixed misspelling. --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1f69644d..28bd233f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "node-switchbot", - "version": "1.0.2", + "version": "1.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.0.2", + "version": "1.0.3", "license": "MIT", "dependencies": { "@abandonware/noble": ">=1.9.2-14" diff --git a/package.json b/package.json index b55c8331..33906fba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-switchbot", - "version": "1.0.2", + "version": "1.0.3", "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).", "main": "./lib/switchbot.js", "files": [