1
- import { injectable , inject } from "inversify" ;
2
- import { MonitorService , ConnectionConfig } from "../../common/protocol/monitor-service" ;
3
- import { Emitter , Event } from "@theia/core" ;
1
+ import { injectable , inject , postConstruct } from 'inversify' ;
2
+ import { Emitter , Event } from '@theia/core/lib/common/event' ;
3
+ // import { ConnectionStatusService } from '@theia/core/lib/browser/connection-status-service';
4
+ import { MessageService } from '@theia/core/lib/common/message-service' ;
5
+ import { MonitorService , MonitorConfig , MonitorError } from '../../common/protocol/monitor-service' ;
6
+ import { BoardsServiceClientImpl } from '../boards/boards-service-client-impl' ;
7
+ import { Port , Board } from '../../common/protocol/boards-service' ;
8
+ import { MonitorServiceClientImpl } from './monitor-service-client-impl' ;
4
9
5
10
@injectable ( )
6
11
export class MonitorConnection {
7
12
8
13
@inject ( MonitorService )
9
14
protected readonly monitorService : MonitorService ;
10
15
11
- connectionId : string | undefined ;
16
+ @inject ( MonitorServiceClientImpl )
17
+ protected readonly monitorServiceClient : MonitorServiceClientImpl ;
12
18
13
- protected _connectionConfig : ConnectionConfig | undefined ;
19
+ @inject ( BoardsServiceClientImpl )
20
+ protected boardsServiceClient : BoardsServiceClientImpl ;
14
21
22
+ @inject ( MessageService )
23
+ protected messageService : MessageService ;
24
+
25
+ // @inject (ConnectionStatusService)
26
+ // protected readonly connectionStatusService: ConnectionStatusService;
27
+
28
+ protected state : MonitorConnection . State | undefined ;
15
29
protected readonly onConnectionChangedEmitter = new Emitter < string | undefined > ( ) ;
30
+
16
31
readonly onConnectionChanged : Event < string | undefined > = this . onConnectionChangedEmitter . event ;
17
32
18
- get connectionConfig ( ) : ConnectionConfig | undefined {
19
- return this . _connectionConfig ;
33
+ @postConstruct ( )
34
+ protected init ( ) : void {
35
+ this . monitorServiceClient . onError ( error => {
36
+ if ( this . state ) {
37
+ const { code, connectionId, config } = error ;
38
+ if ( this . state . connectionId === connectionId ) {
39
+ switch ( code ) {
40
+ case MonitorError . ErrorCodes . CLIENT_CANCEL : {
41
+ console . log ( `Connection was canceled by client: ${ MonitorConnection . State . toString ( this . state ) } .` ) ;
42
+ break ;
43
+ }
44
+ case MonitorError . ErrorCodes . DEVICE_BUSY : {
45
+ const { port } = config ;
46
+ this . messageService . warn ( `Connection failed. Serial port is busy: ${ Port . toString ( port ) } .` ) ;
47
+ break ;
48
+ }
49
+ case MonitorError . ErrorCodes . DEVICE_NOT_CONFIGURED : {
50
+ const { port } = config ;
51
+ this . messageService . info ( `Disconnected from ${ Port . toString ( port ) } .` ) ;
52
+ break ;
53
+ }
54
+ }
55
+ this . state = undefined ;
56
+ } else {
57
+ console . warn ( `Received an error from unexpected connection: ${ MonitorConnection . State . toString ( { connectionId, config } ) } .` ) ;
58
+ }
59
+ }
60
+ } ) ;
20
61
}
21
62
22
- async connect ( config : ConnectionConfig ) : Promise < string | undefined > {
23
- if ( this . connectionId ) {
24
- await this . disconnect ( ) ;
25
- }
26
- const { connectionId } = await this . monitorService . connect ( config ) ;
27
- this . connectionId = connectionId ;
28
- this . _connectionConfig = config ;
63
+ get connectionId ( ) : string | undefined {
64
+ return this . state ? this . state . connectionId : undefined ;
65
+ }
29
66
30
- this . onConnectionChangedEmitter . fire ( this . connectionId ) ;
67
+ get connectionConfig ( ) : MonitorConfig | undefined {
68
+ return this . state ? this . state . config : undefined ;
69
+ }
31
70
71
+ async connect ( config : MonitorConfig ) : Promise < string | undefined > {
72
+ if ( this . state ) {
73
+ throw new Error ( `Already connected to ${ MonitorConnection . State . toString ( this . state ) } .` ) ;
74
+ }
75
+ const { connectionId } = await this . monitorService . connect ( config ) ;
76
+ this . state = { connectionId, config } ;
77
+ this . onConnectionChangedEmitter . fire ( connectionId ) ;
32
78
return connectionId ;
33
79
}
34
80
35
81
async disconnect ( ) : Promise < boolean > {
36
- let result = true ;
37
- const connections = await this . monitorService . getConnectionIds ( ) ;
38
- if ( this . connectionId && connections . findIndex ( id => id === this . connectionId ) >= 0 ) {
39
- console . log ( '>>> Disposing existing monitor connection before establishing a new one...' ) ;
40
- result = await this . monitorService . disconnect ( this . connectionId ) ;
41
- if ( ! result ) {
42
- // TODO: better!!!
43
- console . error ( `Could not close connection: ${ this . connectionId } . Check the backend logs.` ) ;
44
- } else {
45
- console . log ( `<<< Disposed ${ this . connectionId } connection.` ) ;
46
- this . connectionId = undefined ;
47
- this . _connectionConfig = undefined ;
48
- this . onConnectionChangedEmitter . fire ( this . connectionId ) ;
49
- }
82
+ if ( ! this . state ) {
83
+ throw new Error ( 'Not connected. Nothing to disconnect.' ) ;
84
+ }
85
+ console . log ( '>>> Disposing existing monitor connection before establishing a new one...' ) ;
86
+ const result = await this . monitorService . disconnect ( this . state . connectionId ) ;
87
+ if ( result ) {
88
+ console . log ( `<<< Disposed connection. Was: ${ MonitorConnection . State . toString ( this . state ) } ` ) ;
89
+ this . state = undefined ;
90
+ this . onConnectionChangedEmitter . fire ( undefined ) ;
91
+ } else {
92
+ console . warn ( `<<< Could not dispose connection. Activate connection: ${ MonitorConnection . State . toString ( this . state ) } ` ) ;
50
93
}
51
94
return result ;
52
95
}
53
- }
96
+
97
+ }
98
+
99
+ export namespace MonitorConnection {
100
+
101
+ export interface State {
102
+ readonly connectionId : string ;
103
+ readonly config : MonitorConfig ;
104
+ }
105
+
106
+ export namespace State {
107
+ export function toString ( state : State ) : string {
108
+ const { connectionId, config } = state ;
109
+ const { board, port } = config ;
110
+ return `${ Board . toString ( board ) } ${ Port . toString ( port ) } [ID: ${ connectionId } ]` ;
111
+ }
112
+ }
113
+
114
+ }
0 commit comments