workaround: stop discoveries before install/uninstall boards/libs (#674)

This commit is contained in:
Francesco Stasi 2021-12-10 17:03:24 +01:00 committed by GitHub
parent 49d12d99ff
commit a8ae0bb4e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 18 deletions

View File

@ -60,11 +60,29 @@ export class BoardDiscovery extends CoreClientAware {
this.startBoardListWatch(coreClient);
}
stopBoardListWatch(coreClient: CoreClientProvider.Client): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.boardWatchDuplex) {
return resolve();
}
const { instance } = coreClient;
const req = new BoardListWatchRequest();
req.setInstance(instance);
try {
this.boardWatchDuplex.write(req.setInterrupt(true), resolve);
} catch (e) {
this.discoveryLogger.error(e);
resolve();
}
});
}
startBoardListWatch(coreClient: CoreClientProvider.Client): void {
if (this.watching) {
// We want to avoid starting the board list watch process multiple
// times to meet unforseen consequences
return
return;
}
this.watching = true;
const { client, instance } = coreClient;
@ -73,9 +91,19 @@ export class BoardDiscovery extends CoreClientAware {
this.boardWatchDuplex = client.boardListWatch();
this.boardWatchDuplex.on('end', () => {
this.watching = false;
console.info('board watch ended')
})
console.info('board watch ended');
});
this.boardWatchDuplex.on('close', () => {
this.watching = false;
console.info('board watch ended');
});
this.boardWatchDuplex.on('data', (resp: BoardListWatchResponse) => {
if (resp.getEventType() === 'quit') {
this.watching = false;
console.info('board watch ended');
return;
}
const detectedPort = resp.getPort();
if (detectedPort) {
let eventType: 'add' | 'remove' | 'unknown' = 'unknown';
@ -96,7 +124,7 @@ export class BoardDiscovery extends CoreClientAware {
const address = (detectedPort as any).getPort().getAddress();
const protocol = (detectedPort as any).getPort().getProtocol();
const label = (detectedPort as any).getPort().getLabel();;
const label = (detectedPort as any).getPort().getLabel();
const port = { address, protocol, label };
const boards: Board[] = [];
for (const item of detectedPort.getMatchingBoardsList()) {
@ -111,7 +139,9 @@ export class BoardDiscovery extends CoreClientAware {
if (newState[port.address]) {
const [, knownBoards] = newState[port.address];
console.warn(
`Port '${port.address}' was already available. Known boards before override: ${JSON.stringify(
`Port '${
port.address
}' was already available. Known boards before override: ${JSON.stringify(
knownBoards
)}`
);

View File

@ -45,7 +45,8 @@ import { InstallWithProgress } from './grpc-installable';
@injectable()
export class BoardsServiceImpl
extends CoreClientAware
implements BoardsService {
implements BoardsService
{
@inject(ILogger)
protected logger: ILogger;
@ -247,7 +248,10 @@ export class BoardsServiceImpl
return boards;
}
async getBoardUserFields(options: { fqbn: string, protocol: string }): Promise<BoardUserField[]> {
async getBoardUserFields(options: {
fqbn: string;
protocol: string;
}): Promise<BoardUserField[]> {
await this.coreClientProvider.initialized;
const coreClient = await this.coreClient();
const { client, instance } = coreClient;
@ -257,25 +261,23 @@ export class BoardsServiceImpl
supportedUserFieldsReq.setFqbn(options.fqbn);
supportedUserFieldsReq.setProtocol(options.protocol);
const supportedUserFieldsResp = await new Promise<SupportedUserFieldsResponse>(
(resolve, reject) => {
const supportedUserFieldsResp =
await new Promise<SupportedUserFieldsResponse>((resolve, reject) => {
client.supportedUserFields(supportedUserFieldsReq, (err, resp) => {
(!!err ? reject : resolve)(!!err ? err : resp)
})
}
);
return supportedUserFieldsResp.getUserFieldsList().map(e => {
(!!err ? reject : resolve)(!!err ? err : resp);
});
});
return supportedUserFieldsResp.getUserFieldsList().map((e) => {
return {
toolId: e.getToolId(),
name: e.getName(),
label: e.getLabel(),
secret: e.getSecret(),
value: "",
value: '',
};
});
}
async search(options: { query?: string }): Promise<BoardsPackage[]> {
await this.coreClientProvider.initialized;
const coreClient = await this.coreClient();
@ -408,6 +410,10 @@ export class BoardsServiceImpl
req.setVersion(version);
console.info('>>> Starting boards package installation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.platformInstall(req);
resp.on(
'data',
@ -418,7 +424,7 @@ export class BoardsServiceImpl
);
await new Promise<void>((resolve, reject) => {
resp.on('end', () => {
this.boardDiscovery.startBoardListWatch(coreClient)
this.boardDiscovery.startBoardListWatch(coreClient);
resolve();
});
resp.on('error', (error) => {
@ -456,6 +462,10 @@ export class BoardsServiceImpl
req.setPlatformPackage(platform);
console.info('>>> Starting boards package uninstallation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.platformUninstall(req);
resp.on(
'data',
@ -466,7 +476,7 @@ export class BoardsServiceImpl
);
await new Promise<void>((resolve, reject) => {
resp.on('end', () => {
this.boardDiscovery.startBoardListWatch(coreClient)
this.boardDiscovery.startBoardListWatch(coreClient);
resolve();
});
resp.on('error', reject);

View File

@ -271,6 +271,10 @@ export class LibraryServiceImpl
req.setNoDeps(!options.installDependencies);
console.info('>>> Starting library package installation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.libraryInstall(req);
resp.on(
'data',
@ -322,6 +326,10 @@ export class LibraryServiceImpl
if (typeof overwrite === 'boolean') {
req.setOverwrite(overwrite);
}
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.zipLibraryInstall(req);
resp.on(
'data',
@ -354,6 +362,10 @@ export class LibraryServiceImpl
req.setVersion(item.installedVersion!);
console.info('>>> Starting library package uninstallation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.libraryUninstall(req);
resp.on(
'data',