diff --git a/src/colordistancesensor.ts b/src/colordistancesensor.ts index 306b892..5506677 100644 --- a/src/colordistancesensor.ts +++ b/src/colordistancesensor.ts @@ -9,15 +9,16 @@ export class ColorDistanceSensor extends Device { super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR); this.on("newListener", (event) => { - switch (event) { - case "color": - this.subscribe(0x00); - break; - case "distance": - this.subscribe(0x01); - break; + if (this.autoSubscribe) { + switch (event) { + case "color": + this.subscribe(0x00); + break; + case "distance": + this.subscribe(0x01); + break; + } } - }); } diff --git a/src/device.ts b/src/device.ts index 8fe202e..4ed700e 100644 --- a/src/device.ts +++ b/src/device.ts @@ -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); } diff --git a/src/hub.ts b/src/hub.ts index 323f53d..36fe735 100644 --- a/src/hub.ts +++ b/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]; }