diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index cbf5de8..f952e63 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -21,7 +21,6 @@ export class ColorDistanceSensor extends Device { /** * Emits when a color sensor is activated. * @event ColorDistanceSensor#color - * @param {string} port * @param {Color} color */ this.emit("color", color); @@ -38,7 +37,6 @@ export class ColorDistanceSensor extends Device { /** * Emits when a distance sensor is activated. * @event ColorDistanceSensor#distance - * @param {string} port * @param {number} distance Distance, in millimeters. */ this.emit("distance", distance); @@ -62,7 +60,6 @@ export class ColorDistanceSensor extends Device { /** * A combined color and distance event, emits when the sensor is activated. * @event ColorDistanceSensor#colorAndDistance - * @param {string} port * @param {Color} color * @param {number} distance Distance, in millimeters. */ diff --git a/src/devices/device.ts b/src/devices/device.ts index 91f11aa..20664d2 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -61,7 +61,7 @@ export class Device extends EventEmitter { return this._portId; } - public get port () { + public get portName () { return this.hub.getPortNameForPortId(this.portId); } diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 1ad7572..0a88b84 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -61,7 +61,7 @@ export class BoostMoveHub extends LPF2Hub { export namespace BoostMoveHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 0, "B": 1, "C": 2, diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index 54a849b..b088d1d 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -116,7 +116,7 @@ export class ControlPlusHub extends LPF2Hub { export namespace ControlPlusHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 0, "B": 1, "C": 2, diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index 3d3399b..fd8d529 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -71,7 +71,7 @@ export class DuploTrainBase extends LPF2Hub { export namespace DuploTrainBase { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "MOTOR": 0, "COLOR": 18, "SPEEDOMETER": 19 diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 04d7d44..a063d97 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -27,7 +27,7 @@ const debug = Debug("hub"); export class Hub extends EventEmitter { protected _attachedDevices: {[portId: number]: Device} = {}; - // protected _virtualPorts: {[port: string]: Port} = {}; + // protected _virtualPorts: {[portName: string]: Port} = {}; protected _name: string = ""; protected _firmwareVersion: string = "0.0.00.0000"; @@ -41,9 +41,10 @@ export class Hub extends EventEmitter { protected _bleDevice: IBLEAbstraction; private _type: Consts.HubType; - private _portMap: {[port: string]: number} = {}; + private _portMap: {[portName: string]: number} = {}; + private _attachCallbacks: ((device: Device) => void)[] = []; - constructor (device: IBLEAbstraction, portMap: {[port: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { + constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { super(); this._type = type; this._bleDevice = device; @@ -196,6 +197,21 @@ export class Hub extends EventEmitter { } + public waitForDeviceAtPort (portName: string) { + return new Promise((resolve) => { + const existingDevice = this.getDeviceAtPort(portName); + if (existingDevice) { + return resolve(existingDevice); + } + this._attachCallbacks.push((device) => { + if (device.portName === portName) { + return resolve(device); + } + }); + }); + } + + public getDevices () { return Object.values(this._attachedDevices); } @@ -206,6 +222,21 @@ export class Hub extends EventEmitter { } + public waitForDeviceByType (deviceType: number) { + return new Promise((resolve) => { + const existingDevices = this.getDevicesByType(deviceType); + if (existingDevices.length >= 1) { + return resolve(existingDevices[0]); + } + this._attachCallbacks.push((device) => { + if (device.type === deviceType) { + return resolve(device); + } + }) + }); + } + + public getPortNameForPortId (portId: number) { for (const port of Object.keys(this._portMap)) { if (this._portMap[port] === portId) { @@ -264,6 +295,9 @@ export class Hub extends EventEmitter { * @param {Device} device */ this.emit("attach", device); + this._attachCallbacks.forEach((callback) => { + callback(device); + }); } diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 70bcdc1..4975dd9 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -442,97 +442,6 @@ export class LPF2Hub extends Hub { // if (port && port.connected) { // switch (port.type) { - // case Consts.DeviceType.WEDO2_DISTANCE: { - // let distance = data[4]; - // if (data[5] === 1) { - // distance = data[4] + 255; - // } - // /** - // * Emits when a distance sensor is activated. - // * @event LPF2Hub#distance - // * @param {string} port - // * @param {number} distance Distance, in millimeters. - // */ - // this.emit("distance", port.id, distance * 10); - // break; - // } - // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { - - // /** - // * Emits when a color sensor is activated. - // * @event LPF2Hub#color - // * @param {string} port - // * @param {Color} color - // */ - // if (data[4] <= 10) { - // this.emit("color", port.id, data[4]); - // } - - // let distance = data[5]; - // const partial = data[7]; - - // if (partial > 0) { - // distance += 1.0 / partial; - // } - - // distance = Math.floor(distance * 25.4) - 20; - - // this.emit("distance", port.id, distance); - - // /** - // * A combined color and distance event, emits when the sensor is activated. - // * @event LPF2Hub#colorAndDistance - // * @param {string} port - // * @param {Color} color - // * @param {number} distance Distance, in millimeters. - // */ - // if (data[4] <= 10) { - // this.emit("colorAndDistance", port.id, data[4], distance); - // } - // break; - // } - // case Consts.DeviceType.WEDO2_TILT: { - // const tiltX = data.readInt8(4); - // const tiltY = data.readInt8(5); - // this._lastTiltX = tiltX; - // this._lastTiltY = tiltY; - // /** - // * Emits when a tilt sensor is activated. - // * @event LPF2Hub#tilt - // * @param {string} port If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used. - // * @param {number} x - // * @param {number} y - // * @param {number} z (Only available when using a Control+ Hub) - // */ - // this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ); - // break; - // } - // case Consts.DeviceType.BOOST_TACHO_MOTOR: { - // const rotation = data.readInt32LE(4); - // /** - // * Emits when a rotation sensor is activated. - // * @event LPF2Hub#rotate - // * @param {string} port - // * @param {number} rotation - // */ - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: { - // const rotation = data.readInt32LE(4); - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: { - // const rotation = data.readInt32LE(4); - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: { - // const rotation = data.readInt32LE(4); - // this.emit("rotate", port.id, rotation); - // break; - // } // case Consts.DeviceType.CONTROL_PLUS_TILT: { // const tiltZ = data.readInt16LE(4); // const tiltY = data.readInt16LE(6); diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts index dfb4673..cd66c53 100644 --- a/src/hubs/puphub.ts +++ b/src/hubs/puphub.ts @@ -61,7 +61,7 @@ export class PUPHub extends LPF2Hub { export namespace PUPHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 0, "B": 1 } diff --git a/src/hubs/pupremote.ts b/src/hubs/pupremote.ts index 64b9550..96b93bf 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/pupremote.ts @@ -57,7 +57,7 @@ export class PUPRemote extends LPF2Hub { export namespace PUPRemote { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "LEFT": 0, "RIGHT": 1 } diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index ce085e7..7cd607a 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -352,7 +352,7 @@ export class WeDo2SmartHub extends Hub { export namespace WeDo2SmartHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 1, "B": 2 }