@@ -63,16 +63,6 @@ namespace ErrorWithCode {
63
63
64
64
@injectable ( )
65
65
export class SerialServiceImpl implements SerialService {
66
- @named ( SerialServiceName )
67
- @inject ( ILogger )
68
- protected readonly logger : ILogger ;
69
-
70
- @inject ( MonitorClientProvider )
71
- protected readonly serialClientProvider : MonitorClientProvider ;
72
-
73
- @inject ( WebSocketService )
74
- protected readonly webSocketService : WebSocketService ;
75
-
76
66
protected theiaFEClient ?: SerialServiceClient ;
77
67
protected serialConfig ?: SerialConfig ;
78
68
@@ -88,6 +78,18 @@ export class SerialServiceImpl implements SerialService {
88
78
89
79
uploadInProgress = false ;
90
80
81
+ constructor (
82
+ @inject ( ILogger )
83
+ @named ( SerialServiceName )
84
+ protected readonly logger : ILogger ,
85
+
86
+ @inject ( MonitorClientProvider )
87
+ protected readonly serialClientProvider : MonitorClientProvider ,
88
+
89
+ @inject ( WebSocketService )
90
+ protected readonly webSocketService : WebSocketService
91
+ ) { }
92
+
91
93
async isSerialPortOpen ( ) : Promise < boolean > {
92
94
return ! ! this . serialConnection ;
93
95
}
@@ -115,7 +117,6 @@ export class SerialServiceImpl implements SerialService {
115
117
public async connectSerialIfRequired ( ) : Promise < void > {
116
118
if ( this . uploadInProgress ) return ;
117
119
const clients = await this . clientsAttached ( ) ;
118
- this . logger . info ( `WS clients: ${ clients } ` ) ;
119
120
clients > 0 ? await this . connect ( ) : await this . disconnect ( ) ;
120
121
}
121
122
@@ -144,7 +145,7 @@ export class SerialServiceImpl implements SerialService {
144
145
this . webSocketService . sendMessage ( JSON . stringify ( msg ) ) ;
145
146
}
146
147
147
- async connect ( ) : Promise < Status > {
148
+ private async connect ( ) : Promise < Status > {
148
149
if ( ! this . serialConfig ) {
149
150
return Status . CONFIG_MISSING ;
150
151
}
@@ -155,8 +156,6 @@ export class SerialServiceImpl implements SerialService {
155
156
) } on port ${ Port . toString ( this . serialConfig . port ) } ...`
156
157
) ;
157
158
158
- // check if the board/port is available
159
-
160
159
if ( this . serialConnection ) {
161
160
return Status . ALREADY_CONNECTED ;
162
161
}
@@ -187,7 +186,6 @@ export class SerialServiceImpl implements SerialService {
187
186
// Log the original, unexpected error.
188
187
this . logger . error ( error ) ;
189
188
}
190
- // });
191
189
} ) . bind ( this )
192
190
) ;
193
191
@@ -259,9 +257,19 @@ export class SerialServiceImpl implements SerialService {
259
257
}
260
258
req . setConfig ( monitorConfig ) ;
261
259
262
- return new Promise < Status > ( ( resolve ) => {
263
- if ( this . serialConnection ) {
264
- this . serialConnection . duplex . write ( req , ( ) => {
260
+ if ( ! this . serialConnection ) {
261
+ return await this . disconnect ( ) ;
262
+ }
263
+
264
+ const writeTimeout = new Promise < Status > ( ( resolve ) => {
265
+ setTimeout ( async ( ) => {
266
+ resolve ( Status . NOT_CONNECTED ) ;
267
+ } , 1000 ) ;
268
+ } ) ;
269
+
270
+ const writePromise = ( serialConnection : any ) => {
271
+ return new Promise < Status > ( ( resolve ) => {
272
+ serialConnection . duplex . write ( req , ( ) => {
265
273
const boardName = this . serialConfig ?. board
266
274
? Board . toString ( this . serialConfig . board , {
267
275
useFqbn : false ,
@@ -276,14 +284,23 @@ export class SerialServiceImpl implements SerialService {
276
284
) ;
277
285
resolve ( Status . OK ) ;
278
286
} ) ;
279
- return ;
280
- }
281
- this . disconnect ( ) . then ( ( ) => resolve ( Status . NOT_CONNECTED ) ) ;
282
- } ) ;
287
+ } ) ;
288
+ } ;
289
+
290
+ const status = await Promise . race ( [
291
+ writeTimeout ,
292
+ writePromise ( this . serialConnection ) ,
293
+ ] ) ;
294
+
295
+ if ( status === Status . NOT_CONNECTED ) {
296
+ this . disconnect ( ) ;
297
+ }
298
+
299
+ return status ;
283
300
}
284
301
285
302
public async disconnect ( reason ?: SerialError ) : Promise < Status > {
286
- return new Promise < Status > ( ( resolve , reject ) => {
303
+ return new Promise < Status > ( ( resolve ) => {
287
304
try {
288
305
if ( this . onMessageReceived ) {
289
306
this . onMessageReceived . dispose ( ) ;
@@ -299,12 +316,14 @@ export class SerialServiceImpl implements SerialService {
299
316
reason &&
300
317
reason . code === SerialError . ErrorCodes . CLIENT_CANCEL
301
318
) {
302
- return Status . OK ;
319
+ resolve ( Status . OK ) ;
320
+ return ;
303
321
}
304
322
this . logger . info ( '>>> Disposing serial connection...' ) ;
305
323
if ( ! this . serialConnection ) {
306
324
this . logger . warn ( '<<< Not connected. Nothing to dispose.' ) ;
307
- return Status . NOT_CONNECTED ;
325
+ resolve ( Status . NOT_CONNECTED ) ;
326
+ return ;
308
327
}
309
328
const { duplex, config } = this . serialConnection ;
310
329
0 commit comments