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
* @param {number} absolute
*/
this.emitGlobal("absolute", { angle });
this.notify("absolute", { angle });
break;
default:
super.receive(message);

View File

@ -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;

View File

@ -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;
}

View File

@ -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);
}
}

View File

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

View File

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

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

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

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}