Devices now cache notified values for easy retrieval without events
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Nathan Kellenicki 2020-01-13 12:56:58 -08:00
parent dfd22b1d3d
commit 0c3ff8b00a
15 changed files with 24 additions and 22 deletions

View File

@ -22,7 +22,7 @@ export class AbsoluteMotor extends TachoMotor {
* @event AbsoluteMotor#absolute * @event AbsoluteMotor#absolute
* @param {number} absolute * @param {number} absolute
*/ */
this.emitGlobal("absolute", { angle }); this.notify("absolute", { angle });
break; break;
default: default:
super.receive(message); super.receive(message);

View File

@ -23,7 +23,7 @@ export class ColorDistanceSensor extends Device {
* @event ColorDistanceSensor#color * @event ColorDistanceSensor#color
* @param {Color} color * @param {Color} color
*/ */
this.emitGlobal("color", { color }); this.notify("color", { color });
} }
break; break;
@ -39,7 +39,7 @@ export class ColorDistanceSensor extends Device {
* @event ColorDistanceSensor#distance * @event ColorDistanceSensor#distance
* @param {number} distance Distance, in millimeters. * @param {number} distance Distance, in millimeters.
*/ */
this.emitGlobal("distance", { distance }); this.notify("distance", { distance });
} }
break; break;
@ -65,7 +65,7 @@ export class ColorDistanceSensor extends Device {
*/ */
if (message[4] <= 10) { if (message[4] <= 10) {
const color = message[4]; const color = message[4];
this.emitGlobal("colorAndDistance", { color, distance }); this.notify("colorAndDistance", { color, distance });
} }
break; break;

View File

@ -17,7 +17,7 @@ export class CurrentSensor extends Device {
case Mode.CURRENT: case Mode.CURRENT:
if (this.isWeDo2SmartHub) { if (this.isWeDo2SmartHub) {
const current = message.readInt16LE(2) / 1000; const current = message.readInt16LE(2) / 1000;
this.emitGlobal("current", { current }); this.notify("current", { current });
} else { } else {
let maxCurrentValue = MaxCurrentValue[this.hub.type]; let maxCurrentValue = MaxCurrentValue[this.hub.type];
if (maxCurrentValue === undefined) { if (maxCurrentValue === undefined) {
@ -33,7 +33,7 @@ export class CurrentSensor extends Device {
* @event CurrentSensor#current * @event CurrentSensor#current
* @param {number} current * @param {number} current
*/ */
this.emitGlobal("current", { current }); this.notify("current", { current });
} }
break; break;
} }

View File

@ -7,6 +7,7 @@ import * as Consts from "../consts";
export class Device extends EventEmitter { export class Device extends EventEmitter {
public autoSubscribe: boolean = true; public autoSubscribe: boolean = true;
public values: {[event: string]: any} = {};
protected _mode: number | undefined; protected _mode: number | undefined;
protected _busy: boolean = false; protected _busy: boolean = false;
@ -123,13 +124,14 @@ export class Device extends EventEmitter {
} }
public receive (message: Buffer) { public receive (message: Buffer) {
this.emitGlobal("receive", { message }); this.notify("receive", { message });
} }
public emitGlobal (event: string, ...args: any[]) { public notify (event: string, values: any) {
this.emit(event, ...args); this.values[event] = values;
this.emit(event, values);
if (this.hub.listenerCount(event) > 0) { if (this.hub.listenerCount(event) > 0) {
this.hub.emit(event, this, ...args); this.hub.emit(event, this, values);
} }
} }

View File

@ -23,7 +23,7 @@ export class DuploTrainBaseColorSensor extends Device {
* @event DuploTrainBaseColorSensor#color * @event DuploTrainBaseColorSensor#color
* @param {Color} color * @param {Color} color
*/ */
this.emitGlobal("color", { color }); this.notify("color", { color });
} }
break; break;

View File

@ -22,7 +22,7 @@ export class DuploTrainBaseSpeedometer extends Device {
* @event DuploTrainBaseSpeedometer#speed * @event DuploTrainBaseSpeedometer#speed
* @param {number} speed * @param {number} speed
*/ */
this.emitGlobal("speed", { speed }); this.notify("speed", { speed });
break; break;
} }

View File

@ -25,7 +25,7 @@ export class MotionSensor extends Device {
* @event MotionSensor#distance * @event MotionSensor#distance
* @param {number} distance Distance, in millimeters. * @param {number} distance Distance, in millimeters.
*/ */
this.emitGlobal("distance", { distance }); this.notify("distance", { distance });
break; break;
} }
} }

View File

@ -23,7 +23,7 @@ export class MoveHubTiltSensor extends Device {
*/ */
const x = -message.readInt8(4); const x = -message.readInt8(4);
const y = message.readInt8(5); const y = message.readInt8(5);
this.emitGlobal("tilt", { x, y }); this.notify("tilt", { x, y });
break; break;
} }
} }

View File

@ -21,7 +21,7 @@ export class RemoteControlButton extends Device {
* @param {number} event * @param {number} event
*/ */
const event = message[4]; const event = message[4];
this.emitGlobal("remoteButton", { event }); this.notify("remoteButton", { event });
break; break;
} }
} }

View File

@ -22,7 +22,7 @@ export class TachoMotor extends BasicMotor {
* @event TachoMotor#rotate * @event TachoMotor#rotate
* @param {number} rotation * @param {number} rotation
*/ */
this.emitGlobal("rotate", { degrees }); this.notify("rotate", { degrees });
break; break;
} }
} }

View File

@ -26,7 +26,7 @@ export class TechnicMediumHubAccelerometerSensor extends Device {
const x = Math.round(message.readInt16LE(4) / 4.096); const x = Math.round(message.readInt16LE(4) / 4.096);
const y = Math.round(message.readInt16LE(6) / 4.096); const y = Math.round(message.readInt16LE(6) / 4.096);
const z = Math.round(message.readInt16LE(8) / 4.096); const z = Math.round(message.readInt16LE(8) / 4.096);
this.emitGlobal("accel", { x, y, z }); this.notify("accel", { x, y, z });
break; break;
} }
} }

View File

@ -25,7 +25,7 @@ export class TechnicMediumHubGyroSensor extends Device {
const x = Math.round(message.readInt16LE(4) * 7 / 400); const x = Math.round(message.readInt16LE(4) * 7 / 400);
const y = Math.round(message.readInt16LE(6) * 7 / 400); const y = Math.round(message.readInt16LE(6) * 7 / 400);
const z = Math.round(message.readInt16LE(8) * 7 / 400); const z = Math.round(message.readInt16LE(8) * 7 / 400);
this.emitGlobal("gyro", { x, y, z }); this.notify("gyro", { x, y, z });
break; break;
} }
} }

View File

@ -25,7 +25,7 @@ export class TechnicMediumHubTiltSensor extends Device {
const z = -message.readInt16LE(4); const z = -message.readInt16LE(4);
const y = message.readInt16LE(6); const y = message.readInt16LE(6);
const x = message.readInt16LE(8); const x = message.readInt16LE(8);
this.emitGlobal("tilt", { x, y, z }); this.notify("tilt", { x, y, z });
break; break;
} }
} }

View File

@ -23,7 +23,7 @@ export class TiltSensor extends Device {
* @param {number} x * @param {number} x
* @param {number} y * @param {number} y
*/ */
this.emitGlobal("tilt", { x, y }); this.notify("tilt", { x, y });
break; break;
} }
} }

View File

@ -17,7 +17,7 @@ export class VoltageSensor extends Device {
case Mode.VOLTAGE: case Mode.VOLTAGE:
if (this.isWeDo2SmartHub) { if (this.isWeDo2SmartHub) {
const voltage = message.readInt16LE(2) / 40; const voltage = message.readInt16LE(2) / 40;
this.emitGlobal("voltage", { voltage }); this.notify("voltage", { voltage });
} else { } else {
let maxVoltageValue = MaxVoltageValue[this.hub.type]; let maxVoltageValue = MaxVoltageValue[this.hub.type];
if (maxVoltageValue === undefined) { if (maxVoltageValue === undefined) {
@ -33,7 +33,7 @@ export class VoltageSensor extends Device {
* @event VoltageSensor#voltage * @event VoltageSensor#voltage
* @param {number} voltage * @param {number} voltage
*/ */
this.emitGlobal("voltage", { voltage }); this.notify("voltage", { voltage });
} }
break; break;
} }