@@ -63,16 +63,6 @@ namespace ErrorWithCode {
6363
6464@injectable ( )
6565export 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-
7666 protected theiaFEClient ?: SerialServiceClient ;
7767 protected serialConfig ?: SerialConfig ;
7868
@@ -88,6 +78,18 @@ export class SerialServiceImpl implements SerialService {
8878
8979 uploadInProgress = false ;
9080
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+
9193 async isSerialPortOpen ( ) : Promise < boolean > {
9294 return ! ! this . serialConnection ;
9395 }
@@ -115,7 +117,6 @@ export class SerialServiceImpl implements SerialService {
115117 public async connectSerialIfRequired ( ) : Promise < void > {
116118 if ( this . uploadInProgress ) return ;
117119 const clients = await this . clientsAttached ( ) ;
118- this . logger . info ( `WS clients: ${ clients } ` ) ;
119120 clients > 0 ? await this . connect ( ) : await this . disconnect ( ) ;
120121 }
121122
@@ -144,7 +145,7 @@ export class SerialServiceImpl implements SerialService {
144145 this . webSocketService . sendMessage ( JSON . stringify ( msg ) ) ;
145146 }
146147
147- async connect ( ) : Promise < Status > {
148+ private async connect ( ) : Promise < Status > {
148149 if ( ! this . serialConfig ) {
149150 return Status . CONFIG_MISSING ;
150151 }
@@ -155,8 +156,6 @@ export class SerialServiceImpl implements SerialService {
155156 ) } on port ${ Port . toString ( this . serialConfig . port ) } ...`
156157 ) ;
157158
158- // check if the board/port is available
159-
160159 if ( this . serialConnection ) {
161160 return Status . ALREADY_CONNECTED ;
162161 }
@@ -187,7 +186,6 @@ export class SerialServiceImpl implements SerialService {
187186 // Log the original, unexpected error.
188187 this . logger . error ( error ) ;
189188 }
190- // });
191189 } ) . bind ( this )
192190 ) ;
193191
@@ -259,9 +257,19 @@ export class SerialServiceImpl implements SerialService {
259257 }
260258 req . setConfig ( monitorConfig ) ;
261259
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 , ( ) => {
265273 const boardName = this . serialConfig ?. board
266274 ? Board . toString ( this . serialConfig . board , {
267275 useFqbn : false ,
@@ -276,14 +284,23 @@ export class SerialServiceImpl implements SerialService {
276284 ) ;
277285 resolve ( Status . OK ) ;
278286 } ) ;
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 ;
283300 }
284301
285302 public async disconnect ( reason ?: SerialError ) : Promise < Status > {
286- return new Promise < Status > ( ( resolve , reject ) => {
303+ return new Promise < Status > ( ( resolve ) => {
287304 try {
288305 if ( this . onMessageReceived ) {
289306 this . onMessageReceived . dispose ( ) ;
@@ -299,12 +316,14 @@ export class SerialServiceImpl implements SerialService {
299316 reason &&
300317 reason . code === SerialError . ErrorCodes . CLIENT_CANCEL
301318 ) {
302- return Status . OK ;
319+ resolve ( Status . OK ) ;
320+ return ;
303321 }
304322 this . logger . info ( '>>> Disposing serial connection...' ) ;
305323 if ( ! this . serialConnection ) {
306324 this . logger . warn ( '<<< Not connected. Nothing to dispose.' ) ;
307- return Status . NOT_CONNECTED ;
325+ resolve ( Status . NOT_CONNECTED ) ;
326+ return ;
308327 }
309328 const { duplex, config } = this . serialConnection ;
310329
0 commit comments