Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions lib/switchbot-advertising.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -175,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;
Expand Down
4 changes: 2 additions & 2 deletions lib/switchbot-device-wocurtain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -106,4 +106,4 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
}
}

export default SwitchbotDeviceWoCurtain;
module.exports = SwitchbotDeviceWoCurtain;
4 changes: 2 additions & 2 deletions lib/switchbot-device-wohand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -103,4 +103,4 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {

}

export default SwitchbotDeviceWoHand;
module.exports = SwitchbotDeviceWoHand;
16 changes: 16 additions & 0 deletions lib/switchbot-device-wosensorcs.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions lib/switchbot-device-wosensorms.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions lib/switchbot-device-wosensorth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = SwitchbotDeviceWoSensorTH;
13 changes: 7 additions & 6 deletions lib/switchbot-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/* ------------------------------------------------------------------
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -494,4 +495,4 @@ class SwitchbotDevice {

}

export default SwitchbotDevice;
module.exports = SwitchbotDevice;
86 changes: 52 additions & 34 deletions lib/switchbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* 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 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 {
/* ------------------------------------------------------------------
Expand Down Expand Up @@ -40,7 +42,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'];
Expand All @@ -55,11 +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", "T" or "c".
* | | | 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.
* - 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.
Expand All @@ -79,15 +81,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', 'M', 'CS', 'c'] },
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;
}

Expand Down Expand Up @@ -181,17 +183,27 @@ 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);
} else if (ad.serviceData.model === 'T') {
device = new SwitchbotDeviceWoSensorTH(peripheral);
} else if (ad.serviceData.model === 'c') {
device = new SwitchbotDeviceWoCurtain(peripheral);
} else {
device = new SwitchbotDevice(peripheral);
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 SwitchbotDeviceWoSensorMS(peripheral, this.noble);
break;
case 'CS':
device = new SwitchbotDeviceWoSensorCS(peripheral, this.noble);
break;
case 'c':
device = new SwitchbotDeviceWoCurtain(peripheral, this.noble);
break;
default: // 'resetting', 'unknown'
device = new SwitchbotDevice(peripheral, this.noble);
}
return device;
} else {
Expand Down Expand Up @@ -222,15 +234,21 @@ class Switchbot {
* - Start to monitor advertising packets coming from switchbot devices
*
* [Arguments]
* - params | Object | Optional |
* - model | String | Optional | "H", or "T" or "c".
* - 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 "c" is specified, the `onadvertisement`
* | | | 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`
Expand All @@ -248,13 +266,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', 'M', 'CS', 'c'] },
id: { required: false, type: 'string', min: 12, max: 17 },
}, false);

if (!valid) {
reject(new Error(_error.message));
reject(new Error(parameterChecker.error.message));
return;
}

Expand All @@ -273,7 +291,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);
Expand Down Expand Up @@ -325,12 +343,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
Expand All @@ -340,4 +358,4 @@ class Switchbot {

}

export default Switchbot;
module.exports = Switchbot;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading