Skip to content

Commit 61b8bde

Browse files
silvanocerzaAlberto Iannaccone
authored and
Alberto Iannaccone
committed
Add monitor proxy functions for the frontend
1 parent c5695d3 commit 61b8bde

File tree

4 files changed

+109
-51
lines changed

4 files changed

+109
-51
lines changed

arduino-ide-extension/src/common/protocol/monitor-service.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { Board, Port } from './boards-service';
44
export const MonitorManagerProxyPath = '/services/monitor-manager-proxy';
55
export const MonitorManagerProxy = Symbol('MonitorManagerProxy');
66
export interface MonitorManagerProxy extends JsonRpcServer<MonitorManagerProxyClient> {
7-
//set the monitor settings, which includes address, port and other monitor-specific settings
8-
setMonitorSettings(board: Board, port: Port, settings: MonitorSettings): Promise<void>;
7+
startMonitor(board: Board, port: Port, settings?: MonitorSettings): Promise<void>;
8+
changeMonitorSettings(board: Board, port: Port, settings: MonitorSettings): Promise<void>;
9+
stopMonitor(board: Board, port: Port): Promise<void>;
10+
getSupportedSettings(protocol: string, fqbn: string): Promise<MonitorSettings>;
911
}
1012

1113
export const MonitorManagerProxyClient = Symbol('MonitorManagerProxyClient');
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Emitter, ILogger } from "@theia/core";
1+
import { ILogger } from "@theia/core";
22
import { inject, injectable, named } from "@theia/core/shared/inversify";
3-
import { Disposable } from "@theia/core/shared/vscode-languageserver-protocol";
43
import { MonitorManagerProxy, MonitorManagerProxyClient, MonitorSettings, Status } from "../common/protocol";
54
import { Board, Port } from "../common/protocol";
65
import { MonitorManager } from "./monitor-manager";
@@ -9,9 +8,6 @@ import { MonitorManager } from "./monitor-manager";
98
export class MonitorManagerProxyImpl implements MonitorManagerProxy {
109
protected client: MonitorManagerProxyClient;
1110

12-
protected selectedBoard: Board | null;
13-
protected selectedPort: Port | null;
14-
1511
constructor(
1612
@inject(ILogger)
1713
@named("monitor-manager-proxy")
@@ -26,32 +22,64 @@ export class MonitorManagerProxyImpl implements MonitorManagerProxy {
2622
// NOOP
2723
}
2824

25+
/**
26+
* Start a pluggable monitor and/or change its settings.
27+
* If settings are defined they'll be set before starting the monitor,
28+
* otherwise default ones will be used by the monitor.
29+
* @param board board connected to port
30+
* @param port port to monitor
31+
* @param settings map of supported configuration by the monitor
32+
*/
33+
async startMonitor(board: Board, port: Port, settings?: MonitorSettings): Promise<void> {
34+
if (settings) {
35+
await this.changeMonitorSettings(board, port, settings);
36+
}
37+
const status = await this.manager.startMonitor(board, port);
38+
if (status === Status.ALREADY_CONNECTED || status === Status.OK) {
39+
this.client.notifyWebSocketChanged(this.manager.getWebsocketAddressPort(board, port));
40+
}
41+
}
2942

30-
// setMonitorConfig is called by the FE when trying to establish a monitor connection to a board or when changing some
31-
// settings (such as the baudrate, when available)
32-
async setMonitorSettings(board: Board, port: Port, settings: MonitorSettings): Promise<void> {
33-
34-
// check if it's a different connection or a change in the settings
35-
if (board === this.selectedBoard && port === this.selectedPort) {
36-
37-
// TODO: update the settings
43+
/**
44+
* Changes the settings of a running pluggable monitor, if that monitor is not
45+
* started this function is a noop.
46+
* @param board board connected to port
47+
* @param port port monitored
48+
* @param settings map of supported configuration by the monitor
49+
*/
50+
async changeMonitorSettings(board: Board, port: Port, settings: MonitorSettings): Promise<void> {
51+
if (!this.manager.isStarted(board, port)) {
52+
// Monitor is not running, no need to change settings
3853
return;
3954
}
55+
return this.manager.changeMonitorSettings(board, port, settings);
56+
}
4057

41-
const startStatus: Status = await this.manager.startMonitor(board, port);
42-
43-
if (startStatus === Status.ALREADY_CONNECTED || startStatus === Status.OK) {
44-
this.client.notifyWebSocketChanged(this.manager.getWebsocketAddress(board, port));
45-
}
58+
/**
59+
* Stops a running pluggable monitor.
60+
* @param board board connected to port
61+
* @param port port monitored
62+
*/
63+
async stopMonitor(board: Board, port: Port): Promise<void> {
64+
return this.manager.stopMonitor(board, port);
65+
}
4666

67+
/**
68+
* Returns the settings supported by the pluggable monitor for the specified
69+
* protocol, the fqbn is necessary since it's used to tell different monitors
70+
* using the same protocol.
71+
* @param protocol protocol of a pluggable monitor
72+
* @param fqbn unique ID of a board
73+
* @returns a map of MonitorSetting
74+
*/
75+
async getSupportedSettings(protocol: string, fqbn: string): Promise<MonitorSettings> {
76+
return this.manager.portMonitorSettings(protocol, fqbn);
4777
}
4878

4979
setClient(client: MonitorManagerProxyClient | undefined): void {
5080
if (!client) {
5181
return;
5282
}
5383
this.client = client;
54-
5584
}
56-
5785
}

arduino-ide-extension/src/node/monitor-manager.ts

+56-28
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ export class MonitorManager extends CoreClientAware {
1515
// be started.
1616
private monitorServices = new Map<MonitorID, MonitorService>();
1717

18-
// Used to notify a monitor service that an upload process started
19-
// to the board/port combination it manages
20-
protected readonly onUploadStartedEmitter = new Emitter<{ board: Board, port: Port }>();
21-
readonly onUploadStarted = this.onUploadStartedEmitter.event;
22-
23-
// Used to notify a monitor service that an upload process finished
24-
// to the board/port combination it manages
25-
26-
27-
2818
constructor(
2919
@inject(ILogger)
3020
@named('monitor-manager')
@@ -55,7 +45,7 @@ export class MonitorManager extends CoreClientAware {
5545
}
5646
resolve(resp)
5747
})
58-
})
48+
});
5949

