From 0c3ff8b00afa75c2f38586dcdfca3ee247848c4c Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 13 Jan 2020 12:56:58 -0800 Subject: [PATCH] Devices now cache notified values for easy retrieval without events --- src/devices/absolutemotor.ts | 2 +- src/devices/colordistancesensor.ts | 6 +++--- src/devices/currentsensor.ts | 4 ++-- src/devices/device.ts | 10 ++++++---- src/devices/duplotrainbasecolorsensor.ts | 2 +- src/devices/duplotrainbasespeedometer.ts | 2 +- src/devices/motionsensor.ts | 2 +- src/devices/movehubtiltsensor.ts | 2 +- src/devices/remotecontrolbutton.ts | 2 +- src/devices/tachomotor.ts | 2 +- src/devices/technicmediumhubaccelerometersensor.ts | 2 +- src/devices/technicmediumhubgyrosensor.ts | 2 +- src/devices/technicmediumhubtiltsensor.ts | 2 +- src/devices/tiltsensor.ts | 2 +- src/devices/voltagesensor.ts | 4 ++-- 15 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/devices/absolutemotor.ts b/src/devices/absolutemotor.ts index 817e7f5..056a13c 100644 --- a/src/devices/absolutemotor.ts +++ b/src/devices/absolutemotor.ts @@ -22,7 +22,7 @@ export class AbsoluteMotor extends TachoMotor { * @event AbsoluteMotor#absolute * @param {number} absolute */ - this.emitGlobal("absolute", { angle }); + this.notify("absolute", { angle }); break; default: super.receive(message); diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 14a63a3..4f7752d 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -23,7 +23,7 @@ export class ColorDistanceSensor extends Device { * @event ColorDistanceSensor#color * @param {Color} color */ - this.emitGlobal("color", { color }); + this.notify("color", { color }); } break; @@ -39,7 +39,7 @@ export class ColorDistanceSensor extends Device { * @event ColorDistanceSensor#distance * @param {number} distance Distance, in millimeters. */ - this.emitGlobal("distance", { distance }); + this.notify("distance", { distance }); } break; @@ -65,7 +65,7 @@ export class ColorDistanceSensor extends Device { */ if (message[4] <= 10) { const color = message[4]; - this.emitGlobal("colorAndDistance", { color, distance }); + this.notify("colorAndDistance", { color, distance }); } break; diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index 82c1a28..b39fa28 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -17,7 +17,7 @@ export class CurrentSensor extends Device { case Mode.CURRENT: if (this.isWeDo2SmartHub) { const current = message.readInt16LE(2) / 1000; - this.emitGlobal("current", { current }); + this.notify("current", { current }); } else { let maxCurrentValue = MaxCurrentValue[this.hub.type]; if (maxCurrentValue === undefined) { @@ -33,7 +33,7 @@ export class CurrentSensor extends Device { * @event CurrentSensor#current * @param {number} current */ - this.emitGlobal("current", { current }); + this.notify("current", { current }); } break; } diff --git a/src/devices/device.ts b/src/devices/device.ts index 245c5d6..46cf5a4 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -7,6 +7,7 @@ import * as Consts from "../consts"; export class Device extends EventEmitter { public autoSubscribe: boolean = true; + public values: {[event: string]: any} = {}; protected _mode: number | undefined; protected _busy: boolean = false; @@ -123,13 +124,14 @@ export class Device extends EventEmitter { } public receive (message: Buffer) { - this.emitGlobal("receive", { message }); + this.notify("receive", { message }); } - public emitGlobal (event: string, ...args: any[]) { - this.emit(event, ...args); + public notify (event: string, values: any) { + this.values[event] = values; + this.emit(event, values); if (this.hub.listenerCount(event) > 0) { - this.hub.emit(event, this, ...args); + this.hub.emit(event, this, values); } } diff --git a/src/devices/duplotrainbasecolorsensor.ts b/src/devices/duplotrainbasecolorsensor.ts index e4d94f2..46d9de7 100644 --- a/src/devices/duplotrainbasecolorsensor.ts +++ b/src/devices/duplotrainbasecolorsensor.ts @@ -23,7 +23,7 @@ export class DuploTrainBaseColorSensor extends Device { * @event DuploTrainBaseColorSensor#color * @param {Color} color */ - this.emitGlobal("color", { color }); + this.notify("color", { color }); } break; diff --git a/src/devices/duplotrainbasespeedometer.ts b/src/devices/duplotrainbasespeedometer.ts index e9a7442..a21b622 100644 --- a/src/devices/duplotrainbasespeedometer.ts +++ b/src/devices/duplotrainbasespeedometer.ts @@ -22,7 +22,7 @@ export class DuploTrainBaseSpeedometer extends Device { * @event DuploTrainBaseSpeedometer#speed * @param {number} speed */ - this.emitGlobal("speed", { speed }); + this.notify("speed", { speed }); break; } diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index 559fecd..f37a745 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -25,7 +25,7 @@ export class MotionSensor extends Device { * @event MotionSensor#distance * @param {number} distance Distance, in millimeters. */ - this.emitGlobal("distance", { distance }); + this.notify("distance", { distance }); break; } } diff --git a/src/devices/movehubtiltsensor.ts b/src/devices/movehubtiltsensor.ts index ed6a649..34768a9 100644 --- a/src/devices/movehubtiltsensor.ts +++ b/src/devices/movehubtiltsensor.ts @@ -23,7 +23,7 @@ export class MoveHubTiltSensor extends Device { */ const x = -message.readInt8(4); const y = message.readInt8(5); - this.emitGlobal("tilt", { x, y }); + this.notify("tilt", { x, y }); break; } } diff --git a/src/devices/remotecontrolbutton.ts b/src/devices/remotecontrolbutton.ts index 031744e..0e423aa 100644 --- a/src/devices/remotecontrolbutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -21,7 +21,7 @@ export class RemoteControlButton extends Device { * @param {number} event */ const event = message[4]; - this.emitGlobal("remoteButton", { event }); + this.notify("remoteButton", { event }); break; } } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 9e2f9f4..d266522 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -22,7 +22,7 @@ export class TachoMotor extends BasicMotor { * @event TachoMotor#rotate * @param {number} rotation */ - this.emitGlobal("rotate", { degrees }); + this.notify("rotate", { degrees }); break; } } diff --git a/src/devices/technicmediumhubaccelerometersensor.ts b/src/devices/technicmediumhubaccelerometersensor.ts index 28990d0..5ee8472 100644 --- a/src/devices/technicmediumhubaccelerometersensor.ts +++ b/src/devices/technicmediumhubaccelerometersensor.ts @@ -26,7 +26,7 @@ export class TechnicMediumHubAccelerometerSensor extends Device { const x = Math.round(message.readInt16LE(4) / 4.096); const y = Math.round(message.readInt16LE(6) / 4.096); const z = Math.round(message.readInt16LE(8) / 4.096); - this.emitGlobal("accel", { x, y, z }); + this.notify("accel", { x, y, z }); break; } } diff --git a/src/devices/technicmediumhubgyrosensor.ts b/src/devices/technicmediumhubgyrosensor.ts index 929592a..74f8bd0 100644 --- a/src/devices/technicmediumhubgyrosensor.ts +++ b/src/devices/technicmediumhubgyrosensor.ts @@ -25,7 +25,7 @@ export class TechnicMediumHubGyroSensor extends Device { const x = Math.round(message.readInt16LE(4) * 7 / 400); const y = Math.round(message.readInt16LE(6) * 7 / 400); const z = Math.round(message.readInt16LE(8) * 7 / 400); - this.emitGlobal("gyro", { x, y, z }); + this.notify("gyro", { x, y, z }); break; } } diff --git a/src/devices/technicmediumhubtiltsensor.ts b/src/devices/technicmediumhubtiltsensor.ts index 8f8b384..a05d643 100644 --- a/src/devices/technicmediumhubtiltsensor.ts +++ b/src/devices/technicmediumhubtiltsensor.ts @@ -25,7 +25,7 @@ export class TechnicMediumHubTiltSensor extends Device { const z = -message.readInt16LE(4); const y = message.readInt16LE(6); const x = message.readInt16LE(8); - this.emitGlobal("tilt", { x, y, z }); + this.notify("tilt", { x, y, z }); break; } } diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index cd99d6f..27e6136 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -23,7 +23,7 @@ export class TiltSensor extends Device { * @param {number} x * @param {number} y */ - this.emitGlobal("tilt", { x, y }); + this.notify("tilt", { x, y }); break; } } diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index 906cd3f..ecadb5e 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -17,7 +17,7 @@ export class VoltageSensor extends Device { case Mode.VOLTAGE: if (this.isWeDo2SmartHub) { const voltage = message.readInt16LE(2) / 40; - this.emitGlobal("voltage", { voltage }); + this.notify("voltage", { voltage }); } else { let maxVoltageValue = MaxVoltageValue[this.hub.type]; if (maxVoltageValue === undefined) { @@ -33,7 +33,7 @@ export class VoltageSensor extends Device { * @event VoltageSensor#voltage * @param {number} voltage */ - this.emitGlobal("voltage", { voltage }); + this.notify("voltage", { voltage }); } break; }