/*! phoscon-app 2024-02-26 */

"use strict";function bbInitLights(){let pollBusy=!1,pollQueue=[],pollTimer=null;function processPoll(){if(pollTimer=null,pollBusy||0===pollQueue.length)return;pollBusy=!0;let r=pollQueue.shift();pollQueue.length>3&&(console.log("poll /lights instead",pollQueue.length,"single requests"),pollQueue=[],r=app.lights),restGetResource(r).catch(console.log).then(()=>{pollBusy=!1,pollQueue.length>0&&(pollTimer=setTimeout(processPoll,50))})}var Light=Backbone.Model.extend({sync:function(method,model,options){return"patch"===method&&(method="update",options.attrs.state&&(options.url=model.url()+"/state",options.wait=!0,options.attrs=options.attrs.state)),"delete"===method&&(options.data=JSON.stringify({reset:!0}),options.dataType="json"),Backbone.sync(method,model,options)},parse:function(response,options){if("length"in response)return options.patch=!0,{};if("manufacturer"in response?response.manufacturername=response.manufacturer:"manufacturername"in response&&(response.manufacturer=response.manufacturername),!response.etag&&response.state){response.etag="1";for(let key in response.state)"xy"===key?(response.etag+=response.state.xy[0],response.etag+=response.state.xy[1]):response.etag+=response.state[key];!response.state||"on"in response.state||(response.state.on=!1)}return response},set:function(key,value,options){var attrs;if("object"==typeof key){attrs={};for(let k in key)attrs[k]=key[k];if(attrs.state&&this.has("state")){attrs.state=this.get("state");for(let s in key.state)attrs.state[s]=key.state[s]}}else"object"==typeof key||null==key?(attrs=key,options=value):(attrs={})[key]=value;return Backbone.Model.prototype.set.call(this,attrs,options)}});app.api.on("event",function(e){if("lights"!==e.r)return;if("added"===e.e)return void app.lights.fetch();if("changed"!==e.e)return;let l=app.lights.get(e.id);if(l){pollQueue.push(l),pollTimer||(pollTimer=setTimeout(processPoll,1e3));var args={};_.map(e,function(val,key){if(l.has(key)){if("state"===key){var mod=!1;let state=l.get("state")||{};return _.map(val,function(val2,key2){["x","y","xy","hue","sat","ct"].find(x=>x===key2)||state[key2]!==val2&&(state[key2]=val2,mod=!0)}),void(mod&&(l.unset("state",{silent:!0}),l.set({state:state})))}l.get(key)!==val&&(args[key]=val)}}),l.set(args)}}),app.LightsCollection=Backbone.Collection.extend({name:"lights",model:Light,url:"//"+app.session.gw+"/api/"+app.apikey+"/lights",parse:function(response,options){return _.map(response,function(light,lid){return light.id=lid,light.modelid&&0!==light.modelid.length||(light.modelid="Unknown"),light.manufacturer=light.manufacturer||light.manufacturername,light}).filter(r=>r.type&&"Configuration tool"!==r.type)}});var LightScan=Backbone.Model.extend({url:"//"+app.session.gw+"/api/"+app.apikey+"/sensors/new",isActive:function(){return this.has("lastscan")&&"active"===this.get("lastscan")},start:function(){this.clear(),Backbone.sync("create",this,{url:"//"+app.session.gw+"/api/"+app.apikey+"/sensors"})}});app.lights=new app.LightsCollection,app.lightScan=new LightScan;var cache=sessionStorage.getItem("app.lights");cache&&app.lights.reset(JSON.parse(cache))}