Skip to content

Commit 3db9254

Browse files
author
Akos Kitta
committed
Use port properties from the discovery.
Signed-off-by: Akos Kitta <[email protected]> Closes #740
1 parent b5f9aa0 commit 3db9254

File tree

14 files changed

+238
-196
lines changed

14 files changed

+238
-196
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export class BoardsConfig extends React.Component<
354354
<div className="ports list">
355355
{ports.map((port) => (
356356
<Item<Port>
357-
key={`${port.id}`}
357+
key={`${Port.keyOf(port)}`}
358358
item={port}
359359
label={Port.toString(port)}
360360
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 {
@@ -96,11 +98,12 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
9698
);
9799

98100
this.appStateService.reachedState('ready').then(async () => {
99-
const [attachedBoards, availablePorts] = await Promise.all([
100-
this.boardsService.getAttachedBoards(),
101-
this.boardsService.getAvailablePorts(),
101+
const [state] = await Promise.all([
102+
this.boardsService.getState(),
102103
this.loadState(),
103104
]);
105+
const { boards: attachedBoards, ports: availablePorts } =
106+
AvailablePorts.split(state);
104107
this._attachedBoards = attachedBoards;
105108
this._availablePorts = availablePorts;
106109
this.onAvailablePortsChangedEmitter.fire(this._availablePorts);
@@ -558,7 +561,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
558561
};
559562
} else {
560563
availableBoard = {
561-
name: nls.localize('arduino/common/unknown', 'Unknown'),
564+
name: Unknown,
562565
port: boardPort,
563566
state: AvailableBoard.State.incomplete,
564567
};

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 {
@@ -117,16 +132,6 @@ export const BoardsService = Symbol('BoardsService');
117132
export interface BoardsService
118133
extends Installable<BoardsPackage>,
119134
Searchable<BoardsPackage> {
120-
/**
121-
* Deprecated. `getState` should be used to correctly map a board with a port.
122-
* @deprecated
123-
*/
124-
getAttachedBoards(): Promise<Board[]>;
125-
/**
126-
* Deprecated. `getState` should be used to correctly map a board with a port.
127-
* @deprecated
128-
*/
129-
getAvailablePorts(): Promise<Port[]>;
130135
getState(): Promise<AvailablePorts>;
131136
getBoardDetails(options: { fqbn: string }): Promise<BoardDetails | undefined>;
132137
getBoardPackage(options: { id: string }): Promise<BoardsPackage | undefined>;
@@ -141,28 +146,55 @@ export interface BoardsService
141146
}
142147

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

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

168200
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)