Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various 2.0.0 issues #224

Merged
merged 2 commits into from
Feb 9, 2024
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
21 changes: 13 additions & 8 deletions src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SwitchbotDevice {
_onconnect: () => void;
_ondisconnect: () => void;
_ondisconnect_internal: () => void;
_onnotify_internal: () => void;
_onnotify_internal: (buf: Buffer) => void;
/* ------------------------------------------------------------------
* Constructor
*
Expand All @@ -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;
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/device/woblindtilt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wobulb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/device/wocontact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wocurtain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wohand.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wohumi.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/woiosensorth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SwitchbotDevice } from '../switchbot.js';
import { SwitchbotDevice } from '../device.js';

export class WoIOSensorTH extends SwitchbotDevice {
static parseServiceData(serviceDataBuf, manufacturerDataBuf, onlog) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/woplugmini.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/device/wopresence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SwitchbotDevice } from '../switchbot.js';
import { SwitchbotDevice } from '../device.js';

export class WoPresence extends SwitchbotDevice {
static parseServiceData(buf, onlog) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wosensorth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SwitchbotDevice } from '../switchbot.js';
import { SwitchbotDevice } from '../device.js';

export class WoSensorTH extends SwitchbotDevice {
static parseServiceData(buf, onlog) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wosmartlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wostrip.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/switchbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -322,7 +322,7 @@ export class SwitchBot {
}
}

static filterAdvertising(ad, id, model) {
filterAdvertising(ad, id, model) {
if (!ad) {
return false;
}
Expand Down Expand Up @@ -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'
Expand Down
Loading