Skip to content

Commit 7fbcf33

Browse files
author
Alberto Iannaccone
committed
refine monitor connection settings
1 parent 5c05240 commit 7fbcf33

File tree

4 files changed

+26
-40
lines changed

4 files changed

+26
-40
lines changed

arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import '../../src/browser/style/index.css';
22
import { ContainerModule } from 'inversify';
33
import { WidgetFactory } from '@theia/core/lib/browser/widget-manager';
4-
import {
5-
CommandContribution,
6-
CommandRegistry,
7-
} from '@theia/core/lib/common/command';
4+
import { CommandContribution } from '@theia/core/lib/common/command';
85
import { bindViewContribution } from '@theia/core/lib/browser/shell/view-contribution';
96
import {
107
TabBarToolbarContribution,
@@ -424,8 +421,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
424421
context.container.get<MonitorManagerProxyClient>(
425422
MonitorManagerProxyClient
426423
),
427-
context.container.get<BoardsServiceProvider>(BoardsServiceProvider),
428-
context.container.get<CommandRegistry>(CommandRegistry)
424+
context.container.get<BoardsServiceProvider>(BoardsServiceProvider)
429425
);
430426
},
431427
}));

arduino-ide-extension/src/browser/monitor-manager-proxy-client-impl.ts

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Emitter, MessageService } from '@theia/core';
1+
import { CommandRegistry, Emitter, MessageService } from '@theia/core';
22
import { inject, injectable } from '@theia/core/shared/inversify';
33
import { Board, Port } from '../common/protocol';
44
import {
@@ -10,6 +10,8 @@ import {
1010
PluggableMonitorSettings,
1111
MonitorSettings,
1212
} from '../node/monitor-settings/monitor-settings-provider';
13+
import { BoardsConfig } from './boards/boards-config';
14+
import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution';
1315

1416
@injectable()
1517
export class MonitorManagerProxyClientImpl
@@ -29,13 +31,11 @@ export class MonitorManagerProxyClientImpl
2931
readonly onMonitorSettingsDidChange =
3032
this.onMonitorSettingsDidChangeEmitter.event;
3133

32-
protected readonly onWSConnectionChangedEmitter = new Emitter<boolean>();
33-
readonly onWSConnectionChanged = this.onWSConnectionChangedEmitter.event;
34-
3534
// WebSocket used to handle pluggable monitor communication between
3635
// frontend and backend.
3736
private webSocket?: WebSocket;
3837
private wsPort?: number;
38+
private lastConnectedBoard: BoardsConfig.Config;
3939

4040
getWebSocketPort(): number | undefined {
4141
return this.wsPort;
@@ -47,20 +47,20 @@ export class MonitorManagerProxyClientImpl
4747

4848
// This is necessary to call the backend methods from the frontend
4949
@inject(MonitorManagerProxyFactory)
50-
protected server: MonitorManagerProxyFactory
50+
protected server: MonitorManagerProxyFactory,
51+
52+
@inject(CommandRegistry)
53+
protected readonly commandRegistry: CommandRegistry
5154
) {}
5255

5356
/**
5457
* Connects a localhost WebSocket using the specified port.
5558
* @param addressPort port of the WebSocket
5659
*/
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;
6162
try {
6263
this.webSocket = new WebSocket(`ws://localhost:${addressPort}`);
63-
this.onWSConnectionChangedEmitter.fire(true);
6464
} catch {
6565
this.messageService.error('Unable to connect to websocket');
6666
return;
@@ -87,7 +87,6 @@ export class MonitorManagerProxyClientImpl
8787
try {
8888
this.webSocket?.close();
8989
this.webSocket = undefined;
90-
this.onWSConnectionChangedEmitter.fire(false);
9190
} catch {
9291
this.messageService.error('Unable to close websocket');
9392
}
@@ -102,7 +101,18 @@ export class MonitorManagerProxyClientImpl
102101
port: Port,
103102
settings?: PluggableMonitorSettings
104103
): 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+
};
106116
}
107117

108118
getCurrentSettings(board: Board, port: Port): Promise<MonitorSettings> {

arduino-ide-extension/src/browser/serial/monitor/monitor-widget.tsx

+2-21
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ import { ArduinoSelect } from '../../widgets/arduino-select';
1313
import { SerialMonitorSendInput } from './serial-monitor-send-input';
1414
import { SerialMonitorOutput } from './serial-monitor-send-output';
1515
import { BoardsServiceProvider } from '../../boards/boards-service-provider';
16-
import { CommandRegistry, nls } from '@theia/core/lib/common';
16+
import { nls } from '@theia/core/lib/common';
1717
import { MonitorManagerProxyClient } from '../../../common/protocol';
1818
import { MonitorModel } from '../../monitor-model';
1919
import { MonitorSettings } from '../../../node/monitor-settings/monitor-settings-provider';
20-
import { MonitorViewContribution } from './monitor-view-contribution';
21-
import { BoardsConfig } from '../../boards/boards-config';
2220

2321
@injectable()
2422
export class MonitorWidget extends ReactWidget {
@@ -43,8 +41,6 @@ export class MonitorWidget extends ReactWidget {
4341
protected closing = false;
4442
protected readonly clearOutputEmitter = new Emitter<void>();
4543

46-
protected lastConnectedBoard: BoardsConfig.Config;
47-
4844
constructor(
4945
@inject(MonitorModel)
5046
protected readonly monitorModel: MonitorModel,
@@ -53,10 +49,7 @@ export class MonitorWidget extends ReactWidget {
5349
protected readonly monitorManagerProxy: MonitorManagerProxyClient,
5450

5551
@inject(BoardsServiceProvider)
56-
protected readonly boardsServiceProvider: BoardsServiceProvider,
57-
58-
@inject(CommandRegistry)
59-
protected readonly commandRegistry: CommandRegistry
52+
protected readonly boardsServiceProvider: BoardsServiceProvider
6053
) {
6154
super();
6255
this.id = MonitorWidget.ID;
@@ -85,18 +78,6 @@ export class MonitorWidget extends ReactWidget {
8578
selectedPort
8679
);
8780

88-
if (
89-
selectedBoard.fqbn !==
90-
this.lastConnectedBoard?.selectedBoard?.fqbn ||
91-
selectedPort.id !== this.lastConnectedBoard?.selectedPort?.id
92-
)
93-
await this.commandRegistry.executeCommand(
94-
MonitorViewContribution.RESET_SERIAL_MONITOR
95-
);
96-
this.lastConnectedBoard = {
97-
selectedBoard,
98-
selectedPort,
99-
};
10081
this.update();
10182
}
10283
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const MonitorManagerProxyClient = Symbol('MonitorManagerProxyClient');
3030
export interface MonitorManagerProxyClient {
3131
onMessagesReceived: Event<{ messages: string[] }>;
3232
onMonitorSettingsDidChange: Event<MonitorSettings>;
33-
onWSConnectionChanged: Event<boolean>;
3433
connect(addressPort: number): void;
3534
disconnect(): void;
3635
getWebSocketPort(): number | undefined;

0 commit comments

Comments
 (0)