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);
|
super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
|
||||||
|
|
||||||
this.on("newListener", (event) => {
|
this.on("newListener", (event) => {
|
||||||
|
if (this.autoSubscribe) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case "color":
|
case "color":
|
||||||
this.subscribe(0x00);
|
this.subscribe(0x00);
|
||||||
@ -17,7 +18,7 @@ export class ColorDistanceSensor extends Device {
|
|||||||
this.subscribe(0x01);
|
this.subscribe(0x01);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import * as Consts from "./consts";
|
|||||||
|
|
||||||
export class Device extends EventEmitter {
|
export class Device extends EventEmitter {
|
||||||
|
|
||||||
|
public autoSubscribe: boolean = true;
|
||||||
|
|
||||||
private _hub: Hub;
|
private _hub: Hub;
|
||||||
private _portId: number;
|
private _portId: number;
|
||||||
private _connected: boolean = true;
|
private _connected: boolean = true;
|
||||||
@ -18,7 +20,7 @@ export class Device extends EventEmitter {
|
|||||||
this._portId = portId;
|
this._portId = portId;
|
||||||
this._type = type;
|
this._type = type;
|
||||||
this.hub.on("detach", (device) => {
|
this.hub.on("detach", (device) => {
|
||||||
if (device.portId === this.portId) {
|
if (device === this) {
|
||||||
this._connected = false;
|
this._connected = false;
|
||||||
this.emit("detach");
|
this.emit("detach");
|
||||||
}
|
}
|
||||||
@ -46,6 +48,9 @@ export class Device extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) {
|
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);
|
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;
|
public useSpeedMap: boolean = true;
|
||||||
protected _type: Consts.HubType = Consts.HubType.UNKNOWN;
|
protected _type: Consts.HubType = Consts.HubType.UNKNOWN;
|
||||||
|
|
||||||
protected _attachedDevices: Device[] = [];
|
protected _attachedDevices: {[portId: number]: Device} = {};
|
||||||
|
|
||||||
protected _portNames: {[port: string]: number} = {};
|
protected _portNames: {[port: string]: number} = {};
|
||||||
// protected _virtualPorts: {[port: string]: Port} = {};
|
// protected _virtualPorts: {[port: string]: Port} = {};
|
||||||
@ -176,7 +176,6 @@ export class Hub extends EventEmitter {
|
|||||||
|
|
||||||
public getPortNameForPortId (portId: number) {
|
public getPortNameForPortId (portId: number) {
|
||||||
for (const port of Object.keys(this._portNames)) {
|
for (const port of Object.keys(this._portNames)) {
|
||||||
console.log(port);
|
|
||||||
if (this._portNames[port] === portId) {
|
if (this._portNames[port] === portId) {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
@ -285,13 +284,7 @@ export class Hub extends EventEmitter {
|
|||||||
|
|
||||||
protected _attachDevice (device: Device) {
|
protected _attachDevice (device: Device) {
|
||||||
|
|
||||||
const exists = this._getDeviceByPortId(device.portId);
|
this._attachedDevices[device.portId] = device;
|
||||||
|
|
||||||
if (exists) {
|
|
||||||
this._attachedDevices.splice(this._attachedDevices.findIndex((attachedDevice) => attachedDevice.portId === device.portId), 1);
|
|
||||||
} else {
|
|
||||||
this._attachedDevices.push(device);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emits when a device is attached to the Hub.
|
* Emits when a device is attached to the Hub.
|
||||||
@ -330,7 +323,7 @@ export class Hub extends EventEmitter {
|
|||||||
|
|
||||||
|
|
||||||
protected _detachDevice (device: Device) {
|
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.
|
* Emits when a device is detached from the Hub.
|
||||||
* @event Hub#attach
|
* @event Hub#attach
|
||||||
@ -341,7 +334,7 @@ export class Hub extends EventEmitter {
|
|||||||
|
|
||||||
|
|
||||||
protected _getDeviceByPortId (portId: number) {
|
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