From 31b704cdb9f8450ad90ed65eafe280d638f94254 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Tue, 8 Mar 2022 17:14:31 +0100 Subject: [PATCH] Moved settings to MonitorService --- .../src/node/monitor-manager.ts | 60 ++++++------------- .../src/node/monitor-service.ts | 54 ++++++++++++++++- 2 files changed, 71 insertions(+), 43 deletions(-) diff --git a/arduino-ide-extension/src/node/monitor-manager.ts b/arduino-ide-extension/src/node/monitor-manager.ts index a1627d3c..1f38bac8 100644 --- a/arduino-ide-extension/src/node/monitor-manager.ts +++ b/arduino-ide-extension/src/node/monitor-manager.ts @@ -1,7 +1,6 @@ -import { Emitter, ILogger } from "@theia/core"; +import { ILogger } from "@theia/core"; import { inject, injectable, named } from "@theia/core/shared/inversify"; -import { Board, Port, Status, MonitorSetting, MonitorSettings } from "../common/protocol"; -import { EnumerateMonitorPortSettingsRequest, EnumerateMonitorPortSettingsResponse } from "./cli-protocol/cc/arduino/cli/commands/v1/monitor_pb"; +import { Board, Port, Status, MonitorSettings } from "../common/protocol"; import { CoreClientAware } from "./core-client-provider"; import { MonitorService } from "./monitor-service"; @@ -23,43 +22,6 @@ export class MonitorManager extends CoreClientAware { super(); } - /** - * Returns the possible configurations used to connect a monitor - * to the board specified by fqbn using the specified protocol - * @param protocol the protocol of the monitor we want get settings for - * @param fqbn the fqbn of the board we want to monitor - * @returns a map of all the settings supported by the monitor - */ - async portMonitorSettings(protocol: string, fqbn: string): Promise { - const coreClient = await this.coreClient(); - const { client, instance } = coreClient; - const req = new EnumerateMonitorPortSettingsRequest(); - req.setInstance(instance); - req.setPortProtocol(protocol); - req.setFqbn(fqbn); - - const res = await new Promise((resolve, reject) => { - client.enumerateMonitorPortSettings(req, (err, resp) => { - if (!!err) { - reject(err) - } - resolve(resp) - }) - }); - - let settings: MonitorSettings = {}; - for (const iterator of res.getSettingsList()) { - settings[iterator.getSettingId()] = { - 'id': iterator.getSettingId(), - 'label': iterator.getLabel(), - 'type': iterator.getType(), - 'values': iterator.getEnumValuesList(), - 'selectedValue': iterator.getValue(), - } - } - return settings; - } - /** * Used to know if a monitor is started * @param board board connected to port @@ -178,7 +140,7 @@ export class MonitorManager extends CoreClientAware { * @param port port to monitor * @param settings monitor settings to change */ - changeMonitorSettings(board: Board, port: Port, settings: Record) { + changeMonitorSettings(board: Board, port: Port, settings: MonitorSettings) { const monitorID = this.monitorID(board, port); let monitor = this.monitorServices.get(monitorID); if (!monitor) { @@ -187,6 +149,22 @@ export class MonitorManager extends CoreClientAware { } } + /** + * Returns the settings currently used by the pluggable monitor + * that's communicating with the specified board/port combination. + * @param board board connected to port + * @param port port monitored + * @returns map of current monitor settings + */ + currentMonitorSettings(board: Board, port: Port): MonitorSettings { + const monitorID = this.monitorID(board, port); + const monitor = this.monitorServices.get(monitorID); + if (!monitor) { + return {}; + } + return monitor.currentSettings(); + } + /** * Creates a MonitorService that handles the lifetime and the * communication via WebSocket with the frontend. diff --git a/arduino-ide-extension/src/node/monitor-service.ts b/arduino-ide-extension/src/node/monitor-service.ts index a5459cf8..f843565b 100644 --- a/arduino-ide-extension/src/node/monitor-service.ts +++ b/arduino-ide-extension/src/node/monitor-service.ts @@ -1,8 +1,8 @@ import { ClientDuplexStream } from "@grpc/grpc-js"; import { Disposable, Emitter, ILogger } from "@theia/core"; import { inject, named } from "@theia/core/shared/inversify"; -import { Board, Port, Status, MonitorSettings } from "../common/protocol"; -import { MonitorPortConfiguration, MonitorPortSetting, MonitorRequest, MonitorResponse } from "./cli-protocol/cc/arduino/cli/commands/v1/monitor_pb"; +import { Board, Port, Status, MonitorSettings, Monitor } from "../common/protocol"; +import { EnumerateMonitorPortSettingsRequest, EnumerateMonitorPortSettingsResponse, MonitorPortConfiguration, MonitorPortSetting, MonitorRequest, MonitorResponse } from "./cli-protocol/cc/arduino/cli/commands/v1/monitor_pb"; import { CoreClientAware } from "./core-client-provider"; import { WebSocketProvider } from "./web-socket/web-socket-provider"; import { Port as gRPCPort } from 'arduino-ide-extension/src/node/cli-protocol/cc/arduino/cli/commands/v1/port_pb' @@ -55,6 +55,11 @@ export class MonitorService extends CoreClientAware implements Disposable { this.dispose(); } }); + + // Sets default settings for this monitor + this.portMonitorSettings(port.protocol, board.fqbn!).then( + settings => this.settings = settings + ); } getWebsocketAddressPort(): number { @@ -222,6 +227,51 @@ export class MonitorService extends CoreClientAware implements Disposable { }) } + /** + * + * @returns map of current monitor settings + */ + currentSettings(): MonitorSettings { + return this.settings; + } + + /** + * Returns the possible configurations used to connect a monitor + * to the board specified by fqbn using the specified protocol + * @param protocol the protocol of the monitor we want get settings for + * @param fqbn the fqbn of the board we want to monitor + * @returns a map of all the settings supported by the monitor + */ + private async portMonitorSettings(protocol: string, fqbn: string): Promise { + const coreClient = await this.coreClient(); + const { client, instance } = coreClient; + const req = new EnumerateMonitorPortSettingsRequest(); + req.setInstance(instance); + req.setPortProtocol(protocol); + req.setFqbn(fqbn); + + const res = await new Promise((resolve, reject) => { + client.enumerateMonitorPortSettings(req, (err, resp) => { + if (!!err) { + reject(err) + } + resolve(resp) + }) + }); + + let settings: MonitorSettings = {}; + for (const iterator of res.getSettingsList()) { + settings[iterator.getSettingId()] = { + 'id': iterator.getSettingId(), + 'label': iterator.getLabel(), + 'type': iterator.getType(), + 'values': iterator.getEnumValuesList(), + 'selectedValue': iterator.getValue(), + } + } + return settings; + } + /** * Set monitor settings, if there is a running monitor they'll be sent * to it, otherwise they'll be used when starting one.