@@ -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,21 @@ export namespace AvailablePorts {
20
20
}
21
21
return grouped ;
22
22
}
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
+ }
23
38
}
24
39
25
40
export interface AttachedBoardsChangeEvent {
@@ -116,16 +131,6 @@ export const BoardsService = Symbol('BoardsService');
116
131
export interface BoardsService
117
132
extends Installable < BoardsPackage > ,
118
133
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
134
getState ( ) : Promise < AvailablePorts > ;
130
135
getBoardDetails ( options : { fqbn : string } ) : Promise < BoardDetails | undefined > ;
131
136
getBoardPackage ( options : { id : string } ) : Promise < BoardsPackage | undefined > ;
@@ -140,28 +145,55 @@ export interface BoardsService
140
145
}
141
146
142
147
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 ;
147
148
readonly address : string ;
148
149
readonly addressLabel : string ;
149
150
readonly protocol : string ;
150
151
readonly protocolLabel : string ;
152
+ readonly properties ?: Record < string , string > ;
151
153
}
152
154
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 } ` ;
161
193
}
162
194
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 } ` ;
165
197
}
166
198
167
199
export function compare ( left : Port , right : Port ) : number {
0 commit comments