Skip to content

Commit 9cafe95

Browse files
author
Akos Kitta
committed
feat: use latest CLI API to detect port changes
1 parent 0b9afda commit 9cafe95

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

Diff for: arduino-ide-extension/src/node/board-discovery.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export class BoardDiscovery
248248
if (!rpcPort) {
249249
return undefined;
250250
}
251-
const port = this.fromRpcPort(rpcPort);
251+
const port = createApiPort(rpcPort);
252252
const boards = detectedPort.getMatchingBoardsList().map(
253253
(board) =>
254254
({
@@ -262,17 +262,6 @@ export class BoardDiscovery
262262
};
263263
}
264264

265-
private fromRpcPort(rpcPort: RpcPort): Port {
266-
return {
267-
address: rpcPort.getAddress(),
268-
addressLabel: rpcPort.getLabel(),
269-
protocol: rpcPort.getProtocol(),
270-
protocolLabel: rpcPort.getProtocolLabel(),
271-
properties: Port.Properties.create(rpcPort.getPropertiesMap().toObject()),
272-
hardwareId: rpcPort.getHardwareId() || undefined, // prefer undefined over empty string
273-
};
274-
}
275-
276265
private fireSoonHandle: NodeJS.Timeout | undefined;
277266
private readonly bufferedEvents: DetectedPortChangeEvent[] = [];
278267
private fireSoon(event: DetectedPortChangeEvent): void {
@@ -352,3 +341,14 @@ interface DetectedPortChangeEvent {
352341
readonly detectedPort: DetectedPort;
353342
readonly eventType: EventType.Add | EventType.Remove;
354343
}
344+
345+
export function createApiPort(rpcPort: RpcPort): Port {
346+
return {
347+
address: rpcPort.getAddress(),
348+
addressLabel: rpcPort.getLabel(),
349+
protocol: rpcPort.getProtocol(),
350+
protocolLabel: rpcPort.getProtocolLabel(),
351+
properties: Port.Properties.create(rpcPort.getPropertiesMap().toObject()),
352+
hardwareId: rpcPort.getHardwareId() || undefined, // prefer undefined over empty string
353+
};
354+
}

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

+45-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
PortIdentifier,
3333
Port,
3434
UploadResponse as ApiUploadResponse,
35+
portIdentifierEquals,
3536
} from '../common/protocol';
3637
import { ArduinoCoreServiceClient } from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb';
3738
import { Port as RpcPort } from './cli-protocol/cc/arduino/cli/commands/v1/port_pb';
@@ -44,7 +45,7 @@ import { firstToUpperCase, notEmpty } from '../common/utils';
4445
import { ServiceError } from './service-error';
4546
import { ExecuteWithProgress, ProgressResponse } from './grpc-progressible';
4647
import type { Mutable } from '@theia/core/lib/common/types';
47-
import { BoardDiscovery } from './board-discovery';
48+
import { BoardDiscovery, createApiPort } from './board-discovery';
4849

4950
namespace Uploadable {
5051
export type Request = UploadRequest | UploadUsingProgrammerRequest;
@@ -208,14 +209,53 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
208209
): Promise<ApiUploadResponse> {
209210
const uploadResponseFragment: Mutable<Partial<ApiUploadResponse>> = {
210211
portBeforeUpload: options.port,
212+
portAfterUpload: options.port, // assume no port changes during the upload
211213
};
212214
const coreClient = await this.coreClient;
213215
const { client, instance } = coreClient;
214216
const progressHandler = this.createProgressHandler(options);
215-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
216-
const updateUploadResponseFragmentHandler = (_: RESP) => {
217-
// TODO: update the upload response fragment with the new port.
218-
// https://github.com/arduino/arduino-cli/issues/2245
217+
// Track responses for port changes:
218+
// No port changes are expected when uploading using a programmer.
219+
// When the `updatedUploadPort` is missing, the port did not change.
220+
// The CLI does not tell anything when no changes.
221+
// IDE2 provides the same port for after as before when no changes.
222+
const updateUploadResponseFragmentHandler = (response: RESP) => {
223+
if (response instanceof UploadResponse) {
224+
// TODO: this instanceof should not be here
225+
if (response.hasResult()) {
226+
const port = response.getResult()?.getUpdatedUploadPort();
227+
if (port) {
228+
const portAfterUpload = createApiPort(port);
229+
if (
230+
portIdentifierEquals(
231+
uploadResponseFragment.portBeforeUpload,
232+
portAfterUpload
233+
)
234+
) {
235+
console.info(
236+
`Detected a port change during the upload from the CLI [${
237+
options.port ? Port.keyOf(options.port) : ''
238+
}, ${options.fqbn}, ${
239+
options.sketch.name
240+
}], but they are the same. Before port: ${JSON.stringify(
241+
uploadResponseFragment.portBeforeUpload
242+
)}, after port: ${JSON.stringify(portAfterUpload)}`
243+
);
244+
} else {
245+
console.info(
246+
`Detected a port change during the upload from the CLI [${
247+
options.port ? Port.keyOf(options.port) : ''
248+
}, ${options.fqbn}, ${
249+
options.sketch.name
250+
}]. Before port: ${JSON.stringify(
251+
uploadResponseFragment.portBeforeUpload
252+
)}, after port: ${JSON.stringify(portAfterUpload)}`
253+
);
254+
uploadResponseFragment.portAfterUpload = portAfterUpload;
255+
}
256+
}
257+
}
258+
}
219259
};
220260
const handler = this.createOnDataHandler(
221261
progressHandler,

0 commit comments

Comments
 (0)