@@ -5,7 +5,7 @@ import { ArduinoComponent } from './arduino-component';
5
5
6
6
export type AvailablePorts = Record < string , [ Port , Array < Board > ] > ;
7
7
export namespace AvailablePorts {
8
- export function byProtocol (
8
+ export function groupByProtocol (
9
9
availablePorts : AvailablePorts
10
10
) : Map < string , AvailablePorts > {
11
11
const grouped = new Map < string , AvailablePorts > ( ) ;
@@ -20,6 +20,27 @@ export namespace AvailablePorts {
20
20
}
21
21
return grouped ;
22
22
}
23
+ export function boards ( state : AvailablePorts ) : ReadonlyArray < Board > {
24
+ return split ( state ) . boards ;
25
+ }
26
+ export function ports ( state : AvailablePorts ) : ReadonlyArray < Port > {
27
+ return split ( state ) . ports ;
28
+ }
29
+ export function split (
30
+ state : AvailablePorts
31
+ ) : Readonly < { boards : Board [ ] ; ports : Port [ ] } > {
32
+ const availablePorts : Port [ ] = [ ] ;
33
+ const attachedBoards : Board [ ] = [ ] ;
34
+ for ( const key of Object . keys ( state ) ) {
35
+ const [ port , boards ] = state [ key ] ;
36
+ availablePorts . push ( port ) ;
37
+ attachedBoards . push ( ...boards ) ;
38
+ }
39
+ return {
40
+ boards : attachedBoards ,
41
+ ports : availablePorts ,
42
+ } ;
43
+ }
23
44
}
24
45
25
46
export interface AttachedBoardsChangeEvent {
@@ -116,16 +137,6 @@ export const BoardsService = Symbol('BoardsService');
116
137
export interface BoardsService
117
138
extends Installable < BoardsPackage > ,
118
139
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 [ ] > ;
129
140
getState ( ) : Promise < AvailablePorts > ;
130
141
getBoardDetails ( options : { fqbn : string } ) : Promise < BoardDetails | undefined > ;
131
142
getBoardPackage ( options : { id : string } ) : Promise < BoardsPackage | undefined > ;
@@ -143,25 +154,56 @@ export interface Port {
143
154
// id is the combination of address and protocol
144
155
// formatted like "<address>|<protocol>" used
145
156
// to uniquely recognize a port
146
- readonly id : string ;
157
+ // readonly id: string;
147
158
readonly address : string ;
148
159
readonly addressLabel : string ;
149
160
readonly protocol : string ;
150
161
readonly protocolLabel : string ;
162
+ readonly properties : Record < string , string > ;
151
163
}
152
164
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
- ) ;
165
+ export type Properties = Record < string , string > ;
166
+ export namespace Properties {
167
+ export function create (
168
+ properties : [ string , string ] [ ] | undefined
169
+ ) : Properties {
170
+ if ( ! properties ) {
171
+ return { } ;
172
+ }
173
+ return properties . reduce ( ( acc , curr ) => {
174
+ const [ key , value ] = curr ;
175
+ acc [ key ] = value ;
176
+ return acc ;
177
+ } , { } as Record < string , string > ) ;
178
+ }
179
+ }
180
+ export function is ( arg : unknown ) : arg is Port {
181
+ if ( typeof arg === 'object' ) {
182
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
183
+ const object = arg as any ;
184
+ return (
185
+ 'address' in object &&
186
+ typeof object [ 'address' ] === 'string' &&
187
+ 'addressLabel' in object &&
188
+ typeof object [ 'addressLabel' ] === 'string' &&
189
+ 'protocol' in object &&
190
+ typeof object [ 'protocol' ] === 'string' &&
191
+ 'protocolLabel' in object &&
192
+ typeof object [ 'protocolLabel' ] === 'string'
193
+ ) ;
194
+ }
195
+ return false ;
196
+ }
197
+
198
+ /**
199
+ * Key is the combination of address and protocol formatted like `'${address}|${protocol}'` used to uniquely identify a port.
200
+ */
201
+ export function keyOf ( { address, protocol } : Port ) : string {
202
+ return `${ address } |${ protocol } ` ;
161
203
}
162
204
163
- export function toString ( port : Port ) : string {
164
- return `${ port . addressLabel } ${ port . protocolLabel } ` ;
205
+ export function toString ( { addressLabel , protocolLabel } : Port ) : string {
206
+ return `${ addressLabel } ${ protocolLabel } ` ;
165
207
}
166
208
167
209
export function compare ( left : Port , right : Port ) : number {
0 commit comments