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

"use strict";function bbInitSensors(){_.debounce(function(){},500);app.api.on("event",function(e){if("sensors"!==e.r)return;if("added"===e.e){let s=app.sensors.get(e.sensor.id);if(s&&app.switches){let sw=app.switches.get(s.get("mac"));sw&&"switchAddHandler"in window&&switchAddHandler(sw),sw&&app.api.trigger("switch-added",sw)}return void app.sensors.fetch()}if("changed"!==e.e)return;let s=app.sensors.get(e.id);if(s){var args={};args.ec=s.get("ec")||0,_.map(e,function(val,key){["state","config","name"].forEach(a=>{key===a&&(args.ec++,args[a]=val)})}),s.set(args),setTimeout(()=>s.fetch(),0)}}),app.Sensor=Backbone.Model.extend({sync:function(method,model,options){return"patch"===method&&(method="update",options.attrs.state?(options.url=model.url()+"/state",options.attrs=options.attrs.state):options.attrs.config&&(options.url=model.url()+"/config",options.attrs=options.attrs.config)),Backbone.sync(method,model,options)},rename:function(name){this.save({name:name},{patch:!0})},initialize:function(){if(!(this.has("type")&&0===this.get("type").indexOf("CLIP")||this.has("type")&&0===this.get("type").indexOf("Daylight"))){var uuid=this.get("uniqueid");if(uuid){var p=uuid.split("-");this.set({mac:p[0],endpoint:p[1]})}}},parse:function(sensor,options){return sensor.etag||(sensor.etag="1",sensor.state&&_.map(sensor.state,(val,key)=>{sensor.etag+=val}),sensor.config&&_.map(sensor.config,(val,key)=>{sensor.etag+=val})),sensor},url:function(){return this.id?"//"+app.session.gw+"/api/"+app.apikey+"/sensors/"+this.id:"//"+app.session.gw+"/api/"+app.apikey+"/sensors"}}),app.SensorCollection=Backbone.Collection.extend({initialize:function(){this.sortKey="id"},name:"sensors",model:app.Sensor,url:"//"+app.session.gw+"/api/"+app.apikey+"/sensors",parse:function(response,options){return _.map(response,function(sensor,sid){return sensor.id=sid,sensor.manufacturername=sensor.manufacturername||"Unknown",sensor})},sync:function(method,collection,options){return Backbone.sync(method,collection,options)},comparator:function(item){let data=item.get(this.sortKey);if(data)return"vendor"==this.sortKey&&(data=data||item.get("manufacturername")),data.toUpperCase()},sortByKey:function(key){this.sortKey=key,this.sort()}});var SensorScan=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.sensors=new app.SensorCollection,app.sensorScan=new SensorScan;var cache=sessionStorage.getItem("app.sensors");cache&&app.sensors.reset(JSON.parse(cache))}