Skip to content

Commit 6ebc00b

Browse files
Alberto Iannacconefstasi
Alberto Iannaccone
authored andcommitted
fix upload when serial port is open
1 parent 4f097ba commit 6ebc00b

File tree

7 files changed

+53
-31
lines changed

7 files changed

+53
-31
lines changed

arduino-ide-extension/src/browser/monitor/monitor-connection.ts

+27-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { BoardsConfig } from '../boards/boards-config';
1919
import { MonitorModel } from './monitor-model';
2020
import { ThemeService } from '@theia/core/lib/browser/theming';
2121
import { nls } from '@theia/core/lib/browser/nls';
22+
import { CoreService } from '../../common/protocol';
2223

2324
@injectable()
2425
export class SerialConnectionManager {
@@ -64,7 +65,8 @@ export class SerialConnectionManager {
6465
@inject(BoardsServiceProvider)
6566
protected readonly boardsServiceProvider: BoardsServiceProvider,
6667
@inject(MessageService) protected messageService: MessageService,
67-
@inject(ThemeService) protected readonly themeService: ThemeService
68+
@inject(ThemeService) protected readonly themeService: ThemeService,
69+
@inject(CoreService) protected readonly core: CoreService
6870
) {
6971
this.monitorServiceClient.onWebSocketChanged(
7072
this.handleWebSocketChanged.bind(this)
@@ -121,20 +123,25 @@ export class SerialConnectionManager {
121123
*
122124
* @param newConfig the porperties of the config that has changed
123125
*/
124-
setConfig(newConfig: Partial<MonitorConfig>): void {
126+
async setConfig(newConfig: Partial<MonitorConfig>): Promise<void> {
125127
let configHasChanged = false;
126128
Object.keys(this.config).forEach((key: keyof MonitorConfig) => {
127129
if (newConfig[key] !== this.config[key]) {
128130
configHasChanged = true;
129131
this.config = { ...this.config, [key]: newConfig[key] };
130132
}
131133
});
132-
if (configHasChanged && this.isSerialOpen()) {
134+
if (
135+
configHasChanged &&
136+
this.isSerialOpen() &&
137+
!(await this.core.isUploading())
138+
) {
133139
this.monitorService.updateWsConfigParam({
134140
currentBaudrate: this.config.baudRate,
135141
serialPort: this.config.port?.address,
136142
});
137-
this.disconnect().then(() => this.connect());
143+
await this.disconnect();
144+
await this.connect();
138145
}
139146
}
140147

@@ -176,22 +183,25 @@ export class SerialConnectionManager {
176183
/**
177184
* Sets the types of connections needed by the client.
178185
*
179-
* @param s The new types of connections (can be 'Monitor', 'Plotter', none or both).
186+
* @param newState The new types of connections (can be 'Monitor', 'Plotter', none or both).
180187
* If the previuos state was empty and 's' is not, it tries to reconnect to the serial service
181188
* If the provios state was NOT empty and now it is, it disconnects to the serial service
182189
* @returns The status of the operation
183190
*/
184-
protected async setState(s: Serial.State): Promise<Status> {
191+
protected async setState(newState: Serial.State): Promise<Status> {
185192
const oldState = deepClone(this._state);
186-
this._state = s;
187193
let status = Status.OK;
188194

189-
if (this.isSerialOpen(oldState) && !this.isSerialOpen()) {
195+
if (this.isSerialOpen(oldState) && !this.isSerialOpen(newState)) {
190196
status = await this.disconnect();
191-
} else if (!this.isSerialOpen(oldState) && this.isSerialOpen()) {
197+
} else if (!this.isSerialOpen(oldState) && this.isSerialOpen(newState)) {
198+
if (await this.core.isUploading()) {
199+
this.messageService.error(`Cannot open serial port when uploading`);
200+
return Status.NOT_CONNECTED;
201+
}
192202
status = await this.connect();
193203
}
194-
204+
this._state = newState;
195205
return status;
196206
}
197207

@@ -226,6 +236,12 @@ export class SerialConnectionManager {
226236
* @returns the status of the operation
227237
*/
228238
async openSerial(type: Serial.Type): Promise<Status> {
239+
if (!isMonitorConfig(this.config)) {
240+
this.messageService.error(
241+
`Please select a board and a port to open the serial connection.`
242+
);
243+
return Status.NOT_CONNECTED;
244+
}
229245
if (this.state.includes(type)) return Status.OK;
230246
const newState = deepClone(this.state);
231247
newState.push(type);
@@ -360,12 +376,7 @@ export class SerialConnectionManager {
360376

361377
async connect(): Promise<Status> {
362378
if (this.connected) return Status.ALREADY_CONNECTED;
363-
if (!isMonitorConfig(this.config)) {
364-
this.messageService.error(
365-
`Please select a board and a port to open the serial connection.`
366-
);
367-
return Status.NOT_CONNECTED;
368-
}
379+
if (!isMonitorConfig(this.config)) return Status.NOT_CONNECTED;
369380

370381
console.info(
371382
`>>> Creating serial connection for ${Board.toString(

arduino-ide-extension/src/browser/monitor/monitor-utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function messagesToLines(
99
const linesToAdd: Line[] = prevLines.length
1010
? [prevLines[prevLines.length - 1]]
1111
: [{ message: '', lineLen: 0 }];
12+
if (!(Symbol.iterator in Object(messages))) return [prevLines, charCount];
1213

1314
for (const message of messages) {
1415
const messageLen = message.length;

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

+4-10
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ export class PlotterFrontendContribution extends Contribution {
5555
}
5656
});
5757

58-
this.monitorConnection.onConnectionChanged((connected) => {
59-
if (!!this.window) {
60-
}
61-
});
62-
6358
return super.onStart(app);
6459
}
6560

@@ -78,17 +73,16 @@ export class PlotterFrontendContribution extends Contribution {
7873
}
7974

8075
async connect(): Promise<void> {
81-
if (this.monitorConnection.connected) {
82-
if (!!this.window) {
83-
this.window.focus();
84-
return;
85-
}
76+
if (!!this.window) {
77+
this.window.focus();
78+
return;
8679
}
8780
const status = await this.monitorConnection.openSerial(Serial.Type.Plotter);
8881
const wsPort = this.monitorConnection.getWsPort();
8982
if (Status.isOK(status) && wsPort) {
9083
this.open(wsPort);
9184
} else {
85+
this.monitorConnection.closeSerial(Serial.Type.Plotter);
9286
this.messageService.error(`Couldn't open serial plotter`);
9387
}
9488
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface CoreService {
2222
upload(options: CoreService.Upload.Options): Promise<void>;
2323
uploadUsingProgrammer(options: CoreService.Upload.Options): Promise<void>;
2424
burnBootloader(options: CoreService.Bootloader.Options): Promise<void>;
25+
isUploading(): Promise<boolean>;
2526
}
2627

2728
export namespace CoreService {

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

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
3232
@inject(NotificationServiceServer)
3333
protected readonly notificationService: NotificationServiceServer;
3434

35+
protected uploading = false;
36+
3537
async compile(
3638
options: CoreService.Compile.Options & {
3739
exportBinaries?: boolean;
@@ -110,6 +112,10 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
110112
);
111113
}
112114

115+
isUploading(): Promise<boolean> {
116+
return Promise.resolve(this.uploading);
117+
}
118+
113119
protected async doUpload(
114120
options: CoreService.Upload.Options,
115121
requestProvider: () => UploadRequest | UploadUsingProgrammerRequest,
@@ -120,6 +126,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
120126
) => ClientReadableStream<UploadResponse | UploadUsingProgrammerResponse>,
121127
task = 'upload'
122128
): Promise<void> {
129+
this.uploading = true;
123130
await this.compile(Object.assign(options, { exportBinaries: false }));
124131
const { sketchUri, fqbn, port, programmer } = options;
125132
const sketchPath = FileUri.fsPath(sketchUri);
@@ -173,6 +180,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
173180
severity: 'error',
174181
});
175182
throw e;
183+
} finally {
184+
this.uploading = false;
176185
}
177186
}
178187

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Emitter, MessageService } from '@theia/core';
1111
import { BoardsServiceProvider } from '../../browser/boards/boards-service-provider';
1212
import {
1313
BoardsService,
14+
CoreService,
1415
MonitorService,
1516
MonitorServiceClient,
1617
Status,
@@ -51,6 +52,7 @@ describe.only('SerialConnectionManager', () => {
5152
let boardsServiceProvider: IMock<BoardsServiceProvider>;
5253
let messageService: IMock<MessageService>;
5354
let themeService: IMock<ThemeService>;
55+
let core: IMock<CoreService>;
5456

5557
let handleBoardConfigChange: (
5658
boardsConfig: BoardsConfig.Config
@@ -68,6 +70,7 @@ describe.only('SerialConnectionManager', () => {
6870
boardsServiceProvider = Mock.ofType<BoardsServiceProvider>();
6971
messageService = Mock.ofType<MessageService>();
7072
themeService = Mock.ofType<ThemeService>();
73+
core = Mock.ofType<CoreService>();
7174

7275
boardsServiceProvider
7376
.setup((b) => b.boardsConfig)
@@ -103,14 +106,17 @@ describe.only('SerialConnectionManager', () => {
103106
.setup((m) => m.disconnect())
104107
.returns(() => Promise.resolve(Status.OK));
105108

109+
core.setup((u) => u.isUploading()).returns(() => Promise.resolve(false));
110+
106111
subject = new SerialConnectionManager(
107112
monitorModel.object,
108113
monitorService.object,
109114
monitorServiceClient.object,
110115
boardsService.object,
111116
boardsServiceProvider.object,
112117
messageService.object,
113-
themeService.object
118+
themeService.object,
119+
core.object
114120
);
115121
});
116122

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -4109,10 +4109,10 @@ archive-type@^4.0.0:
41094109
dependencies:
41104110
file-type "^4.2.0"
41114111

4112-
4113-
version "0.0.10"
4114-
resolved "https://registry.yarnpkg.com/arduino-serial-plotter-webapp/-/arduino-serial-plotter-webapp-0.0.10.tgz#1379e91705a0462ae31badffabaea8563ef59a51"
4115-
integrity sha512-FqFPk2k8iPrMr2vuZfkEbxhbWPy7zn5a9+PMZp2C4akmjdWD6V2ZO89rDOs8ODr0IZgEJxyKpGG212css5vS6g==
4112+
4113+
version "0.0.11"
4114+
resolved "https://registry.yarnpkg.com/arduino-serial-plotter-webapp/-/arduino-serial-plotter-webapp-0.0.11.tgz#a4197f8b2d005ba782bef57ce1aa9b73398ef8a3"
4115+
integrity sha512-WcPsCC0S6GefRVdsvFlng4+6ohjiIcTfolr/39zDMV3OjoRPOCqBTEEiYtrymWX9ujaaas1m6aFhTGDjisZwmQ==
41164116

41174117
are-we-there-yet@~1.1.2:
41184118
version "1.1.5"

0 commit comments

Comments
 (0)