1
- import { Emitter , MessageService } from '@theia/core' ;
1
+ import { CommandRegistry , Emitter , MessageService } from '@theia/core' ;
2
2
import { inject , injectable } from '@theia/core/shared/inversify' ;
3
3
import { Board , Port } from '../common/protocol' ;
4
4
import {
@@ -10,6 +10,8 @@ import {
10
10
PluggableMonitorSettings ,
11
11
MonitorSettings ,
12
12
} from '../node/monitor-settings/monitor-settings-provider' ;
13
+ import { BoardsConfig } from './boards/boards-config' ;
14
+ import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution' ;
13
15
14
16
@injectable ( )
15
17
export class MonitorManagerProxyClientImpl
@@ -29,13 +31,11 @@ export class MonitorManagerProxyClientImpl
29
31
readonly onMonitorSettingsDidChange =
30
32
this . onMonitorSettingsDidChangeEmitter . event ;
31
33
32
- protected readonly onWSConnectionChangedEmitter = new Emitter < boolean > ( ) ;
33
- readonly onWSConnectionChanged = this . onWSConnectionChangedEmitter . event ;
34
-
35
34
// WebSocket used to handle pluggable monitor communication between
36
35
// frontend and backend.
37
36
private webSocket ?: WebSocket ;
38
37
private wsPort ?: number ;
38
+ private lastConnectedBoard : BoardsConfig . Config ;
39
39
40
40
getWebSocketPort ( ) : number | undefined {
41
41
return this . wsPort ;
@@ -47,20 +47,20 @@ export class MonitorManagerProxyClientImpl
47
47
48
48
// This is necessary to call the backend methods from the frontend
49
49
@inject ( MonitorManagerProxyFactory )
50
- protected server : MonitorManagerProxyFactory
50
+ protected server : MonitorManagerProxyFactory ,
51
+
52
+ @inject ( CommandRegistry )
53
+ protected readonly commandRegistry : CommandRegistry
51
54
) { }
52
55
53
56
/**
54
57
* Connects a localhost WebSocket using the specified port.
55
58
* @param addressPort port of the WebSocket
56
59
*/
57
- connect ( addressPort : number ) : void {
58
- if ( this . webSocket ) {
59
- return ;
60
- }
60
+ async connect ( addressPort : number ) : Promise < void > {
61
+ if ( ! ! this . webSocket && this . wsPort === addressPort ) return ;
61
62
try {
62
63
this . webSocket = new WebSocket ( `ws://localhost:${ addressPort } ` ) ;
63
- this . onWSConnectionChangedEmitter . fire ( true ) ;
64
64
} catch {
65
65
this . messageService . error ( 'Unable to connect to websocket' ) ;
66
66
return ;
@@ -87,7 +87,6 @@ export class MonitorManagerProxyClientImpl
87
87
try {
88
88
this . webSocket ?. close ( ) ;
89
89
this . webSocket = undefined ;
90
- this . onWSConnectionChangedEmitter . fire ( false ) ;
91
90
} catch {
92
91
this . messageService . error ( 'Unable to close websocket' ) ;
93
92
}
@@ -102,7 +101,18 @@ export class MonitorManagerProxyClientImpl
102
101
port : Port ,
103
102
settings ?: PluggableMonitorSettings
104
103
) : Promise < void > {
105
- return this . server ( ) . startMonitor ( board , port , settings ) ;
104
+ await this . server ( ) . startMonitor ( board , port , settings ) ;
105
+ if (
106
+ board . fqbn !== this . lastConnectedBoard ?. selectedBoard ?. fqbn ||
107
+ port . id !== this . lastConnectedBoard ?. selectedPort ?. id
108
+ )
109
+ await this . commandRegistry . executeCommand (
110
+ MonitorViewContribution . RESET_SERIAL_MONITOR
111
+ ) ;
112
+ this . lastConnectedBoard = {
113
+ selectedBoard : board ,
114
+ selectedPort : port ,
115
+ } ;
106
116
}
107
117
108
118
getCurrentSettings ( board : Board , port : Port ) : Promise < MonitorSettings > {
0 commit comments