@@ -45,7 +45,7 @@ export class MonitorServiceImpl implements MonitorService {
45
45
protected readonly monitorClientProvider : MonitorClientProvider ;
46
46
47
47
protected client ?: MonitorServiceClient ;
48
- protected connection ?: ClientDuplexStream < StreamingOpenReq , StreamingOpenResp > ;
48
+ protected connection ?: { duplex : ClientDuplexStream < StreamingOpenReq , StreamingOpenResp > , config : MonitorConfig } ;
49
49
50
50
setClient ( client : MonitorServiceClient | undefined ) : void {
51
51
this . client = client ;
@@ -66,20 +66,23 @@ export class MonitorServiceImpl implements MonitorService {
66
66
return Status . ALREADY_CONNECTED ;
67
67
}
68
68
const client = await this . monitorClientProvider . client ;
69
- this . connection = client . streamingOpen ( ) ;
70
- this . connection . on ( 'error' , ( ( error : Error ) => {
69
+ const duplex = client . streamingOpen ( ) ;
70
+ this . connection = { duplex, config } ;
71
+
72
+ duplex . on ( 'error' , ( ( error : Error ) => {
71
73
const monitorError = ErrorWithCode . toMonitorError ( error , config ) ;
72
- if ( monitorError . code === undefined ) {
73
- this . logger . error ( error ) ;
74
- }
75
- ( ( monitorError . code === undefined ? this . disconnect ( ) : Promise . resolve ( ) ) as Promise < any > ) . then ( ( ) => {
74
+ this . disconnect ( ) . then ( ( ) => {
76
75
if ( this . client ) {
77
76
this . client . notifyError ( monitorError ) ;
78
77
}
79
- } )
78
+ if ( monitorError . code === undefined ) {
79
+ // Log the original, unexpected error.
80
+ this . logger . error ( error ) ;
81
+ }
82
+ } ) ;
80
83
} ) . bind ( this ) ) ;
81
84
82
- this . connection . on ( 'data' , ( ( resp : StreamingOpenResp ) => {
85
+ duplex . on ( 'data' , ( ( resp : StreamingOpenResp ) => {
83
86
if ( this . client ) {
84
87
const raw = resp . getData ( ) ;
85
88
const data = typeof raw === 'string' ? raw : new TextDecoder ( 'utf8' ) . decode ( raw ) ;
@@ -99,21 +102,25 @@ export class MonitorServiceImpl implements MonitorService {
99
102
100
103
return new Promise < Status > ( resolve => {
101
104
if ( this . connection ) {
102
- this . connection . write ( req , ( ) => {
103
- this . logger . info ( `<<< Serial monitor connection created for ${ Board . toString ( config . board ) } on port ${ Port . toString ( config . port ) } .` ) ;
105
+ this . connection . duplex . write ( req , ( ) => {
106
+ this . logger . info ( `<<< Serial monitor connection created for ${ Board . toString ( config . board , { useFqbn : false } ) } on port ${ Port . toString ( config . port ) } .` ) ;
104
107
resolve ( Status . OK ) ;
105
108
} ) ;
106
109
return ;
107
110
}
108
- resolve ( Status . NOT_CONNECTED ) ;
111
+ this . disconnect ( ) . then ( ( ) => resolve ( Status . NOT_CONNECTED ) ) ;
109
112
} ) ;
110
113
}
111
114
112
115
async disconnect ( ) : Promise < Status > {
116
+ this . logger . info ( `>>> Disposing monitor connection...` ) ;
113
117
if ( ! this . connection ) {
118
+ this . logger . warn ( `<<< Not connected. Nothing to dispose.` ) ;
114
119
return Status . NOT_CONNECTED ;
115
120
}
116
- this . connection . cancel ( ) ;
121
+ const { duplex, config } = this . connection ;
122
+ duplex . cancel ( ) ;
123
+ this . logger . info ( `<<< Disposed monitor connection for ${ Board . toString ( config . board , { useFqbn : false } ) } on port ${ Port . toString ( config . port ) } .` ) ;
117
124
this . connection = undefined ;
118
125
return Status . OK ;
119
126
}
@@ -126,12 +133,12 @@ export class MonitorServiceImpl implements MonitorService {
126
133
req . setData ( new TextEncoder ( ) . encode ( data ) ) ;
127
134
return new Promise < Status > ( resolve => {
128
135
if ( this . connection ) {
129
- this . connection . write ( req , ( ) => {
136
+ this . connection . duplex . write ( req , ( ) => {
130
137
resolve ( Status . OK ) ;
131
138
} ) ;
132
139
return ;
133
140
}
134
- resolve ( Status . NOT_CONNECTED ) ;
141
+ this . disconnect ( ) . then ( ( ) => resolve ( Status . NOT_CONNECTED ) ) ;
135
142
} ) ;
136
143
}
137
144
0 commit comments