Close serial port connection before flashing firmware (#688)

This commit is contained in:
Alberto Iannaccone 2021-12-15 10:31:12 +01:00 committed by GitHub
parent cc5764e536
commit c064673ce1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 22 deletions

View File

@ -4,6 +4,7 @@ import {
} from '../common/protocol/arduino-firmware-uploader'; } from '../common/protocol/arduino-firmware-uploader';
import { injectable, inject, named } from 'inversify'; import { injectable, inject, named } from 'inversify';
import { ExecutableService } from '../common/protocol'; import { ExecutableService } from '../common/protocol';
import { SerialService } from '../common/protocol/serial-service';
import { getExecPath, spawnCommand } from './exec-util'; import { getExecPath, spawnCommand } from './exec-util';
import { ILogger } from '@theia/core/lib/common/logger'; import { ILogger } from '@theia/core/lib/common/logger';
@ -18,6 +19,9 @@ export class ArduinoFirmwareUploaderImpl implements ArduinoFirmwareUploader {
@named('fwuploader') @named('fwuploader')
protected readonly logger: ILogger; protected readonly logger: ILogger;
@inject(SerialService)
protected readonly serialService: SerialService;
protected onError(error: any): void { protected onError(error: any): void {
this.logger.error(error); this.logger.error(error);
} }
@ -66,15 +70,26 @@ export class ArduinoFirmwareUploaderImpl implements ArduinoFirmwareUploader {
} }
async flash(firmware: FirmwareInfo, port: string): Promise<string> { async flash(firmware: FirmwareInfo, port: string): Promise<string> {
return await this.runCommand([ let output;
'firmware', try {
'flash', this.serialService.uploadInProgress = true;
'--fqbn', await this.serialService.disconnect();
firmware.board_fqbn, output = await this.runCommand([
'--address', 'firmware',
port, 'flash',
'--module', '--fqbn',
`${firmware.module}@${firmware.firmware_version}`, firmware.board_fqbn,
]); '--address',
port,
'--module',
`${firmware.module}@${firmware.firmware_version}`,
]);
} catch (e) {
throw e;
} finally {
this.serialService.uploadInProgress = false;
this.serialService.connectSerialIfRequired();
return output;
}
} }
} }

View File

@ -203,7 +203,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
// #endregion Theia customizations // #endregion Theia customizations
// Monitor client provider per connected frontend. // Serial client provider per connected frontend.
bind(ConnectionContainerModule).toConstantValue( bind(ConnectionContainerModule).toConstantValue(
ConnectionContainerModule.create(({ bind, bindBackendService }) => { ConnectionContainerModule.create(({ bind, bindBackendService }) => {
bind(MonitorClientProvider).toSelf().inSingletonScope(); bind(MonitorClientProvider).toSelf().inSingletonScope();
@ -260,17 +260,14 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
) )
.inSingletonScope(); .inSingletonScope();
bind(ArduinoFirmwareUploaderImpl).toSelf().inSingletonScope(); // Singleton per BE, each FE connection gets its proxy.
bind(ArduinoFirmwareUploader).toService(ArduinoFirmwareUploaderImpl); bind(ConnectionContainerModule).toConstantValue(
bind(BackendApplicationContribution).toService(ArduinoFirmwareUploaderImpl); ConnectionContainerModule.create(({ bind, bindBackendService }) => {
bind(ConnectionHandler) bind(ArduinoFirmwareUploaderImpl).toSelf().inSingletonScope();
.toDynamicValue( bind(ArduinoFirmwareUploader).toService(ArduinoFirmwareUploaderImpl);
(context) => bindBackendService(ArduinoFirmwareUploaderPath, ArduinoFirmwareUploader);
new JsonRpcConnectionHandler(ArduinoFirmwareUploaderPath, () => })
context.container.get(ArduinoFirmwareUploader) );
)
)
.inSingletonScope();
// Logger for the Arduino daemon // Logger for the Arduino daemon
bind(ILogger) bind(ILogger)