import { Injectable , NgZone } from '@angular/core' ; import { Action , Store } from '@ngrx/store' ; import { DeviceConnectionState , FirmwareJson , FirmwareUpgradeIpcResponse , IpcEvents , IpcResponse , LogService , SaveUserConfigurationData , UpdateFirmwareData , UploadFileData } from 'uhk-common' ; import { AppState } from '../store' ; import { IpcCommonRenderer } from './ipc-common-renderer' ; import { ConnectionStateChangedAction , CurrentlyUpdatingModuleAction , ReadConfigSizesReplyAction , RecoveryDeviceReplyAction , RecoveryModuleReplyAction , SaveConfigurationReplyAction , SetPrivilegeOnLinuxReplyAction , UpdateFirmwareJsonAction , UpdateFirmwareReplyAction } from '../store/actions/device' ; import { LoadConfigFromDeviceReplyAction , LoadUserConfigurationFromFileAction } from '../store/actions/user-config' ; import { LoadUserConfigurationHistorySuccessAction } from '../store/actions/user-configuration-history.actions' ; @ Injectable ( ) export class DeviceRendererService { constructor ( private store , private zone , private ipcRenderer , private logService ) { this . registerEvents ( ) ; this . logService . misc ( '[DeviceRendererService] init success ' ) ; } setPrivilegeOnLinux ( ) { this . ipcRenderer . send ( IpcEvents . device . setPrivilegeOnLinux ) ; } saveUserConfiguration ( data ) { this . ipcRenderer . send ( IpcEvents . device . saveUserConfiguration , JSON . stringify ( data ) ) ; } loadConfigurationFromKeyboard ( ) { this . ipcRenderer . send ( IpcEvents . device . loadConfigurations ) ; } updateFirmware ( data ) { this . ipcRenderer . send ( IpcEvents . device . updateFirmware , JSON . stringify ( data ) ) ; } startConnectionPoller ( ) { this . ipcRenderer . send ( IpcEvents . device . startConnectionPoller ) ; } recoveryDevice ( ) { this . ipcRenderer . send ( IpcEvents . device . recoveryDevice ) ; } recoveryModule ( moduleId ) { this . ipcRenderer . send ( IpcEvents . device . recoveryModule , moduleId ) ; } enableUsbStackTest ( ) { this . ipcRenderer . send ( IpcEvents . device . enableUsbStackTest ) ; } readConfigSizes ( ) { this . ipcRenderer . send ( IpcEvents . device . readConfigSizes ) ; } loadUserConfigurationHistory ( ) { this . ipcRenderer . send ( IpcEvents . device . loadUserConfigHistory ) ; } getUserConfigurationFromHistory ( fileName ) { this . ipcRenderer . send ( IpcEvents . device . getUserConfigFromHistory , fileName ) ; } private registerEvents ( ) { this . ipcRenderer . on ( IpcEvents . device . deviceConnectionStateChanged , ( event , arg ) => { this . dispachStoreAction ( new ConnectionStateChangedAction ( arg ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . recoveryDeviceReply , ( event , response ) => { this . dispachStoreAction ( new RecoveryDeviceReplyAction ( response ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . recoveryModuleReply , ( event , response ) => { this . dispachStoreAction ( new RecoveryModuleReplyAction ( response ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . setPrivilegeOnLinuxReply , ( event , response ) => { this . dispachStoreAction ( new SetPrivilegeOnLinuxReplyAction ( response ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . saveUserConfigurationReply , ( event , response ) => { this . dispachStoreAction ( new SaveConfigurationReplyAction ( response ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . loadConfigurationReply , ( event , response ) => { this . dispachStoreAction ( new LoadConfigFromDeviceReplyAction ( JSON . parse ( response ) ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . updateFirmwareJson , ( event , data ) => { this . dispachStoreAction ( new UpdateFirmwareJsonAction ( data ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . moduleFirmwareUpgrading , ( event , response ) => { this . dispachStoreAction ( new CurrentlyUpdatingModuleAction ( response ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . updateFirmwareReply , ( event , response ) => { this . dispachStoreAction ( new UpdateFirmwareReplyAction ( response ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . readConfigSizesReply , ( event , response ) => { this . dispachStoreAction ( new ReadConfigSizesReplyAction ( JSON . parse ( response ) ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . loadUserConfigHistoryReply , ( event , response ) => { this . dispachStoreAction ( new LoadUserConfigurationHistorySuccessAction ( response ) ) ; } ) ; this . ipcRenderer . on ( IpcEvents . device . getUserConfigFromHistoryReply , ( event , response ) => { this . dispachStoreAction ( new LoadUserConfigurationFromFileAction ( { uploadFileData : response , autoSave : false } ) ) ; } ) ; } private dispachStoreAction ( action ) { this . logService . misc ( `[DeviceRendererService] dispatch action ${ action . type }` ) ; this . zone . run ( ( ) => this . store . dispatch ( action ) ) ; } }