@@ -19,6 +19,7 @@ import {
19
19
import { MonitorClientProvider } from './monitor-client-provider' ;
20
20
import { Board , Port } from '../../common/protocol/boards-service' ;
21
21
import { WebSocketService } from '../web-socket/web-socket-service' ;
22
+ import { SerialPlotter } from '../../browser/plotter/protocol' ;
22
23
23
24
interface ErrorWithCode extends Error {
24
25
readonly code : number ;
@@ -71,7 +72,7 @@ export class MonitorServiceImpl implements MonitorService {
71
72
protected readonly webSocketService : WebSocketService ;
72
73
73
74
protected client ?: MonitorServiceClient ;
74
- protected connection ?: {
75
+ protected serialConnection ?: {
75
76
duplex : ClientDuplexStream < StreamingOpenRequest , StreamingOpenResponse > ;
76
77
config : MonitorConfig ;
77
78
} ;
@@ -84,20 +85,30 @@ export class MonitorServiceImpl implements MonitorService {
84
85
85
86
dispose ( ) : void {
86
87
this . logger . info ( '>>> Disposing monitor service...' ) ;
87
- if ( this . connection ) {
88
+ if ( this . serialConnection ) {
88
89
this . disconnect ( ) ;
89
90
}
90
91
this . logger . info ( '<<< Disposed monitor service.' ) ;
91
92
this . client = undefined ;
92
93
}
93
94
95
+ async updateWsConfigParam (
96
+ config : Partial < SerialPlotter . Config >
97
+ ) : Promise < void > {
98
+ const msg : SerialPlotter . Protocol . Message = {
99
+ command : SerialPlotter . Protocol . Command . MIDDLEWARE_CONFIG_CHANGED ,
100
+ data : config ,
101
+ } ;
102
+ this . webSocketService . sendMessage ( JSON . stringify ( msg ) ) ;
103
+ }
104
+
94
105
async connect ( config : MonitorConfig ) : Promise < Status > {
95
106
this . logger . info (
96
107
`>>> Creating serial monitor connection for ${ Board . toString (
97
108
config . board
98
109
) } on port ${ Port . toString ( config . port ) } ...`
99
110
) ;
100
- if ( this . connection ) {
111
+ if ( this . serialConnection ) {
101
112
return Status . ALREADY_CONNECTED ;
102
113
}
103
114
const client = await this . monitorClientProvider . client ( ) ;
@@ -108,7 +119,7 @@ export class MonitorServiceImpl implements MonitorService {
108
119
return { message : client . message } ;
109
120
}
110
121
const duplex = client . streamingOpen ( ) ;
111
- this . connection = { duplex, config } ;
122
+ this . serialConnection = { duplex, config } ;
112
123
113
124
duplex . on (
114
125
'error' ,
@@ -137,6 +148,27 @@ export class MonitorServiceImpl implements MonitorService {
137
148
}
138
149
} ;
139
150
151
+ this . webSocketService . onMessageReceived ( ( msg : string ) => {
152
+ try {
153
+ const message : SerialPlotter . Protocol . Message = JSON . parse ( msg ) ;
154
+
155
+ switch ( message . command ) {
156
+ case SerialPlotter . Protocol . Command . PLOTTER_SEND_MESSAGE :
157
+ this . sendMessageToSerial ( message . data ) ;
158
+ break ;
159
+
160
+ case SerialPlotter . Protocol . Command . PLOTTER_SET_BAUDRATE :
161
+ break ;
162
+
163
+ case SerialPlotter . Protocol . Command . PLOTTER_SET_LINE_ENDING :
164
+ break ;
165
+
166
+ default :
167
+ break ;
168
+ }
169
+ } catch ( error ) { }
170
+ } ) ;
171
+
140
172
// empty the queue every 16ms (~60fps)
141
173
setInterval ( flushMessagesToFrontend , 32 ) ;
142
174
@@ -187,8 +219,8 @@ export class MonitorServiceImpl implements MonitorService {
187
219
req . setConfig ( monitorConfig ) ;
188
220
189
221
return new Promise < Status > ( ( resolve ) => {
190
- if ( this . connection ) {
191
- this . connection . duplex . write ( req , ( ) => {
222
+ if ( this . serialConnection ) {
223
+ this . serialConnection . duplex . write ( req , ( ) => {
192
224
this . logger . info (
193
225
`<<< Serial monitor connection created for ${ Board . toString (
194
226
config . board ,
@@ -206,40 +238,40 @@ export class MonitorServiceImpl implements MonitorService {
206
238
async disconnect ( reason ?: MonitorError ) : Promise < Status > {
207
239
try {
208
240
if (
209
- ! this . connection &&
241
+ ! this . serialConnection &&
210
242
reason &&
211
243
reason . code === MonitorError . ErrorCodes . CLIENT_CANCEL
212
244
) {
213
245
return Status . OK ;
214
246
}
215
247
this . logger . info ( '>>> Disposing monitor connection...' ) ;
216
- if ( ! this . connection ) {
248
+ if ( ! this . serialConnection ) {
217
249
this . logger . warn ( '<<< Not connected. Nothing to dispose.' ) ;
218
250
return Status . NOT_CONNECTED ;
219
251
}
220
- const { duplex, config } = this . connection ;
252
+ const { duplex, config } = this . serialConnection ;
221
253
duplex . cancel ( ) ;
222
254
this . logger . info (
223
255
`<<< Disposed monitor connection for ${ Board . toString ( config . board , {
224
256
useFqbn : false ,
225
257
} ) } on port ${ Port . toString ( config . port ) } .`
226
258
) ;
227
- this . connection = undefined ;
259
+ this . serialConnection = undefined ;
228
260
return Status . OK ;
229
261
} finally {
230
262
this . messages . length = 0 ;
231
263
}
232
264
}
233
265
234
- async send ( message : string ) : Promise < Status > {
235
- if ( ! this . connection ) {
266
+ async sendMessageToSerial ( message : string ) : Promise < Status > {
267
+ if ( ! this . serialConnection ) {
236
268
return Status . NOT_CONNECTED ;
237
269
}
238
270
const req = new StreamingOpenRequest ( ) ;
239
271
req . setData ( new TextEncoder ( ) . encode ( message ) ) ;
240
272
return new Promise < Status > ( ( resolve ) => {
241
- if ( this . connection ) {
242
- this . connection . duplex . write ( req , ( ) => {
273
+ if ( this . serialConnection ) {
274
+ this . serialConnection . duplex . write ( req , ( ) => {
243
275
resolve ( Status . OK ) ;
244
276
} ) ;
245
277
return ;
0 commit comments