From 0aa9c73b4b0d37545cee912426d23b0cd689e7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Sun, 24 Jun 2018 19:56:11 +0200 Subject: [PATCH] feat: log firmware version before upgrading firmware (#693) --- packages/uhk-agent/src/electron-main.ts | 2 +- packages/uhk-agent/src/models/tmp-firmware.ts | 1 + .../uhk-agent/src/services/device.service.ts | 24 ++++++++++++++++--- .../util/get-package-json-from-path-async.ts | 13 ++++++++++ packages/uhk-agent/src/util/index.ts | 3 +++ .../src/util/save-extract-firmware.ts | 4 ++-- 6 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 packages/uhk-agent/src/util/get-package-json-from-path-async.ts create mode 100644 packages/uhk-agent/src/util/index.ts diff --git a/packages/uhk-agent/src/electron-main.ts b/packages/uhk-agent/src/electron-main.ts index 921e8030..decb2798 100644 --- a/packages/uhk-agent/src/electron-main.ts +++ b/packages/uhk-agent/src/electron-main.ts @@ -106,7 +106,7 @@ function createWindow() { uhkHidDeviceService = new UhkHidDevice(logger, options); uhkBlhost = new UhkBlhost(logger, packagesDir); uhkOperations = new UhkOperations(logger, uhkBlhost, uhkHidDeviceService, packagesDir); - deviceService = new DeviceService(logger, win, uhkHidDeviceService, uhkOperations); + deviceService = new DeviceService(logger, win, uhkHidDeviceService, uhkOperations, packagesDir); appUpdateService = new AppUpdateService(logger, win, app); appService = new AppService(logger, win, deviceService, options, uhkHidDeviceService); sudoService = new SudoService(logger, options); diff --git a/packages/uhk-agent/src/models/tmp-firmware.ts b/packages/uhk-agent/src/models/tmp-firmware.ts index 8960976a..f3cff3aa 100644 --- a/packages/uhk-agent/src/models/tmp-firmware.ts +++ b/packages/uhk-agent/src/models/tmp-firmware.ts @@ -3,5 +3,6 @@ import { SynchrounousResult } from 'tmp'; export interface TmpFirmware { rightFirmwarePath: string; leftFirmwarePath: string; + packageJsonPath: string; tmpDirectory: SynchrounousResult; } diff --git a/packages/uhk-agent/src/services/device.service.ts b/packages/uhk-agent/src/services/device.service.ts index 527ec84a..87ad3935 100644 --- a/packages/uhk-agent/src/services/device.service.ts +++ b/packages/uhk-agent/src/services/device.service.ts @@ -15,6 +15,7 @@ import { deviceConnectionStateComparer, snooze, UhkHidDevice, UhkOperations } fr import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import { emptyDir } from 'fs-extra'; +import * as path from 'path'; import 'rxjs/add/observable/interval'; import 'rxjs/add/operator/startWith'; @@ -22,10 +23,14 @@ import 'rxjs/add/operator/map'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/distinctUntilChanged'; -import { saveTmpFirmware } from '../util/save-extract-firmware'; import { TmpFirmware } from '../models/tmp-firmware'; import { QueueManager } from './queue-manager'; -import { backupUserConfiguration, getBackupUserConfigurationContent } from '../util/backup-user-confoguration'; +import { + backupUserConfiguration, + getBackupUserConfigurationContent, + getPackageJsonFromPathAsync, + saveTmpFirmware +} from '../util'; /** * IpcMain pair of the UHK Communication @@ -40,7 +45,8 @@ export class DeviceService { constructor(private logService: LogService, private win: Electron.BrowserWindow, private device: UhkHidDevice, - private operations: UhkOperations) { + private operations: UhkOperations, + private rootDir: string) { this.pollUhkDevice(); ipcMain.on(IpcEvents.device.saveUserConfiguration, (...args: any[]) => { @@ -146,15 +152,27 @@ export class DeviceService { let firmwarePathData: TmpFirmware; try { + const hardwareModules = await this.getHardwareModules(false); + this.logService.debug('Device right firmware version:', hardwareModules.rightModuleInfo.firmwareVersion); + this.logService.debug('Device left firmware version:', hardwareModules.leftModuleInfo.firmwareVersion); + this.device.resetDeviceCache(); this.stopPollTimer(); if (args && args.length > 0) { firmwarePathData = await saveTmpFirmware(args[0]); + + const packageJson = await getPackageJsonFromPathAsync(firmwarePathData.packageJsonPath); + this.logService.debug('New firmware version:', packageJson.firmwareVersion); + await this.operations.updateRightFirmware(firmwarePathData.rightFirmwarePath); await this.operations.updateLeftModule(firmwarePathData.leftFirmwarePath); } else { + const packageJsonPath = path.join(this.rootDir, 'packages/firmware/package.json'); + const packageJson = await getPackageJsonFromPathAsync(packageJsonPath); + this.logService.debug('New firmware version:', packageJson.firmwareVersion); + await this.operations.updateRightFirmware(); await this.operations.updateLeftModule(); } diff --git a/packages/uhk-agent/src/util/get-package-json-from-path-async.ts b/packages/uhk-agent/src/util/get-package-json-from-path-async.ts new file mode 100644 index 00000000..41a12649 --- /dev/null +++ b/packages/uhk-agent/src/util/get-package-json-from-path-async.ts @@ -0,0 +1,13 @@ +import * as fs from 'fs'; + +export const getPackageJsonFromPathAsync = async (filePath: string): Promise => { + return new Promise((resolve, reject) => { + fs.readFile(filePath, {encoding: 'utf-8'}, (err, data) => { + if (err) { + return reject(err); + } + + resolve(JSON.parse(data)); + }); + }); +}; diff --git a/packages/uhk-agent/src/util/index.ts b/packages/uhk-agent/src/util/index.ts new file mode 100644 index 00000000..a19e8257 --- /dev/null +++ b/packages/uhk-agent/src/util/index.ts @@ -0,0 +1,3 @@ +export * from './backup-user-confoguration'; +export * from './get-package-json-from-path-async'; +export * from './save-extract-firmware'; diff --git a/packages/uhk-agent/src/util/save-extract-firmware.ts b/packages/uhk-agent/src/util/save-extract-firmware.ts index b7163a60..d67c1661 100644 --- a/packages/uhk-agent/src/util/save-extract-firmware.ts +++ b/packages/uhk-agent/src/util/save-extract-firmware.ts @@ -16,8 +16,8 @@ export async function saveTmpFirmware(data: string): Promise { return { tmpDirectory, rightFirmwarePath: path.join(tmpDirectory.name, 'devices/uhk60-right/firmware.hex'), - leftFirmwarePath: path.join(tmpDirectory.name, 'modules/uhk60-left.bin') - + leftFirmwarePath: path.join(tmpDirectory.name, 'modules/uhk60-left.bin'), + packageJsonPath: path.join(tmpDirectory.name, 'package.json') }; }