Skip to content

Commit

Permalink
change device mode after feedback only
Browse files Browse the repository at this point in the history
device._mode is used to interpret incoming port value messages.
When changing the mode on a hub, the recieved port values are for
the old mode until the port input format message with the new mode
has arrived.
  • Loading branch information
Debenben committed May 29, 2022
1 parent e4c68e5 commit 35f91dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/devices/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export class Device extends EventEmitter {
public subscribe (mode: number) {
this._ensureConnected();
if (mode !== this._mode) {
this._mode = mode;
this.hub.subscribe(this.portId, this.type, mode);
}
}
Expand Down Expand Up @@ -178,6 +177,10 @@ export class Device extends EventEmitter {
}
}

public setMode (message: number) {
this._mode = message;
}

public setEventTimer (timer: NodeJS.Timer) {
this._eventTimer = timer;
}
Expand Down
17 changes: 12 additions & 5 deletions src/hubs/lpf2hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ export class LPF2Hub extends BaseHub {
this._parseSensorMessage(message);
break;
}
case Consts.MessageType.PORT_INPUT_FORMAT_SINGLE: {
this._parsePortInputFormatMessage(message);
break;
}
case Consts.MessageType.PORT_OUTPUT_COMMAND_FEEDBACK: {
this._parsePortAction(message);
break;
}
}
}

if (this._messageBuffer.length > 0) {
Expand Down Expand Up @@ -346,7 +350,6 @@ export class LPF2Hub extends BaseHub {
}
}


private _parsePortAction (message: Buffer) {
for (let offset = 3; offset < message.length; offset += 2) {
const device = this._getDeviceByPortId(message[offset]);
Expand All @@ -357,17 +360,21 @@ export class LPF2Hub extends BaseHub {
}
}


private _parseSensorMessage (message: Buffer) {

const portId = message[3];
const device = this._getDeviceByPortId(portId);

if (device) {
device.receive(message);
}

}

private _parsePortInputFormatMessage (message: Buffer) {
const portId = message[3];
const device = this._getDeviceByPortId(portId);

if (device) {
device.setMode(message[4]);
}
}
}

0 comments on commit 35f91dd

Please sign in to comment.