/*! phoscon-app 2025-08-28 */

function Poll(){const STATE_IDLE=1,STATE_WAIT=2,MAX_POLL_ERRORS=4;let init=!0,pos=0;const items=[];let timer,state=STATE_IDLE,timerInterval=5e3,errStat=0;function fetchSuccess(item,response,options){if(state===STATE_WAIT&&(state=STATE_IDLE),errStat>=MAX_POLL_ERRORS){app.trigger("gateway-status",{status:"reachable"});let bridgeid=app.config.attributes.bridgeid,ip=app.config.attributes.ipaddress,port=app.config.attributes.port;app.Devicetype.sendMessage("GW_REACHABLE",'{"bridgeid":"'+bridgeid+'","ip":"'+ip+'","port":"'+port+'"}')}errStat=0}function fetchError(item,response,options){if(state===STATE_WAIT&&(state=STATE_IDLE),0!==response.readyState&&"timeout"!==response.statusText&&403!==response.status||errStat++,errStat===MAX_POLL_ERRORS)if(0===response.readyState||"timeout"===response.statusText){app.trigger("gateway-status",{status:"not-reachable"});let bridgeid=app.config.attributes.bridgeid,ip=app.config.attributes.ipaddress,port=app.config.attributes.port;app.Devicetype.sendMessage("GW_NOT_REACHABLE",'{"bridgeid":"'+bridgeid+'","ip":"'+ip+'","port":"'+port+'"}')}else 403===response.status&&app.trigger("gateway-status",{status:"not-authenticated"});console.log("poll error",response)}function mountIntervalTimer(callback,interval){timer&&interval!==timerInterval&&(clearInterval(timer),timer=void 0),timer&&interval===timerInterval||(timerInterval=interval,timer=setInterval(callback,interval))}return mountIntervalTimer(function run(){let t=timerInterval;if(state===STATE_IDLE&&items.length>0){pos>=items.length&&(pos=0,init=!1);var cur=items[pos];function doPoll(){cur.item.fetch({cache:!1,success:fetchSuccess,error:fetchError,timeout:1e4})}state=STATE_WAIT,window.requestIdleCallback?window.requestIdleCallback(doPoll):doPoll(),t=init?50:pos===items.length-1?5e3:1e3,pos++}mountIntervalTimer(run,t)},50),{pause:function(){state=3},resume:function(){3===state&&(state=STATE_IDLE)},pollRepeated:function(item){item.name&&console.log("add poll repeated",item.name),items.push({item:item,count:0,repeat:!0})},pollOnce:function(item){items.push({item:item,count:0,repeat:!1})}}}