Virtual port attachment and detachment
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Nathan Kellenicki 2020-01-08 15:37:07 -08:00
parent 875293a742
commit bf721eddf6
4 changed files with 27 additions and 25 deletions

View File

@ -28,6 +28,7 @@ export class Device extends EventEmitter {
this._type = type; this._type = type;
this._modeMap = modeMap; this._modeMap = modeMap;
this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB);
this._isVirtualPort = this.hub.isPortVirtual(portId);
const eventAttachListener = (event: string) => { const eventAttachListener = (event: string) => {
if (event === "detach") { if (event === "detach") {
@ -87,6 +88,10 @@ export class Device extends EventEmitter {
return this._isWeDo2SmartHub; return this._isWeDo2SmartHub;
} }
protected get isVirtualPort () {
return this._isVirtualPort;
}
public writeDirect (mode: number, data: Buffer, callback?: () => void) { public writeDirect (mode: number, data: Buffer, callback?: () => void) {
if (this.isWeDo2SmartHub) { if (this.isWeDo2SmartHub) {
this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);

View File

@ -50,11 +50,11 @@ export class BaseHub extends EventEmitter {
protected _batteryLevel: number = 100; protected _batteryLevel: number = 100;
protected _rssi: number = -60; protected _rssi: number = -60;
protected _portMap: {[portName: string]: number} = {}; protected _portMap: {[portName: string]: number} = {};
protected _virtualPorts: number[] = [];
protected _bleDevice: IBLEAbstraction; protected _bleDevice: IBLEAbstraction;
private _type: Consts.HubType; private _type: Consts.HubType;
private _virtualPorts: number[] = [];
private _attachCallbacks: Array<((device: Device) => boolean)> = []; private _attachCallbacks: Array<((device: Device) => boolean)> = [];
constructor (bleDevice: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { constructor (bleDevice: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) {
@ -62,7 +62,7 @@ export class BaseHub extends EventEmitter {
this.setMaxListeners(23); // Technic Medium Hub has 9 built in devices + 4 external ports. Node.js throws a warning after 10 attached event listeners. this.setMaxListeners(23); // Technic Medium Hub has 9 built in devices + 4 external ports. Node.js throws a warning after 10 attached event listeners.
this._type = type; this._type = type;
this._bleDevice = bleDevice; this._bleDevice = bleDevice;
this._portMap = portMap; this._portMap = Object.assign({}, portMap);
bleDevice.on("disconnect", () => { bleDevice.on("disconnect", () => {
/** /**
* Emits when the hub is disconnected. * Emits when the hub is disconnected.

View File

@ -273,31 +273,27 @@ export class LPF2Hub extends BaseHub {
const device = this._getDeviceByPortId(portId); const device = this._getDeviceByPortId(portId);
if (device) { if (device) {
this._detachDevice(device); this._detachDevice(device);
if (this.isPortVirtual(portId)) {
const portName = this.getPortNameForPortId(portId);
if (portName) {
delete this._portMap[portName];
}
this._virtualPorts = this._virtualPorts.filter((virtualPortId) => virtualPortId !== portId);
} }
} }
// let port = this._getPortForPortNumber(data[3]); // Handle virtual port creation
} else if (event === 0x02) {
// if (!port) { const firstPortName = this.getPortNameForPortId(message[7]);
// if (data[4] === 0x02) { const secondPortName = this.getPortNameForPortId(message[8]);
// const portA = this._getPortForPortNumber(data[7]); // @ts-ignore NK These should never be undefined
// const portB = this._getPortForPortNumber(data[8]); const virtualPortName = firstPortName + secondPortName;
// if (portA && portB) { const virtualPortId = message[3];
// this._virtualPorts[`${portA.id}${portB.id}`] = new Port(`${portA.id}${portB.id}`, data[3]); this._portMap[virtualPortName] = virtualPortId;
// port = this._getPortForPortNumber(data[3]); this._virtualPorts.push(virtualPortId);
// if (port) { const device = this._createDevice(deviceType, virtualPortId);
// port.connected = true; this._attachDevice(device);
// this._registerDeviceAttachment(port, deviceType); }
// } else {
// return;
// }
// } else {
// return;
// }
// } else {
// return;
// }
// }
} }

View File

@ -21,4 +21,5 @@ export interface IDeviceInterface extends EventEmitter {
getPortNameForPortId: (portId: number) => string | undefined; getPortNameForPortId: (portId: number) => string | undefined;
send: (message: Buffer, uuid: string, callback?: () => void) => void; send: (message: Buffer, uuid: string, callback?: () => void) => void;
subscribe: (portId: number, deviceType: number, mode: number) => void; subscribe: (portId: number, deviceType: number, mode: number) => void;
isPortVirtual: (portId: number) => boolean;
} }