attachedDevices now a map
This commit is contained in:
parent
4af1d3d69b
commit
8ffc60b924
@ -9,6 +9,7 @@ export class ColorDistanceSensor extends Device {
|
||||
super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
|
||||
|
||||
this.on("newListener", (event) => {
|
||||
if (this.autoSubscribe) {
|
||||
switch (event) {
|
||||
case "color":
|
||||
this.subscribe(0x00);
|
||||
@ -17,7 +18,7 @@ export class ColorDistanceSensor extends Device {
|
||||
this.subscribe(0x01);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import * as Consts from "./consts";
|
||||
|
||||
export class Device extends EventEmitter {
|
||||
|
||||
public autoSubscribe: boolean = true;
|
||||
|
||||
private _hub: Hub;
|
||||
private _portId: number;
|
||||
private _connected: boolean = true;
|
||||
@ -18,7 +20,7 @@ export class Device extends EventEmitter {
|
||||
this._portId = portId;
|
||||
this._type = type;
|
||||
this.hub.on("detach", (device) => {
|
||||
if (device.portId === this.portId) {
|
||||
if (device === this) {
|
||||
this._connected = false;
|
||||
this.emit("detach");
|
||||
}
|
||||
@ -46,6 +48,9 @@ export class Device extends EventEmitter {
|
||||
}
|
||||
|
||||
public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) {
|
||||
if (!this.connected) {
|
||||
throw new Error("Device is not connected");
|
||||
}
|
||||
this.hub.send(data, characteristic, callback);
|
||||
}
|
||||
|
||||
|
15
src/hub.ts
15
src/hub.ts
@ -21,7 +21,7 @@ export class Hub extends EventEmitter {
|
||||
public useSpeedMap: boolean = true;
|
||||
protected _type: Consts.HubType = Consts.HubType.UNKNOWN;
|
||||
|
||||
protected _attachedDevices: Device[] = [];
|
||||
protected _attachedDevices: {[portId: number]: Device} = {};
|
||||
|
||||
protected _portNames: {[port: string]: number} = {};
|
||||
// protected _virtualPorts: {[port: string]: Port} = {};
|
||||
@ -176,7 +176,6 @@ export class Hub extends EventEmitter {
|
||||
|
||||
public getPortNameForPortId (portId: number) {
|
||||
for (const port of Object.keys(this._portNames)) {
|
||||
console.log(port);
|
||||
if (this._portNames[port] === portId) {
|
||||
return port;
|
||||
}
|
||||
@ -285,13 +284,7 @@ export class Hub extends EventEmitter {
|
||||
|
||||
protected _attachDevice (device: Device) {
|
||||
|
||||
const exists = this._getDeviceByPortId(device.portId);
|
||||
|
||||
if (exists) {
|
||||
this._attachedDevices.splice(this._attachedDevices.findIndex((attachedDevice) => attachedDevice.portId === device.portId), 1);
|
||||
} else {
|
||||
this._attachedDevices.push(device);
|
||||
}
|
||||
this._attachedDevices[device.portId] = device;
|
||||
|
||||
/**
|
||||
* Emits when a device is attached to the Hub.
|
||||
@ -330,7 +323,7 @@ export class Hub extends EventEmitter {
|
||||
|
||||
|
||||
protected _detachDevice (device: Device) {
|
||||
this._attachedDevices.splice(this._attachedDevices.findIndex((attachedDevice) => attachedDevice.portId === device.portId), 1);
|
||||
delete this._attachedDevices[device.portId];
|
||||
/**
|
||||
* Emits when a device is detached from the Hub.
|
||||
* @event Hub#attach
|
||||
@ -341,7 +334,7 @@ export class Hub extends EventEmitter {
|
||||
|
||||
|
||||
protected _getDeviceByPortId (portId: number) {
|
||||
return this._attachedDevices.find((attachedDevice) => attachedDevice.portId === portId);
|
||||
return this._attachedDevices[portId];
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user