text: var xnm=xnm||{};xnm.aio=xnm.aio||{};xnm.aio.ez=xnm.aio.ez||{};xnm.aio.ez.Util={clearHttpOptionForLog:function(b){try{var a=JSON.parse(JSON.stringify(b));a.auth&&(a.auth="xxxxxx:xxxxxx");if(a.headers)for(var c in headers)if(c&&"authorization"==c.toLocaleLowerCase()){var d=headers[c];if(d){var f=d.indexOf(" ");headers[c]=-1==f?"xxxxxx":d.substr(0,f)+" xxxxxx"}}return a}catch(e){return b}}};
  xnm.aio.ez.XS1=function(){var b=function(a,c,d){this._logger=require("x.logger.js").getLogger("xnm.aio.ez.XS1");this.ip=a;this.port=80;c&&(c=parseInt(c),0<c&&(this.port=c));this.username="admin";this.password=d?d:null;this._getDevicesCBs=[];this._deviceBuffer=null};b.prototype._doRequest=function(a,c,d){a={host:this.ip,port:this.port,path:"/control?callback=data&cmd="+a,method:"GET",headers:{Connection:"close"}};if(c)for(var b in c)c.hasOwnProperty(b)&&(a.path+="&"+b+"="+encodeURI(c[b]));this.username&&
  this.password&&(a.headers.Authorization="Basic "+(new Buffer(this.username+":"+this.password)).toString("base64"));this._logger.log("trace","send http request:"+JSON.stringify(xnm.aio.ez.Util.clearHttpOptionForLog(a)));var e=d,g=this,l=require("http").request(a,function(a){var c="";a.setEncoding("utf-8");a.on("data",function(a){c+=a});a.on("end",function(){g._logger.log("trace","http response code:"+a.statusCode+" body:"+c);if(200==a.statusCode)if(c=c.trim(),6>c.length||"data("!=c.substr(0,5))e&&
  e(Error("response fomart error:"+c)),e=null;else{for(var d=c.substr(5),b=d.length-1;0<=b&&")"!=d.charAt(b);)--b;if(0>b)e&&e(Error("response fomart error:"+c)),e=null;else{d=d.substr(0,b);try{d=JSON.parse(d),e&&e(null,d),e=null}catch(m){e&&e(Error("response fomart error:"+c)),e=null}}}else e&&e(Error("http response code:"+a.statusCode+" "+c)),e=null})});l.setTimeout(2E3,function(){g._logger.log("trace","http request timeout");l.abort();e&&e(Error("http request timeout"));e=null});l.on("error",function(a){g._logger.log("trace",
  "http request error:"+a.message);e&&e(a);e=null});l.end()};b.prototype.getDevices=function(a){a||(a=function(){});var c=this._getDevicesCBs.length;-1==this._getDevicesCBs.indexOf(a)&&this._getDevicesCBs.push(a);if(0==c){var d=this;this.getActuators(function(a,c){if(a){c=d._getDevicesCBs;d._getDevicesCBs=[];for(var b=0;b<c.length;b++)c[b]&&c[b](a)}else{var f=[];c&&(f=f.concat(c));d.getSensors(function(a,c){var b=d._getDevicesCBs;d._getDevicesCBs=[];if(a)for(c=0;c<b.length;c++)b[c]&&b[c](a);else for(c&&
  (f=f.concat(c)),c=0;c<b.length;c++)b[c]&&b[c](null,f)})}})}};b.prototype.getState=function(a){var c=function(a){var c={actuator:{},sensor:{}};if(a)for(var d=0;d<a.length;d++){var b=a[d];b.address&&("actuator"==b.type?c.actuator[b.address]=b.value:"sensor"==b.type&&(c.sensor[b.address]=b.value))}return c};if(this._deviceBuffer){var d=c(this._deviceBuffer);a&&a(null,d);a=null}this.getDevices(function(b,d){b?a&&a(b):(b=c(d),a&&a(null,b),a=null)})};b.prototype.executeCommand=function(a,c,b){a.address?
  c?this._doRequest("set_state_actuator",{number:a.address,"function":c},function(a){b&&b(a)}):b&&b(Error("command invalid")):b&&b(Error("address invalid"))};b.prototype.executeCommandCreator=function(a,c,b){c&&c.value&&"func"==c.value&&c.ext?this.executeCommand(a,c.ext,function(a){b&&b(a)}):b&&b(Error("command invalid"))};b.prototype.getStatesCreator=function(a,c,b){this.getState(function(a,d){if(a)b&&b(a);else{a=[];for(var f=0;f<c.length;f++){var e=c[f];if(e&&e.id&&e.device){var h=e.device;if(h.type&&
  h.address){var k;"actuator"==h.type?k=d.actuator[h.address]:"sensor"==h.type&&(k=d.sensor[h.address]);"undefined"!=typeof k&&null!=k&&a.push({id:e.id,status:k})}}}b&&b(null,a)}})};b.prototype.getActuators=function(a){this._doRequest("GET_LIST_ACTUATORS",null,function(c,b){if(c)a&&a(c);else{c=[];if(b&&b.actuator)for(var d=0;d<b.actuator.length;d++)"disabled"!=b.actuator[d].type&&c.push({sys:"xs1",type:"actuator",name:b.actuator[d].name,address:b.actuator[d].id,data:b.actuator[d].type,value:b.actuator[d].value,
  unit:b.actuator[d].unit,"function":b.actuator[d]["function"]});a&&a(null,c)}})};b.prototype.getSensors=function(a){this._doRequest("GET_LIST_SENSORS",null,function(b,d){if(b)a&&a(b);else{b=[];if(d&&d.sensor)for(var c=0;c<d.sensor.length;c++)"disabled"!=d.sensor[c].type&&b.push({sys:"xs1",type:"sensor",name:d.sensor[c].name,address:d.sensor[c].id,data:d.sensor[c].type,value:d.sensor[c].value,unit:d.sensor[c].unit});a&&a(null,b)}})};b.prototype.toJSON=function(){return{sys:"xs1",ip:this.ip,port:this.port,
  password:this.password}};b.prototype.equalsTo=function(a){return a&&a instanceof b?this.ip==a.ip&&this.port==a.port&&this.password==a.password:!1};return b}();xnm.aio.ez.DMError=function(b,a){this.code=b;this.message=a;this.stack=Error().stack};xnm.aio.ez.prototype=Error();xnm.aio.ez.prototype.name="EZDMError";xnm.aio.ez.prototype.code=0;xnm.aio.ez.ERROR_CODE={DEFAULT:0,INDEX_EXISTS:1,ID_EXISTS:2,INVALID:3,UNKNOW:4,NOT_FOUND:5,IP_EXISTS:6};
  xnm.aio.ez.DM={XS1_SYS:"xs1",getGatewayType:function(){return[{sys:this.XS1_SYS,name:"EZcontrol XS1"}]},getGatewayDeviceType:function(b){switch(b.sys){case this.XS1_SYS:return[{type:"actuator",name:"Actuator"},{type:"sensor",name:"Sensor"}];default:return null}},createGateway:function(b,a,c){a=xnm.aio.ez.DM.DMError;var d=xnm.aio.ez.DM.ERROR_CODE;b?b.ip?(b.port||(b.port=80),c(null,b)):c(new a(d.INVALID,"ip is invalid")):c(new a(d.INVALID,"info is invalid"))},updateGateway:function(b,a,c,d){b=xnm.aio.ez.DM.DMError;
  c=xnm.aio.ez.DM.ERROR_CODE;a?a.ip?(a.port||(a.port=80),d(null,a)):d(new b(c.INVALID,"ip is invalid")):d(new b(c.INVALID,"update is invalid"))},deleteGateway:function(b,a,c){c()},createDevice:function(b,a,c){a=xnm.aio.ez.DMError;var d=xnm.aio.ez.DM.ERROR_CODE;b?b.type?b.address?c(null,b):c(new a(d.INVALID,"address is invalid")):c(new a(d.INVALID,"type is invalid")):c(new a(d.INVALID,"info is invalid"))},updateDevice:function(b,a,c){a=xnm.aio.ez.DMError;var d=xnm.aio.ez.DM.ERROR_CODE;b?b.type?b.address?
  c(null,b):c(new a(d.INVALID,"address is invalid")):c(new a(d.INVALID,"type is invalid")):c(new a(d.INVALID,"info is invalid"))},deleteDevice:function(b,a,c){c()},getCommandStatusMap:function(b){if(!b.sys||!b.type)return null;var a={command:[],status:[{type:"float",name:"status",ref:{value:"state",ext:"%f"}}]};switch(b.sys){case this.XS1_SYS:if(b["function"])for(var c=0;c<b["function"].length;c++){var d=b["function"][c];d&&d.type&&"disabled"!=d.type&&a.command.push({type:"enum",name:d.dsc,ref:{value:"func",
  ext:c+1}})}return a;default:return null}},applyUIFilter:function(b,a){var c=function(a,b){if("*"==a)return!0;for(var c=!1,d=0;d<a.length;d++)if(a[d]==b){c=!0;break}return c},d=function(a,b,d){var e=[];switch(d.catagory){case "command":a.command&&a.command.forEach(function(a){c(d.elems,a.type)&&(a=JSON.parse(JSON.stringify(a)),e.push(a))});break;case "status":a.status&&a.status.forEach(function(a){c(d.elems,a.type)&&(a=JSON.parse(JSON.stringify(a)),e.push(a))})}return e},f=function(a,b){var c=[],e=
  xnm.aio.ez.DM.getCommandStatusMap(a);e&&(a=d(e,a,b),c=c.concat(a));return c};if(!b.sys)return null;var e=JSON.parse(JSON.stringify(b)),g=null;switch(a.catagory){case "command":g=f(b,a);e.command=g;break;case "status":g=f(b,a);e.status=g;break;default:return null}return g&&0!=g.length?e:null}};
  xnm.aio.ez.Handler=function(){var b=function(){};b.prototype.devicemanager=xnm.aio.ez.DM;b.prototype.XS1=xnm.aio.ez.XS1;b.prototype.registModule=function(a){a.register(this.devicemanager.XS1_SYS,"ez")};b.prototype.resolveGateway=function(a){if(!a||!a.sys||!a.ip)return null;switch(a.sys){case this.devicemanager.XS1_SYS:return new xnm.aio.ez.XS1(a.ip,a.port,a.password);default:return null}};b.prototype.getDeviceCommands=function(a,b){a=(a=this.devicemanager.applyUIFilter(a,{catagory:"command",elems:"*"}))?
  a.command:[];b&&b(null,a)};b.prototype.getDeviceStatus=function(a,b){a=(a=this.devicemanager.applyUIFilter(a,{catagory:"status",elems:"*"}))?a.status:[];b&&b(null,a)};b.prototype.doCommand=function(a,b,d,f){(a=this.resolveGateway(a))?a.executeCommandCreator(b,d,f):f&&f(Error("gateway json format invalid"))};b.prototype.doStatus=function(a,b,d,f,e){(b=this.resolveGateway(b))?b.getStatesCreator(null,[{id:a,device:d,status:f}],function(a,b){a?e&&e(a):e&&e(null,b[0])}):e&&e(Error("gateway json format invalid"))};
  b.prototype.getDeviceList=function(a,b){(a=this.resolveGateway(a))?a.getDevices(function(a,c){b&&b(a,c)}):b&&b(Error("gateway json format invalid"))};return b}();"undefined"!=typeof exports&&"undefined"!=typeof module&&module.exports&&(exports=module.exports=new xnm.aio.ez.Handler);
  