-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy patharduino-firmware-uploader-impl.ts
97 lines (85 loc) · 2.63 KB
/
arduino-firmware-uploader-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
import {
ArduinoFirmwareUploader,
FirmwareInfo,
} from '../common/protocol/arduino-firmware-uploader';
import { injectable, inject, named } from '@theia/core/shared/inversify';
import { ExecutableService, Port } from '../common/protocol';
import { getExecPath, spawnCommand } from './exec-util';
import { ILogger } from '@theia/core/lib/common/logger';
import { MonitorManager } from './monitor-manager';
@injectable()
export class ArduinoFirmwareUploaderImpl implements ArduinoFirmwareUploader {
@inject(ExecutableService)
protected executableService: ExecutableService;
protected _execPath: string | undefined;
@inject(ILogger)
@named('fwuploader')
protected readonly logger: ILogger;
@inject(MonitorManager)
protected readonly monitorManager: MonitorManager;
protected onError(error: any): void {
this.logger.error(error);
}
async getExecPath(): Promise<string> {
if (this._execPath) {
return this._execPath;
}
this._execPath = await getExecPath('arduino-fwuploader');
return this._execPath;
}
async runCommand(args: string[]): Promise<any> {
const execPath = await this.getExecPath();
return await spawnCommand(`"${execPath}"`, args, this.onError.bind(this));
}
async uploadCertificates(command: string): Promise<any> {
return await this.runCommand(['certificates', 'flash', command]);
}
async list(fqbn?: string): Promise<FirmwareInfo[]> {
const fqbnFlag = fqbn ? ['--fqbn', fqbn] : [];
const firmwares: FirmwareInfo[] =
JSON.parse(
await this.runCommand([
'firmware',
'list',
...fqbnFlag,
'--format',
'json',
])
) || [];
return firmwares.reverse();
}
async updatableBoards(): Promise<string[]> {
return (await this.list()).reduce(
(a, b) => (a.includes(b.board_fqbn) ? a : [...a, b.board_fqbn]),
[] as string[]
);
}
async availableFirmwares(fqbn: string): Promise<FirmwareInfo[]> {
return await this.list(fqbn);
}
async flash(firmware: FirmwareInfo, port: Port): Promise<string> {
let output;
const board = {
name: firmware.board_name,
fqbn: firmware.board_fqbn,
};
try {
await this.monitorManager.notifyUploadStarted(board, port);
output = await this.runCommand([
'firmware',
'flash',
'--fqbn',
firmware.board_fqbn,
'--address',
port.address,
'--module',
`${firmware.module}@${firmware.firmware_version}`,
]);
} catch (e) {
throw e;
} finally {
await this.monitorManager.notifyUploadFinished(board, port);
return output;
}
}
}