import { eiotDeviceManagerService } from '../device-manager/eiot-device-manager.service'; import { eiotSessionVarsService } from '../session-vars/eiot-session-vars.service'; class EIoTGPSService { gpsModuleIdx: number; gpsModuleInterface: string; constructor() { this.gpsModuleIdx = Number(eiotSessionVarsService.DevMgrComps?.gps?.Idx); this.gpsModuleInterface = String(eiotSessionVarsService.DevMgrComps?.gps?.Interface); } updateParams() { this.gpsModuleIdx = Number(eiotSessionVarsService.DevMgrComps?.gps?.Idx); this.gpsModuleInterface = String(eiotSessionVarsService.DevMgrComps?.gps?.Interface); } async tryGetCoordinates() { for (let index = 0; index < 20; index++) { //console.log('Fetch IoT_GPS_GetCoordinates ' + (index + 1) + ' time.'); let res = false; let coord_res: any; await new Promise((resolve) => setTimeout(async () => { const coord = await this.getCoordinates(); //console.log(coord); res = !JSON.stringify(coord).includes('null'); coord_res = coord; resolve(true); }, 500) ); if (res) { //break; return coord_res } else { if (index == 19) return coord_res; continue; } } } async getCoordinates() { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() //console.log('Start invoke method on IoT_GPS_GetCoordinates'); return await new Promise((resolve, reject) => proxy .invoke( 'IoT_GPS_GetCoordinates', this.gpsModuleIdx, this.gpsModuleInterface ) .done((coord: any) => { //console.log('IoT_GPS_GetCoordinates return : ' + JSON.stringify(coord)); resolve(coord); }) .fail((err: any) => { console.error(JSON.stringify(err)); console.log('Failed to invoke method on IoT_GPS_GetCoordinates.'); reject(err); }) ); } catch (err) { console.error(JSON.stringify(err)); } } async tryStart() { for (let index = 0; index < 20; index++) { console.log('Check IoT_GPS_IsWatching ' + (index + 1) + ' time.'); let res: any = false; await new Promise((resolve) => setTimeout(async () => { res = await this.isStarted(); resolve(true); }, 500) ); if (res == true) { //break; return res; } else { if (index == 19) return res; continue; } } } async start() { try { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() return await new Promise((resolve, reject) => { proxy.off('IoT_GPS_Stop'); proxy.on('IoT_GPS_Stop', () => undefined); proxy .invoke('IoT_GPS_Watch', this.gpsModuleIdx, this.gpsModuleInterface) .done((res: any) => { console.log(res); if (res) { console.log('Starts the Windows location watching services..'); resolve(true); } else { console.log('Not start windows location watching services..'); resolve(false); } }) .fail((err: any) => { console.error(JSON.stringify(err)); reject(err); }); }) } catch (err) { console.error(JSON.stringify(err)); } } async isStarted():Promise { try { this.updateParams() const state = await eiotDeviceManagerService.getServiceStatus(); if (state === 1) { const proxy = await eiotDeviceManagerService.getSignalRConn(); //console.log('Start invoke method on IoT_GPS_IsWatching'); return await new Promise((resolve, reject) => proxy .invoke( 'IoT_GPS_IsWatching', this.gpsModuleIdx, this.gpsModuleInterface ) .done((res: boolean) => { //console.log('IoT_GPS_IsWatching return : ' + res); resolve(res); }) .fail((err: any) => { console.log('Failed to invoke method on GPS IsWatching'); reject(err); }) ); } else { await eiotDeviceManagerService.tryToConn(); return await this.isStarted(); } } catch (err) { console.error(JSON.stringify(err)); } } } export const eiotGPSService = new EIoTGPSService();