Run the Board.List requests sequentially.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2019-07-25 08:50:05 +02:00
parent 7494beca33
commit 19f058cb7f

View File

@ -1,3 +1,4 @@
import * as PQueue from 'p-queue';
import { injectable, inject } from 'inversify'; import { injectable, inject } from 'inversify';
import { BoardsService, AttachedSerialBoard, BoardPackage, Board, AttachedNetworkBoard } from '../common/protocol/boards-service'; import { BoardsService, AttachedSerialBoard, BoardPackage, Board, AttachedNetworkBoard } from '../common/protocol/boards-service';
import { PlatformSearchReq, PlatformSearchResp, PlatformInstallReq, PlatformInstallResp, PlatformListReq, PlatformListResp } from './cli-protocol/commands/core_pb'; import { PlatformSearchReq, PlatformSearchResp, PlatformInstallReq, PlatformInstallResp, PlatformListReq, PlatformListResp } from './cli-protocol/commands/core_pb';
@ -15,42 +16,48 @@ export class BoardsServiceImpl implements BoardsService {
protected readonly toolOutputService: ToolOutputServiceServer; protected readonly toolOutputService: ToolOutputServiceServer;
protected selectedBoard: Board | undefined; protected selectedBoard: Board | undefined;
protected readonly queue = new PQueue({ autoStart: true, concurrency: 1 });
public async getAttachedBoards(): Promise<{ boards: Board[] }> { public async getAttachedBoards(): Promise<{ boards: Board[] }> {
const coreClient = await this.coreClientProvider.getClient(); return this.queue.add(() => {
const boards: Board[] = []; return new Promise<{ boards: Board[] }>(async resolve => {
if (!coreClient) { const coreClient = await this.coreClientProvider.getClient();
return { boards }; const boards: Board[] = [];
} if (!coreClient) {
const { client, instance } = coreClient; resolve({ boards });
return;
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)));
for (const portsList of resp.getPortsList()) {
const protocol = portsList.getProtocol();
const address = portsList.getAddress();
for (const board of portsList.getBoardsList()) {
const name = board.getName() || 'unknown';
const fqbn = board.getFqbn();
const port = address;
if (protocol === 'serial') {
boards.push(<AttachedSerialBoard>{
name,
fqbn,
port
});
} else { // We assume, it is a `network` board.
boards.push(<AttachedNetworkBoard>{
name,
fqbn,
address,
port
});
} }
}
} const { client, instance } = coreClient;
return { boards }; 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)));
for (const portsList of resp.getPortsList()) {
const protocol = portsList.getProtocol();
const address = portsList.getAddress();
for (const board of portsList.getBoardsList()) {
const name = board.getName() || 'unknown';
const fqbn = board.getFqbn();
const port = address;
if (protocol === 'serial') {
boards.push(<AttachedSerialBoard>{
name,
fqbn,
port
});
} else { // We assume, it is a `network` board.
boards.push(<AttachedNetworkBoard>{
name,
fqbn,
address,
port
});
}
}
}
resolve({boards});
})
});
} }
async selectBoard(board: Board): Promise<void> { async selectBoard(board: Board): Promise<void> {