-
-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathboards-service-impl.ts
132 lines (111 loc) · 5.23 KB
/
boards-service-impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { injectable, inject } from 'inversify';
import { BoardsService, AttachedSerialBoard, AttachedNetworkBoard, BoardPackage, Board } from '../common/protocol/boards-service';
import { PlatformSearchReq, PlatformSearchResp, PlatformInstallReq, PlatformInstallResp, PlatformListReq, PlatformListResp } from './cli-protocol/core_pb';
import { CoreClientProvider } from './core-client-provider';
import { BoardListReq, BoardListResp } from './cli-protocol/board_pb';
import { ToolOutputServiceServer } from '../common/protocol/tool-output-service';
@injectable()
export class BoardsServiceImpl implements BoardsService {
@inject(CoreClientProvider)
protected readonly coreClientProvider: CoreClientProvider;
@inject(ToolOutputServiceServer)
protected readonly toolOutputService: ToolOutputServiceServer;
protected selectedBoard: Board | undefined;
public async getAttachedBoards(): Promise<{ boards: Board[] }> {
const coreClient = await this.coreClientProvider.getClient();
if (!coreClient) {
return { boards: [] };
}
const { client, instance } = coreClient;
const req = new BoardListReq();
req.setInstance(instance);
const resp = await new Promise<BoardListResp>((resolve, reject) => client.boardList(req, (err, resp) => (!!err ? reject : resolve)(!!err ? err : resp)));
const serialBoards: Board[] = resp.getSerialList().map(b => <AttachedSerialBoard>{
name: b.getName() || "unknown",
fqbn: b.getFqbn(),
port: b.getPort(),
type: 'serial',
serialNumber: b.getSerialnumber(),
productID: b.getProductid(),
vendorID: b.getVendorid()
});
const networkBoards: Board[] = resp.getNetworkList().map(b => <AttachedNetworkBoard>{
name: b.getName(),
fqbn: b.getFqbn(),
address: b.getAddress(),
info: b.getInfo(),
port: b.getPort(),
type: 'network'
});
return { boards: serialBoards.concat(networkBoards) };
}
async selectBoard(board: Board): Promise<void> {
this.selectedBoard = board;
}
async getSelectBoard(): Promise<Board | undefined> {
return this.selectedBoard;
}
async search(options: { query?: string }): Promise<{ items: BoardPackage[] }> {
const coreClient = await this.coreClientProvider.getClient();
if (!coreClient) {
return { items: [] };
}
const { client, instance } = coreClient;
const installedPlatformsReq = new PlatformListReq();
installedPlatformsReq.setInstance(instance);
const installedPlatformsResp = await new Promise<PlatformListResp>((resolve, reject) =>
client.platformList(installedPlatformsReq, (err, resp) => (!!err ? reject : resolve)(!!err ? err : resp))
);
const installedPlatforms = installedPlatformsResp.getInstalledPlatformList();
const req = new PlatformSearchReq();
req.setSearchArgs(options.query || "");
req.setInstance(instance);
const resp = await new Promise<PlatformSearchResp>((resolve, reject) => client.platformSearch(req, (err, resp) => (!!err ? reject : resolve)(!!err ? err : resp)));
let items = resp.getSearchOutputList().map(item => {
let installedVersion: string | undefined;
const matchingPlatform = installedPlatforms.find(ip => ip.getId().startsWith(`${item.getId()}@`));
if (!!matchingPlatform) {
installedVersion = matchingPlatform.getInstalled();
}
const result: BoardPackage = {
id: item.getId(),
name: item.getName(),
author: item.getAuthor(),
availableVersions: [ item.getVersion() ],
description: item.getBoardsList().map(b => b.getName()).join(", "),
installable: true,
summary: "Boards included in this package:",
installedVersion,
boards: item.getBoardsList().map(b => <Board>{ name: b.getName(), fqbn: b.getFqbn() }),
}
return result;
});
return { items };
}
async install(pkg: BoardPackage): Promise<void> {
const coreClient = await this.coreClientProvider.getClient();
if (!coreClient) {
return;
}
const { client, instance } = coreClient;
const [ platform, boardName ] = pkg.id.split(":");
const req = new PlatformInstallReq();
req.setInstance(instance);
req.setArchitecture(boardName);
req.setPlatformPackage(platform);
req.setVersion(pkg.availableVersions[0]);
console.info("Starting board installation", pkg);
const resp = client.platformInstall(req);
resp.on('data', (r: PlatformInstallResp) => {
const prog = r.getProgress();
if (prog && prog.getFile()) {
this.toolOutputService.publishNewOutput("board download", `downloading ${prog.getFile()}\n`)
}
});
await new Promise<void>((resolve, reject) => {
resp.on('end', resolve);
resp.on('error', reject);
});
console.info("Board installation done", pkg);
}
}