diff --git a/boostmovehub.ts b/boostmovehub.ts index 8b8525f..f08191b 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -76,7 +76,7 @@ export class BoostMoveHub extends LPF2Hub { */ public setMotorSpeed (port: string, speed: number, time?: number) { return new Promise((resolve, reject) => { - const portObj = this._ports[port]; + const portObj = this._portLookup(port); if (time) { if (portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) { portObj.busy = true; @@ -113,7 +113,7 @@ export class BoostMoveHub extends LPF2Hub { * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ public setMotorAngle (port: string, angle: number, speed: number = 100) { - const portObj = this._ports[port]; + const portObj = this._portLookup(port); if (!(portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR)) { throw new Error("Angle rotation is only available when using a Boost Interactive Motor or Boost Move Hub Motor"); } diff --git a/hub.ts b/hub.ts index 8188ee4..fcd37c8 100644 --- a/hub.ts +++ b/hub.ts @@ -169,11 +169,11 @@ export class Hub extends EventEmitter { */ public subscribe (port: string, mode?: number) { return new Promise((resolve, reject) => { - let newMode = this._getModeForDeviceType(this._ports[port].type); + let newMode = this._getModeForDeviceType(this._portLookup(port).type); if (mode) { newMode = mode; } - this._activatePortDevice(this._ports[port].value, this._ports[port].type, newMode, 0x00, () => { + this._activatePortDevice(this._portLookup(port).value, this._portLookup(port).type, newMode, 0x00, () => { return resolve(); }); }); @@ -187,8 +187,8 @@ export class Hub extends EventEmitter { */ public unsubscribe (port: string) { return new Promise((resolve, reject) => { - const mode = this._getModeForDeviceType(this._ports[port].type); - this._deactivatePortDevice(this._ports[port].value, this._ports[port].type, mode, 0x00, () => { + const mode = this._getModeForDeviceType(this._portLookup(port).type); + this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => { return resolve(); }); }); @@ -310,6 +310,14 @@ export class Hub extends EventEmitter { } + protected _portLookup (port: string) { + if (!this._ports[port]) { + throw new Error(`Port ${port} does not exist on this Hub type`); + } + return this._ports[port]; + } + + private _getModeForDeviceType (type: Consts.Devices) { switch (type) { case Consts.Devices.BASIC_MOTOR: diff --git a/puphub.ts b/puphub.ts index 5c43dca..e17b666 100644 --- a/puphub.ts +++ b/puphub.ts @@ -80,7 +80,7 @@ export class PUPHub extends LPF2Hub { */ public setMotorSpeed (port: string, speed: number, time?: number) { return new Promise((resolve, reject) => { - const portObj = this._ports[port]; + const portObj = this._portLookup(port); if (time) { const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); diff --git a/wedo2hub.ts b/wedo2hub.ts index 7cb937e..250662e 100644 --- a/wedo2hub.ts +++ b/wedo2hub.ts @@ -102,10 +102,10 @@ export class WeDo2Hub extends Hub { */ public setMotorSpeed (port: string, speed: number, time?: number) { return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)])); + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, this._mapSpeed(speed)])); if (time) { setTimeout(() => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, 0x00])); + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, 0x00])); return resolve(); }, time); } else {