/*! phoscon-app 2024-05-27 */

const SEARCH_SENSORS_DISABLED=0,SEARCH_SENSORS_ENABLED=1,FAST_POLL_DELAY=500,SLOW_POLL_DELAY=5e3;var pollState=SEARCH_SENSORS_DISABLED,pollDelay=SLOW_POLL_DELAY;function pollDone(){pollState===SEARCH_SENSORS_DISABLED?(pollDelay=SLOW_POLL_DELAY,(app.sensorScan&&app.sensorScan.isActive()||app.config.get("permitjoin")>0)&&app.config.save({permitjoin:0},{patch:!0})):pollState===SEARCH_SENSORS_ENABLED&&(pollDelay=FAST_POLL_DELAY,app.sensorScan&&!app.sensorScan.isActive()&&app.sensorScan.start()),app.sensorScan&&restGetResource(app.sensorScan).then(res=>{"lastscan"in res&&"active"===res.lastscan&&(console.log(res),Object.keys(res).forEach(x=>{if(isNaN(x));else{app.sensors.get(x)||restGetResource(app.sensors)}}))}),setTimeout(pollDone,pollDelay)}setTimeout(pollDone,1e3);export function enableSensorSearch(){-1==location.hash.indexOf("scan")&&(location.hash+="?scan"),pollState!==SEARCH_SENSORS_ENABLED&&(pollState=SEARCH_SENSORS_ENABLED,console.log("enable sensor search"))};export function disableSensorSearch(){pollState!==SEARCH_SENSORS_DISABLED&&(pollState=SEARCH_SENSORS_DISABLED,console.log("disable sensor search"))};disableSensorSearch();export function blinkAllLights(){};export function renameSwitch(sw,doneCallback){for(var name=sw.get("product")||translate("text_switch"),num=1;app.switches.find(sw2=>sw!==sw2&&(1===num&&sw2.get("name")===name||sw2.get("name")===name+" ("+String(num)+")"));)num++;num>1&&(name+=" ("+String(num)+")"),sw.set({name:name}),function updateSensors(sensorIds){var s=app.sensors.get(sensorIds.pop());s?s.save({name:name},{patch:!0,success:()=>{updateSensors(sensorIds)},error:doneCallback}):doneCallback()}(_.reduce(sw.get("endpoints"),function(acc,ep){return acc.push(ep.id),acc},[]))};export function renameDevice(sn,doneCallback){for(var name=sn.get("product")||translate("text_sensor"),num=1;app.sensors.find(sn2=>sn!==sn2&&(1===num&&sn2.get("name")===name||sn2.get("name")===name+" ("+String(num)+")"));)num++;num>1&&(name+=" ("+String(num)+")"),sn.save({name:name},{patch:!0,success:()=>{console.log("rename success")},error:err=>{console.debug(err)}})};