Skip to content

Commit

Permalink
Fix TypeScript issues (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolson authored Apr 24, 2024
1 parent f78e22d commit 5104e00
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions src/advertising.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export type Ad = {
id: string;
address: string;
rssi: number,
serviceData: any;
} | null
serviceData: Record<string, unknown>;
} | null;

export class Advertising {

Expand Down
46 changes: 22 additions & 24 deletions src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -122,10 +122,10 @@ export class SwitchbotDevice {
_connect() {
return new Promise<void>((resolve, reject) => {
// Check the bluetooth state
if (this._noble.state !== 'poweredOn') {
if (this._noble._state !== 'poweredOn') {

Check failure on line 125 in src/device.ts

View workflow job for this annotation

GitHub Actions / build_and_test / build (18, ubuntu-latest)

Property '_state' does not exist on type 'typeof import("/home/runner/work/node-switchbot/node-switchbot/node_modules/@abandonware/noble/index")'. Did you mean 'state'?

Check failure on line 125 in src/device.ts

View workflow job for this annotation

GitHub Actions / build_and_test / build (20, ubuntu-latest)

Property '_state' does not exist on type 'typeof import("/home/runner/work/node-switchbot/node-switchbot/node_modules/@abandonware/noble/index")'. Did you mean 'state'?
reject(
new Error(
'The Bluetooth status is ' + this._noble.state + ', not poweredOn.',
'The Bluetooth status is ' + this._noble._state + ', not poweredOn.',

Check failure on line 128 in src/device.ts

View workflow job for this annotation

GitHub Actions / build_and_test / build (18, ubuntu-latest)

Property '_state' does not exist on type 'typeof import("/home/runner/work/node-switchbot/node-switchbot/node_modules/@abandonware/noble/index")'. Did you mean 'state'?

Check failure on line 128 in src/device.ts

View workflow job for this annotation

GitHub Actions / build_and_test / build (20, ubuntu-latest)

Property '_state' does not exist on type 'typeof import("/home/runner/work/node-switchbot/node-switchbot/node_modules/@abandonware/noble/index")'. Did you mean 'state'?
),
);
return;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -249,7 +249,7 @@ export class SwitchbotDevice {
});
}

_discoverServices() {
_discoverServices(): Promise<Noble.Service[]> {
return new Promise((resolve, reject) => {
this._peripheral.discoverServices([], (error, service_list) => {
if (error) {
Expand All @@ -273,7 +273,7 @@ export class SwitchbotDevice {
});
}

_discoverCharacteristics(service: Service) {
_discoverCharacteristics(service: Noble.Service): Promise<Noble.Characteristic[]> {
return new Promise((resolve, reject) => {
service.discoverCharacteristics([], (error, char_list) => {
if (error) {
Expand Down Expand Up @@ -357,9 +357,7 @@ export class SwitchbotDevice {

_disconnect() {
if (this._was_connected_explicitly) {
return new Promise<void>((resolve) => {
resolve();
});
return Promise.resolve();
} else {
return this.disconnect();
}
Expand Down Expand Up @@ -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();
})
Expand Down Expand Up @@ -419,7 +417,7 @@ export class SwitchbotDevice {
setDeviceName(name: string) {
return new Promise<void>((resolve, reject) => {
// Check the parameters
const valid = ParameterChecker.check(
const valid = parameterChecker.check(
{ name: name },
{
name: { required: true, type: 'string', minBytes: 1, maxBytes: 100 },
Expand All @@ -428,7 +426,7 @@ export class SwitchbotDevice {
);

if (!valid) {
reject(new Error(ParameterChecker.error.message));
reject(new Error(parameterChecker?.error?.message));
return;
}

Expand Down Expand Up @@ -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<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: Buffer | unknown;
let res_buf: Buffer;

this._connect()
.then(() => {
Expand All @@ -492,7 +490,7 @@ export class SwitchbotDevice {
});
}

_waitCommandResponse() {
_waitCommandResponse(): Promise<Buffer> {
return new Promise((resolve, reject) => {
let timer: NodeJS.Timeout | undefined = setTimeout(() => {
timer = undefined;
Expand All @@ -512,7 +510,7 @@ export class SwitchbotDevice {
}

// Read data from the specified characteristic
_read(char: Characteristic) {
_read(char: Noble.Characteristic): Promise<Buffer>{
return new Promise((resolve, reject) => {
// Set a timeout timer
let timer: NodeJS.Timeout | undefined = setTimeout(() => {
Expand All @@ -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<void | string>{
return new Promise<void>((resolve, reject) => {
// Set a timeout timer
let timer: NodeJS.Timeout | undefined = setTimeout(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/device/woblindtilt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ export class WoBlindTilt extends SwitchbotDevice {
return new Promise<void>((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'),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/device/wobulb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/device/wocurtain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ export class WoCurtain extends SwitchbotDevice {
return new Promise<void>((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'),
),
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/device/wohand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ export class WoHand extends SwitchbotDevice {
return new Promise<void>((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'),
),
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/device/wohumi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ export class WoHumi extends SwitchbotDevice {
return new Promise<void>((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'),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/device/woplugmini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/device/wostrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 5104e00

Please sign in to comment.