import { eiotDeviceManagerService } from '../device-manager/eiot-device-manager.service'; import { eiotSessionVarsService } from '../session-vars/eiot-session-vars.service'; class EIoTWeatherStationService { weatherModuleIdx:number; weatherModuleInterface:string; constructor() { this.weatherModuleIdx = Number(eiotSessionVarsService.DevMgrComps?.weather?.Idx); this.weatherModuleInterface = String(eiotSessionVarsService.DevMgrComps?.weather?.Interface); } updateParams() { this.weatherModuleIdx = Number(eiotSessionVarsService.DevMgrComps?.weather?.Idx); this.weatherModuleInterface = String(eiotSessionVarsService.DevMgrComps?.weather?.Interface); } async tryConnect() { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() return await new Promise((resolve) => { proxy.invoke('WeatherStation_IsConnected', this.weatherModuleIdx, this.weatherModuleInterface).done((res1:boolean) => { if(res1) { console.log('WeatherStation is connected.'); resolve(true); } else { proxy.off('onWeatherStationConnected_Event'); proxy.on('onWeatherStationConnected_Event', (evt:any) => { resolve(true); }); proxy.off('onWeatherStationConnectedError_Event'); proxy.on('onWeatherStationConnectedError_Event', (evt:any) => { resolve(evt.Payload); }); proxy.invoke('WeatherStation_Connect', this.weatherModuleIdx, this.weatherModuleInterface).done((res2:any) => { //console.log(res2); console.log('WeatherStation connected successfully.'); }).fail((err:any) => { console.error(JSON.stringify(err)); console.log("Failed to invoke method on WeatherStation_Connect"); }); } }).fail((err:any) => { console.error(JSON.stringify(err)); console.log("Failed to invoke method on WeatherStation_IsConnected"); resolve(false) }); }) } // async getStatus() { // for (let index = 0; index < 20; index++) { // console.log('Fetch WeatherStation_GetStatus ' + (index + 1) + ' time.'); // let res: Boolean = false; // let weather_res: any; // await new Promise((resolve) => // setTimeout(async () => { // const res_getWeather:any = await this.getWeatherData(); // console.log(res_getWeather); // Object.keys(res_getWeather).forEach(o => { // if (o === 'GPSLocation' && res_getWeather[o] === null) { // console.log(res_getWeather[o]); // console.log(o); // res = false // } // }) // weather_res = res_getWeather; // resolve(true); // }, 500) // ); // if (res == true) { // //break; // return weather_res // } else { // if (index == 19) return weather_res; // continue; // } // } // } async getStatus() { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() return await new Promise((resolve) => { proxy.invoke('WeatherStation_GetStatus', this.weatherModuleIdx, this.weatherModuleInterface).done((weatherData:any) => { //console.log(weatherData); resolve(weatherData) }).fail((err:any) => { console.error(JSON.stringify(err)); console.log("Failed to invoke method on Device Manager"); resolve(false) }); }) } async waitForReady() { const proxy = await eiotDeviceManagerService.getSignalRConn(); this.updateParams() await new Promise((resolve) => { proxy.invoke('WeatherStation_IsBusy', this.weatherModuleIdx, this.weatherModuleInterface).done((isBusy:boolean) => { if(isBusy) { setTimeout(()=>{ this.waitForReady(); },500); } else { resolve(true) } }).fail((err:any) => { console.error(JSON.stringify(err)); resolve(true) }); }) } } export const eiotWeatherStationService = new EIoTWeatherStationService()