diff --git a/src/device.ts b/src/device.ts index 4019441..9aa6075 100644 --- a/src/device.ts +++ b/src/device.ts @@ -30,7 +30,7 @@ export class SwitchbotDevice { _onconnect: () => void; _ondisconnect: () => void; _ondisconnect_internal: () => void; - _onnotify_internal: () => void; + _onnotify_internal: (buf: Buffer) => void; /* ------------------------------------------------------------------ * Constructor * @@ -46,6 +46,13 @@ export class SwitchbotDevice { this._SERV_UUID_PRIMARY = 'cba20d00224d11e69fb80002a5d5c51b'; this._CHAR_UUID_WRITE = 'cba20002224d11e69fb80002a5d5c51b'; + this._CHAR_UUID_NOTIFY = 'cba20003224d11e69fb80002a5d5c51b'; + this._CHAR_UUID_DEVICE = '2a00'; + + this._READ_TIMEOUT_MSEC = 3000; + this._WRITE_TIMEOUT_MSEC = 3000; + this._COMMAND_TIMEOUT_MSEC = 3000; + // Save the device information const ad: ad = Advertising.parse(peripheral); this._id = ad?.id; @@ -296,8 +303,8 @@ export class SwitchbotDevice { reject(error); return; } - char.on('data', () => { // Remove the argument passed to the _onnotify_internal function - this._onnotify_internal(); + char.on('data', (buf) => { // Remove the argument passed to the _onnotify_internal function + this._onnotify_internal(buf); }); resolve(); }); @@ -459,14 +466,14 @@ export class SwitchbotDevice { // Write the specified Buffer data to the write characteristic // and receive the response from the notify characteristic // with connection handling - _command(req_buf) { + _command(req_buf: Buffer) { return new Promise((resolve, reject) => { if (!Buffer.isBuffer(req_buf)) { reject(new Error('The specified data is not acceptable for writing.')); return; } - let res_buf; + let res_buf: Buffer | unknown; this._connect() .then(() => { @@ -493,15 +500,13 @@ export class SwitchbotDevice { _waitCommandResponse() { return new Promise((resolve, reject) => { - const buf: Buffer | null = null; - let timer: NodeJS.Timeout | undefined = setTimeout(() => { timer = undefined; this._onnotify_internal = () => { }; reject(new Error('COMMAND_TIMEOUT')); }, this._COMMAND_TIMEOUT_MSEC); - this._onnotify_internal = () => { + this._onnotify_internal = (buf) => { if (timer) { clearTimeout(timer); timer = undefined; diff --git a/src/device/woblindtilt.ts b/src/device/woblindtilt.ts index b5b30d1..d53ded5 100644 --- a/src/device/woblindtilt.ts +++ b/src/device/woblindtilt.ts @@ -4,7 +4,7 @@ */ import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoBlindTilt extends SwitchbotDevice { static parseServiceData(buf, onlog) { diff --git a/src/device/wobulb.ts b/src/device/wobulb.ts index 1cc2f7c..38250b3 100644 --- a/src/device/wobulb.ts +++ b/src/device/wobulb.ts @@ -4,7 +4,7 @@ */ import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; /** * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md diff --git a/src/device/wocontact.ts b/src/device/wocontact.ts index 9cf4713..3232dd4 100644 --- a/src/device/wocontact.ts +++ b/src/device/wocontact.ts @@ -2,7 +2,7 @@ * * wocontact.ts: Switchbot BLE API registration. */ -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoContact extends SwitchbotDevice { static parseServiceData(buf, onlog) { diff --git a/src/device/wocurtain.ts b/src/device/wocurtain.ts index 9f966bc..618a8a7 100644 --- a/src/device/wocurtain.ts +++ b/src/device/wocurtain.ts @@ -4,7 +4,7 @@ */ import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoCurtain extends SwitchbotDevice { static parseServiceData(buf, onlog) { diff --git a/src/device/wohand.ts b/src/device/wohand.ts index d695d9d..5bf16f4 100644 --- a/src/device/wohand.ts +++ b/src/device/wohand.ts @@ -1,6 +1,6 @@ import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoHand extends SwitchbotDevice { static parseServiceData(buf, onlog) { diff --git a/src/device/wohumi.ts b/src/device/wohumi.ts index 308e6be..34c79bf 100644 --- a/src/device/wohumi.ts +++ b/src/device/wohumi.ts @@ -1,6 +1,6 @@ import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoHumi extends SwitchbotDevice { static parseServiceData(buf, onlog) { diff --git a/src/device/woiosensorth.ts b/src/device/woiosensorth.ts index 605f43d..824cd73 100644 --- a/src/device/woiosensorth.ts +++ b/src/device/woiosensorth.ts @@ -1,4 +1,4 @@ -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoIOSensorTH extends SwitchbotDevice { static parseServiceData(serviceDataBuf, manufacturerDataBuf, onlog) { diff --git a/src/device/woplugmini.ts b/src/device/woplugmini.ts index b152467..51dda56 100644 --- a/src/device/woplugmini.ts +++ b/src/device/woplugmini.ts @@ -1,6 +1,6 @@ import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; /** * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/plugmini.md diff --git a/src/device/wopresence.ts b/src/device/wopresence.ts index 4facc44..44641e2 100644 --- a/src/device/wopresence.ts +++ b/src/device/wopresence.ts @@ -1,4 +1,4 @@ -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoPresence extends SwitchbotDevice { static parseServiceData(buf, onlog) { diff --git a/src/device/wosensorth.ts b/src/device/wosensorth.ts index 7a22473..81262d8 100644 --- a/src/device/wosensorth.ts +++ b/src/device/wosensorth.ts @@ -1,4 +1,4 @@ -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoSensorTH extends SwitchbotDevice { static parseServiceData(buf, onlog) { diff --git a/src/device/wosmartlock.ts b/src/device/wosmartlock.ts index 61f3121..fab89c6 100644 --- a/src/device/wosmartlock.ts +++ b/src/device/wosmartlock.ts @@ -4,7 +4,7 @@ */ //import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; export class WoSmartLock extends SwitchbotDevice { static parseServiceData(manufacturerData, onlog) { diff --git a/src/device/wostrip.ts b/src/device/wostrip.ts index 5fbb9c1..be8c3cc 100644 --- a/src/device/wostrip.ts +++ b/src/device/wostrip.ts @@ -1,6 +1,6 @@ import { Buffer } from 'buffer'; -import { SwitchbotDevice } from '../switchbot.js'; +import { SwitchbotDevice } from '../device.js'; /** * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md diff --git a/src/switchbot.ts b/src/switchbot.ts index 3967722..7ebd179 100644 --- a/src/switchbot.ts +++ b/src/switchbot.ts @@ -186,7 +186,7 @@ export class SwitchBot { // Set a handler for the 'discover' event this.noble.on('discover', (peripheral) => { - const device = SwitchBot.getDeviceObject(peripheral, p.id, p.model) as SwitchbotDevice; + const device = this.getDeviceObject(peripheral, p.id, p.model) as SwitchbotDevice; if (!device) { return; } @@ -265,7 +265,7 @@ export class SwitchBot { return promise; } - static getDeviceObject(peripheral, id, model) { + getDeviceObject(peripheral, id, model) { const ad = Advertising.parse(peripheral, this.onlog); if (this.filterAdvertising(ad, id, model)) { let device; @@ -322,7 +322,7 @@ export class SwitchBot { } } - static filterAdvertising(ad, id, model) { + filterAdvertising(ad, id, model) { if (!ad) { return false; } @@ -451,7 +451,7 @@ export class SwitchBot { // Set a handler for the 'discover' event this.noble.on('discover', (peripheral) => { const ad = Advertising.parse(peripheral, this.onlog); - if (SwitchBot.filterAdvertising(ad, p.id, p.model)) { + if (this.filterAdvertising(ad, p.id, p.model)) { if ( this.onadvertisement && typeof this.onadvertisement === 'function'