diff --git a/README.md b/README.md index 757e4c58..67183f4d 100644 --- a/README.md +++ b/README.md @@ -110,9 +110,9 @@ Monitoring the advertising packets, you can find your devices and know the lates ```JavaScript // Load the node-switchbot and get a `Switchbot` constructor object -let Switchbot = require('node-switchbot'); +const Switchbot = require('node-switchbot'); // Create an `Switchbot` object -let switchbot = new Switchbot(); +const switchbot = new Switchbot(); (async () => { // Start to monitor advertisement packets @@ -137,9 +137,9 @@ The [`startScan()`](#startscan-method) and [`wait()`](#Switchbot-wait-method) me ```javascript // Load the node-switchbot and get a `Switchbot` constructor object -let Switchbot = require("node-switchbot"); +const Switchbot = require("node-switchbot"); // Create an `Switchbot` object -let switchbot = new Switchbot(); +const switchbot = new Switchbot(); // Start to monitor advertisement packets switchbot @@ -213,18 +213,18 @@ This sample discovers a Bot (WoHand), then put the Bot's arm down, finally put i ```javascript // Load the node-switchbot and get a `Switchbot` constructor object -let Switchbot = require("node-switchbot"); +const Switchbot = require("node-switchbot"); // Create an `Switchbot` object -let switchbot = new Switchbot(); +const switchbot = new Switchbot(); (async () => { // Find a Bot (WoHand) - let bot_list = await switchbot.discover({ model: "H", quick: true }); + const bot_list = await switchbot.discover({ model: "H", quick: true }); if (bot_list.length === 0) { throw new Error("No device was found."); } // The `SwitchbotDeviceWoHand` object representing the found Bot. - let device = bot_list[0]; + const device = bot_list[0]; // Put the Bot's arm down (stretch the arm) await device.down(); // Wait for 5 seconds @@ -246,13 +246,13 @@ In this code, you can get a [`SwitchbotDeviceWoHand`](#SwitchbotDeviceWoHand-obj In order to use the node-switchbot, you have to load the node-switchbot module as follows: ```JavaScript -let Switchbot = require('node-switchbot'); +const Switchbot = require('node-switchbot'); ``` You can get an `Switchbot` constructor from the code above. Then you have to create an `Switchbot` object from the `Switchbot` constructor as follows: ```javascript -let switchbot = new Switchbot(); +const switchbot = new Switchbot(); ``` The `Switchbot` constructor takes an argument optionally. It must be a hash object containing the properties as follows: @@ -263,15 +263,15 @@ The `Switchbot` constructor takes an argument optionally. It must be a hash obje The node-switchbot module uses the [`@abandonware/noble`](https://github.com/abandonware/noble) module in order to interact with BLE devices. If you want to interact other BLE devices using the `@abandonware/noble` module, you can create an `Noble` object by yourself, then pass it to this module. If you don't specify a `Noble` object to the `noble` property, this module automatically create a `Noble` object internally. -The sample code below shows how to pass a `Nobel` object to the `Switchbot` constructor. +The sample code below shows how to pass a `Noble` object to the `Switchbot` constructor. ```JavaScript // Create a Noble object -let noble = require('@abandonware/noble'); +const noble = require('@abandonware/noble'); // Create a Switchbot object -let Switchbot = require('node-switchbot'); -let switchbot = new Switchbot({'noble': noble}); +const Switchbot = require('node-switchbot'); +const switchbot = new Switchbot({'noble': noble}); ``` In the code snippet above, the variable `switchbot` is an `Switchbot` object. The `Switchbot` object has a lot of methods as described in sections below. @@ -565,7 +565,7 @@ The code below calls the [`press()`](#SwitchbotDeviceWoHand-press-method) method switchbot .discover({ model: "H", quick: true }) .then((device_list) => { - let device = device_list[0]; + const device = device_list[0]; if (!device) { console.log("No device was found."); process.exit(); diff --git a/lib/parameter-checker.js b/lib/parameter-checker.js index cbb3f2c6..39a2d5a7 100644 --- a/lib/parameter-checker.js +++ b/lib/parameter-checker.js @@ -37,7 +37,7 @@ class ParameterChecker { * an `Error` object will be set to `this._error`. * * [Usage] - * let valid = parameterChecker.check(params, { + * const valid = parameterChecker.check(params, { * level: { * required: false, * type: 'integer', @@ -50,7 +50,7 @@ class ParameterChecker { * } * }); * if(!valid) { - * let e = parameterChecker.error.message; + * const e = parameterChecker.error.message; * throw new Error(message); * } * ---------------------------------------------------------------- */ @@ -79,11 +79,11 @@ class ParameterChecker { } let result = true; - let name_list = Object.keys(rules); + const name_list = Object.keys(rules); for (let i = 0; i < name_list.length; i++) { - let name = name_list[i]; - let v = obj[name]; + const name = name_list[i]; + const v = obj[name]; let rule = rules[name]; if (!rule) { @@ -458,7 +458,7 @@ class ParameterChecker { } } if (typeof rule.minBytes === "number") { - let blen = Buffer.from(value, "utf8").length; + const blen = Buffer.from(value, "utf8").length; if (blen < rule.minBytes) { this._error = { code: "LENGTH_UNDERFLOW", @@ -475,7 +475,7 @@ class ParameterChecker { } } if (typeof rule.maxBytes === "number") { - let blen = Buffer.from(value, "utf8").length; + const blen = Buffer.from(value, "utf8").length; if (blen > rule.maxBytes) { this._error = { code: "LENGTH_OVERFLOW", diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 9d7511ff..ca766ad4 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -61,16 +61,16 @@ class SwitchbotAdvertising { * device, this method will return `null`. * ---------------------------------------------------------------- */ parse(peripheral, onlog) { - let ad = peripheral.advertisement; + const ad = peripheral.advertisement; if (!ad || !ad.serviceData) { return null; } - let serviceData = ad.serviceData[0] || ad.serviceData; - let manufacturerData = ad.manufacturerData; - let buf = serviceData.data; + const serviceData = ad.serviceData[0] || ad.serviceData; + const manufacturerData = ad.manufacturerData; + const buf = serviceData.data; - let bufIsInvalid = !buf || !Buffer.isBuffer(buf) || buf.length < 3; - let manufacturerDataIsInvalid = + const bufIsInvalid = !buf || !Buffer.isBuffer(buf) || buf.length < 3; + const manufacturerDataIsInvalid = !manufacturerData || !Buffer.isBuffer(manufacturerData) || manufacturerData.length < 3; @@ -79,7 +79,7 @@ class SwitchbotAdvertising { return null; } - let model = buf.slice(0, 1).toString("utf8"); + const model = buf.slice(0, 1).toString("utf8"); let sd = null; if (model === "H") { @@ -129,7 +129,7 @@ class SwitchbotAdvertising { if (address === "") { address = peripheral.advertisement.manufacturerData || ""; if (address !== "") { - let str = peripheral.advertisement.manufacturerData + const str = peripheral.advertisement.manufacturerData .toString("hex") .slice(4, 16); address = str.substr(0, 2); @@ -141,7 +141,7 @@ class SwitchbotAdvertising { } else { address = address.replace(/-/g, ":"); } - let data = { + const data = { id: peripheral.id, address: address, rssi: peripheral.rssi, @@ -167,14 +167,14 @@ class SwitchbotAdvertising { } return null; } - let byte1 = buf.readUInt8(1); - let byte2 = buf.readUInt8(2); + const byte1 = buf.readUInt8(1); + const byte2 = buf.readUInt8(2); - let mode = byte1 & 0b10000000 ? true : false; // Whether the light switch Add-on is used or not - let state = byte1 & 0b01000000 ? true : false; // Whether the switch status is ON or OFF - let battery = byte2 & 0b01111111; // % + const mode = byte1 & 0b10000000 ? true : false; // Whether the light switch Add-on is used or not + const state = byte1 & 0b01000000 ? true : false; // Whether the switch status is ON or OFF + const battery = byte2 & 0b01111111; // % - let data = { + const data = { model: "H", modelName: "WoHand", mode: mode, @@ -194,17 +194,17 @@ class SwitchbotAdvertising { } return null; } - let byte2 = buf.readUInt8(2); - let byte3 = buf.readUInt8(3); - let byte4 = buf.readUInt8(4); - let byte5 = buf.readUInt8(5); - - let temp_sign = byte4 & 0b10000000 ? 1 : -1; - let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10); - let temp_f = (temp_c * 9 / 5) + 32; + const byte2 = buf.readUInt8(2); + const byte3 = buf.readUInt8(3); + const byte4 = buf.readUInt8(4); + const byte5 = buf.readUInt8(5); + + const temp_sign = byte4 & 0b10000000 ? 1 : -1; + const temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10); + const temp_f = (temp_c * 9 / 5) + 32; temp_f = Math.round(temp_f * 10) / 10; - let data = { + const data = { model: "T", modelName: "WoSensorTH", temperature: { @@ -228,15 +228,15 @@ class SwitchbotAdvertising { } return null; } - let byte1 = buf.readUInt8(1); - let byte4 = buf.readUInt8(4); + const byte1 = buf.readUInt8(1); + const byte4 = buf.readUInt8(4); - let onState = byte1 & 0b10000000 ? true : false; // 1 - on - let autoMode = byte4 & 0b10000000 ? true : false; // 1 - auto - let percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3 + const onState = byte1 & 0b10000000 ? true : false; // 1 - on + const autoMode = byte4 & 0b10000000 ? true : false; // 1 - auto + const percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3 - let data = { + const data = { model: "e", modelName: "WoHumi", onState: onState, @@ -257,20 +257,20 @@ class SwitchbotAdvertising { return null; } - let byte1 = buf.readUInt8(1); - let byte2 = buf.readUInt8(2); - let byte5 = buf.readUInt8(5); + const byte1 = buf.readUInt8(1); + const byte2 = buf.readUInt8(2); + const byte5 = buf.readUInt8(5); - let tested = byte1 & 0b10000000 ? true : false; - let movement = byte1 & 0b01000000 ? true : false; - let battery = byte2 & 0b01111111; - let led = (byte5 & 0b00100000) >> 5; - let iot = (byte5 & 0b00010000) >> 4; - let sense_distance = (byte5 & 0b00001100) >> 2; - let lightLevel = byte5 & 0b00000011; - let is_light = byte5 & 0b00000010 ? true : false; + const tested = byte1 & 0b10000000 ? true : false; + const movement = byte1 & 0b01000000 ? true : false; + const battery = byte2 & 0b01111111; + const led = (byte5 & 0b00100000) >> 5; + const iot = (byte5 & 0b00010000) >> 4; + const sense_distance = (byte5 & 0b00001100) >> 2; + const lightLevel = byte5 & 0b00000011; + const is_light = byte5 & 0b00000010 ? true : false; - let data = { + const data = { model: "s", modelName: "WoMotion", tested: tested, @@ -297,21 +297,21 @@ class SwitchbotAdvertising { return null; } - let byte1 = buf.readUInt8(1); - let byte2 = buf.readUInt8(2); - let byte3 = buf.readUInt8(3); - let byte8 = buf.readUInt8(8); - - let hallState = (byte3 >> 1) & 0b00000011; - let tested = byte1 & 0b10000000; - let movement = byte1 & 0b01000000 ? true : false; // 1 - Movement detected - let battery = byte2 & 0b01111111; // % - let contact_open = byte3 & 0b00000010 == 0b00000010; - let contact_timeout = byte3 & 0b00000100 == 0b00000100; - let lightLevel = byte3 & 0b00000001; - let button_count = byte8 & 0b00001111; - - let data = { + const byte1 = buf.readUInt8(1); + const byte2 = buf.readUInt8(2); + const byte3 = buf.readUInt8(3); + const byte8 = buf.readUInt8(8); + + const hallState = (byte3 >> 1) & 0b00000011; + const tested = byte1 & 0b10000000; + const movement = byte1 & 0b01000000 ? true : false; // 1 - Movement detected + const battery = byte2 & 0b01111111; // % + const contact_open = byte3 & 0b00000010 == 0b00000010; + const contact_timeout = byte3 & 0b00000100 == 0b00000100; + const lightLevel = byte3 & 0b00000001; + const button_count = byte8 & 0b00001111; + + const data = { model: "d", modelName: "WoContact", movement: movement, @@ -341,19 +341,19 @@ class SwitchbotAdvertising { } return null; } - let byte1 = buf.readUInt8(1); - let byte2 = buf.readUInt8(2); - let byte3 = buf.readUInt8(3); - let byte4 = buf.readUInt8(4); - - let calibration = byte1 & 0b01000000 ? true : false; // Whether the calibration is completed - let battery = byte2 & 0b01111111; // % - let inMotion = byte3 & 0b10000000 ? true : false; - let currPosition = byte3 & 0b01111111; // current positon % - let lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10) - let deviceChain = byte4 & 0b00000111; - - let data = { + const byte1 = buf.readUInt8(1); + const byte2 = buf.readUInt8(2); + const byte3 = buf.readUInt8(3); + const byte4 = buf.readUInt8(4); + + const calibration = byte1 & 0b01000000 ? true : false; // Whether the calibration is compconsted + const battery = byte2 & 0b01111111; // % + const inMotion = byte3 & 0b10000000 ? true : false; + const currPosition = byte3 & 0b01111111; // current positon % + const lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10) + const deviceChain = byte4 & 0b00000111; + + const data = { model: "c", modelName: "WoCurtain", calibration: calibration, @@ -407,31 +407,31 @@ class SwitchbotAdvertising { } return null; } - let byte1 = manufacturerData.readUInt8(1);//power and light status - let byte2 = manufacturerData.readUInt8(2);//bulb brightness - let byte3 = manufacturerData.readUInt8(3);//bulb R - let byte4 = manufacturerData.readUInt8(4);//bulb G - let byte5 = manufacturerData.readUInt8(5);//bulb B - let byte6 = manufacturerData.readUInt8(6);//bulb temperature - let byte7 = manufacturerData.readUInt8(7); - let byte8 = manufacturerData.readUInt8(8); - let byte9 = manufacturerData.readUInt8(9); - let byte10 = manufacturerData.readUInt8(10);//bulb mode - - let power = byte1; - let red = byte3; - let green = byte4; - let blue = byte5; - let color_temperature = byte6; - let state = byte7 & 0b01111111 ? true : false; - let brightness = byte7 & 0b01111111; - let delay = byte8 & 0b10000000; - let preset = byte8 & 0b00001000; - let color_mode = byte8 & 0b00000111; - let speed = byte9 & 0b01111111; - let loop_index = byte10 & 0b11111110; - - let data = { + const byte1 = manufacturerData.readUInt8(1);//power and light status + const byte2 = manufacturerData.readUInt8(2);//bulb brightness + const byte3 = manufacturerData.readUInt8(3);//bulb R + const byte4 = manufacturerData.readUInt8(4);//bulb G + const byte5 = manufacturerData.readUInt8(5);//bulb B + const byte6 = manufacturerData.readUInt8(6);//bulb temperature + const byte7 = manufacturerData.readUInt8(7); + const byte8 = manufacturerData.readUInt8(8); + const byte9 = manufacturerData.readUInt8(9); + const byte10 = manufacturerData.readUInt8(10);//bulb mode + + const power = byte1; + const red = byte3; + const green = byte4; + const blue = byte5; + const color_temperature = byte6; + const state = byte7 & 0b01111111 ? true : false; + const brightness = byte7 & 0b01111111; + const delay = byte8 & 0b10000000; + const preset = byte8 & 0b00001000; + const color_mode = byte8 & 0b00000111; + const speed = byte9 & 0b01111111; + const loop_index = byte10 & 0b11111110; + + const data = { model: "u", modelName: "WoBulb", color_temperature: color_temperature, @@ -460,22 +460,22 @@ class SwitchbotAdvertising { } return null; } - let byte9 = manufacturerData.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on - let byte10 = manufacturerData.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time - let byte11 = manufacturerData.readUInt8(11); // byte11: wifi rssi - let byte12 = manufacturerData.readUInt8(12); // byte12: bit7: overload? - let byte13 = manufacturerData.readUInt8(13); // byte12[bit0~6] + byte13: current power value - - let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null; - let delay = !!(byte10 & 0b00000001); - let timer = !!(byte10 & 0b00000010); - let syncUtcTime = !!(byte10 & 0b00000100); - let wifiRssi = byte11; - let overload = !!(byte12 & 0b10000000); - let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt + const byte9 = manufacturerData.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on + const byte10 = manufacturerData.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time + const byte11 = manufacturerData.readUInt8(11); // byte11: wifi rssi + const byte12 = manufacturerData.readUInt8(12); // byte12: bit7: overload? + const byte13 = manufacturerData.readUInt8(13); // byte12[bit0~6] + byte13: current power value + + const state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null; + const delay = !!(byte10 & 0b00000001); + const timer = !!(byte10 & 0b00000010); + const syncUtcTime = !!(byte10 & 0b00000100); + const wifiRssi = byte11; + const overload = !!(byte12 & 0b10000000); + const currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt // TODO: voltage ??? - let data = { + const data = { model: "g", modelName: "WoPlugMini", state: state, @@ -499,22 +499,22 @@ class SwitchbotAdvertising { } return null; } - let byte9 = manufacturerData.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on - let byte10 = manufacturerData.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time - let byte11 = manufacturerData.readUInt8(11); // byte11: wifi rssi - let byte12 = manufacturerData.readUInt8(12); // byte12: bit7: overload? - let byte13 = manufacturerData.readUInt8(13); // byte12[bit0~6] + byte13: current power value - - let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null; - let delay = !!(byte10 & 0b00000001); - let timer = !!(byte10 & 0b00000010); - let syncUtcTime = !!(byte10 & 0b00000100); - let wifiRssi = byte11; - let overload = !!(byte12 & 0b10000000); - let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt + const byte9 = manufacturerData.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on + const byte10 = manufacturerData.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time + const byte11 = manufacturerData.readUInt8(11); // byte11: wifi rssi + const byte12 = manufacturerData.readUInt8(12); // byte12: bit7: overload? + const byte13 = manufacturerData.readUInt8(13); // byte12[bit0~6] + byte13: current power value + + const state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null; + const delay = !!(byte10 & 0b00000001); + const timer = !!(byte10 & 0b00000010); + const syncUtcTime = !!(byte10 & 0b00000100); + const wifiRssi = byte11; + const overload = !!(byte12 & 0b10000000); + const currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt // TODO: voltage ??? - let data = { + const data = { model: "j", modelName: "WoPlugMini", state: state, @@ -538,12 +538,12 @@ class SwitchbotAdvertising { } return null; } - let byte2 = manufacturerData.readUInt8(2); - let byte7 = manufacturerData.readUInt8(7); - let byte8 = manufacturerData.readUInt8(8); + const byte2 = manufacturerData.readUInt8(2); + const byte7 = manufacturerData.readUInt8(7); + const byte8 = manufacturerData.readUInt8(8); - let LockStatus = { + const LockStatus = { LOCKED: 0b0000000, UNLOCKED: 0b0010000, LOCKING: 0b0100000, @@ -553,17 +553,17 @@ class SwitchbotAdvertising { NOT_FULLY_LOCKED: 0b1100000, //Only EU lock type } - let battery = byte2 & 0b01111111; // % - let calibration = byte7 & 0b10000000 ? true : false; - let status = LockStatus(byte7 & 0b01110000); - let update_from_secondary_lock = byte7 & 0b00001000 ? true : false; - let door_open = byte7 & 0b00000100 ? true : false; - let double_lock_mode = byte8 & 0b10000000 ? true : false; - let unclosed_alarm = byte8 & 0b00100000 ? true : false; - let unlocked_alarm = byte8 & 0b00010000 ? true : false; - let auto_lock_paused = byte8 & 0b00000010 ? true : false; - - let data = { + const battery = byte2 & 0b01111111; // % + const calibration = byte7 & 0b10000000 ? true : false; + const status = LockStatus(byte7 & 0b01110000); + const update_from_secondary_lock = byte7 & 0b00001000 ? true : false; + const door_open = byte7 & 0b00000100 ? true : false; + const double_lock_mode = byte8 & 0b10000000 ? true : false; + const unclosed_alarm = byte8 & 0b00100000 ? true : false; + const unlocked_alarm = byte8 & 0b00010000 ? true : false; + const auto_lock_paused = byte8 & 0b00000010 ? true : false; + + const data = { model: "o", modelName: "WoSmartLock", battery: battery, @@ -589,17 +589,17 @@ class SwitchbotAdvertising { } return null; } - let byte2 = buf.readUInt8(2); - let byte3 = buf.readUInt8(3); - let byte4 = buf.readUInt8(4); - let byte5 = buf.readUInt8(5); - - let temp_sign = byte4 & 0b10000000 ? 1 : -1; - let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10); - let temp_f = (temp_c * 9 / 5) + 32; + const byte2 = buf.readUInt8(2); + const byte3 = buf.readUInt8(3); + const byte4 = buf.readUInt8(4); + const byte5 = buf.readUInt8(5); + + const temp_sign = byte4 & 0b10000000 ? 1 : -1; + const temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10); + const temp_f = (temp_c * 9 / 5) + 32; temp_f = Math.round(temp_f * 10) / 10; - let data = { + const data = { model: "i", modelName: "WoSensorTHPlus", temperature: { @@ -624,28 +624,28 @@ class SwitchbotAdvertising { return null; } - let byte1 = buf.readUInt8(1);//power and light status - let byte2 = buf.readUInt8(2);//bulb brightness - let byte3 = buf.readUInt8(3);//bulb R - let byte4 = buf.readUInt8(4);//bulb G - let byte5 = buf.readUInt8(5);//bulb B - let byte7 = buf.readUInt8(7); - let byte8 = buf.readUInt8(8); - let byte9 = buf.readUInt8(9); - let byte10 = buf.readUInt8(10); - - let state = byte7 & 0b10000000 ? true : false; - let brightness = byte7 & 0b01111111; - let red = byte3; - let green = byte4; - let blue = byte5; - let delay = byte8 & 0b10000000; - let preset = byte8 & 0b00001000; - let color_mode = byte8 & 0b00000111; - let speed = byte9 & 0b01111111; - let loop_index = byte10 & 0b11111110; - - let data = { + const byte1 = buf.readUInt8(1);//power and light status + const byte2 = buf.readUInt8(2);//bulb brightness + const byte3 = buf.readUInt8(3);//bulb R + const byte4 = buf.readUInt8(4);//bulb G + const byte5 = buf.readUInt8(5);//bulb B + const byte7 = buf.readUInt8(7); + const byte8 = buf.readUInt8(8); + const byte9 = buf.readUInt8(9); + const byte10 = buf.readUInt8(10); + + const state = byte7 & 0b10000000 ? true : false; + const brightness = byte7 & 0b01111111; + const red = byte3; + const green = byte4; + const blue = byte5; + const delay = byte8 & 0b10000000; + const preset = byte8 & 0b00001000; + const color_mode = byte8 & 0b00000111; + const speed = byte9 & 0b01111111; + const loop_index = byte10 & 0b11111110; + + const data = { model: "r", modelName: "WoStrip", state: state, diff --git a/lib/switchbot-device-wobulb.js b/lib/switchbot-device-wobulb.js index 3146709c..cb0e2a7e 100644 --- a/lib/switchbot-device-wobulb.js +++ b/lib/switchbot-device-wobulb.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); /** * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md @@ -16,7 +16,7 @@ class SwitchbotDeviceWoBulb extends SwitchbotDevice { * @private */ _setState(reqByteArray) { - let base = [0x57, 0x0f, 0x47, 0x01]; + const base = [0x57, 0x0f, 0x47, 0x01]; return this._operateBot([].concat(base, reqByteArray)); } @@ -149,15 +149,15 @@ class SwitchbotDeviceWoBulb extends SwitchbotDevice { * @private */ _operateBot(bytes) { - let req_buf = Buffer.from(bytes); + const req_buf = Buffer.from(bytes); return new Promise((resolve, reject) => { this._command(req_buf) .then((res_bytes) => { - let res_buf = Buffer.from(res_bytes); + const res_buf = Buffer.from(res_bytes); if (res_buf.length === 2) { - let code = res_buf.readUInt8(1); + const code = res_buf.readUInt8(1); if (code === 0x00 || code === 0x80) { - let is_on = code === 0x80; + const is_on = code === 0x80; resolve(is_on); } else { reject( diff --git a/lib/switchbot-device-wocontact.js b/lib/switchbot-device-wocontact.js index 287959ae..63443786 100644 --- a/lib/switchbot-device-wocontact.js +++ b/lib/switchbot-device-wocontact.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); class SwitchbotDeviceWoContact extends SwitchbotDevice {} diff --git a/lib/switchbot-device-wocurtain.js b/lib/switchbot-device-wocurtain.js index 84200a51..90da249d 100644 --- a/lib/switchbot-device-wocurtain.js +++ b/lib/switchbot-device-wocurtain.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); class SwitchbotDeviceWoCurtain extends SwitchbotDevice { /* ------------------------------------------------------------------ @@ -93,10 +93,10 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice { _operateCurtain(bytes) { return new Promise((resolve, reject) => { - let req_buf = Buffer.from(bytes); + const req_buf = Buffer.from(bytes); this._command(req_buf) .then((res_buf) => { - let code = res_buf.readUInt8(0); + const code = res_buf.readUInt8(0); if (res_buf.length === 3 && code === 0x01) { resolve(); } else { diff --git a/lib/switchbot-device-wohand.js b/lib/switchbot-device-wohand.js index 891da386..3cd9378f 100644 --- a/lib/switchbot-device-wohand.js +++ b/lib/switchbot-device-wohand.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); class SwitchbotDeviceWoHand extends SwitchbotDevice { /* ------------------------------------------------------------------ @@ -79,10 +79,10 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice { _operateBot(bytes) { return new Promise((resolve, reject) => { - let req_buf = Buffer.from(bytes); + const req_buf = Buffer.from(bytes); this._command(req_buf) .then((res_buf) => { - let code = res_buf.readUInt8(0); + const code = res_buf.readUInt8(0); if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) { resolve(); } else { diff --git a/lib/switchbot-device-wohumi.js b/lib/switchbot-device-wohumi.js index 82c8d3e9..e7d055bd 100644 --- a/lib/switchbot-device-wohumi.js +++ b/lib/switchbot-device-wohumi.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); class SwitchbotDeviceWoHumi extends SwitchbotDevice { /* ------------------------------------------------------------------ @@ -79,10 +79,10 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice { _operateBot(bytes) { return new Promise((resolve, reject) => { - let req_buf = Buffer.from(bytes); + const req_buf = Buffer.from(bytes); this._command(req_buf) .then((res_buf) => { - let code = res_buf.readUInt8(0); + const code = res_buf.readUInt8(0); if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) { resolve(); } else { diff --git a/lib/switchbot-device-woplugmini.js b/lib/switchbot-device-woplugmini.js index f156c24a..0e91068d 100644 --- a/lib/switchbot-device-woplugmini.js +++ b/lib/switchbot-device-woplugmini.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); /** * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/plugmini.md @@ -16,7 +16,7 @@ class SwitchbotDeviceWoPlugMini extends SwitchbotDevice { * @private */ _setState(reqByteArray) { - let base = [0x57, 0x0f, 0x50, 0x01]; + const base = [0x57, 0x0f, 0x50, 0x01]; return this._operateBot([].concat(base, reqByteArray)); } @@ -45,15 +45,15 @@ class SwitchbotDeviceWoPlugMini extends SwitchbotDevice { * @private */ _operateBot(bytes) { - let req_buf = Buffer.from(bytes); + const req_buf = Buffer.from(bytes); return new Promise((resolve, reject) => { this._command(req_buf) .then((res_bytes) => { - let res_buf = Buffer.from(res_bytes); + const res_buf = Buffer.from(res_bytes); if (res_buf.length === 2) { - let code = res_buf.readUInt8(1); + const code = res_buf.readUInt8(1); if (code === 0x00 || code === 0x80) { - let is_on = code === 0x80; + const is_on = code === 0x80; resolve(is_on); } else { reject( diff --git a/lib/switchbot-device-wopresence.js b/lib/switchbot-device-wopresence.js index f0cd5779..e5f28ca9 100644 --- a/lib/switchbot-device-wopresence.js +++ b/lib/switchbot-device-wopresence.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); class SwitchbotDeviceWoPresence extends SwitchbotDevice {} diff --git a/lib/switchbot-device-wosensorth.js b/lib/switchbot-device-wosensorth.js index 340b42f5..8f0579a5 100644 --- a/lib/switchbot-device-wosensorth.js +++ b/lib/switchbot-device-wosensorth.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); class SwitchbotDeviceWoSensorTH extends SwitchbotDevice {} diff --git a/lib/switchbot-device-wostrip.js b/lib/switchbot-device-wostrip.js index 37516788..db444e61 100644 --- a/lib/switchbot-device-wostrip.js +++ b/lib/switchbot-device-wostrip.js @@ -1,5 +1,5 @@ "use strict"; -let SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDevice = require("./switchbot-device.js"); /** * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md @@ -16,7 +16,7 @@ class SwitchbotDeviceWoStrip extends SwitchbotDevice { * @private */ _setState(reqByteArray) { - let base = [0x57, 0x0f, 0x49, 0x01]; + const base = [0x57, 0x0f, 0x49, 0x01]; return this._operateBot([].concat(base, reqByteArray)); } @@ -143,15 +143,15 @@ class SwitchbotDeviceWoStrip extends SwitchbotDevice { * @private */ _operateBot(bytes) { - let req_buf = Buffer.from(bytes); + const req_buf = Buffer.from(bytes); return new Promise((resolve, reject) => { this._command(req_buf) .then((res_bytes) => { - let res_buf = Buffer.from(res_bytes); + const res_buf = Buffer.from(res_bytes); if (res_buf.length === 2) { - let code = res_buf.readUInt8(1); + const code = res_buf.readUInt8(1); if (code === 0x00 || code === 0x80) { - let is_on = code === 0x80; + const is_on = code === 0x80; resolve(is_on); } else { reject( diff --git a/lib/switchbot-device.js b/lib/switchbot-device.js index afa185fa..b1ddaa8c 100644 --- a/lib/switchbot-device.js +++ b/lib/switchbot-device.js @@ -1,6 +1,6 @@ "use strict"; -let parameterChecker = require("./parameter-checker.js"); -let switchbotAdvertising = require("./switchbot-advertising.js"); +const parameterChecker = require("./parameter-checker.js"); +const switchbotAdvertising = require("./switchbot-advertising.js"); class SwitchbotDevice { /* ------------------------------------------------------------------ @@ -9,7 +9,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. + * - noble | Noble | Required | The Noble object created by the noble module. * ---------------------------------------------------------------- */ constructor(peripheral, noble) { this._peripheral = peripheral; @@ -26,7 +26,7 @@ class SwitchbotDevice { this._COMMAND_TIMEOUT_MSEC = 3000; // Save the device information - let ad = switchbotAdvertising.parse(peripheral); + const ad = switchbotAdvertising.parse(peripheral); this._id = ad.id; this._address = ad.address; this._model = ad.serviceData.model; @@ -105,7 +105,7 @@ class SwitchbotDevice { } // Check the connection state - let state = this.connectionState; + const state = this.connectionState; if (state === "connected") { resolve(); return; @@ -179,19 +179,19 @@ class SwitchbotDevice { // Discover services and characteristics (async () => { - let service_list = await this._discoverServices(); + const service_list = await this._discoverServices(); if (!timer) { throw new Error(""); } - let chars = { + const chars = { write: null, notify: null, device: null, }; for (let service of service_list) { - let char_list = await this._discoverCharacteristics(service); + const char_list = await this._discoverCharacteristics(service); for (let char of char_list) { if (char.uuid === this._CHAR_UUID_WRITE) { chars.write = char; @@ -260,7 +260,7 @@ class SwitchbotDevice { _subscribe() { return new Promise((resolve, reject) => { - let char = this._chars.notify; + const char = this._chars.notify; if (!char) { reject(new Error("No notify characteristic was found.")); return; @@ -280,7 +280,7 @@ class SwitchbotDevice { _unsubscribe() { return new Promise((resolve) => { - let char = this._chars.notify; + const char = this._chars.notify; if (!char) { resolve(); return; @@ -307,7 +307,7 @@ class SwitchbotDevice { return new Promise((resolve, reject) => { this._was_connected_explicitly = false; // Check the connection state - let state = this._peripheral.state; + const state = this._peripheral.state; if (state === "disconnected") { resolve(); return; @@ -351,7 +351,7 @@ class SwitchbotDevice { * ---------------------------------------------------------------- */ getDeviceName() { return new Promise((resolve, reject) => { - let name = ""; + const name = ""; this._connect() .then(() => { if (!this._chars.device) { @@ -392,7 +392,7 @@ class SwitchbotDevice { setDeviceName(name) { return new Promise((resolve, reject) => { // Check the parameters - let valid = parameterChecker.check( + const valid = parameterChecker.check( { name: name }, { name: { required: true, type: "string", minBytes: 1, maxBytes: 100 }, @@ -404,7 +404,7 @@ class SwitchbotDevice { return; } - let buf = Buffer.from(name, "utf8"); + const buf = Buffer.from(name, "utf8"); this._connect() .then(() => { if (!this._chars.device) { diff --git a/lib/switchbot.js b/lib/switchbot.js index 362553c2..93557361 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -1,18 +1,18 @@ "use strict"; -let parameterChecker = require("./parameter-checker.js"); -let switchbotAdvertising = require("./switchbot-advertising.js"); +const parameterChecker = require("./parameter-checker.js"); +const switchbotAdvertising = require("./switchbot-advertising.js"); -let SwitchbotDevice = require("./switchbot-device.js"); -let SwitchbotDeviceWoHand = require("./switchbot-device-wohand.js"); -let SwitchbotDeviceWoCurtain = require("./switchbot-device-wocurtain.js"); -let SwitchbotDeviceWoBlindTilt = require("./switchbot-device-woblindtilt.js"); -let SwitchbotDeviceWoPresence = require("./switchbot-device-wopresence.js"); -let SwitchbotDeviceWoContact = require("./switchbot-device-wocontact.js"); -let SwitchbotDeviceWoSensorTH = require("./switchbot-device-wosensorth.js"); -let SwitchbotDeviceWoHumi = require("./switchbot-device-wohumi.js"); -let SwitchbotDeviceWoPlugMini = require("./switchbot-device-woplugmini.js"); -let SwitchbotDeviceWoBulb = require("./switchbot-device-wobulb.js"); -let SwitchbotDeviceWoStrip = require("./switchbot-device-wostrip.js"); +const SwitchbotDevice = require("./switchbot-device.js"); +const SwitchbotDeviceWoHand = require("./switchbot-device-wohand.js"); +const SwitchbotDeviceWoCurtain = require("./switchbot-device-wocurtain.js"); +const SwitchbotDeviceWoBlindTilt = require("./switchbot-device-woblindtilt.js"); +const SwitchbotDeviceWoPresence = require("./switchbot-device-wopresence.js"); +const SwitchbotDeviceWoContact = require("./switchbot-device-wocontact.js"); +const SwitchbotDeviceWoSensorTH = require("./switchbot-device-wosensorth.js"); +const SwitchbotDeviceWoHumi = require("./switchbot-device-wohumi.js"); +const SwitchbotDeviceWoPlugMini = require("./switchbot-device-woplugmini.js"); +const SwitchbotDeviceWoBulb = require("./switchbot-device-wobulb.js"); +const SwitchbotDeviceWoStrip = require("./switchbot-device-wostrip.js"); class Switchbot { /* ------------------------------------------------------------------ @@ -20,7 +20,7 @@ class Switchbot { * * [Arguments] * - params | Object | Optional | - * - noble | Noble | Optional | The Nobel object created by the noble module. + * - noble | Noble | Optional | The Noble object created by the noble module. * | | | This parameter is optional. * | | | If you don't specify this parameter, this * | | | module automatically creates it. @@ -84,9 +84,9 @@ class Switchbot { * `SwitchbotDevice` objects representing the found devices. * ---------------------------------------------------------------- */ discover(params = {}) { - let promise = new Promise((resolve, reject) => { + const promise = new Promise((resolve, reject) => { // Check the parameters - let valid = parameterChecker.check( + const valid = parameterChecker.check( params, { duration: { required: false, type: "integer", min: 1, max: 60000 }, @@ -125,7 +125,7 @@ class Switchbot { } // Determine the values of the parameters - let p = { + const p = { duration: params.duration || this._DEFAULT_DISCOVERY_DURATION, model: params.model || "", id: params.id || "", @@ -137,7 +137,7 @@ class Switchbot { .then(() => { let peripherals = {}; let timer = null; - let finishDiscovery = () => { + const finishDiscovery = () => { if (timer) { clearTimeout(timer); } @@ -154,11 +154,11 @@ class Switchbot { // Set a handler for the 'discover' event this.noble.on("discover", (peripheral) => { - let device = this._getDeviceObject(peripheral, p.id, p.model); + const device = this._getDeviceObject(peripheral, p.id, p.model); if (!device) { return; } - let id = device.id; + const id = device.id; peripherals[id] = device; if (this.ondiscover && typeof this.ondiscover === "function") { @@ -194,13 +194,13 @@ class Switchbot { } _init() { - let promise = new Promise((resolve, reject) => { + const promise = new Promise((resolve, reject) => { switch (this.noble.state) { case "poweredOn": resolve(); return; case ("unsupported", "unauthorized", "poweredOff"): - let err = new Error( + const err = new Error( "Failed to initialize the Noble object: " + this.noble.state ); reject(err); @@ -212,7 +212,7 @@ class Switchbot { if (state === "poweredOn") { resolve(); } else { - let err = new Error( + const err = new Error( "Failed to initialize the Noble object: " + state ); reject(err); @@ -224,7 +224,7 @@ class Switchbot { } _getDeviceObject(peripheral, id, model) { - let ad = switchbotAdvertising.parse(peripheral, this.onlog); + const ad = switchbotAdvertising.parse(peripheral, this.onlog); if (this._filterAdvertising(ad, id, model)) { let device = null; switch (ad.serviceData.model) { @@ -280,7 +280,7 @@ class Switchbot { } if (id) { id = id.toLowerCase().replace(/\:/g, ""); - let ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, ""); + const ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, ""); if (ad_id !== id) { return false; } @@ -349,9 +349,9 @@ class Switchbot { * Nothing will be passed to the `resolve()`. * ---------------------------------------------------------------- */ startScan(params) { - let promise = new Promise((resolve, reject) => { + const promise = new Promise((resolve, reject) => { // Check the parameters - let valid = parameterChecker.check( + const valid = parameterChecker.check( params, { model: { @@ -392,14 +392,14 @@ class Switchbot { this._init() .then(() => { // Determine the values of the parameters - let p = { + const p = { model: params.model || "", id: params.id || "", }; // Set a handler for the 'discover' event this.noble.on("discover", (peripheral) => { - let ad = switchbotAdvertising.parse(peripheral, this.onlog); + const ad = switchbotAdvertising.parse(peripheral, this.onlog); if (this._filterAdvertising(ad, p.id, p.model)) { if ( this.onadvertisement && @@ -459,7 +459,7 @@ class Switchbot { wait(msec) { return new Promise((resolve, reject) => { // Check the parameters - let valid = parameterChecker.check( + const valid = parameterChecker.check( { msec: msec }, { msec: { required: true, type: "integer", min: 0 },