Skip to content

Commit cd0c22b

Browse files
authored
Fix various 2.0.0 issues (#224)
1 parent d3333df commit cd0c22b

File tree

14 files changed

+29
-24
lines changed

14 files changed

+29
-24
lines changed

src/device.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class SwitchbotDevice {
3030
_onconnect: () => void;
3131
_ondisconnect: () => void;
3232
_ondisconnect_internal: () => void;
33-
_onnotify_internal: () => void;
33+
_onnotify_internal: (buf: Buffer) => void;
3434
/* ------------------------------------------------------------------
3535
* Constructor
3636
*
@@ -46,6 +46,13 @@ export class SwitchbotDevice {
4646

4747
this._SERV_UUID_PRIMARY = 'cba20d00224d11e69fb80002a5d5c51b';
4848
this._CHAR_UUID_WRITE = 'cba20002224d11e69fb80002a5d5c51b';
49+
this._CHAR_UUID_NOTIFY = 'cba20003224d11e69fb80002a5d5c51b';
50+
this._CHAR_UUID_DEVICE = '2a00';
51+
52+
this._READ_TIMEOUT_MSEC = 3000;
53+
this._WRITE_TIMEOUT_MSEC = 3000;
54+
this._COMMAND_TIMEOUT_MSEC = 3000;
55+
4956
// Save the device information
5057
const ad: ad = Advertising.parse(peripheral);
5158
this._id = ad?.id;
@@ -296,8 +303,8 @@ export class SwitchbotDevice {
296303
reject(error);
297304
return;
298305
}
299-
char.on('data', () => { // Remove the argument passed to the _onnotify_internal function
300-
this._onnotify_internal();
306+
char.on('data', (buf) => { // Remove the argument passed to the _onnotify_internal function
307+
this._onnotify_internal(buf);
301308
});
302309
resolve();
303310
});
@@ -459,14 +466,14 @@ export class SwitchbotDevice {
459466
// Write the specified Buffer data to the write characteristic
460467
// and receive the response from the notify characteristic
461468
// with connection handling
462-
_command(req_buf) {
469+
_command(req_buf: Buffer) {
463470
return new Promise((resolve, reject) => {
464471
if (!Buffer.isBuffer(req_buf)) {
465472
reject(new Error('The specified data is not acceptable for writing.'));
466473
return;
467474
}
468475

469-
let res_buf;
476+
let res_buf: Buffer | unknown;
470477

471478
this._connect()
472479
.then(() => {
@@ -493,15 +500,13 @@ export class SwitchbotDevice {
493500

494501
_waitCommandResponse() {
495502
return new Promise((resolve, reject) => {
496-
const buf: Buffer | null = null;
497-
498503
let timer: NodeJS.Timeout | undefined = setTimeout(() => {
499504
timer = undefined;
500505
this._onnotify_internal = () => { };
501506
reject(new Error('COMMAND_TIMEOUT'));
502507
}, this._COMMAND_TIMEOUT_MSEC);
503508

504-
this._onnotify_internal = () => {
509+
this._onnotify_internal = (buf) => {
505510
if (timer) {
506511
clearTimeout(timer);
507512
timer = undefined;

src/device/woblindtilt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
import { Buffer } from 'buffer';
66

7-
import { SwitchbotDevice } from '../switchbot.js';
7+
import { SwitchbotDevice } from '../device.js';
88

99
export class WoBlindTilt extends SwitchbotDevice {
1010
static parseServiceData(buf, onlog) {

src/device/wobulb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
import { Buffer } from 'buffer';
66

7-
import { SwitchbotDevice } from '../switchbot.js';
7+
import { SwitchbotDevice } from '../device.js';
88

99
/**
1010
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md

src/device/wocontact.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* wocontact.ts: Switchbot BLE API registration.
44
*/
5-
import { SwitchbotDevice } from '../switchbot.js';
5+
import { SwitchbotDevice } from '../device.js';
66

77
export class WoContact extends SwitchbotDevice {
88
static parseServiceData(buf, onlog) {

src/device/wocurtain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
import { Buffer } from 'buffer';
66

7-
import { SwitchbotDevice } from '../switchbot.js';
7+
import { SwitchbotDevice } from '../device.js';
88

99
export class WoCurtain extends SwitchbotDevice {
1010
static parseServiceData(buf, onlog) {

src/device/wohand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Buffer } from 'buffer';
22

3-
import { SwitchbotDevice } from '../switchbot.js';
3+
import { SwitchbotDevice } from '../device.js';
44

55
export class WoHand extends SwitchbotDevice {
66
static parseServiceData(buf, onlog) {

src/device/wohumi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Buffer } from 'buffer';
22

3-
import { SwitchbotDevice } from '../switchbot.js';
3+
import { SwitchbotDevice } from '../device.js';
44

55
export class WoHumi extends SwitchbotDevice {
66
static parseServiceData(buf, onlog) {

src/device/woiosensorth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SwitchbotDevice } from '../switchbot.js';
1+
import { SwitchbotDevice } from '../device.js';
22

33
export class WoIOSensorTH extends SwitchbotDevice {
44
static parseServiceData(serviceDataBuf, manufacturerDataBuf, onlog) {

src/device/woplugmini.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Buffer } from 'buffer';
22

3-
import { SwitchbotDevice } from '../switchbot.js';
3+
import { SwitchbotDevice } from '../device.js';
44

55
/**
66
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/plugmini.md

src/device/wopresence.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SwitchbotDevice } from '../switchbot.js';
1+
import { SwitchbotDevice } from '../device.js';
22

33
export class WoPresence extends SwitchbotDevice {
44
static parseServiceData(buf, onlog) {

0 commit comments

Comments
 (0)