/*! phoscon-app 2024-10-07 */

import{types}from"./constants.js";export function discoverGateways(service){queryDresdenDiscovery("https://phoscon.de/discover").catch(err=>{console.log("Failed Discovery at https://phoscon.de/discover - trying dresden-light.appspot.com"),queryDresdenDiscovery("https://dresden-light.appspot.com/discover")}).then(gws=>{var res=[];if(gws.forEach(gw=>{var rsp={};rsp.id=gw.id,rsp.name=gw.name,rsp.ip=gw.internalipaddress,gw.internalport&&(rsp.port=gw.internalport),rsp.reachable=!0;var gw2=app.gateways.get(rsp.id);if(gw2)gw2.set(rsp);else{if(addGateway(rsp),!(gw2=app.gateways.get(gw.id)))return;localStorage.setItem("app.gateways",JSON.stringify(app.gateways.toJSON())),app.Devicetype.sendMessage("GATEWAYS_DATA",JSON.stringify(app.gateways.toJSON()))}gw.internalipaddress.match(/^(192\.168\.|10\.|172\.(1[6-9]|2\d|3[01]))/)&&res.push(gw.internalipaddress+(rsp.port?":"+rsp.port:""))}),res.length>0)return res;throw"empty"}).then(res=>{service.send({ADD_GW:res}),app.probeWorker.postMessage({scan:!0})}).catch(()=>{service.send("DISCOVERY_FAILED")})};function queryDresdenDiscovery(url){return new Promise((resolve,reject)=>navigator.onLine?url&&"string"==typeof url?void $.ajax({url:url,cache:!1,timeout:3e3,success:function(json,status,xhr){return Array.isArray(json)?resolve(json):reject("unexpected response")},error:function(xhr,textStatus,errorThrown){reject(textStatus)}}):reject("invalid url parameter"):reject("offline"))}