diff --git a/boostmovehub.ts b/boostmovehub.ts index cc03636..3aac515 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -61,6 +61,7 @@ export class BoostMoveHub extends LPF2Hub { if (portObj.id !== "AB" && speed instanceof Array) { throw new Error(`Port ${portObj.id} can only accept a single speed`); } + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { if (time) { @@ -82,11 +83,12 @@ export class BoostMoveHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); - setTimeout(() => { + const timeout = setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); return resolve(); }, time); + portObj.setEventTimer(timeout); } } else { @@ -125,8 +127,10 @@ export class BoostMoveHub extends LPF2Hub { * @returns {Promise} Resolved upon successful completion of command. */ public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { + const portObj = this._portLookup(port); + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time) + this._calculateRamp(fromSpeed, toSpeed, time, portObj) .on("changeSpeed", (speed) => { this.setMotorSpeed(port, speed); }) @@ -151,6 +155,7 @@ export class BoostMoveHub extends LPF2Hub { if (portObj.id !== "AB" && speed instanceof Array) { throw new Error(`Port ${portObj.id} can only accept a single speed`); } + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { portObj.busy = true; let data = null; @@ -183,11 +188,12 @@ export class BoostMoveHub extends LPF2Hub { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); if (time) { - setTimeout(() => { + const timeout = setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); return resolve(); }, time); + portObj.setEventTimer(timeout); } else { return resolve(); } diff --git a/hub.ts b/hub.ts index 23cd983..c4160bb 100644 --- a/hub.ts +++ b/hub.ts @@ -304,7 +304,7 @@ export class Hub extends EventEmitter { } - protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number) { + protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number, port: Port) { const emitter = new EventEmitter(); const steps = Math.abs(toSpeed - fromSpeed); let delay = time / steps; @@ -330,6 +330,7 @@ export class Hub extends EventEmitter { emitter.emit("finished"); } }, delay); + port.setEventTimer(interval); return emitter; } diff --git a/port.ts b/port.ts index 2f3ae31..508f703 100644 --- a/port.ts +++ b/port.ts @@ -6,19 +6,28 @@ export class Port { public id: string; public value: number; - public connected: boolean; public type: Consts.Devices; - public busy: boolean; - public finished: (() => void) | null; + public connected: boolean = false; + public busy: boolean = false; + public finished: (() => void) | null = null; + private _eventTimer: NodeJS.Timer | null = null; constructor (id: string, value: number) { this.id = id; this.value = value; - this.connected = false; - this.busy = false; - this.finished = null; this.type = Consts.Devices.UNKNOWN; } + public cancelEventTimer () { + if (this._eventTimer) { + clearTimeout(this._eventTimer); + this._eventTimer = null; + } + } + + public setEventTimer (timer: NodeJS.Timer) { + this._eventTimer = timer; + } + } diff --git a/puphub.ts b/puphub.ts index 2f29a66..699f8d0 100644 --- a/puphub.ts +++ b/puphub.ts @@ -74,6 +74,7 @@ export class PUPHub extends LPF2Hub { throw new Error(`Port ${portObj.id} requires both motors be of the same type`); } } + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { if (time) { let data = null; @@ -84,7 +85,7 @@ export class PUPHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); } this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); - setTimeout(() => { + const timeout = setTimeout(() => { let data = null; if (portObj.id === "AB") { data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00, 0x00]); @@ -94,6 +95,7 @@ export class PUPHub extends LPF2Hub { this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); return resolve(); }, time); + portObj.setEventTimer(timeout); } else { let data = null; if (portObj.id === "AB") { @@ -119,8 +121,10 @@ export class PUPHub extends LPF2Hub { * @returns {Promise} Resolved upon successful completion of command. */ public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { + const portObj = this._portLookup(port); + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time) + this._calculateRamp(fromSpeed, toSpeed, time, portObj) .on("changeSpeed", (speed) => { this.setMotorSpeed(port, speed); }) @@ -139,15 +143,17 @@ export class PUPHub extends LPF2Hub { */ public setLightBrightness (port: string, brightness: number, time?: number) { const portObj = this._portLookup(port); + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); if (time) { - setTimeout(() => { + const timeout = setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); return resolve(); }, time); + portObj.setEventTimer(timeout); } else { return resolve(); } diff --git a/wedo2smarthub.ts b/wedo2smarthub.ts index b50a38d..6494871 100644 --- a/wedo2smarthub.ts +++ b/wedo2smarthub.ts @@ -125,13 +125,16 @@ export class WeDo2SmartHub extends Hub { * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. */ public setMotorSpeed (port: string, speed: number, time?: number) { + const portObj = this._portLookup(port); + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, this._mapSpeed(speed)])); + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); if (time) { - setTimeout(() => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, 0x00])); + const timeout = setTimeout(() => { + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); return resolve(); }, time); + portObj.setEventTimer(timeout); } else { return resolve(); } @@ -149,8 +152,10 @@ export class WeDo2SmartHub extends Hub { * @returns {Promise} Resolved upon successful completion of command. */ public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { + const portObj = this._portLookup(port); + portObj.cancelEventTimer(); return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time) + this._calculateRamp(fromSpeed, toSpeed, time, portObj) .on("changeSpeed", (speed) => { this.setMotorSpeed(port, speed); }) @@ -191,11 +196,12 @@ export class WeDo2SmartHub extends Hub { const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); if (time) { - setTimeout(() => { + const timeout = setTimeout(() => { const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }, time); + portObj.setEventTimer(timeout); } else { return resolve(); }