Added functions to wait for port attachments
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Nathan Kellenicki 2019-12-18 10:24:39 -08:00
parent 364089b703
commit de4feb0b89
10 changed files with 44 additions and 104 deletions

View File

@ -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.
*/

View File

@ -61,7 +61,7 @@ export class Device extends EventEmitter {
return this._portId;
}
public get port () {
public get portName () {
return this.hub.getPortNameForPortId(this.portId);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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