Skip to content

Commit 4731b33

Browse files
fstasiAlberto Iannaccone
authored and
Alberto Iannaccone
committed
get serial connection status from BE
1 parent 8839793 commit 4731b33

File tree

7 files changed

+52
-46
lines changed

7 files changed

+52
-46
lines changed

arduino-ide-extension/src/browser/contributions/burn-bootloader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class BurnBootloader extends SketchContribution {
8787
}
8888
this.messageService.error(errorMessage);
8989
} finally {
90-
if (this.serialConnection.isSerialOpen()) {
90+
if (this.serialConnection.widgetsAttached()) {
9191
await this.serialConnection.connect();
9292
}
9393
}

arduino-ide-extension/src/browser/contributions/upload-sketch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export class UploadSketch extends SketchContribution {
289289
this.onDidChangeEmitter.fire();
290290

291291
if (
292-
this.serialConnection.isSerialOpen() &&
292+
this.serialConnection.widgetsAttached() &&
293293
this.serialConnection.serialConfig
294294
) {
295295
const { board, port } = this.serialConnection.serialConfig;

arduino-ide-extension/src/browser/serial/plotter/plotter-frontend-contribution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ export class PlotterFrontendContribution extends Contribution {
8787
}
8888
}
8989

90-
protected open(wsPort: number): void {
90+
protected async open(wsPort: number): Promise<void> {
9191
const initConfig: Partial<SerialPlotter.Config> = {
9292
baudrates: SerialConfig.BaudRates.map((b) => b),
9393
currentBaudrate: this.model.baudRate,
9494
currentLineEnding: this.model.lineEnding,
9595
darkTheme: this.themeService.getCurrentTheme().type === 'dark',
9696
wsPort,
9797
interpolate: this.model.interpolate,
98-
connected: this.serialConnection.connected,
98+
connected: await this.serialConnection.isBESerialConnected(),
9999
serialPort: this.boardsServiceProvider.boardsConfig.selectedPort?.address,
100100
};
101101
const urlWithParams = queryString.stringifyUrl(

arduino-ide-extension/src/browser/serial/serial-connection-manager.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { nls } from '@theia/core/lib/common/nls';
2424
@injectable()
2525
export class SerialConnectionManager {
2626
protected _state: Serial.State = [];
27-
protected _connected = false;
2827
protected config: Partial<SerialConfig> = {
2928
board: undefined,
3029
port: undefined,
@@ -89,8 +88,11 @@ export class SerialConnectionManager {
8988
);
9089

9190
// Handles the `baudRate` changes by reconnecting if required.
92-
this.serialModel.onChange(({ property }) => {
93-
if (property === 'baudRate' && this.connected) {
91+
this.serialModel.onChange(async ({ property }) => {
92+
if (
93+
property === 'baudRate' &&
94+
(await this.serialService.isSerialPortOpen())
95+
) {
9496
const { boardsConfig } = this.boardsServiceProvider;
9597
this.handleBoardConfigChange(boardsConfig);
9698
}
@@ -129,7 +131,7 @@ export class SerialConnectionManager {
129131
});
130132
if (
131133
configHasChanged &&
132-
this.isSerialOpen() &&
134+
this.widgetsAttached() &&
133135
!(await this.core.isUploading())
134136
) {
135137
this.serialService.updateWsConfigParam({
@@ -188,9 +190,12 @@ export class SerialConnectionManager {
188190
const oldState = deepClone(this._state);
189191
let status = Status.OK;
190192

191-
if (this.isSerialOpen(oldState) && !this.isSerialOpen(newState)) {
193+
if (this.widgetsAttached(oldState) && !this.widgetsAttached(newState)) {
192194
status = await this.disconnect();
193-
} else if (!this.isSerialOpen(oldState) && this.isSerialOpen(newState)) {
195+
} else if (
196+
!this.widgetsAttached(oldState) &&
197+
this.widgetsAttached(newState)
198+
) {
194199
if (await this.core.isUploading()) {
195200
this.messageService.error(`Cannot open serial port when uploading`);
196201
return Status.NOT_CONNECTED;
@@ -205,7 +210,7 @@ export class SerialConnectionManager {
205210
return this._state;
206211
}
207212

208-
isSerialOpen(state?: Serial.State): boolean {
213+
widgetsAttached(state?: Serial.State): boolean {
209214
return (state ? state : this._state).length > 0;
210215
}
211216

@@ -215,15 +220,10 @@ export class SerialConnectionManager {
215220
: undefined;
216221
}
217222

218-
get connected(): boolean {
219-
return this._connected;
223+
async isBESerialConnected(): Promise<boolean> {
224+
return await this.serialService.isSerialPortOpen();
220225
}
221226

222-
set connected(c: boolean) {
223-
this._connected = c;
224-
this.serialService.updateWsConfigParam({ connected: c });
225-
this.onConnectionChangedEmitter.fire(this._connected);
226-
}
227227
/**
228228
* Called when a client opens the serial from the GUI
229229
*
@@ -275,8 +275,8 @@ export class SerialConnectionManager {
275275
/**
276276
* Handles error on the SerialServiceClient and try to reconnect, eventually
277277
*/
278-
handleError(error: SerialError): void {
279-
if (!this.connected) return;
278+
async handleError(error: SerialError): Promise<void> {
279+
if (!(await this.serialService.isSerialPortOpen())) return;
280280
const { code, config } = error;
281281
const { board, port } = config;
282282
const options = { timeout: 3000 };
@@ -329,9 +329,8 @@ export class SerialConnectionManager {
329329
break;
330330
}
331331
}
332-
this.connected = false;
333332

334-
if (this.isSerialOpen()) {
333+
if (this.widgetsAttached()) {
335334
if (this.serialErrors.length >= 10) {
336335
this.messageService.warn(
337336
nls.localize(
@@ -371,7 +370,8 @@ export class SerialConnectionManager {
371370
}
372371

373372
async connect(): Promise<Status> {
374-
if (this.connected) return Status.ALREADY_CONNECTED;
373+
if (await this.serialService.isSerialPortOpen())
374+
return Status.ALREADY_CONNECTED;
375375
if (!isSerialConfig(this.config)) return Status.NOT_CONNECTED;
376376

377377
console.info(
@@ -381,7 +381,6 @@ export class SerialConnectionManager {
381381
);
382382
const connectStatus = await this.serialService.connect(this.config);
383383
if (Status.isOK(connectStatus)) {
384-
this.connected = true;
385384
console.info(
386385
`<<< Serial connection created for ${Board.toString(this.config.board, {
387386
useFqbn: false,
@@ -393,14 +392,13 @@ export class SerialConnectionManager {
393392
}
394393

395394
async disconnect(): Promise<Status> {
396-
if (!this.connected) {
395+
if (!(await this.serialService.isSerialPortOpen())) {
397396
return Status.OK;
398397
}
399398

400399
console.log('>>> Disposing existing serial connection...');
401400
const status = await this.serialService.disconnect();
402401
if (Status.isOK(status)) {
403-
this.connected = false;
404402
console.log(
405403
`<<< Disposed serial connection. Was: ${Serial.Config.toString(
406404
this.config
@@ -424,7 +422,7 @@ export class SerialConnectionManager {
424422
* It is a NOOP if connected.
425423
*/
426424
async send(data: string): Promise<Status> {
427-
if (!this.connected) {
425+
if (!(await this.serialService.isSerialPortOpen())) {
428426
return Status.NOT_CONNECTED;
429427
}
430428
return new Promise<Status>((resolve) => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface SerialService extends JsonRpcServer<SerialServiceClient> {
2727
disconnect(): Promise<Status>;
2828
sendMessageToSerial(message: string): Promise<Status>;
2929
updateWsConfigParam(config: Partial<SerialPlotter.Config>): Promise<void>;
30+
isSerialPortOpen(): Promise<boolean>;
3031
}
3132

3233
export interface SerialConfig {

arduino-ide-extension/src/node/serial/serial-service-impl.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export class SerialServiceImpl implements SerialService {
8282
protected onMessageReceived: Disposable | null;
8383
protected flushMessagesInterval: NodeJS.Timeout | null;
8484

85+
async isSerialPortOpen(): Promise<boolean> {
86+
return !!this.serialConnection;
87+
}
88+
8589
setClient(client: SerialServiceClient | undefined): void {
8690
this.client = client;
8791
}
@@ -140,6 +144,8 @@ export class SerialServiceImpl implements SerialService {
140144
}).bind(this)
141145
);
142146

147+
this.updateWsConfigParam({ connected: !!this.serialConnection });
148+
143149
this.client?.notifyWebSocketChanged(
144150
this.webSocketService.getAddress().port
145151
);
@@ -280,6 +286,7 @@ export class SerialServiceImpl implements SerialService {
280286
this.serialConnection = undefined;
281287
return Status.OK;
282288
} finally {
289+
this.updateWsConfigParam({ connected: !!this.serialConnection });
283290
this.messages.length = 0;
284291
}
285292
}

arduino-ide-extension/src/test/browser/serial-connection-manager.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe.only('SerialConnectionManager', () => {
147147
it('should do nothing', async () => {
148148
const status = await subject.disconnect();
149149
expect(status).to.be.ok;
150-
expect(subject.connected).to.be.false;
150+
expect(await subject.isBESerialConnected()).to.be.false;
151151
});
152152
});
153153
context('and the config changes', () => {
@@ -188,17 +188,17 @@ describe.only('SerialConnectionManager', () => {
188188
serialService.verify((m) => m.disconnect(), Times.never());
189189
serialService.verify((m) => m.connect(It.isAny()), Times.once());
190190
expect(status).to.be.ok;
191-
expect(subject.connected).to.be.true;
191+
expect(await subject.isBESerialConnected()).to.be.true;
192192
expect(subject.getWsPort()).to.equal(wsPort);
193-
expect(subject.isSerialOpen()).to.be.true;
193+
expect(subject.widgetsAttached()).to.be.true;
194194
expect(subject.isWebSocketConnected()).to.be.false;
195195
});
196196
context('and it tries to open the serial monitor', () => {
197197
let status: Status;
198198
beforeEach(async () => {
199199
status = await subject.openSerial(Serial.Type.Monitor);
200200
});
201-
it('should open it using the same serial connection', () => {
201+
it('should open it using the same serial connection', async () => {
202202
messageService.verify(
203203
(m) => m.error(It.isAnyString()),
204204
Times.never()
@@ -209,8 +209,8 @@ describe.only('SerialConnectionManager', () => {
209209
Times.once()
210210
);
211211
expect(status).to.be.ok;
212-
expect(subject.connected).to.be.true;
213-
expect(subject.isSerialOpen()).to.be.true;
212+
expect(await subject.isBESerialConnected()).to.be.true;
213+
expect(subject.widgetsAttached()).to.be.true;
214214
});
215215
it('should create a websocket connection', () => {
216216
expect(subject.getWsPort()).to.equal(wsPort);
@@ -220,7 +220,7 @@ describe.only('SerialConnectionManager', () => {
220220
beforeEach(async () => {
221221
status = await subject.closeSerial(Serial.Type.Plotter);
222222
});
223-
it('should close the plotter without disconnecting from the serial', () => {
223+
it('should close the plotter without disconnecting from the serial', async () => {
224224
messageService.verify(
225225
(m) => m.error(It.isAnyString()),
226226
Times.never()
@@ -231,8 +231,8 @@ describe.only('SerialConnectionManager', () => {
231231
Times.once()
232232
);
233233
expect(status).to.be.ok;
234-
expect(subject.connected).to.be.true;
235-
expect(subject.isSerialOpen()).to.be.true;
234+
expect(await subject.isBESerialConnected()).to.be.true;
235+
expect(subject.widgetsAttached()).to.be.true;
236236
expect(subject.getWsPort()).to.equal(wsPort);
237237
});
238238
it('should not close the websocket connection', () => {
@@ -243,7 +243,7 @@ describe.only('SerialConnectionManager', () => {
243243
beforeEach(async () => {
244244
status = await subject.closeSerial(Serial.Type.Monitor);
245245
});
246-
it('should close the monitor without disconnecting from the serial', () => {
246+
it('should close the monitor without disconnecting from the serial', async () => {
247247
messageService.verify(
248248
(m) => m.error(It.isAnyString()),
249249
Times.never()
@@ -254,9 +254,9 @@ describe.only('SerialConnectionManager', () => {
254254
Times.once()
255255
);
256256
expect(status).to.be.ok;
257-
expect(subject.connected).to.be.true;
257+
expect(await subject.isBESerialConnected()).to.be.true;
258258
expect(subject.getWsPort()).to.equal(wsPort);
259-
expect(subject.isSerialOpen()).to.be.true;
259+
expect(subject.widgetsAttached()).to.be.true;
260260
});
261261
it('should close the websocket connection', () => {
262262
expect(subject.isWebSocketConnected()).to.be.false;
@@ -267,7 +267,7 @@ describe.only('SerialConnectionManager', () => {
267267
beforeEach(async () => {
268268
status = await subject.closeSerial(Serial.Type.Plotter);
269269
});
270-
it('should successfully disconnect from the serial', () => {
270+
it('should successfully disconnect from the serial', async () => {
271271
messageService.verify(
272272
(m) => m.error(It.isAnyString()),
273273
Times.never()
@@ -278,9 +278,9 @@ describe.only('SerialConnectionManager', () => {
278278
Times.once()
279279
);
280280
expect(status).to.be.ok;
281-
expect(subject.connected).to.be.false;
281+
expect(await subject.isBESerialConnected()).to.be.false;
282282
expect(subject.getWsPort()).to.be.undefined;
283-
expect(subject.isSerialOpen()).to.be.false;
283+
expect(subject.widgetsAttached()).to.be.false;
284284
expect(subject.isWebSocketConnected()).to.be.false;
285285
});
286286
});
@@ -337,9 +337,9 @@ describe.only('SerialConnectionManager', () => {
337337
Times.once()
338338
);
339339
expect(status).to.be.false;
340-
expect(subject.connected).to.be.false;
340+
expect(await subject.isBESerialConnected()).to.be.false;
341341
expect(subject.getWsPort()).to.be.undefined;
342-
expect(subject.isSerialOpen()).to.be.true;
342+
expect(subject.widgetsAttached()).to.be.true;
343343
});
344344

345345
context(
@@ -360,9 +360,9 @@ describe.only('SerialConnectionManager', () => {
360360
(m) => m.connect(It.isValue(anotherSerialConfig)),
361361
Times.once()
362362
);
363-
expect(subject.connected).to.be.true;
363+
expect(await subject.isBESerialConnected()).to.be.true;
364364
expect(subject.getWsPort()).to.equal(wsPort);
365-
expect(subject.isSerialOpen()).to.be.true;
365+
expect(subject.widgetsAttached()).to.be.true;
366366
expect(subject.isWebSocketConnected()).to.be.false;
367367
});
368368
}

0 commit comments

Comments
 (0)