6050
let settings: MonitorSettings = {};
6151
for (const iterator of res.getSettingsList()) {
@@ -71,9 +61,28 @@ export class MonitorManager extends CoreClientAware {
7161
}
7262

7363
/**
74-
*
75-
* @param board
76-
* @param port
64+
* Used to know if a monitor is started
65+
* @param board board connected to port
66+
* @param port port to monitor
67+
* @returns true if the monitor is currently monitoring the board/port
68+
* combination specifed, false in all other cases.
69+
*/
70+
isStarted(board: Board, port: Port): boolean {
71+
const monitorID = this.monitorID(board, port);
72+
const monitor = this.monitorServices.get(monitorID);
73+
if (monitor) {
74+
return monitor.isStarted();
75+
}
76+
return false;
77+
}
78+
79+
/**
80+
* Start a pluggable monitor that receives and sends messages
81+
* to the specified board and port combination.
82+
* @param board board connected to port
83+
* @param port port to monitor
84+
* @returns a Status object to know if the process has been
85+
* started or if there have been errors.
7786
*/
7887
async startMonitor(board: Board, port: Port): Promise<Status> {
7988
const monitorID = this.monitorID(board, port);
@@ -82,12 +91,16 @@ export class MonitorManager extends CoreClientAware {
8291
monitor = this.createMonitor(board, port)
8392
}
8493
return await monitor.start();
85-
// TODO: I need to return the address here right?
8694
}
8795

96+
/**
97+
* Stop a pluggable monitor connected to the specified board/port
98+
* combination. It's a noop if monitor is not running.
99+
* @param board board connected to port
100+
* @param port port monitored
101+
*/
88102
async stopMonitor(board: Board, port: Port): Promise<void> {
89103
const monitorID = this.monitorID(board, port);
90-
91104
const monitor = this.monitorServices.get(monitorID);
92105
if (!monitor) {
93106
// There's no monitor to stop, bail
@@ -96,23 +109,29 @@ export class MonitorManager extends CoreClientAware {
96109
return await monitor.stop();
97110
}
98111

99-
getWebsocketAddress(board: Board, port: Port): number {
112+
/**
113+
* Returns the port of the WebSocket used by the MonitorService
114+
* that is handling the board/port combination
115+
* @param board board connected to port
116+
* @param port port to monitor
117+
* @returns port of the MonitorService's WebSocket
118+
*/
119+
getWebsocketAddressPort(board: Board, port: Port): number {
100120
const monitorID = this.monitorID(board, port);
101-
102121
const monitor = this.monitorServices.get(monitorID);
103122
if (!monitor) {
104123
return -1;
105124
}
106-
return monitor.getWebsocketAddress();
125+
return monitor.getWebsocketAddressPort();
107126
}
108127

109128
/**
110129
* Notifies the monitor service of that board/port combination
111130
* that an upload process started on that exact board/port combination.
112131
* This must be done so that we can stop the monitor for the time being
113132
* until the upload process finished.
114-
* @param board
115-
* @param port
133+
* @param board board connected to port
134+
* @param port port to monitor
116135
*/
117136
async notifyUploadStarted(board?: Board, port?: Port): Promise<void> {
118137
if (!board || !port) {
@@ -132,9 +151,10 @@ export class MonitorManager extends CoreClientAware {
132151
/**
133152
* Notifies the monitor service of that board/port combination
134153
* that an upload process started on that exact board/port combination.
135-
* @param board
136-
* @param port
137-
* @returns
154+
* @param board board connected to port
155+
* @param port port to monitor
156+
* @returns a Status object to know if the process has been
157+
* started or if there have been errors.
138158
*/
139159
async notifyUploadFinished(board?: Board, port?: Port): Promise<Status> {
140160
if (!board || !port) {
@@ -152,10 +172,11 @@ export class MonitorManager extends CoreClientAware {
152172
}
153173

154174
/**
155-
*
156-
* @param board
157-
* @param port
158-
* @param settings map of monitor settings to change
175+
* Changes the settings of a pluggable monitor even if it's running.
176+
* If monitor is not running they're going to be used as soon as it's started.
177+
* @param board board connected to port
178+
* @param port port to monitor
179+
* @param settings monitor settings to change
159180
*/
160181
changeMonitorSettings(board: Board, port: Port, settings: Record<string, MonitorSetting>) {
161182
const monitorID = this.monitorID(board, port);
@@ -166,6 +187,13 @@ export class MonitorManager extends CoreClientAware {
166187
}
167188
}
168189

190+
/**
191+
* Creates a MonitorService that handles the lifetime and the
192+
* communication via WebSocket with the frontend.
193+
* @param board board connected to specified port
194+
* @param port port to monitor
195+
* @returns a new instance of MonitorService ready to use.
196+
*/
169197
private createMonitor(board: Board, port: Port): MonitorService {
170198
const monitorID = this.monitorID(board, port);
171199
const monitor = new MonitorService(

arduino-ide-extension/src/node/monitor-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class MonitorService extends CoreClientAware implements Disposable {
5757
});
5858
}
5959

60-
getWebsocketAddress(): number {
60+
getWebsocketAddressPort(): number {
6161
return this.webSocketProvider.getAddress().port;
6262
}
6363

0 commit comments

Comments
 (0)