import { eiotDeviceManagerService } from '../device-manager/eiot-device-manager.service'; import { eiotSessionVarsService } from '../session-vars/eiot-session-vars.service'; class EIoTWebcamService { webcamModuleIdx: number; webcamModuleInterface: string; constructor() { this.webcamModuleIdx = Number(eiotSessionVarsService.DevMgrComps?.webcam?.Idx); this.webcamModuleInterface = String(eiotSessionVarsService.DevMgrComps?.webcam?.Interface); } //#region Private Methods updateParams() { this.webcamModuleIdx = Number(eiotSessionVarsService.DevMgrComps?.webcam?.Idx); this.webcamModuleInterface = String(eiotSessionVarsService.DevMgrComps?.webcam?.Interface); } //#endregion Private Methods //#region Public Methods async tryStart(connCb:any, connErrCb:any) { this.isStarted((started:any)=> { if(started) { connCb(true); } else { this.start((success:any) => { connCb(success); }, (err:any) => { connErrCb(err); }); } }, (err:any) => { connErrCb(err); }); } async start(connCb:any, connErrCb:any) { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() proxy.on('onCameraStart_Event',function() { connCb(true); }); proxy.invoke('Webcam_StartCamera', this.webcamModuleIdx, this.webcamModuleInterface).done((res:any) => { if(!res) connCb(false); }).fail((err:any) => { console.error(JSON.stringify(err)); connErrCb("Failed to invoke method on Webcam_StartCamera"); }); } catch (err) { console.error(JSON.stringify(err)); } } async isStarted(connCb:any, connErrCb:any) { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() proxy.invoke('Webcam_IsStarted', this.webcamModuleIdx, this.webcamModuleInterface).done((res:any) => { connCb(res); }).fail((err:any) => { console.error(JSON.stringify(err)); connErrCb("Failed to invoke method on Webcam_IsStarted"); }); } catch (err) { console.error(JSON.stringify(err)); } } async capture(connCb:any, connErrCb:any) { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() proxy.invoke('Webcam_Capture', this.webcamModuleIdx, this.webcamModuleInterface).done((res:any) => { connCb(res); }).fail((err:any) => { console.error(JSON.stringify(err)); connErrCb("Failed to invoke method on Webcam_Capture"); }); } catch (err) { console.error(JSON.stringify(err)); } } async saveCapturedImage(fileName:any, base64:any, connCb:any, connErrCb:any) { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() proxy.invoke('Webcam_SaveCapturedImage', this.webcamModuleIdx, this.webcamModuleInterface , fileName, base64).done((res:any) => { connCb(res); }).fail((err:any) => { console.error(JSON.stringify(err)); connErrCb("Failed to invoke method on Webcam_SaveCapturedImage"); }); } catch (err) { console.error(JSON.stringify(err)); } } async saveLastCapturedImage(fileName:any, connCb:any, connErrCb:any) { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() proxy.invoke('Webcam_SaveLastCapturedImage', this.webcamModuleIdx, this.webcamModuleInterface , fileName).done((res:any) => { connCb(res); }).fail((err:any) => { console.error(JSON.stringify(err)); connErrCb("Failed to invoke method on Webcam_SaveLastCapturedImage"); }); } catch (err) { console.error(JSON.stringify(err)); } } async getCurrentFrame(connCb:any, connErrCb:any) { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() proxy.invoke('Webcam_GetCurrentFrame', this.webcamModuleIdx, this.webcamModuleInterface).done((res:any) => { connCb(res); }).fail((err:any) => { console.error(JSON.stringify(err)); connErrCb("Failed to invoke method on Webcam_GetCurrentFrame"); }); } catch (err) { console.error(JSON.stringify(err)); } } async stopCamera(connCb:any, connErrCb:any) { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() proxy.on('onCameraStop_Event',function() { connCb(true); }); proxy.invoke('Webcam_StopCamera', this.webcamModuleIdx, this.webcamModuleInterface).done((res:any) => { if(!res) { connCb(false); } }).fail((err:any) => { console.error(JSON.stringify(err)); connErrCb("Failed to invoke method on Webcam_StopCamera"); }); } catch (err) { console.error(JSON.stringify(err)); } } //#endregion Public Methods } export const eiotWebcamService = new EIoTWebcamService();