Skip to content

Commit 6615713

Browse files
authored
Add some TypeScript types (#228)
1 parent 678103e commit 6615713

17 files changed

+122
-134
lines changed

src/advertising.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Buffer } from 'buffer';
22

3+
import { Peripheral } from '@abandonware/noble';
4+
35
import { WoHand } from './device/wohand.js';
46
import { WoCurtain } from './device/wocurtain.js';
57
import { WoBlindTilt } from './device/woblindtilt.js';
@@ -13,6 +15,13 @@ import { WoBulb } from './device/wobulb.js';
1315
import { WoStrip } from './device/wostrip.js';
1416
import { WoSmartLock } from './device/wosmartlock.js';
1517

18+
export type Ad = {
19+
id: string;
20+
address: string;
21+
rssi: number,
22+
serviceData: any;
23+
} | null
24+
1625
export class Advertising {
1726

1827
constructor() {}
@@ -81,7 +90,7 @@ export class Advertising {
8190
* @param onlog - The logging function.
8291
* @returns The parsed data of the peripheral device.
8392
*/
84-
static parse(peripheral, onlog?) {
93+
static parse(peripheral: Peripheral, onlog?: (message: string) => void) {
8594
const ad = peripheral.advertisement;
8695
if (!ad || !ad.serviceData) {
8796
return null;
@@ -150,16 +159,14 @@ export class Advertising {
150159
}
151160
let address = peripheral.address || '';
152161
if (address === '') {
153-
address = peripheral.advertisement.manufacturerData || '';
154-
if (address !== '') {
155-
const str = peripheral.advertisement.manufacturerData
156-
.toString('hex')
157-
.slice(4, 16);
162+
const str = peripheral.advertisement.manufacturerData
163+
.toString('hex')
164+
.slice(4, 16);
165+
if (str !== '') {
158166
address = str.substr(0, 2);
159167
for (let i = 2; i < str.length; i += 2) {
160168
address = address + ':' + str.substr(i, 2);
161169
}
162-
// console.log("address", typeof(address), address);
163170
}
164171
} else {
165172
address = address.replace(/-/g, ':');

src/device.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { Buffer } from 'buffer';
22

3+
import { Characteristic, Peripheral, Service } from '@abandonware/noble';
34
import { ParameterChecker } from './parameter-checker.js';
45
import { Advertising } from './advertising.js';
56

6-
type ad = {
7-
id: any;
8-
address: any;
9-
rssi: any;
10-
serviceData: any;
7+
type Chars = {
8+
write: Characteristic | null,
9+
notify: Characteristic | null,
10+
device: Characteristic | null,
1111
} | null;
1212

1313
export class SwitchbotDevice {
1414
_peripheral;
1515
_noble;
16-
_chars;
17-
_SERV_UUID_PRIMARY;
18-
_CHAR_UUID_WRITE;
19-
_CHAR_UUID_NOTIFY;
20-
_CHAR_UUID_DEVICE;
21-
_READ_TIMEOUT_MSEC;
22-
_WRITE_TIMEOUT_MSEC;
23-
_COMMAND_TIMEOUT_MSEC;
16+
_chars: Chars;
17+
_SERV_UUID_PRIMARY = 'cba20d00224d11e69fb80002a5d5c51b';
18+
_CHAR_UUID_WRITE = 'cba20002224d11e69fb80002a5d5c51b';
19+
_CHAR_UUID_NOTIFY = 'cba20003224d11e69fb80002a5d5c51b';
20+
_CHAR_UUID_DEVICE = '2a00';
21+
_READ_TIMEOUT_MSEC = 3000;
22+
_WRITE_TIMEOUT_MSEC = 3000;
23+
_COMMAND_TIMEOUT_MSEC = 3000;
2424
_id;
2525
_address;
2626
_model;
@@ -39,22 +39,13 @@ export class SwitchbotDevice {
3939
* | | | which represents this device
4040
* - noble | Noble | Required | The Noble object created by the noble module.
4141
* ---------------------------------------------------------------- */
42-
constructor(peripheral, noble) {
42+
constructor(peripheral: Peripheral, noble: any) {
4343
this._peripheral = peripheral;
4444
this._noble = noble;
4545
this._chars = null;
4646

47-
this._SERV_UUID_PRIMARY = 'cba20d00224d11e69fb80002a5d5c51b';
48-
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-
5647
// Save the device information
57-
const ad: ad = Advertising.parse(peripheral);
48+
const ad = Advertising.parse(peripheral);
5849
this._id = ad?.id;
5950
this._address = ad?.address;
6051
this._model = ad?.serviceData.model;
@@ -95,14 +86,14 @@ export class SwitchbotDevice {
9586
}
9687

9788
// Setters
98-
set onconnect(func) {
89+
set onconnect(func: () => void) {
9990
if (!func || typeof func !== 'function') {
10091
throw new Error('The `onconnect` must be a function.');
10192
}
10293
this._onconnect = func;
10394
}
10495

105-
set ondisconnect(func) {
96+
set ondisconnect(func: () => void) {
10697
if (!func || typeof func !== 'function') {
10798
throw new Error('The `ondisconnect` must be a function.');
10899
}
@@ -185,7 +176,7 @@ export class SwitchbotDevice {
185176
});
186177
}
187178

188-
_getCharacteristics() {
179+
_getCharacteristics(): Promise<Chars> {
189180
return new Promise((resolve, reject) => {
190181
// Set timeout timer
191182
let timer: NodeJS.Timeout | null = setTimeout(() => {
@@ -279,7 +270,7 @@ export class SwitchbotDevice {
279270
});
280271
}
281272

282-
_discoverCharacteristics(service) {
273+
_discoverCharacteristics(service: Service) {
283274
return new Promise((resolve, reject) => {
284275
service.discoverCharacteristics([], (error, char_list) => {
285276
if (error) {
@@ -293,7 +284,7 @@ export class SwitchbotDevice {
293284

294285
_subscribe() {
295286
return new Promise<void>((resolve, reject) => {
296-
const char = this._chars.notify;
287+
const char = this._chars?.notify;
297288
if (!char) {
298289
reject(new Error('No notify characteristic was found.'));
299290
return;
@@ -313,7 +304,7 @@ export class SwitchbotDevice {
313304

314305
_unsubscribe() {
315306
return new Promise<void>((resolve) => {
316-
const char = this._chars.notify;
307+
const char = this._chars?.notify;
317308
if (!char) {
318309
resolve();
319310
return;
@@ -387,7 +378,7 @@ export class SwitchbotDevice {
387378
let name = '';
388379
this._connect()
389380
.then(() => {
390-
if (!this._chars.device) {
381+
if (!this._chars?.device) {
391382
// Some models of Bot don't seem to support this characteristic UUID
392383
throw new Error(
393384
'The device does not support the characteristic UUID 0x' +
@@ -422,7 +413,7 @@ export class SwitchbotDevice {
422413
* - Promise object
423414
* Nothing will be passed to the `resolve()`.
424415
* ---------------------------------------------------------------- */
425-
setDeviceName(name) {
416+
setDeviceName(name: string) {
426417
return new Promise<void>((resolve, reject) => {
427418
// Check the parameters
428419
const valid = ParameterChecker.check(
@@ -441,7 +432,7 @@ export class SwitchbotDevice {
441432
const buf = Buffer.from(name, 'utf8');
442433
this._connect()
443434
.then(() => {
444-
if (!this._chars.device) {
435+
if (!this._chars?.device) {
445436
// Some models of Bot don't seem to support this characteristic UUID
446437
throw new Error(
447438
'The device does not support the characteristic UUID 0x' +
@@ -477,7 +468,7 @@ export class SwitchbotDevice {
477468

478469
this._connect()
479470
.then(() => {
480-
if (!this._chars) {
471+
if (!this._chars?.write) {
481472
return reject(new Error('No characteristics available.'));
482473
}
483474
return this._write(this._chars.write, req_buf);
@@ -518,7 +509,7 @@ export class SwitchbotDevice {
518509
}
519510

520511
// Read data from the specified characteristic
521-
_read(char) {
512+
_read(char: Characteristic) {
522513
return new Promise((resolve, reject) => {
523514
// Set a timeout timer
524515
let timer: NodeJS.Timeout | undefined = setTimeout(() => {
@@ -541,7 +532,7 @@ export class SwitchbotDevice {
541532
}
542533

543534
// Write the specified Buffer data to the specified characteristic
544-
_write(char, buf) {
535+
_write(char: Characteristic, buf: Buffer) {
545536
return new Promise<void>((resolve, reject) => {
546537
// Set a timeout timer
547538
let timer: NodeJS.Timeout | undefined = setTimeout(() => {

src/device/woblindtilt.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Buffer } from 'buffer';
77
import { SwitchbotDevice } from '../device.js';
88

99
export class WoBlindTilt extends SwitchbotDevice {
10-
static parseServiceData(buf, onlog) {
10+
static parseServiceData(buf: Buffer, onlog: ((message: string) => void) | undefined) {
1111
if (buf.length !== 5 && buf.length !== 6) {
1212
if (onlog && typeof onlog === 'function') {
1313
onlog(
@@ -94,7 +94,7 @@ export class WoBlindTilt extends SwitchbotDevice {
9494
* - Promise object
9595
* Nothing will be passed to the `resolve()`.
9696
* ---------------------------------------------------------------- */
97-
runToPos(percent, mode) {
97+
runToPos(percent: number, mode: number) {
9898
if (typeof percent !== 'number') {
9999
return new Promise((resolve, reject) => {
100100
reject(
@@ -127,7 +127,7 @@ export class WoBlindTilt extends SwitchbotDevice {
127127
return this._operateBlindTilt([0x57, 0x0f, 0x45, 0x01, 0x05, mode, percent]);
128128
}
129129

130-
_operateBlindTilt(bytes) {
130+
_operateBlindTilt(bytes: number[]) {
131131
return new Promise<void>((resolve, reject) => {
132132
const req_buf = Buffer.from(bytes);
133133
this._command(req_buf)

src/device/wobulb.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SwitchbotDevice } from '../device.js';
1010
* @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/colorbulb.md
1111
*/
1212
export class WoBulb extends SwitchbotDevice {
13-
static parseServiceData(manufacturerData, onlog) {
13+
static parseServiceData(manufacturerData: Buffer, onlog: ((message: string) => void) | undefined) {
1414
if (manufacturerData.length !== 13) {
1515
if (onlog && typeof onlog === 'function') {
1616
onlog(
@@ -73,7 +73,7 @@ export class WoBulb extends SwitchbotDevice {
7373
/**
7474
* @private
7575
*/
76-
_setState(reqByteArray) {
76+
_setState(reqByteArray: number[]) {
7777
const base = [0x57, 0x0f, 0x47, 0x01];
7878
return this._operateBot(base.concat(reqByteArray));
7979
}
@@ -95,7 +95,7 @@ export class WoBulb extends SwitchbotDevice {
9595
/**
9696
* @returns {Promise<number>} resolves with brightness percent
9797
*/
98-
setBrightness(brightness) {
98+
setBrightness(brightness: number) {
9999
if (typeof brightness !== 'number') {
100100
return new Promise((resolve, reject) => {
101101
reject(
@@ -117,7 +117,7 @@ export class WoBulb extends SwitchbotDevice {
117117
/**
118118
* @returns {Promise<number>} resolves with color_temperature percent
119119
*/
120-
setColorTemperature(color_temperature) {
120+
setColorTemperature(color_temperature: number) {
121121
if (typeof color_temperature !== 'number') {
122122
return new Promise((resolve, reject) => {
123123
reject(
@@ -139,7 +139,7 @@ export class WoBulb extends SwitchbotDevice {
139139
/**
140140
* @returns {Promise<number>} resolves with brightness percent
141141
*/
142-
setRGB(brightness, red, green, blue) {
142+
setRGB(brightness: number, red: number, green: number, blue: number) {
143143
if (typeof brightness !== 'number') {
144144
return new Promise((resolve, reject) => {
145145
reject(
@@ -206,7 +206,7 @@ export class WoBulb extends SwitchbotDevice {
206206
/**
207207
* @private
208208
*/
209-
_operateBot(bytes) {
209+
_operateBot(bytes: number[]) {
210210
const req_buf = Buffer.from(bytes);
211211
return new Promise((resolve, reject) => {
212212
this._command(req_buf)

src/device/wocontact.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { SwitchbotDevice } from '../device.js';
66

77
export class WoContact extends SwitchbotDevice {
8-
static parseServiceData(buf, onlog) {
8+
static parseServiceData(buf: Buffer, onlog: ((message: string) => void) | undefined) {
99
if (buf.length !== 9) {
1010
if (onlog && typeof onlog === 'function') {
1111
onlog(

src/device/wocurtain.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Buffer } from 'buffer';
77
import { SwitchbotDevice } from '../device.js';
88

99
export class WoCurtain extends SwitchbotDevice {
10-
static parseServiceData(buf, onlog) {
10+
static parseServiceData(buf: Buffer, onlog: ((message: string) => void) | undefined) {
1111
if (buf.length !== 5 && buf.length !== 6) {
1212
if (onlog && typeof onlog === 'function') {
1313
onlog(
@@ -54,7 +54,7 @@ export class WoCurtain extends SwitchbotDevice {
5454
* - Promise object
5555
* Nothing will be passed to the `resolve()`.
5656
* ---------------------------------------------------------------- */
57-
open(mode) {
57+
open(mode?: number) {
5858
return this.runToPos(0, mode);
5959
}
6060

@@ -69,7 +69,7 @@ export class WoCurtain extends SwitchbotDevice {
6969
* - Promise object
7070
* Nothing will be passed to the `resolve()`.
7171
* ---------------------------------------------------------------- */
72-
close(mode) {
72+
close(mode?: number) {
7373
return this.runToPos(100, mode);
7474
}
7575

@@ -100,7 +100,7 @@ export class WoCurtain extends SwitchbotDevice {
100100
* - Promise object
101101
* Nothing will be passed to the `resolve()`.
102102
* ---------------------------------------------------------------- */
103-
runToPos(percent, mode = 0xff) {
103+
runToPos(percent: number, mode = 0xff) {
104104
if (typeof percent !== 'number') {
105105
return new Promise((resolve, reject) => {
106106
reject(
@@ -129,7 +129,7 @@ export class WoCurtain extends SwitchbotDevice {
129129
return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, mode, percent]);
130130
}
131131

132-
_operateCurtain(bytes) {
132+
_operateCurtain(bytes: number[]) {
133133
return new Promise<void>((resolve, reject) => {
134134
const req_buf = Buffer.from(bytes);
135135
this._command(req_buf)

src/device/wohand.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Buffer } from 'buffer';
33
import { SwitchbotDevice } from '../device.js';
44

55
export class WoHand extends SwitchbotDevice {
6-
static parseServiceData(buf, onlog) {
6+
static parseServiceData(buf: Buffer, onlog: ((message: string) => void) | undefined) {
77
if (buf.length !== 3) {
88
if (onlog && typeof onlog === 'function') {
99
onlog(
@@ -104,7 +104,7 @@ export class WoHand extends SwitchbotDevice {
104104
return this._operateBot([0x57, 0x01, 0x04]);
105105
}
106106

107-
_operateBot(bytes) {
107+
_operateBot(bytes: number[]) {
108108
return new Promise<void>((resolve, reject) => {
109109
const req_buf = Buffer.from(bytes);
110110
this._command(req_buf)

src/device/wohumi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Buffer } from 'buffer';
33
import { SwitchbotDevice } from '../device.js';
44

55
export class WoHumi extends SwitchbotDevice {
6-
static parseServiceData(buf, onlog) {
6+
static parseServiceData(buf: Buffer, onlog: ((message: string) => void) | undefined) {
77
if (buf.length !== 8) {
88
if (onlog && typeof onlog === 'function') {
99
onlog(
@@ -106,7 +106,7 @@ export class WoHumi extends SwitchbotDevice {
106106
return this._operateBot([0x57, 0x01, 0x04]);
107107
}
108108

109-
_operateBot(bytes) {
109+
_operateBot(bytes: number[]) {
110110
return new Promise<void>((resolve, reject) => {
111111
const req_buf = Buffer.from(bytes);
112112
this._command(req_buf)

0 commit comments

Comments
 (0)