diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 26ac2b3..bf1a9e6 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.emitGlobal("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.emitGlobal("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.emitGlobal("colorAndDistance", { color, distance }); } break; diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index 69ef549..dc688d4 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -17,7 +17,7 @@ export class CurrentSensor extends Device { case CurrentSensor.Mode.CURRENT: if (this.isWeDo2SmartHub) { const current = message.readInt16LE(2) / 1000; - this.emitGlobal("current", current); + this.emitGlobal("current", { current }); } else { let maxCurrentValue = CurrentSensor.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.emitGlobal("current", { current }); } break; } diff --git a/src/devices/device.ts b/src/devices/device.ts index ddb0222..1a93f78 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -113,7 +113,7 @@ export class Device extends EventEmitter { } public receive (message: Buffer) { - this.emitGlobal("receive", message); + this.emitGlobal("receive", { message }); } public emitGlobal (event: string, ...args: any[]) { diff --git a/src/devices/duplotrainbasecolorsensor.ts b/src/devices/duplotrainbasecolorsensor.ts index 4c3a8ab..4f8d22d 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.emitGlobal("color", { color }); } break; diff --git a/src/devices/duplotrainbasespeedometer.ts b/src/devices/duplotrainbasespeedometer.ts index 780c49a..17186dc 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.emitGlobal("speed", { speed }); break; } diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index a5d234b..1a75961 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -19,12 +19,13 @@ export class MotionSensor extends Device { if (message[this.isWeDo2SmartHub ? 3 : 5] === 1) { distance = distance + 255; } + distance *= 10; /** * Emits when a distance sensor is activated. * @event MotionSensor#distance * @param {number} distance Distance, in millimeters. */ - this.emitGlobal("distance", distance * 10); + this.emitGlobal("distance", { distance }); break; } } diff --git a/src/devices/movehubtiltsensor.ts b/src/devices/movehubtiltsensor.ts index 08cb1b4..b9bc0d7 100644 --- a/src/devices/movehubtiltsensor.ts +++ b/src/devices/movehubtiltsensor.ts @@ -21,9 +21,9 @@ export class MoveHubTiltSensor extends Device { * @param {number} x * @param {number} y */ - const tiltX = message.readInt8(4); - const tiltY = message.readInt8(5); - this.emitGlobal("tilt", -tiltX, tiltY); + const x = -message.readInt8(4); + const y = message.readInt8(5); + this.emitGlobal("tilt", { x, y }); break; } } diff --git a/src/devices/remotecontrolbutton.ts b/src/devices/remotecontrolbutton.ts index 13d0387..c43af4f 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.emitGlobal("remoteButton", { event }); break; } } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 583f11c..69a3a28 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", rotation); + this.emitGlobal("rotate", { rotation }); break; } } diff --git a/src/devices/technicmediumhubaccelerometersensor.ts b/src/devices/technicmediumhubaccelerometersensor.ts index 6beb42c..80e6901 100644 --- a/src/devices/technicmediumhubaccelerometersensor.ts +++ b/src/devices/technicmediumhubaccelerometersensor.ts @@ -23,10 +23,10 @@ export class TechnicMediumHubAccelerometerSensor extends Device { * @param {number} y * @param {number} z */ - const accelX = Math.round(message.readInt16LE(4) / 4.096); - const accelY = Math.round(message.readInt16LE(6) / 4.096); - const accelZ = Math.round(message.readInt16LE(8) / 4.096); - this.emitGlobal("accel", accelX, accelY, accelZ); + 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 }); break; } } diff --git a/src/devices/technicmediumhubgyrosensor.ts b/src/devices/technicmediumhubgyrosensor.ts index 9ecea0b..793749d 100644 --- a/src/devices/technicmediumhubgyrosensor.ts +++ b/src/devices/technicmediumhubgyrosensor.ts @@ -22,10 +22,10 @@ export class TechnicMediumHubGyroSensor extends Device { * @param {number} y * @param {number} z */ - const gyroX = Math.round(message.readInt16LE(4) * 7 / 400); - const gyroY = Math.round(message.readInt16LE(6) * 7 / 400); - const gyroZ = Math.round(message.readInt16LE(8) * 7 / 400); - this.emitGlobal("gyro", gyroX, gyroY, -gyroZ); + 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 }); break; } } diff --git a/src/devices/technicmediumhubtiltsensor.ts b/src/devices/technicmediumhubtiltsensor.ts index 47bd4c0..53f0595 100644 --- a/src/devices/technicmediumhubtiltsensor.ts +++ b/src/devices/technicmediumhubtiltsensor.ts @@ -22,10 +22,10 @@ export class TechnicMediumHubTiltSensor extends Device { * @param {number} y * @param {number} z */ - const tiltZ = message.readInt16LE(4); - const tiltY = message.readInt16LE(6); - const tiltX = message.readInt16LE(8); - this.emitGlobal("tilt", tiltX, tiltY, -tiltZ); + const z = -message.readInt16LE(4); + const y = message.readInt16LE(6); + const x = message.readInt16LE(8); + this.emitGlobal("tilt", { x, y, z }); break; } } diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index 91c2a3d..301bfd0 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -15,15 +15,15 @@ export class TiltSensor extends Device { switch (mode) { case TiltSensor.Mode.TILT: - const tiltX = message.readInt8(this.isWeDo2SmartHub ? 2 : 4); - const tiltY = message.readInt8(this.isWeDo2SmartHub ? 3 : 5); + const x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4); + const y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5); /** * Emits when a tilt sensor is activated. * @event LPF2Hub#tilt * @param {number} x * @param {number} y */ - this.emitGlobal("tilt", tiltX, tiltY); + this.emitGlobal("tilt", { x, y }); break; } } diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index 8610e47..c37b9ed 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -17,7 +17,7 @@ export class VoltageSensor extends Device { case VoltageSensor.Mode.VOLTAGE: if (this.isWeDo2SmartHub) { const voltage = message.readInt16LE(2) / 40; - this.emitGlobal("voltage", voltage); + this.emitGlobal("voltage", { voltage }); } else { let maxVoltageValue = VoltageSensor.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.emitGlobal("voltage", { voltage }); } break; } diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 4f4bbc4..adeab88 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -208,10 +208,10 @@ export class LPF2Hub extends BaseHub { * @param {string} button * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonState.PRESSED); + this.emit("button", { event: Consts.ButtonState.PRESSED }); return; } else if (message[5] === 0) { - this.emit("button", "GREEN", Consts.ButtonState.RELEASED); + this.emit("button", { event: Consts.ButtonState.RELEASED }); return; } @@ -229,7 +229,7 @@ export class LPF2Hub extends BaseHub { const rssi = message.readInt8(5); if (rssi !== 0) { this._rssi = rssi; - this.emit("rssiChange", this._rssi); + this.emit("rssi", { rssi: this._rssi }); } // primary MAC Address @@ -241,7 +241,7 @@ export class LPF2Hub extends BaseHub { const batteryLevel = message[5]; if (batteryLevel !== this._batteryLevel) { this._batteryLevel = batteryLevel; - this.emit("batteryLevel", batteryLevel); + this.emit("batteryLevel", { batteryLevel }); } } diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index a164874..1da11d1 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -168,7 +168,7 @@ export class WeDo2SmartHub extends BaseHub { const batteryLevel = data[0]; if (batteryLevel !== this._batteryLevel) { this._batteryLevel = batteryLevel; - this.emit("batteryLevel", batteryLevel); + this.emit("batteryLevel", { batteryLevel }); } } @@ -209,10 +209,10 @@ export class WeDo2SmartHub extends BaseHub { * @param {string} button * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonState.PRESSED); + this.emit("button", { event: Consts.ButtonState.PRESSED }); return; } else if (message[0] === 0x00) { - this.emit("button", "GREEN", Consts.ButtonState.RELEASED); + this.emit("button", { event: Consts.ButtonState.RELEASED }); return; }