1
- import { CommandRegistry , Emitter , MessageService } from '@theia/core' ;
1
+ import {
2
+ CommandRegistry ,
3
+ Disposable ,
4
+ Emitter ,
5
+ MessageService ,
6
+ } from '@theia/core' ;
2
7
import { inject , injectable } from '@theia/core/shared/inversify' ;
3
8
import { Board , Port } from '../common/protocol' ;
4
9
import {
@@ -11,8 +16,7 @@ import {
11
16
MonitorSettings ,
12
17
} from '../node/monitor-settings/monitor-settings-provider' ;
13
18
import { BoardsConfig } from './boards/boards-config' ;
14
- import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution' ;
15
- import { SerialPlotterContribution } from './serial/plotter/plotter-frontend-contribution' ;
19
+ import { BoardsServiceProvider } from './boards/boards-service-provider' ;
16
20
17
21
@injectable ( )
18
22
export class MonitorManagerProxyClientImpl
@@ -32,11 +36,15 @@ export class MonitorManagerProxyClientImpl
32
36
readonly onMonitorSettingsDidChange =
33
37
this . onMonitorSettingsDidChangeEmitter . event ;
34
38
39
+ protected readonly onMonitorShouldResetEmitter = new Emitter ( ) ;
40
+ readonly onMonitorShouldReset = this . onMonitorShouldResetEmitter . event ;
41
+
35
42
// WebSocket used to handle pluggable monitor communication between
36
43
// frontend and backend.
37
44
private webSocket ?: WebSocket ;
38
45
private wsPort ?: number ;
39
46
private lastConnectedBoard : BoardsConfig . Config ;
47
+ private onBoardsConfigChanged : Disposable | undefined ;
40
48
41
49
getWebSocketPort ( ) : number | undefined {
42
50
return this . wsPort ;
@@ -51,7 +59,10 @@ export class MonitorManagerProxyClientImpl
51
59
protected server : MonitorManagerProxyFactory ,
52
60
53
61
@inject ( CommandRegistry )
54
- protected readonly commandRegistry : CommandRegistry
62
+ protected readonly commandRegistry : CommandRegistry ,
63
+
64
+ @inject ( BoardsServiceProvider )
65
+ protected readonly boardsServiceProvider : BoardsServiceProvider
55
66
) { }
56
67
57
68
/**
@@ -89,6 +100,8 @@ export class MonitorManagerProxyClientImpl
89
100
*/
90
101
disconnect ( ) : void {
91
102
if ( ! this . webSocket ) return ;
103
+ this . onBoardsConfigChanged ?. dispose ( ) ;
104
+ this . onBoardsConfigChanged = undefined ;
92
105
try {
93
106
this . webSocket ?. close ( ) ;
94
107
this . webSocket = undefined ;
@@ -101,27 +114,46 @@ export class MonitorManagerProxyClientImpl
101
114
return ! ! this . webSocket ;
102
115
}
103
116
104
- async startMonitor (
105
- board : Board ,
106
- port : Port ,
107
- settings ?: PluggableMonitorSettings
108
- ) : Promise < void > {
109
- await this . server ( ) . startMonitor ( board , port , settings ) ;
110
- if (
111
- board . fqbn !== this . lastConnectedBoard ?. selectedBoard ?. fqbn ||
112
- port . id !== this . lastConnectedBoard ?. selectedPort ?. id
113
- ) {
114
- await this . commandRegistry . executeCommand (
115
- MonitorViewContribution . RESET_SERIAL_MONITOR
116
- ) ;
117
- await this . commandRegistry . executeCommand (
118
- SerialPlotterContribution . Commands . RESET . id
119
- ) ;
120
- }
117
+ async startMonitor ( settings ?: PluggableMonitorSettings ) : Promise < void > {
121
118
this . lastConnectedBoard = {
122
- selectedBoard : board ,
123
- selectedPort : port ,
119
+ selectedBoard : this . boardsServiceProvider . boardsConfig . selectedBoard ,
120
+ selectedPort : this . boardsServiceProvider . boardsConfig . selectedPort ,
124
121
} ;
122
+
123
+ if ( ! this . onBoardsConfigChanged ) {
124
+ this . onBoardsConfigChanged =
125
+ this . boardsServiceProvider . onBoardsConfigChanged (
126
+ async ( { selectedBoard, selectedPort } ) => {
127
+ if (
128
+ typeof selectedBoard === 'undefined' ||
129
+ typeof selectedPort === 'undefined'
130
+ )
131
+ return ;
132
+
133
+ // a board is plugged and it's different from the old connected board
134
+ if (
135
+ selectedBoard ?. fqbn !==
136
+ this . lastConnectedBoard ?. selectedBoard ?. fqbn ||
137
+ selectedPort ?. id !== this . lastConnectedBoard ?. selectedPort ?. id
138
+ ) {
139
+ this . onMonitorShouldResetEmitter . fire ( null ) ;
140
+ this . lastConnectedBoard = {
141
+ selectedBoard : selectedBoard ,
142
+ selectedPort : selectedPort ,
143
+ } ;
144
+ } else {
145
+ // a board is plugged and it's the same as prev, rerun "this.startMonitor" to
146
+ // recreate the listener callback
147
+ this . startMonitor ( ) ;
148
+ }
149
+ }
150
+ ) ;
151
+ }
152
+
153
+ const { selectedBoard, selectedPort } =
154
+ this . boardsServiceProvider . boardsConfig ;
155
+ if ( ! selectedBoard || ! selectedBoard . fqbn || ! selectedPort ) return ;
156
+ await this . server ( ) . startMonitor ( selectedBoard , selectedPort , settings ) ;
125
157
}
126
158
127
159
getCurrentSettings ( board : Board , port : Port ) : Promise < MonitorSettings > {
0 commit comments