11import { Buffer } from 'buffer' ;
22
3+ import { Characteristic , Peripheral , Service } from '@abandonware/noble' ;
34import { ParameterChecker } from './parameter-checker.js' ;
45import { 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
1313export 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 ( ( ) => {
0 commit comments