From a2e254fa18e1d41ce68627764cfd6ec0a331fca6 Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Wed, 20 Jun 2018 14:58:42 +0100 Subject: [PATCH] Fixed ts errors and types --- boosthub.ts | 18 +++++++++--------- consts.ts | 1 + hub.ts | 4 ++-- port.ts | 7 +++++-- wedo2hub.ts | 16 ++++++++-------- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/boosthub.ts b/boosthub.ts index ad0ba8f..3702a37 100644 --- a/boosthub.ts +++ b/boosthub.ts @@ -40,12 +40,12 @@ export class BoostHub extends Hub { } - public connect (callback: () => void) { + public connect (callback?: () => void) { debug("Connecting to Boost Move Hub"); super.connect(() => { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); - characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); + characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]), false); debug("Connect completed"); if (callback) { callback(); @@ -63,12 +63,12 @@ export class BoostHub extends Hub { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; if (characteristic) { let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]); - characteristic.write(data); + characteristic.write(data, false); if (color === false) { color = 0; } data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); - characteristic.write(data); + characteristic.write(data, false); } } @@ -97,10 +97,10 @@ export class BoostHub extends Hub { if (time) { const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]); data.writeUInt16LE(time > 65535 ? 65535 : time, 6); - characteristic.write(data); + characteristic.write(data, false); } else { const data = Buffer.from([0x0a, 0x00, 0x81, this._ports[port].value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]); - characteristic.write(data); + characteristic.write(data, false); } } } @@ -119,7 +119,7 @@ export class BoostHub extends Hub { const data = Buffer.from([0x0e, 0x00, 0x81, this._ports[port].value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); data.writeUInt32LE(angle, 6); data.writeInt8(speed, 10); - characteristic.write(data); + characteristic.write(data, false); } } @@ -127,7 +127,7 @@ export class BoostHub extends Hub { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; if (characteristic) { - characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); + characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), false, callback); } } @@ -135,7 +135,7 @@ export class BoostHub extends Hub { protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; if (characteristic) { - characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); + characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), false, callback); } } diff --git a/consts.ts b/consts.ts index a1c6dca..a632626 100644 --- a/consts.ts +++ b/consts.ts @@ -7,6 +7,7 @@ export enum Hubs { } export enum Devices { + UNKNOWN = 0, BASIC_MOTOR = 1, BOOST_LED = 22, WEDO2_TILT = 34, diff --git a/hub.ts b/hub.ts index 7355cab..354a958 100644 --- a/hub.ts +++ b/hub.ts @@ -40,7 +40,7 @@ export class Hub extends EventEmitter { * @method Hub#connect * @param {function} [callback] */ - public connect (callback: () => void) { + public connect (callback?: () => void) { const self = this; @@ -165,7 +165,7 @@ export class Hub extends EventEmitter { this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00); } } else { - port.type = null; + port.type = Consts.Devices.UNKNOWN; debug(`Port ${port.id} disconnected`); } diff --git a/port.ts b/port.ts index f4d18f6..9ea9979 100644 --- a/port.ts +++ b/port.ts @@ -1,17 +1,20 @@ +import * as Consts from "./consts"; + + export class Port { public id: string; public value: number; public connected: boolean; - public type: number | null; + public type: Consts.Devices; constructor (id: string, value: number) { this.id = id; this.value = value; this.connected = false; - this.type = null; + this.type = Consts.Devices.UNKNOWN; } } diff --git a/wedo2hub.ts b/wedo2hub.ts index 491c465..132ed5e 100644 --- a/wedo2hub.ts +++ b/wedo2hub.ts @@ -36,7 +36,7 @@ export class WeDo2Hub extends Hub { } - public connect (callback: () => void) { + public connect (callback?: () => void) { debug("Connecting to WeDo 2.0 Smart Hub"); super.connect(() => { this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE], this._parsePortMessage.bind(this)); @@ -60,12 +60,12 @@ export class WeDo2Hub extends Hub { const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; if (motorCharacteristic && portCharacteristic) { let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - portCharacteristic.write(data); + portCharacteristic.write(data, false); if (color === false) { color = 0; } data = Buffer.from([0x06, 0x04, 0x01, color]); - motorCharacteristic.write(data); + motorCharacteristic.write(data, false); } } @@ -82,9 +82,9 @@ export class WeDo2Hub extends Hub { const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; if (motorCharacteristic && portCharacteristic) { const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]); - portCharacteristic.write(data1); + portCharacteristic.write(data1, false); const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - motorCharacteristic.write(data2); + motorCharacteristic.write(data2, false); } } @@ -98,7 +98,7 @@ export class WeDo2Hub extends Hub { public setMotorSpeed (port: string, speed: number) { const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; if (characteristic) { - characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed])); + characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false); } } @@ -106,7 +106,7 @@ export class WeDo2Hub extends Hub { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; if (characteristic) { - characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); + characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), false, callback); } } @@ -114,7 +114,7 @@ export class WeDo2Hub extends Hub { protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; if (characteristic) { - characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); + characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), false, callback); } }