Skip to content

Commit 28d5869

Browse files
author
Akos Kitta
committed
Use port properties from the discovery.
Signed-off-by: Akos Kitta <a.kitta@arduino.cc> Closes #740 Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent 79ea0fa commit 28d5869

File tree

14 files changed

+235
-193
lines changed

14 files changed

+235
-193
lines changed

Diff for: arduino-ide-extension/src/browser/boards/boards-config.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export class BoardsConfig extends React.Component<
349349
<div className="ports list">
350350
{ports.map((port) => (
351351
<Item<Port>
352-
key={`${port.id}`}
352+
key={`${Port.keyOf(port)}`}
353353
item={port}
354354
label={Port.toString(port)}
355355
selected={Port.sameAs(this.state.selectedPort, port)}

Diff for: arduino-ide-extension/src/browser/boards/boards-service-provider.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
AttachedBoardsChangeEvent,
1414
BoardWithPackage,
1515
BoardUserField,
16+
AvailablePorts,
1617
} from '../../common/protocol';
1718
import { BoardsConfig } from './boards-config';
1819
import { naturalCompare } from '../../common/utils';
@@ -21,6 +22,7 @@ import { StorageWrapper } from '../storage-wrapper';
2122
import { nls } from '@theia/core/lib/common';
2223
import { Deferred } from '@theia/core/lib/common/promise-util';
2324
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
25+
import { Unknown } from '../../common/nls';
2426

2527
@injectable()
2628
export class BoardsServiceProvider implements FrontendApplicationContribution {
@@ -92,11 +94,12 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
9294
);
9395

9496
this.appStateService.reachedState('ready').then(async () => {
95-
const [attachedBoards, availablePorts] = await Promise.all([
96-
this.boardsService.getAttachedBoards(),
97-
this.boardsService.getAvailablePorts(),
97+
const [state] = await Promise.all([
98+
this.boardsService.getState(),
9899
this.loadState(),
99100
]);
101+
const { boards: attachedBoards, ports: availablePorts } =
102+
AvailablePorts.split(state);
100103
this._attachedBoards = attachedBoards;
101104
this._availablePorts = availablePorts;
102105
this.onAvailablePortsChangedEmitter.fire(this._availablePorts);
@@ -473,7 +476,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
473476
};
474477
} else {
475478
availableBoard = {
476-
name: nls.localize('arduino/common/unknown', 'Unknown'),
479+
name: Unknown,
477480
port: boardPort,
478481
state: AvailableBoard.State.incomplete,
479482
};

Diff for: arduino-ide-extension/src/browser/contributions/board-selection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ PID: ${PID}`;
331331
}
332332
};
333333

334-
const grouped = AvailablePorts.byProtocol(availablePorts);
334+
const grouped = AvailablePorts.groupByProtocol(availablePorts);
335335
let protocolOrder = 100;
336336
// We first show serial and network ports, then all the rest
337337
['serial', 'network'].forEach((protocol) => {

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ export class MonitorManagerProxyClientImpl
145145
if (
146146
selectedBoard?.fqbn !==
147147
this.lastConnectedBoard?.selectedBoard?.fqbn ||
148-
selectedPort?.id !== this.lastConnectedBoard?.selectedPort?.id
148+
Port.keyOf(selectedPort) !==
149+
(this.lastConnectedBoard.selectedPort
150+
? Port.keyOf(this.lastConnectedBoard.selectedPort)
151+
: undefined)
149152
) {
150153
this.onMonitorShouldResetEmitter.fire(null);
151154
this.lastConnectedBoard = {

Diff for: arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isOSX } from '@theia/core/lib/common/os';
55
import { DisposableCollection, nls } from '@theia/core/lib/common';
66
import { BoardsServiceProvider } from '../../boards/boards-service-provider';
77
import { MonitorModel } from '../../monitor-model';
8+
import { Unknown } from '../../../common/nls';
89

910
export namespace SerialMonitorSendInput {
1011
export interface Props {
@@ -86,8 +87,8 @@ export class SerialMonitorSendInput extends React.Component<
8687
? Board.toString(board, {
8788
useFqbn: false,
8889
})
89-
: 'unknown',
90-
port ? port.address : 'unknown'
90+
: Unknown,
91+
port ? port.address : Unknown
9192
);
9293
}
9394

Diff for: arduino-ide-extension/src/browser/widgets/component-list/list-item-renderer.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Installable } from '../../../common/protocol/installable';
55
import { ArduinoComponent } from '../../../common/protocol/arduino-component';
66
import { ComponentListItem } from './component-list-item';
77
import { nls } from '@theia/core/lib/common';
8+
import { Unknown } from '../../../common/nls';
89

910
@injectable()
1011
export class ListItemRenderer<T extends ArduinoComponent> {
@@ -42,11 +43,7 @@ export class ListItemRenderer<T extends ArduinoComponent> {
4243
} else if ((item as any).id) {
4344
nameAndAuthor = <span className="name">{(item as any).id}</span>;
4445
} else {
45-
nameAndAuthor = (
46-
<span className="name">
47-
{nls.localize('arduino/common/unknown', 'Unknown')}
48-
</span>
49-
);
46+
nameAndAuthor = <span className="name">{Unknown}</span>;
5047
}
5148
const onClickUninstall = () => uninstall(item);
5249
const installedVersion = !!item.installedVersion && (

Diff for: arduino-ide-extension/src/common/nls.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { nls } from '@theia/core/lib/common/nls';
2+
3+
export const Unknown = nls.localize('arduino/common/unknown', 'Unknown');

Diff for: arduino-ide-extension/src/common/protocol/boards-service.ts

+57-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ArduinoComponent } from './arduino-component';
55

66
export type AvailablePorts = Record<string, [Port, Array<Board>]>;
77
export namespace AvailablePorts {
8-
export function byProtocol(
8+
export function groupByProtocol(
99
availablePorts: AvailablePorts
1010
): Map<string, AvailablePorts> {
1111
const grouped = new Map<string, AvailablePorts>();
@@ -20,6 +20,21 @@ export namespace AvailablePorts {
2020
}
2121
return grouped;
2222
}
23+
export function split(
24+
state: AvailablePorts
25+
): Readonly<{ boards: Board[]; ports: Port[] }> {
26+
const availablePorts: Port[] = [];
27+
const attachedBoards: Board[] = [];
28+
for (const key of Object.keys(state)) {
29+
const [port, boards] = state[key];
30+
availablePorts.push(port);
31+
attachedBoards.push(...boards);
32+
}
33+
return {
34+
boards: attachedBoards,
35+
ports: availablePorts,
36+
};
37+
}
2338
}
2439

2540
export interface AttachedBoardsChangeEvent {
@@ -116,16 +131,6 @@ export const BoardsService = Symbol('BoardsService');
116131
export interface BoardsService
117132
extends Installable<BoardsPackage>,
118133
Searchable<BoardsPackage> {
119-
/**
120-
* Deprecated. `getState` should be used to correctly map a board with a port.
121-
* @deprecated
122-
*/
123-
getAttachedBoards(): Promise<Board[]>;
124-
/**
125-
* Deprecated. `getState` should be used to correctly map a board with a port.
126-
* @deprecated
127-
*/
128-
getAvailablePorts(): Promise<Port[]>;
129134
getState(): Promise<AvailablePorts>;
130135
getBoardDetails(options: { fqbn: string }): Promise<BoardDetails | undefined>;
131136
getBoardPackage(options: { id: string }): Promise<BoardsPackage | undefined>;
@@ -140,28 +145,55 @@ export interface BoardsService
140145
}
141146

142147
export interface Port {
143-
// id is the combination of address and protocol
144-
// formatted like "<address>|<protocol>" used
145-
// to uniquely recognize a port
146-
readonly id: string;
147148
readonly address: string;
148149
readonly addressLabel: string;
149150
readonly protocol: string;
150151
readonly protocolLabel: string;
152+
readonly properties?: Record<string, string>;
151153
}
152154
export namespace Port {
153-
export function is(arg: any): arg is Port {
154-
return (
155-
!!arg &&
156-
'address' in arg &&
157-
typeof arg['address'] === 'string' &&
158-
'protocol' in arg &&
159-
typeof arg['protocol'] === 'string'
160-
);
155+
export type Properties = Record<string, string>;
156+
export namespace Properties {
157+
export function create(
158+
properties: [string, string][] | undefined
159+
): Properties {
160+
if (!properties) {
161+
return {};
162+
}
163+
return properties.reduce((acc, curr) => {
164+
const [key, value] = curr;
165+
acc[key] = value;
166+
return acc;
167+
}, {} as Record<string, string>);
168+
}
169+
}
170+
export function is(arg: unknown): arg is Port {
171+
if (typeof arg === 'object') {
172+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
173+
const object = arg as any;
174+
return (
175+
'address' in object &&
176+
typeof object['address'] === 'string' &&
177+
'addressLabel' in object &&
178+
typeof object['addressLabel'] === 'string' &&
179+
'protocol' in object &&
180+
typeof object['protocol'] === 'string' &&
181+
'protocolLabel' in object &&
182+
typeof object['protocolLabel'] === 'string'
183+
);
184+
}
185+
return false;
186+
}
187+
188+
/**
189+
* Key is the combination of address and protocol formatted like `'${address}|${protocol}'` used to uniquely identify a port.
190+
*/
191+
export function keyOf({ address, protocol }: Port): string {
192+
return `${address}|${protocol}`;
161193
}
162194

163-
export function toString(port: Port): string {
164-
return `${port.addressLabel} ${port.protocolLabel}`;
195+
export function toString({ addressLabel, protocolLabel }: Port): string {
196+
return `${addressLabel} ${protocolLabel}`;
165197
}
166198

167199
export function compare(left: Port, right: Port): number {

Diff for: arduino-ide-extension/src/node/auth/utils.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { sha256 } from 'hash.js';
33
import { randomBytes } from 'crypto';
44
import btoa = require('btoa'); // TODO: check why we cannot
55
import { AuthenticationSession } from './types';
6+
import { Unknown } from '../../common/nls';
67

78
export interface IToken {
89
accessToken: string; // When unable to refresh due to network problems, the access token becomes undefined
@@ -62,10 +63,10 @@ export function token2IToken(token: Token): IToken {
6263
sessionId: parsedIdToken.sub,
6364
scope: token.scope,
6465
account: {
65-
id: parsedIdToken.sub || 'unknown',
66-
email: parsedIdToken.email || 'unknown',
67-
nickname: parsedIdToken.nickname || 'unknown',
68-
picture: parsedIdToken.picture || 'unknown',
66+
id: parsedIdToken.sub || Unknown,
67+
email: parsedIdToken.email || Unknown,
68+
nickname: parsedIdToken.nickname || Unknown,
69+
picture: parsedIdToken.picture || Unknown,
6970
},
7071
};
7172
}

0 commit comments

Comments
 (0)