8
8
MonitorConfig ,
9
9
MonitorError ,
10
10
Status ,
11
+ MonitorServiceClient ,
11
12
} from '../../common/protocol/monitor-service' ;
12
13
import { BoardsServiceProvider } from '../boards/boards-service-provider' ;
13
14
import {
@@ -16,7 +17,6 @@ import {
16
17
BoardsService ,
17
18
AttachedBoardsChangeEvent ,
18
19
} from '../../common/protocol/boards-service' ;
19
- import { MonitorServiceClientImpl } from './monitor-service-client-impl' ;
20
20
import { BoardsConfig } from '../boards/boards-config' ;
21
21
import { MonitorModel } from './monitor-model' ;
22
22
import { NotificationCenter } from '../notification-center' ;
@@ -29,8 +29,8 @@ export class MonitorConnection {
29
29
@inject ( MonitorService )
30
30
protected readonly monitorService : MonitorService ;
31
31
32
- @inject ( MonitorServiceClientImpl )
33
- protected readonly monitorServiceClient : MonitorServiceClientImpl ;
32
+ @inject ( MonitorServiceClient )
33
+ protected readonly monitorServiceClient : MonitorServiceClient ;
34
34
35
35
@inject ( BoardsService )
36
36
protected readonly boardsService : BoardsService ;
@@ -59,7 +59,7 @@ export class MonitorConnection {
59
59
/**
60
60
* This emitter forwards all read events **iff** the connection is established.
61
61
*/
62
- protected readonly onReadEmitter = new Emitter < { message : string } > ( ) ;
62
+ protected readonly onReadEmitter = new Emitter < { messages : string [ ] } > ( ) ;
63
63
64
64
/**
65
65
* Array for storing previous monitor errors received from the server, and based on the number of elements in this array,
@@ -71,112 +71,15 @@ export class MonitorConnection {
71
71
72
72
@postConstruct ( )
73
73
protected init ( ) : void {
74
- this . monitorServiceClient . onError ( async ( error ) => {
75
- let shouldReconnect = false ;
76
- if ( this . state ) {
77
- const { code, config } = error ;
78
- const { board, port } = config ;
79
- const options = { timeout : 3000 } ;
80
- switch ( code ) {
81
- case MonitorError . ErrorCodes . CLIENT_CANCEL : {
82
- console . debug (
83
- `Connection was canceled by client: ${ MonitorConnection . State . toString (
84
- this . state
85
- ) } .`
86
- ) ;
87
- break ;
88
- }
89
- case MonitorError . ErrorCodes . DEVICE_BUSY : {
90
- this . messageService . warn (
91
- `Connection failed. Serial port is busy: ${ Port . toString ( port ) } .` ,
92
- options
93
- ) ;
94
- shouldReconnect = this . autoConnect ;
95
- this . monitorErrors . push ( error ) ;
96
- break ;
97
- }
98
- case MonitorError . ErrorCodes . DEVICE_NOT_CONFIGURED : {
99
- this . messageService . info (
100
- `Disconnected ${ Board . toString ( board , {
101
- useFqbn : false ,
102
- } ) } from ${ Port . toString ( port ) } .`,
103
- options
104
- ) ;
105
- break ;
106
- }
107
- case undefined : {
108
- this . messageService . error (
109
- `Unexpected error. Reconnecting ${ Board . toString (
110
- board
111
- ) } on port ${ Port . toString ( port ) } .`,
112
- options
113
- ) ;
114
- console . error ( JSON . stringify ( error ) ) ;
115
- shouldReconnect = this . connected && this . autoConnect ;
116
- break ;
117
- }
118
- }
119
- const oldState = this . state ;
120
- this . state = undefined ;
121
- this . onConnectionChangedEmitter . fire ( this . state ) ;
122
- if ( shouldReconnect ) {
123
- if ( this . monitorErrors . length >= 10 ) {
124
- this . messageService . warn (
125
- `Failed to reconnect ${ Board . toString ( board , {
126
- useFqbn : false ,
127
- } ) } to the the serial-monitor after 10 consecutive attempts. The ${ Port . toString (
128
- port
129
- ) } serial port is busy. after 10 consecutive attempts.`
130
- ) ;
131
- this . monitorErrors . length = 0 ;
132
- } else {
133
- const attempts = this . monitorErrors . length || 1 ;
134
- if ( this . reconnectTimeout !== undefined ) {
135
- // Clear the previous timer.
136
- window . clearTimeout ( this . reconnectTimeout ) ;
137
- }
138
- const timeout = attempts * 1000 ;
139
- this . messageService . warn (
140
- `Reconnecting ${ Board . toString ( board , {
141
- useFqbn : false ,
142
- } ) } to ${ Port . toString ( port ) } in ${ attempts } seconds...`,
143
- { timeout }
144
- ) ;
145
- this . reconnectTimeout = window . setTimeout (
146
- ( ) => this . connect ( oldState . config ) ,
147
- timeout
148
- ) ;
149
- }
150
- }
151
- }
152
- } ) ;
74
+ this . monitorServiceClient . onMessage ( this . handleMessage . bind ( this ) ) ;
75
+ this . monitorServiceClient . onError ( this . handleError . bind ( this ) ) ;
153
76
this . boardsServiceProvider . onBoardsConfigChanged (
154
77
this . handleBoardConfigChange . bind ( this )
155
78
) ;
156
- this . notificationCenter . onAttachedBoardsChanged ( ( event ) => {
157
- if ( this . autoConnect && this . connected ) {
158
- const { boardsConfig } = this . boardsServiceProvider ;
159
- if (
160
- this . boardsServiceProvider . canUploadTo ( boardsConfig , {
161
- silent : false ,
162
- } )
163
- ) {
164
- const { attached } = AttachedBoardsChangeEvent . diff ( event ) ;
165
- if (
166
- attached . boards . some (
167
- ( board ) =>
168
- ! ! board . port && BoardsConfig . Config . sameAs ( boardsConfig , board )
169
- )
170
- ) {
171
- const { selectedBoard : board , selectedPort : port } = boardsConfig ;
172
- const { baudRate } = this . monitorModel ;
173
- this . disconnect ( ) . then ( ( ) =>
174
- this . connect ( { board, port, baudRate } )
175
- ) ;
176
- }
177
- }
178
- }
179
- } ) ;
79
+ this . notificationCenter . onAttachedBoardsChanged (
80
+ this . handleAttachedBoardsChanged . bind ( this )
81
+ ) ;
82
+
180
83
// Handles the `baudRate` changes by reconnecting if required.
181
84
this . monitorModel . onChange ( ( { property } ) => {
182
85
if ( property === 'baudRate' && this . autoConnect && this . connected ) {
@@ -186,6 +89,14 @@ export class MonitorConnection {
186
89
} ) ;
187
90
}
188
91
92
+ async handleMessage ( port : string ) : Promise < void > {
93
+ const w = new WebSocket ( `ws://localhost:${ port } ` ) ;
94
+ w . onmessage = ( res ) => {
95
+ const messages = JSON . parse ( res . data ) ;
96
+ this . onReadEmitter . fire ( { messages } ) ;
97
+ } ;
98
+ }
99
+
189
100
get connected ( ) : boolean {
190
101
return ! ! this . state ;
191
102
}
@@ -217,6 +128,109 @@ export class MonitorConnection {
217
128
}
218
129
}
219
130
131
+ handleError ( error : MonitorError ) : void {
132
+ let shouldReconnect = false ;
133
+ if ( this . state ) {
134
+ const { code, config } = error ;
135
+ const { board, port } = config ;
136
+ const options = { timeout : 3000 } ;
137
+ switch ( code ) {
138
+ case MonitorError . ErrorCodes . CLIENT_CANCEL : {
139
+ console . debug (
140
+ `Connection was canceled by client: ${ MonitorConnection . State . toString (
141
+ this . state
142
+ ) } .`
143
+ ) ;
144
+ break ;
145
+ }
146
+ case MonitorError . ErrorCodes . DEVICE_BUSY : {
147
+ this . messageService . warn (
148
+ `Connection failed. Serial port is busy: ${ Port . toString ( port ) } .` ,
149
+ options
150
+ ) ;
151
+ shouldReconnect = this . autoConnect ;
152
+ this . monitorErrors . push ( error ) ;
153
+ break ;
154
+ }
155
+ case MonitorError . ErrorCodes . DEVICE_NOT_CONFIGURED : {
156
+ this . messageService . info (
157
+ `Disconnected ${ Board . toString ( board , {
158
+ useFqbn : false ,
159
+ } ) } from ${ Port . toString ( port ) } .`,
160
+ options
161
+ ) ;
162
+ break ;
163
+ }
164
+ case undefined : {
165
+ this . messageService . error (
166
+ `Unexpected error. Reconnecting ${ Board . toString (
167
+ board
168
+ ) } on port ${ Port . toString ( port ) } .`,
169
+ options
170
+ ) ;
171
+ console . error ( JSON . stringify ( error ) ) ;
172
+ shouldReconnect = this . connected && this . autoConnect ;
173
+ break ;
174
+ }
175
+ }
176
+ const oldState = this . state ;
177
+ this . state = undefined ;
178
+ this . onConnectionChangedEmitter . fire ( this . state ) ;
179
+ if ( shouldReconnect ) {
180
+ if ( this . monitorErrors . length >= 10 ) {
181
+ this . messageService . warn (
182
+ `Failed to reconnect ${ Board . toString ( board , {
183
+ useFqbn : false ,
184
+ } ) } to the the serial-monitor after 10 consecutive attempts. The ${ Port . toString (
185
+ port
186
+ ) } serial port is busy. after 10 consecutive attempts.`
187
+ ) ;
188
+ this . monitorErrors . length = 0 ;
189
+ } else {
190
+ const attempts = this . monitorErrors . length || 1 ;
191
+ if ( this . reconnectTimeout !== undefined ) {
192
+ // Clear the previous timer.
193
+ window . clearTimeout ( this . reconnectTimeout ) ;
194
+ }
195
+ const timeout = attempts * 1000 ;
196
+ this . messageService . warn (
197
+ `Reconnecting ${ Board . toString ( board , {
198
+ useFqbn : false ,
199
+ } ) } to ${ Port . toString ( port ) } in ${ attempts } seconds...`,
200
+ { timeout }
201
+ ) ;
202
+ this . reconnectTimeout = window . setTimeout (
203
+ ( ) => this . connect ( oldState . config ) ,
204
+ timeout
205
+ ) ;
206
+ }
207
+ }
208
+ }
209
+ }
210
+
211
+ handleAttachedBoardsChanged ( event : AttachedBoardsChangeEvent ) : void {
212
+ if ( this . autoConnect && this . connected ) {
213
+ const { boardsConfig } = this . boardsServiceProvider ;
214
+ if (
215
+ this . boardsServiceProvider . canUploadTo ( boardsConfig , {
216
+ silent : false ,
217
+ } )
218
+ ) {
219
+ const { attached } = AttachedBoardsChangeEvent . diff ( event ) ;
220
+ if (
221
+ attached . boards . some (
222
+ ( board ) =>
223
+ ! ! board . port && BoardsConfig . Config . sameAs ( boardsConfig , board )
224
+ )
225
+ ) {
226
+ const { selectedBoard : board , selectedPort : port } = boardsConfig ;
227
+ const { baudRate } = this . monitorModel ;
228
+ this . disconnect ( ) . then ( ( ) => this . connect ( { board, port, baudRate } ) ) ;
229
+ }
230
+ }
231
+ }
232
+ }
233
+
220
234
async connect ( config : MonitorConfig ) : Promise < Status > {
221
235
if ( this . connected ) {
222
236
const disconnectStatus = await this . disconnect ( ) ;
@@ -231,15 +245,22 @@ export class MonitorConnection {
231
245
) ;
232
246
const connectStatus = await this . monitorService . connect ( config ) ;
233
247
if ( Status . isOK ( connectStatus ) ) {
248
+ let j = 0 ;
234
249
const requestMessage = ( ) => {
235
- this . monitorService . request ( ) . then ( ( { message } ) => {
250
+ this . monitorService . request ( ) . then ( ( { messages } ) => {
236
251
if ( this . connected ) {
237
- this . onReadEmitter . fire ( { message } ) ;
252
+ // this.onReadEmitter.fire({ messages });
253
+ j += messages . length ;
254
+ if ( j > 1000 ) {
255
+ j = 0 ;
256
+ // console.log(`read more than 1000 messages`);
257
+ }
258
+
238
259
requestMessage ( ) ;
239
260
}
240
261
} ) ;
241
262
} ;
242
- requestMessage ( ) ;
263
+ // requestMessage();
243
264
this . state = { config } ;
244
265
console . info (
245
266
`<<< Serial monitor connection created for ${ Board . toString (
@@ -300,7 +321,7 @@ export class MonitorConnection {
300
321
return this . onConnectionChangedEmitter . event ;
301
322
}
302
323
303
- get onRead ( ) : Event < { message : string } > {
324
+ get onRead ( ) : Event < { messages : string [ ] } > {
304
325
return this . onReadEmitter . event ;
305
326
}
306
327
0 commit comments