@@ -187,6 +187,7 @@ export class Device extends EventEmitter {
187
187
nextCommand . state = Consts . CommandFeedback . EXECUTION_BUSY ;
188
188
debug ( "sleep command " , nextCommand . duration ) ;
189
189
setTimeout ( ( ) => {
190
+ if ( nextCommand . state !== Consts . CommandFeedback . EXECUTION_BUSY ) return ;
190
191
const command = this . _nextPortOutputCommands . shift ( ) ;
191
192
if ( command ) command . resolve ( Consts . CommandFeedback . EXECUTION_COMPLETED ) ;
192
193
this . transmitNextPortOutputCommand ( ) ;
@@ -196,13 +197,15 @@ export class Device extends EventEmitter {
196
197
}
197
198
if ( this . _bufferLength !== this . _transmittedPortOutputCommands . length ) return ;
198
199
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
206
209
// one could start a timer here to ensure finish function is called
207
210
}
208
211
}
@@ -215,13 +218,15 @@ export class Device extends EventEmitter {
215
218
}
216
219
const command = new PortOutputCommand ( data , interrupt ) ;
217
220
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 ) ;
223
227
}
224
- this . transmitNextPortOutputCommand ( ) ;
228
+ this . _nextPortOutputCommands . push ( command ) ;
229
+ process . nextTick ( ( ) => this . transmitNextPortOutputCommand ( ) ) ;
225
230
return command . promise ;
226
231
}
227
232
0 commit comments