feat(device): Ensure serial the device communication (#508)
This commit is contained in:
committed by
László Monda
parent
19d7b8ebfe
commit
3de9181687
6
packages/uhk-agent/src/models/queue-entry.ts
Normal file
6
packages/uhk-agent/src/models/queue-entry.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface QueueEntry {
|
||||
method: Function;
|
||||
bind: any;
|
||||
params?: any[];
|
||||
asynchronous?: boolean;
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import 'rxjs/add/operator/distinctUntilChanged';
|
||||
|
||||
import { saveTmpFirmware } from '../util/save-extract-firmware';
|
||||
import { TmpFirmware } from '../models/tmp-firmware';
|
||||
import { QueueManager } from './queue-manager';
|
||||
|
||||
/**
|
||||
* IpcMain pair of the UHK Communication
|
||||
@@ -35,16 +36,43 @@ import { TmpFirmware } from '../models/tmp-firmware';
|
||||
export class DeviceService {
|
||||
private pollTimer$: Subscription;
|
||||
private connected = false;
|
||||
private queueManager = new QueueManager();
|
||||
|
||||
constructor(private logService: LogService,
|
||||
private win: Electron.BrowserWindow,
|
||||
private device: UhkHidDevice,
|
||||
private operations: UhkOperations) {
|
||||
this.pollUhkDevice();
|
||||
ipcMain.on(IpcEvents.device.saveUserConfiguration, this.saveUserConfiguration.bind(this));
|
||||
ipcMain.on(IpcEvents.device.loadConfigurations, this.loadConfigurations.bind(this));
|
||||
ipcMain.on(IpcEvents.device.updateFirmware, this.updateFirmware.bind(this));
|
||||
|
||||
ipcMain.on(IpcEvents.device.saveUserConfiguration, (...args: any[]) => {
|
||||
this.queueManager.add({
|
||||
method: this.saveUserConfiguration,
|
||||
bind: this,
|
||||
params: args,
|
||||
asynchronous: true
|
||||
});
|
||||
});
|
||||
|
||||
ipcMain.on(IpcEvents.device.loadConfigurations, (...args: any[]) => {
|
||||
this.queueManager.add({
|
||||
method: this.loadConfigurations,
|
||||
bind: this,
|
||||
params: args,
|
||||
asynchronous: true
|
||||
});
|
||||
});
|
||||
|
||||
ipcMain.on(IpcEvents.device.updateFirmware, (...args: any[]) => {
|
||||
this.queueManager.add({
|
||||
method: this.updateFirmware,
|
||||
bind: this,
|
||||
params: args,
|
||||
asynchronous: true
|
||||
});
|
||||
});
|
||||
|
||||
ipcMain.on(IpcEvents.device.startConnectionPoller, this.pollUhkDevice.bind(this));
|
||||
|
||||
logService.debug('[DeviceService] init success');
|
||||
}
|
||||
|
||||
@@ -144,16 +172,15 @@ export class DeviceService {
|
||||
this.logService.info('[DeviceService] Device connection checker stopped.');
|
||||
}
|
||||
|
||||
public async updateFirmware(event: Electron.Event, data?: string): Promise<void> {
|
||||
public async updateFirmware(event: Electron.Event, args?: Array<string>): Promise<void> {
|
||||
const response = new IpcResponse();
|
||||
|
||||
let firmwarePathData: TmpFirmware;
|
||||
|
||||
try {
|
||||
this.stopPollTimer();
|
||||
|
||||
if (data && data.length > 0) {
|
||||
firmwarePathData = await saveTmpFirmware(data);
|
||||
if (args && args.length > 0) {
|
||||
firmwarePathData = await saveTmpFirmware(args[0]);
|
||||
await this.operations.updateRightFirmware(firmwarePathData.rightFirmwarePath);
|
||||
await this.operations.updateLeftModule(firmwarePathData.leftFirmwarePath);
|
||||
}
|
||||
@@ -216,8 +243,9 @@ export class DeviceService {
|
||||
return configSize;
|
||||
}
|
||||
|
||||
private async saveUserConfiguration(event: Electron.Event, json: string): Promise<void> {
|
||||
private async saveUserConfiguration(event: Electron.Event, args: Array<string>): Promise<void> {
|
||||
const response = new IpcResponse();
|
||||
const json = args[0];
|
||||
|
||||
try {
|
||||
this.logService.debug('[DeviceService] USB[T]: Write user configuration to keyboard');
|
||||
|
||||
30
packages/uhk-agent/src/services/queue-manager.ts
Normal file
30
packages/uhk-agent/src/services/queue-manager.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { QueueEntry } from '../models/queue-entry';
|
||||
|
||||
export class QueueManager {
|
||||
private _queue: QueueEntry[] = [];
|
||||
private _processing = false;
|
||||
|
||||
async add(entry: QueueEntry): Promise<void> {
|
||||
this._queue.push(entry);
|
||||
this.process();
|
||||
}
|
||||
|
||||
private async process(): Promise<void> {
|
||||
if (this._processing) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._processing = true;
|
||||
|
||||
while (this._queue.length !== 0) {
|
||||
const entry = this._queue.splice(0, 1)[0];
|
||||
if (entry.asynchronous) {
|
||||
await entry.method.apply(entry.bind, entry.params);
|
||||
} else {
|
||||
entry.method.apply(entry.bind, entry.params);
|
||||
}
|
||||
}
|
||||
|
||||
this._processing = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user