Skip to content

Commit 3f8a6fa

Browse files
committed
bugfix: wait for send before pushing to transmitted
feedback and interrupts must not resolve commands which are being transmitted
1 parent 8b205db commit 3f8a6fa

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/devices/device.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export class Device extends EventEmitter {
187187
nextCommand.state = Consts.CommandFeedback.EXECUTION_BUSY;
188188
debug("sleep command ", nextCommand.duration);
189189
setTimeout(() => {
190+
if(nextCommand.state !== Consts.CommandFeedback.EXECUTION_BUSY) return;
190191
const command = this._nextPortOutputCommands.shift();
191192
if(command) command.resolve(Consts.CommandFeedback.EXECUTION_COMPLETED);
192193
this.transmitNextPortOutputCommand();
@@ -196,13 +197,15 @@ export class Device extends EventEmitter {
196197
}
197198
if(this._bufferLength !== this._transmittedPortOutputCommands.length) return;
198199
if(this._bufferLength < 2 || nextCommand.interrupt) {
199-
const command = this._nextPortOutputCommands.shift();
200-
if(command) {
201-
debug("transmit command ", command.startupAndCompletion, command.data);
202-
this.send(Buffer.concat([Buffer.from([0x81, this.portId, command.startupAndCompletion]), command.data]));
203-
command.state = Consts.CommandFeedback.TRANSMISSION_BUSY;
204-
this._transmittedPortOutputCommands.push(command);
205-
this.transmitNextPortOutputCommand(); // if PortOutputSleep this starts timeout
200+
if(nextCommand.state === Consts.CommandFeedback.TRANSMISSION_PENDING) {
201+
nextCommand.state = Consts.CommandFeedback.TRANSMISSION_BUSY;
202+
debug("transmit command ", nextCommand.startupAndCompletion, nextCommand.data);
203+
this.send(Buffer.concat([Buffer.from([0x81, this.portId, nextCommand.startupAndCompletion]), nextCommand.data])).then(() => {
204+
if(nextCommand.state !== Consts.CommandFeedback.TRANSMISSION_BUSY) return;
205+
const command = this._nextPortOutputCommands.shift();
206+
if(command instanceof PortOutputCommand) this._transmittedPortOutputCommands.push(command);
207+
});
208+
this.transmitNextPortOutputCommand(); // if the next command is PortOutputSleep this starts sleep timeout
206209
// one could start a timer here to ensure finish function is called
207210
}
208211
}
@@ -215,13 +218,15 @@ export class Device extends EventEmitter {
215218
}
216219
const command = new PortOutputCommand(data, interrupt);
217220
if(interrupt) {
218-
this._nextPortOutputCommands.forEach(command => command.resolve(Consts.CommandFeedback.TRANSMISSION_DISCARDED));
219-
this._nextPortOutputCommands = [ command ];
220-
}
221-
else {
222-
this._nextPortOutputCommands.push(command);
221+
this._nextPortOutputCommands.forEach(command => {
222+
if(command.state !== Consts.CommandFeedback.TRANSMISSION_BUSY) {
223+
command.resolve(Consts.CommandFeedback.TRANSMISSION_DISCARDED);
224+
}
225+
});
226+
this._nextPortOutputCommands = this._nextPortOutputCommands.filter(command => command.state === Consts.CommandFeedback.TRANSMISSION_BUSY);
223227
}
224-
this.transmitNextPortOutputCommand();
228+
this._nextPortOutputCommands.push(command);
229+
process.nextTick(() => this.transmitNextPortOutputCommand());
225230
return command.promise;
226231
}
227232

0 commit comments

Comments
 (0)