From 5104e00993e040029f2db23eb606cdf2eda252c5 Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Wed, 24 Apr 2024 19:43:20 +0200 Subject: [PATCH] Fix TypeScript issues (#239) --- .eslintrc | 2 +- src/advertising.ts | 4 +- src/device.ts | 46 ++++++++-------- src/device/woblindtilt.ts | 8 +-- src/device/wobulb.ts | 2 +- src/device/wocurtain.ts | 8 +-- src/device/wohand.ts | 8 +-- src/device/wohumi.ts | 8 +-- src/device/woplugmini.ts | 2 +- src/device/wostrip.ts | 2 +- src/parameter-checker.ts | 108 +++++++++++++++++--------------------- src/switchbot.ts | 55 +++++++++---------- 12 files changed, 117 insertions(+), 136 deletions(-) diff --git a/.eslintrc b/.eslintrc index b5663330..1f57736e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -78,6 +78,6 @@ "@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/explicit-module-boundary-types": "off", - "@typescript-eslint/no-explicit-any": "off" + "@typescript-eslint/no-explicit-any": "error" } } \ No newline at end of file diff --git a/src/advertising.ts b/src/advertising.ts index e08a5b70..783393db 100644 --- a/src/advertising.ts +++ b/src/advertising.ts @@ -21,8 +21,8 @@ export type Ad = { id: string; address: string; rssi: number, - serviceData: any; -} | null + serviceData: Record; +} | null; export class Advertising { diff --git a/src/device.ts b/src/device.ts index d760ece3..e2ab896f 100644 --- a/src/device.ts +++ b/src/device.ts @@ -2,14 +2,14 @@ * * device.ts: Switchbot BLE API registration. */ -import { Characteristic, Peripheral, Service } from '@abandonware/noble'; -import { ParameterChecker } from './parameter-checker.js'; +import Noble from '@abandonware/noble'; +import { parameterChecker } from './parameter-checker.js'; import { Advertising, Ad } from './advertising.js'; type Chars = { - write: Characteristic | null, - notify: Characteristic | null, - device: Characteristic | null, + write: Noble.Characteristic | null, + notify: Noble.Characteristic | null, + device: Noble.Characteristic | null, } | null; export class SwitchbotDevice { @@ -41,7 +41,7 @@ export class SwitchbotDevice { * | | | which represents this device * - noble | Noble | Required | The Noble object created by the noble module. * ---------------------------------------------------------------- */ - constructor(peripheral: Peripheral, noble: any) { + constructor(peripheral: Noble.Peripheral, noble: typeof Noble) { this._peripheral = peripheral; this._noble = noble; this._chars = null; @@ -122,10 +122,10 @@ export class SwitchbotDevice { _connect() { return new Promise((resolve, reject) => { // Check the bluetooth state - if (this._noble.state !== 'poweredOn') { + if (this._noble._state !== 'poweredOn') { reject( new Error( - 'The Bluetooth status is ' + this._noble.state + ', not poweredOn.', + 'The Bluetooth status is ' + this._noble._state + ', not poweredOn.', ), ); return; @@ -211,15 +211,15 @@ export class SwitchbotDevice { throw new Error(''); } - const chars = { + const chars: Chars = { write: null, notify: null, device: null, }; - for (const service of service_list as any[]) { + for (const service of service_list) { const char_list = await this._discoverCharacteristics(service); - for (const char of char_list as any[]) { + for (const char of char_list) { if (char.uuid === this._CHAR_UUID_WRITE) { chars.write = char; } else if (char.uuid === this._CHAR_UUID_NOTIFY) { @@ -249,7 +249,7 @@ export class SwitchbotDevice { }); } - _discoverServices() { + _discoverServices(): Promise { return new Promise((resolve, reject) => { this._peripheral.discoverServices([], (error, service_list) => { if (error) { @@ -273,7 +273,7 @@ export class SwitchbotDevice { }); } - _discoverCharacteristics(service: Service) { + _discoverCharacteristics(service: Noble.Service): Promise { return new Promise((resolve, reject) => { service.discoverCharacteristics([], (error, char_list) => { if (error) { @@ -357,9 +357,7 @@ export class SwitchbotDevice { _disconnect() { if (this._was_connected_explicitly) { - return new Promise((resolve) => { - resolve(); - }); + return Promise.resolve(); } else { return this.disconnect(); } @@ -391,7 +389,7 @@ export class SwitchbotDevice { } return this._read(this._chars.device); }) - .then((buf: any) => { + .then((buf) => { name = buf.toString('utf8'); return this._disconnect(); }) @@ -419,7 +417,7 @@ export class SwitchbotDevice { setDeviceName(name: string) { return new Promise((resolve, reject) => { // Check the parameters - const valid = ParameterChecker.check( + const valid = parameterChecker.check( { name: name }, { name: { required: true, type: 'string', minBytes: 1, maxBytes: 100 }, @@ -428,7 +426,7 @@ export class SwitchbotDevice { ); if (!valid) { - reject(new Error(ParameterChecker.error.message)); + reject(new Error(parameterChecker?.error?.message)); return; } @@ -460,14 +458,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: Buffer) { + _command(req_buf: Buffer): Promise{ 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: Buffer | unknown; + let res_buf: Buffer; this._connect() .then(() => { @@ -492,7 +490,7 @@ export class SwitchbotDevice { }); } - _waitCommandResponse() { + _waitCommandResponse(): Promise { return new Promise((resolve, reject) => { let timer: NodeJS.Timeout | undefined = setTimeout(() => { timer = undefined; @@ -512,7 +510,7 @@ export class SwitchbotDevice { } // Read data from the specified characteristic - _read(char: Characteristic) { + _read(char: Noble.Characteristic): Promise{ return new Promise((resolve, reject) => { // Set a timeout timer let timer: NodeJS.Timeout | undefined = setTimeout(() => { @@ -535,7 +533,7 @@ export class SwitchbotDevice { } // Write the specified Buffer data to the specified characteristic - _write(char: Characteristic, buf: Buffer) { + _write(char: Noble.Characteristic, buf: Buffer): Promise{ return new Promise((resolve, reject) => { // Set a timeout timer let timer: NodeJS.Timeout | undefined = setTimeout(() => { diff --git a/src/device/woblindtilt.ts b/src/device/woblindtilt.ts index bf850955..c277d945 100644 --- a/src/device/woblindtilt.ts +++ b/src/device/woblindtilt.ts @@ -129,14 +129,14 @@ export class WoBlindTilt extends SwitchbotDevice { return new Promise((resolve, reject) => { const req_buf = Buffer.from(bytes); this._command(req_buf) - .then((res_buf: unknown) => { - const code = (res_buf as Buffer).readUInt8(0); - if ((res_buf as Buffer).length === 3 && code === 0x01) { + .then((res_buf) => { + const code = res_buf.readUInt8(0); + if (res_buf.length === 3 && code === 0x01) { resolve(); } else { reject( new Error( - 'The device returned an error: 0x' + (res_buf as Buffer).toString('hex'), + 'The device returned an error: 0x' + res_buf.toString('hex'), ), ); } diff --git a/src/device/wobulb.ts b/src/device/wobulb.ts index 5ea59596..61782f03 100644 --- a/src/device/wobulb.ts +++ b/src/device/wobulb.ts @@ -209,7 +209,7 @@ export class WoBulb extends SwitchbotDevice { return new Promise((resolve, reject) => { this._command(req_buf) .then((res_bytes) => { - const res_buf = Buffer.from(res_bytes as ArrayBuffer | SharedArrayBuffer); + const res_buf = Buffer.from(res_bytes); if (res_buf.length === 2) { const code = res_buf.readUInt8(1); if (code === 0x00 || code === 0x80) { diff --git a/src/device/wocurtain.ts b/src/device/wocurtain.ts index 6791f148..5d5f9a22 100644 --- a/src/device/wocurtain.ts +++ b/src/device/wocurtain.ts @@ -131,14 +131,14 @@ export class WoCurtain extends SwitchbotDevice { return new Promise((resolve, reject) => { const req_buf = Buffer.from(bytes); this._command(req_buf) - .then((res_buf: unknown) => { - const code = (res_buf as Buffer).readUInt8(0); - if ((res_buf as Buffer).length === 3 && code === 0x01) { + .then((res_buf) => { + const code = res_buf.readUInt8(0); + if (res_buf.length === 3 && code === 0x01) { resolve(); } else { reject( new Error( - 'The device returned an error: 0x' + (res_buf as Buffer).toString('hex'), + 'The device returned an error: 0x' + res_buf.toString('hex'), ), ); } diff --git a/src/device/wohand.ts b/src/device/wohand.ts index 57b7c62e..6a955385 100644 --- a/src/device/wohand.ts +++ b/src/device/wohand.ts @@ -110,14 +110,14 @@ export class WoHand extends SwitchbotDevice { return new Promise((resolve, reject) => { const req_buf = Buffer.from(bytes); this._command(req_buf) - .then((res_buf: unknown) => { - const code = (res_buf as Buffer).readUInt8(0); - if ((res_buf as Buffer).length === 3 && (code === 0x01 || code === 0x05)) { + .then((res_buf) => { + const code = res_buf.readUInt8(0); + if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) { resolve(); } else { reject( new Error( - 'The device returned an error: 0x' + (res_buf as Buffer).toString('hex'), + 'The device returned an error: 0x' + res_buf.toString('hex'), ), ); } diff --git a/src/device/wohumi.ts b/src/device/wohumi.ts index db3e2516..337daaa3 100644 --- a/src/device/wohumi.ts +++ b/src/device/wohumi.ts @@ -112,14 +112,14 @@ export class WoHumi extends SwitchbotDevice { return new Promise((resolve, reject) => { const req_buf = Buffer.from(bytes); this._command(req_buf) - .then((res_buf: unknown) => { - const code = (res_buf as Buffer).readUInt8(0); - if ((res_buf as Buffer).length === 3 && (code === 0x01 || code === 0x05)) { + .then((res_buf) => { + const code = res_buf.readUInt8(0); + if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) { resolve(); } else { reject( new Error( - 'The device returned an error: 0x' + (res_buf as Buffer).toString('hex'), + 'The device returned an error: 0x' + res_buf.toString('hex'), ), ); } diff --git a/src/device/woplugmini.ts b/src/device/woplugmini.ts index 605212ad..590434d7 100644 --- a/src/device/woplugmini.ts +++ b/src/device/woplugmini.ts @@ -130,7 +130,7 @@ export class WoPlugMini extends SwitchbotDevice { return new Promise((resolve, reject) => { this._command(req_buf) .then((res_bytes) => { - const res_buf = Buffer.from(res_bytes as Uint8Array); + const res_buf = Buffer.from(res_bytes); if (res_buf.length === 2) { const code = res_buf.readUInt8(1); if (code === 0x00 || code === 0x80) { diff --git a/src/device/wostrip.ts b/src/device/wostrip.ts index bcd78cbf..1a7ea2dc 100644 --- a/src/device/wostrip.ts +++ b/src/device/wostrip.ts @@ -199,7 +199,7 @@ export class WoStrip extends SwitchbotDevice { return new Promise((resolve, reject) => { this._command(req_buf) .then((res_bytes) => { - const res_buf = Buffer.from(res_bytes as ArrayBuffer | SharedArrayBuffer); + const res_buf = Buffer.from(res_bytes); if (res_buf.length === 2) { const code = res_buf.readUInt8(1); if (code === 0x00 || code === 0x80) { diff --git a/src/parameter-checker.ts b/src/parameter-checker.ts index da19997c..b01f4886 100644 --- a/src/parameter-checker.ts +++ b/src/parameter-checker.ts @@ -9,17 +9,12 @@ type Rule = { minBytes?: number; maxBytes?: number; pattern?: RegExp; - enum?: any[]; + enum?: unknown[]; type?: 'float' | 'integer' | 'boolean' | 'array' | 'object' | 'string'; }; -export class ParameterChecker { - - _error: Error | null; - static error: any; - constructor() { - this._error = null; - } +class ParameterChecker { + _error: { code: string; message: string; } | null = null; get error() { // ---------------------------------- @@ -33,7 +28,7 @@ export class ParameterChecker { return this._error; } - static isSpecified(value: unknown) { + isSpecified(value: unknown) { return value === void 0 ? false : true; } @@ -70,11 +65,12 @@ export class ParameterChecker { * throw new Error(message); * } * ---------------------------------------------------------------- */ - static check(obj: Record, rules: {[key: string]: Rule}, required: boolean) { - this.error; + check(obj: Record, rules: {[key: string]: Rule}, required: boolean) { + this._error = null; + if (required) { if (!this.isSpecified(obj)) { - this.error = { + this._error = { code: 'MISSING_REQUIRED', message: 'The first argument is missing.', }; @@ -87,7 +83,7 @@ export class ParameterChecker { } if (!this.isObject(obj, {})) { - this.error = { + this._error = { code: 'MISSING_REQUIRED', message: 'The first argument is missing.', }; @@ -100,7 +96,7 @@ export class ParameterChecker { for (let i = 0; i < name_list.length; i++) { const name = name_list[i]; const v = obj[name]; - let rule: Rule = rules[name as keyof typeof rules] as Rule; + let rule = rules[name]; if (!rule) { rule = {}; @@ -108,7 +104,7 @@ export class ParameterChecker { if (!this.isSpecified(v)) { if (rule.required) { result = false; - this.error = { + this._error = { code: 'MISSING_REQUIRED', message: 'The `' + name + '` is required.', }; @@ -132,7 +128,7 @@ export class ParameterChecker { result = this.isString(v, rule, name); } else { result = false; - this.error = { + this._error = { code: 'TYPE_UNKNOWN', message: 'The rule specified for the `' + @@ -141,11 +137,6 @@ export class ParameterChecker { rule.type, }; } - - if (result === false) { - this.error.name = name; - break; - } } return result; @@ -172,15 +163,15 @@ export class ParameterChecker { * - If the value is invalid, this method will return `false` and * an `Error` object will be set to `this._error`. * ---------------------------------------------------------------- */ - static isFloat(value: unknown, rule: Rule, name = 'value'): boolean { - this.error; + isFloat(value: unknown, rule: Rule, name = 'value'): boolean { + this._error = null; if (!rule.required && !this.isSpecified(value)) { return true; } if (typeof value !== 'number') { - this.error = { + this._error = { code: 'TYPE_INVALID', message: 'The `' + name + '` must be a number (integer or float).', }; @@ -189,7 +180,7 @@ export class ParameterChecker { if (typeof rule.min === 'number') { if (value < rule.min) { - this.error = { + this._error = { code: 'VALUE_UNDERFLOW', message: 'The `' + @@ -203,7 +194,7 @@ export class ParameterChecker { } if (typeof rule.max === 'number') { if (value > rule.max) { - this.error = { + this._error = { code: 'VALUE_OVERFLOW', message: 'The `' + @@ -217,7 +208,7 @@ export class ParameterChecker { } if (Array.isArray(rule.enum) && rule.enum.length > 0) { if (rule.enum.indexOf(value) === -1) { - this.error = { + this._error = { code: 'ENUM_UNMATCH', message: 'The `' + @@ -254,24 +245,20 @@ export class ParameterChecker { * - If the value is invalid, this method will return `false` and * an `Error` object will be set to `this._error`. * ---------------------------------------------------------------- */ - static isInteger(value: unknown, rule: Rule, name = 'value') { - this.error = null; + isInteger(value: unknown, rule: Rule, name = 'value') { + this._error = null; if (!rule.required && !this.isSpecified(value)) { return true; } - if (this.isFloat(value, rule)) { - if ((value as number) % 1 === 0) { - return true; - } else { - this.error = { - code: 'TYPE_INVALID', - message: 'The `' + name + '` must be an integer.', - }; - return false; - } + if (Number.isInteger(value)) { + return true; } else { + this._error = { + code: 'TYPE_INVALID', + message: 'The `' + name + '` must be an integer.', + }; return false; } } @@ -291,15 +278,15 @@ export class ParameterChecker { * - If the value is invalid, this method will return `false` and * an `Error` object will be set to `this._error`. * ---------------------------------------------------------------- */ - static isBoolean(value: unknown, rule: Rule, name = 'value') { - this.error = null; + isBoolean(value: unknown, rule: Rule, name = 'value') { + this._error = null; if (!rule.required && !this.isSpecified(value)) { return true; } if (typeof value !== 'boolean') { - this.error = { + this._error = { code: 'TYPE_INVALID', message: 'The `' + name + '` must be boolean.', }; @@ -323,14 +310,15 @@ export class ParameterChecker { * - If the value is invalid, this method will return `false` and * an `Error` object will be set to `this._error`. * ---------------------------------------------------------------- */ - static isObject(value: unknown, rule: Rule, name = 'value') { - this.error = null; + isObject(value: unknown, rule: Rule, name = 'value') { + this._error = null; + if (!rule.required && !this.isSpecified(value)) { return true; } if (typeof value !== 'object' || value === null || Array.isArray(value)) { - this.error = { + this._error = { code: 'TYPE_INVALID', message: 'The `' + name + '` must be an object.', }; @@ -359,15 +347,15 @@ export class ParameterChecker { * - If the value is invalid, this method will return `false` and * an `Error` object will be set to `this._error`. * ---------------------------------------------------------------- */ - static isArray(value: unknown, rule: Rule, name = 'value') { - this.error = null; + isArray(value: unknown, rule: Rule, name = 'value') { + this._error = null; if (!rule.required && !this.isSpecified(value)) { return true; } if (!Array.isArray(value)) { - this.error = { + this._error = { code: 'TYPE_INVALID', message: 'The value must be an array.', }; @@ -376,7 +364,7 @@ export class ParameterChecker { if (typeof rule.min === 'number') { if (value.length < rule.min) { - this.error = { + this._error = { code: 'LENGTH_UNDERFLOW', message: 'The number of characters in the `' + @@ -390,7 +378,7 @@ export class ParameterChecker { } if (typeof rule.max === 'number') { if (value.length > rule.max) { - this.error = { + this._error = { code: 'LENGTH_OVERFLOW', message: 'The number of characters in the `' + @@ -430,15 +418,15 @@ export class ParameterChecker { * - If the value is invalid, this method will return `false` and * an `Error` object will be set to `this._error`. * ---------------------------------------------------------------- */ - static isString(value: unknown, rule: Rule, name = 'value') { - this.error = null; + isString(value: unknown, rule: Rule, name = 'value') { + this._error = null; if (!rule.required && !this.isSpecified(value)) { return true; } if (typeof value !== 'string') { - this.error = { + this._error = { code: 'TYPE_INVALID', message: 'The value must be a string.', }; @@ -447,7 +435,7 @@ export class ParameterChecker { if (typeof rule.min === 'number') { if (value.length < rule.min) { - this.error = { + this._error = { code: 'LENGTH_UNDERFLOW', message: 'The number of characters in the `' + @@ -461,7 +449,7 @@ export class ParameterChecker { } if (typeof rule.max === 'number') { if (value.length > rule.max) { - this.error = { + this._error = { code: 'LENGTH_OVERFLOW', message: 'The number of characters in the `' + @@ -476,7 +464,7 @@ export class ParameterChecker { if (typeof rule.minBytes === 'number') { const blen = Buffer.from(value, 'utf8').length; if (blen < rule.minBytes) { - this.error = { + this._error = { code: 'LENGTH_UNDERFLOW', message: 'The byte length of the `' + @@ -493,7 +481,7 @@ export class ParameterChecker { if (typeof rule.maxBytes === 'number') { const blen = Buffer.from(value, 'utf8').length; if (blen > rule.maxBytes) { - this.error = { + this._error = { code: 'LENGTH_OVERFLOW', message: 'The byte length of the `' + @@ -509,7 +497,7 @@ export class ParameterChecker { } if (rule.pattern instanceof RegExp) { if (!rule.pattern.test(value)) { - this.error = { + this._error = { code: 'PATTERN_UNMATCH', message: 'The `' + name + '` does not conform with the pattern.', }; @@ -518,7 +506,7 @@ export class ParameterChecker { } if (Array.isArray(rule.enum) && rule.enum.length > 0) { if (rule.enum.indexOf(value) === -1) { - this.error = { + this._error = { code: 'ENUM_UNMATCH', message: 'The `' + @@ -534,3 +522,5 @@ export class ParameterChecker { return true; } } + +export const parameterChecker = new ParameterChecker(); diff --git a/src/switchbot.ts b/src/switchbot.ts index 8e8bbe51..17ebd872 100644 --- a/src/switchbot.ts +++ b/src/switchbot.ts @@ -2,7 +2,8 @@ * * switchbot.ts: Switchbot BLE API registration. */ -import { ParameterChecker } from './parameter-checker.js'; +import Noble from '@abandonware/noble'; +import { parameterChecker } from './parameter-checker.js'; import { Advertising } from './advertising.js'; import { SwitchbotDevice } from './device.js'; @@ -20,27 +21,22 @@ import { WoStrip } from './device/wostrip.js'; import { WoSmartLock } from './device/wosmartlock.js'; import { Ad } from './advertising.js'; -import { Peripheral } from '@abandonware/noble'; - type Params = { duration?: number, model?: string, id?: string, quick?: false, - noble?: any, + noble?: typeof Noble, } export class SwitchBot { private ready: Promise; - noble?: any; + noble!: typeof Noble; ondiscover?: (device: SwitchbotDevice) => void; onadvertisement?: (ad: Ad) => void; onlog: ((message: string) => void) | undefined; - scanning = false; DEFAULT_DISCOVERY_DURATION = 5000; PRIMARY_SERVICE_UUID_LIST = []; - static onlog: any; - static noble: any; /* ------------------------------------------------------------------ * Constructor * @@ -59,7 +55,7 @@ export class SwitchBot { // Check parameters async init(params?: Params) { - let noble; + let noble: typeof Noble; if (params && params.noble) { noble = params.noble; } else { @@ -68,9 +64,6 @@ export class SwitchBot { // Public properties this.noble = noble; - - // Private properties - this.scanning = false; } /* ------------------------------------------------------------------ @@ -114,7 +107,7 @@ export class SwitchBot { discover(params: Params = {}) { const promise = new Promise((resolve, reject) => { // Check the parameters - const valid = ParameterChecker.check( + const valid = parameterChecker.check( params, { duration: { required: false, type: 'integer', min: 1, max: 60000 }, @@ -146,7 +139,7 @@ export class SwitchBot { ); if (!valid) { - reject(new Error(ParameterChecker.error.message)); + reject(new Error(parameterChecker?.error?.message)); return; } @@ -187,13 +180,13 @@ export class SwitchBot { }; // Set a handler for the 'discover' event - this.noble.on('discover', (peripheral: Peripheral) => { - const device = this.getDeviceObject(peripheral, p.id, p.model) as SwitchbotDevice; + this.noble.on('discover', (peripheral: Noble.Peripheral) => { + const device = this.getDeviceObject(peripheral, p.id, p.model); if (!device) { return; } - const id = device.id as string; - peripherals[id] = device; + const id = device.id; + peripherals[id!] = device; if (this.ondiscover && typeof this.ondiscover === 'function') { this.ondiscover(device); @@ -208,7 +201,7 @@ export class SwitchBot { this.noble.startScanning( this.PRIMARY_SERVICE_UUID_LIST, false, - (error: Error) => { + (error?: Error) => { if (error) { reject(error); return; @@ -230,24 +223,24 @@ export class SwitchBot { await this.ready; const promise = new Promise((resolve, reject) => { let err; - if (this.noble.state === 'poweredOn') { + if (this.noble?._state === 'poweredOn') { resolve(); return; } - this.noble.once('stateChange', (state: any) => { + this.noble.once('stateChange', (state: typeof Noble._state) => { switch (state) { case 'unsupported': case 'unauthorized': case 'poweredOff': err = new Error( - 'Failed to initialize the Noble object: ' + this.noble.state, + 'Failed to initialize the Noble object: ' + this.noble?._state, ); reject(err); return; case 'resetting': case 'unknown': err = new Error( - 'Adapter is not ready: ' + this.noble.state, + 'Adapter is not ready: ' + this.noble?._state, ); reject(err); return; @@ -256,7 +249,7 @@ export class SwitchBot { return; default: err = new Error( - 'Unknown state: ' + this.noble.state, + 'Unknown state: ' + this.noble?._state, ); reject(err); return; @@ -266,7 +259,7 @@ export class SwitchBot { return promise; } - getDeviceObject(peripheral: Peripheral, id: string, model: string) { + getDeviceObject(peripheral: Noble.Peripheral, id: string, model: string) { const ad = Advertising.parse(peripheral, this.onlog); if (this.filterAdvertising(ad, id, model)) { let device; @@ -403,7 +396,7 @@ export class SwitchBot { startScan(params: Params = {}) { const promise = new Promise((resolve, reject) => { // Check the parameters - const valid = ParameterChecker.check( + const valid = parameterChecker.check( params, { model: { @@ -432,7 +425,7 @@ export class SwitchBot { false, ); if (!valid) { - reject(new Error(ParameterChecker.error.message)); + reject(new Error(parameterChecker?.error?.message)); return; } @@ -449,7 +442,7 @@ export class SwitchBot { }; // Set a handler for the 'discover' event - this.noble.on('discover', (peripheral: Peripheral) => { + this.noble.on('discover', (peripheral: Noble.Peripheral) => { const ad = Advertising.parse(peripheral, this.onlog); if (this.filterAdvertising(ad, p.id, p.model)) { if ( @@ -465,7 +458,7 @@ export class SwitchBot { this.noble.startScanning( this.PRIMARY_SERVICE_UUID_LIST, true, - (error: Error) => { + (error?: Error) => { if (error) { reject(error); } else { @@ -512,7 +505,7 @@ export class SwitchBot { wait(msec: number) { return new Promise((resolve, reject) => { // Check the parameters - const valid = ParameterChecker.check( + const valid = parameterChecker.check( { msec: msec }, { msec: { required: true, type: 'integer', min: 0 }, @@ -521,7 +514,7 @@ export class SwitchBot { ); if (!valid) { - reject(new Error(ParameterChecker.error.message)); + reject(new Error(parameterChecker?.error?.message)); return; } // Set a timer