var xnm=xnm||{};xnm.aio=xnm.aio||{};xnm.aio.hcgw=xnm.aio.hcgw||{};xnm.aio.hcgw.Gateway=function(a){this.ip=a;this.port=49880;this.mode=1};xnm.aio.hcgw.Gateway.prototype._doRequest=function(a,b,c){var d=require("dgram").createSocket("udp4");a=new Buffer(a);d.send(a,0,a.length,this.port,this.ip,function(e,f){d.close();c&&setTimeout(function(){c()},Math.floor(b+750))})};
xnm.aio.hcgw.Gateway.prototype.sendSignal=function(a,b){null==a.pause&&(a.pause=0==this.mode?a.data[a.data.length-1]*a.timing:(a.data[a.data.length-1]/2+1)*a.timing);var c=a.data[a.data.length-1];var d="TXP:0,0,"+(a.repeats+",");d+=a.pause+",";d+=a.timing+",";d=0==this.mode?d+(a.data.length/2+1+",0,"):d+(a.data.length/2+",");for(var e=0;e<a.data.length-1;e++)d+=a.data[e]+",",c+=a.data[e];d=0==this.mode?d+(a.data[a.data.length-1]+",0"):d+(Math.floor(a.data[a.data.length-1]/2+1)+";");this._doRequest(d,
c*a.timing*a.repeats/1E3,b)};xnm.aio.hcgw.Discovery=function(){this.Gateways={};this._sockets=[];var a=require("os");require("dgram");if(a&&a.networkInterfaces){a=a.networkInterfaces();for(var b in a)for(var c=a[b],d=0;d<c.length;d++)this._addDiscoverSocket(49880,c[d])}else this._addDiscoverSocket(49880,null)};
xnm.aio.hcgw.Discovery.prototype._receivedData=function(a,b){var c=b.toString();if(5<c.length&&"HCGW:"==c.substr(0,5)){b=new xnm.aio.hcgw.Gateway(a);c=c.substr(5);c=c.split(";");for(var d=0;d<c.length;d++){var e=c[d].split(":");2==e.length&&"VC"==e[0]&&"ITECHNO"==e[1]&&(b.mode=0)}this.Gateways[a]=b}};
xnm.aio.hcgw.Discovery.prototype._addDiscoverSocket=function(a,b){var c=require("dgram"),d=this;if(!b){try{var e=c.createSocket({reuseAddr:!0,type:"udp4"})}catch(f){e=c.createSocket("udp4")}e.on("message",function(f,h){d._receivedData(h.address,f.toString())});e.bind(a);this._sockets.push(e)}else if("IPv4"==b.family&&0==b.internal){try{e=c.createSocket({reuseAddr:!0,type:"udp4"})}catch(f){e=c.createSocket("udp4")}e.on("listening",function(){e.setBroadcast(!0)});e.on("message",function(f,h){d._receivedData(h.address,
f.toString())});e.bind(a,b.address);this._sockets.push(e)}};xnm.aio.hcgw.Discovery.prototype._sendDiscoveryMessages=function(a,b,c,d){a=new Buffer("SEARCH HCGW");for(b=0;b<this._sockets.length;b++)this._sockets[b].send(a,0,a.length,49880,"255.255.255.255")};xnm.aio.hcgw.Discovery.prototype.discoverGateways=function(){var a=this,b=0,c=setInterval(function(){a._sendDiscoveryMessages();1<=b++&&clearInterval(c)},250)};xnm.aio.hcgw.Handler=function(){this._Discovery=null};
xnm.aio.hcgw.Handler.prototype.discoverGateways=function(a){this._Discovery||(this._Discovery=new xnm.aio.hcgw.Discovery);this._Discovery.discoverGateways();if(a){var b=this;setTimeout(function(){a(b._Discovery.Gateways)},600)}};xnm.aio.hcgw.Handler.prototype.getDetectedGateways=function(){this._Discovery||(this._Discovery=new xnm.aio.hcgw.Discovery);return this._Discovery.Gateways};
xnm.aio.hcgw.Handler.prototype._sendNextGatewaySignal=function(a,b){if(0<a.length){var c=this,d=a.pop();d.gateway.sendSignal(d.signal,function(){c._sendNextGatewaySignal(a,b)})}else b&&b()};xnm.aio.hcgw.Handler.prototype.getSignal=function(a,b,c){var d="";"ELRO"==a?d=this.getElroSignal(b,c):"IT"==a?d=this.getIntertechnoSignal(b,c):"BS"==a?d=this.getBrennenstuhlSignal(b,c):"BS2"==a?d=this.getBrennenstuhlSignal(b,c,[1,4]):"DY"==a&&(d=this.getDooyaSignal(b,c));return d};
xnm.aio.hcgw.Handler.prototype.sendSignalToGateways=function(a,b,c){var d=[];if(a)for(var e in b)d.push({gateway:b[e],signal:a});this._sendNextGatewaySignal(d,c)};xnm.aio.hcgw.Handler.prototype.getElroSignal=function(a,b){var c=null;if(6==a.length){var d="0";"on"==b?d="1":"off"==b&&(d="4");c={data:[],timing:350,repeats:6,pause:null};a=XC.getBytes(a.substr(0,5)+d,!0);for(b=0;b<a.length;b++)for(d=0;8>d;d++)c.data=a[b]&128>>d?c.data.concat([3,1]):c.data.concat([1,3]);c.data=c.data.concat([1,32])}return c};
xnm.aio.hcgw.Handler.prototype.getDooyaSignal=function(a,b){var c=null;if(8==a.length){var d="55";"up"==b||"moveUp"==b?d="11":"down"==b||"moveDown"==b?d="33":"learn"==b&&(d="CC");c={data:[57,18],timing:91,repeats:10,pause:null};a=XC.getBytes(a+d,!0);for(b=0;b<a.length;b++)for(d=0;8>d;d++)c.data=a[b]&128>>d?c.data.concat([8,4]):c.data.concat([4,8]);c.data[c.data.length-1]=120}return c};
xnm.aio.hcgw.Handler.prototype.getIntertechnoSignal=function(a,b){var c=null,d=a.split(".");2==d.length&&(1==d[0].length?a="0"+d[0]+"0"+d[1]+"00":7==d[0].length&&(a=d[0]+d[1]));if(6==a.length){d="0";"on"==b?d="E":"off"==b&&(d="6");c={data:[],timing:89,repeats:5,pause:11125};a=XC.getBytes(a.substr(0,5)+d,!0);for(b=0;3>b;b++)for(d=0;8>d;d+=2)c.data=a[b]&1<<d/2?c.data.concat([4,12,12,4]):c.data.concat([4,12,4,12]);c.data=c.data.concat([4,125])}else if(8==a.length){c=null;d=parseInt(a.substr(6,1),16);
d&=-2;"on"==b?d|=1:(0==b.indexOf("dimTo")&&(d|=1,b=b.replace(/dimTo/g,"")),b=b.replace(/%/g,""),"10"==b?c="00":"20"==b?c="10":"30"==b?c="20":"40"==b?c="30":"50"==b?c="50":"60"==b?c="70":"70"==b?c="90":"80"==b?c="B0":"90"==b?c="D0":"100"==b&&(c="F0"));d=null!=c?a.substr(0,6)+d+a.substr(7)+c:a.substr(0,6)+d+a.substr(7);c={data:[],timing:98,repeats:5,pause:10976};a=XC.getBytes(d,!0);c.data=c.data.concat([3,29]);for(d=0;4>d;d++)for(b=0;8>b;b++)c.data=a[d]&128>>b?c.data.concat([3,15,3,3]):c.data.concat([3,
3,3,15]);if(5==a.length)for(c.data[c.data.length-17]=c.data[c.data.length-19]=3,d=0;4>d;d++)c.data=a[4]&128>>d?c.data.concat([3,15,3,3]):c.data.concat([3,3,3,15]);c.data=c.data.concat([3,126])}return c};
xnm.aio.hcgw.Handler.prototype.getBrennenstuhlSignal=function(a,b,c){var d=null;if(6==a.length){a=XC.getBytes(a,!0);if("on"==b||"day"==b)a[2]|=32;else if("off"==b||"night"==b)a[2]&=-33;var e=[2,12,5,14,7,4,1,15,11,13,6,3,8,9,10,0];d=[10,0,4,12,2,14,7,5,1,15,11,13,9,6,3,8];a[0]&32&&(d=e);e=d[a[2]&15];var f=d[e^(a[2]&240)>>4],h=d[f^a[1]&15],g=d[h^(a[1]&240)>>4];a=[((9^(a[0]&240)>>4)<<4)+d[g^a[0]&15],(g<<4)+h,(f<<4)+e];d={data:[],timing:505,repeats:6,pause:0};e=[2,1];f=[1,2];"day"==b||"night"==b?(d.timing=
385,d.repeats=112,d.pause=385,e=[3,1],f=[1,3],d.data=d.data.concat([1,6])):d.data=c?d.data.concat(c):d.data.concat([6,14]);for(b=0;b<a.length;b++)for(c=0;8>c;c++)d.data=a[b]&128>>c?d.data.concat(e):d.data.concat(f)}return d};xnm.aio.hcgw.Handler.prototype.Gateway=xnm.aio.hcgw.Gateway;
xnm.aio.hcgw.Gateway.prototype.learn=function(a,b){var c=function(k,m){if(!k)return null;for(var l=k.length;l--;)if(k[l]===m)return!0;return!1},d=function(k,m){for(k=k.toString(16).toUpperCase();k.length<m;)k="0"+k;return k};if(a&&a.type){var e=!0,f=[];if(a.devices)for(var h=a.devices.length;h--;)a.devices[h].address&&f.push(a.devices[h].address);var g=a.address;g&&(e=!1);switch(a.type){case "IT2":for(;e;)g=d(Math.floor(57070*Math.random())+4369,4)+"008.0",e=c(f,g);c={value:"on"};break;case "DY":case "DY2":for(;e;)g=
d(Math.floor(4294967295*Math.random()),8),e=c(f,g);c="DY"==a.type?{value:"learn"}:{value:"up"};break;case "BS":case "BS2":for(;e;)g=d(Math.floor(65535*Math.random()),4)+"01",e=c(f,g);c={value:"on"};"heater"==a.data&&(c={value:"day"});break;default:b&&b(Error("device type invalid"));return}this.executeCommandCreator({type:a.type,data:a.data?a.data:"switch",address:g},c,function(k){k?b&&b(k):b&&b(null,{address:g})})}else b&&b(Error("options invalid"))};
xnm.aio.hcgw.Gateway.prototype.executeCommandCreator=function(a,b,c){if(b&&b.value)if(a&&a.type&&a.address&&a.data){var d=a.type;"IT2"==d&&(d="IT");"DY2"==d&&(d="DY");a=a.address;var e=b.value;if("dimTo"==b.value){if(null==b.ext||"undefined"==typeof b.ext){c&&c(Error("invalid dimTo command format"));return}var f=b.ext;"string"==typeof b.ext&&(f=parseInt(b.ext));e="dimTo"+f;0==f&&(e="off")}b=require("xnm.aio.hcgw.js");if(d=b.getSignal(d,a,e)){var h;a=[];if(b._Discovery&&b._Discovery.Gateways)for(var g in b._Discovery.Gateways)b._Discovery.Gateways.hasOwnProperty(g)&&
(this.ip==g&&(h=b._Discovery.Gateways[g]),a.push(b._Discovery.Gateways[g]));h?h.sendSignal(d,c):0!=a.length?b.sendSignalToGateways(d,a,c):this.sendSignal(d,c);b.discover()}else c&&c(Error("invalid device address"))}else c&&c(Error("invalid device format"));else c&&c(Error("command json invalid"))};xnm.aio.hcgw.Gateway.prototype.toJSON=function(){return{sys:xnm.aio.hcgw.DM.SYS,ip:this.ip,port:this.port,mode:this.mode}};
xnm.aio.hcgw.Gateway.prototype.equalsTo=function(a){return a&&a instanceof xnm.aio.hcgw.Gateway?this.ip==a.ip&&this.port==a.port:!1};
xnm.aio.hcgw.DM={SYS:"hcgw",MAP:{ELRO:{general:{command:[{type:"enum",name:"off",ref:{value:"off"}},{type:"enum",name:"on",ref:{value:"on"}}]}},DY:{shutter:{command:[{type:"enum",name:"up",ref:{value:"up"}},{type:"enum",name:"down",ref:{value:"down"}},{type:"enum",name:"stop",ref:{value:"stop"}}]}},BS:{general:{command:[{type:"enum",name:"on",ref:{value:"on"}},{type:"enum",name:"off",ref:{value:"off"}}]},heater:{command:[{type:"enum",name:"day",ref:{value:"day"}},{type:"enum",name:"night",ref:{value:"night"}}]}},
BS2:{general:{command:[{type:"enum",name:"on",ref:{value:"on"}},{type:"enum",name:"off",ref:{value:"off"}}]}},IT:{general:{command:[{type:"enum",name:"on",ref:{value:"on"}},{type:"enum",name:"off",ref:{value:"off"}}]},shutter:{command:[{type:"enum",name:"up",ref:{value:"on"}},{type:"enum",name:"down",ref:{value:"off"}}]},dimmer:{command:[{type:"enum",name:"on",ref:{value:"on"}},{type:"enum",name:"off",ref:{value:"off"}},{type:"int",name:"dim to",ref:{value:"dimTo",ext:"%d",extMeta:"0-100",scale:"10"}}]}}},
getGatewayType:function(){return[{sys:this.SYS,name:"brennenstuhl Gateway",vendor:"brennenstuhl"},{sys:this.SYS,name:"intertechno Gateway",vendor:"intertechno"},{sys:this.SYS,name:"HC Gateway",vendor:"hc"}]},getGatewayDeviceType:function(){return{brennenstuhl:{"RC 3600":{type:"BS",data:["switch","light"]},"RCR 2044":{type:"BS",data:["switch","light"]},"RCR 2044 IP44":{type:"BS",data:["switch","light"]},"RCR 3600":{type:"BS",data:["switch","light"]},"RCR 3600 IP44":{type:"BS",data:["switch","light"]},
"RCR CE1 1011 IP20":{type:"BS2",data:["switch","light"]},"RCR CE1 1011 IP44":{type:"BS2",data:["switch","light"]},"RCS 1000N":{type:"ELRO",data:["switch","light"]},"RCS 1044N":{type:"ELRO",data:["switch","light"]},"RCS 2044 N":{type:"ELRO",data:["switch","light"]},"RCS 2044 N IP44":{type:"ELRO",data:["switch","light"]},"RCR EL1 3726":{type:"BS",data:["switch","light"]},"RCR PL1 3726":{type:"BS",data:["switch","light"]}},BAT:{"RC 3500-A IP44 DE":{type:"ELRO",data:["switch","light"]},"RCE AAA1000-A IP44":{type:"ELRO",
data:["switch","light"]},"RCE AAA3680-A IP20":{type:"BS",data:["switch","light"]},"RCR 1000N-A":{type:"ELRO",data:["switch","light"]}},ELRO:{AB440D:{type:"ELRO",data:["light"]},AB440ID:{type:"ELRO",data:["light"]},AB440IS:{type:"ELRO",data:["switch","light"]},AB440L:{type:"ELRO",data:["light"]},AB440S:{type:"ELRO",data:["switch","light"]},AB440WD:{type:"ELRO",data:["switch","light"]}},Vivanco:{"FSS 31000W":{type:"ELRO",data:["switch","light"]},"FSS 33600W":{type:"ELRO",data:["switch","light"]}},Xavax:{"GRR-3500":{type:"IT2",
data:["switch","light"]},"ITLR-300":{type:"IT2",data:["dimmer"]},"ITLR-3500":{type:"IT2",data:["switch","light"]},"ITR-1500":{type:"IT2",data:["switch","light"]},"LBUR-100":{type:"IT2",data:["light"]}},rohrmotor24:{"RMF Motor":{type:"DY",data:["shutter"]},"RMF-R1":{type:"DY",data:["shutter"]},"RMF-R1 (UP)":{type:"DY",data:["shutter"]},"RMF-R1L (UP)":{type:"DY",data:["shutter"]},"RMF-RS1 (AP/UP)":{type:"DY",data:["shutter"]}},intertechno:{"CMR-300":{type:"IT",data:["light"]},"CMR-1000":{type:"IT",
data:["switch","light"]},"CMR-1224":{type:"IT",data:["switch","light"]},"GRR-300":{type:"IT",data:["switch","light"]},"GRR-3500":{type:"IT2",data:["switch","light"]},"IT-300L":{type:"IT2",data:["light"]},"IT-1500":{type:"IT2",data:["switch","light"]},"IT-2300":{type:"IT2",data:["switch","light"]},"IT-3500L":{type:"IT2",data:["switch","light"]},"ITDL-1000":{type:"IT2",data:["switch","light"]},"ITDM-250":{type:"IT2",data:["light"]},"ITE-300":{type:"IT",data:["light"]},"ITE-1000":{type:"IT",data:["switch",
"light"]},"ITL-150":{type:"IT2",data:["light"]},"ITL-210":{type:"IT2",data:["light"]},"ITL-230":{type:"IT2",data:["switch","light"]},"ITL-250":{type:"IT2",data:["light"]},"ITL-300":{type:"IT2",data:["light"]},"ITL-1000":{type:"IT2",data:["switch","light","shutter"]},"ITL-3500":{type:"IT2",data:["switch","light"]},"ITLR-300":{type:"IT2",data:["dimmer"]},"ITLR-3500":{type:"IT2",data:["switch","light"]},"ITLR-3500T":{type:"IT2",data:["switch","light"]},"ITR-300":{type:"IT",data:["light"]},"ITR-1500":{type:"IT2",
data:["switch","light"]},"ITR-3500":{type:"IT",data:["switch","light"]},"ITR-7000":{type:"IT2",data:["switch"]},"ITWR-3500":{type:"IT2",data:["switch","light"]},"LBUR-100":{type:"IT2",data:["light"]},"PA3-1000":{type:"IT",data:["switch","light"]},"PAR-1500":{type:"IT",data:["switch","light"]},"CMR-500":{type:"IT",data:["shutter"]},"ITL-500":{type:"IT2",data:["shutter"]}}}},createGateway:function(a,b,c){b=xnm.aio.hcgw.DM.DMError;var d=xnm.aio.hcgw.DM.DMError.ERROR_CODE;a?a.ip?(a.port||(a.port=49880),
a.vendor||(a.vendor="steckpro"),c(null,a)):c(new b(d.INVALID,"ip is invalid")):c(new b(d.INVALID,"info is invalid"))},updateGateway:function(a,b,c,d){a=xnm.aio.hcgw.DM.DMError;c=xnm.aio.hcgw.DM.DMError.ERROR_CODE;b?b.ip?(b.port||(b.port=49880),b.vendor||(b.vendor="steckpro"),d(null,b)):d(new a(c.INVALID,"ip is invalid")):d(new a(c.INVALID,"update is invalid"))},deleteGateway:function(a,b,c){c()},createDevice:function(a,b,c){var d=xnm.aio.hcgw.DM.DMError,e=xnm.aio.hcgw.DM.DMError.ERROR_CODE;if(a)if(a.gateway)if(a.type)if(a.address)if(a.data)if(b.data&&
b.data.gateways&&0!==b.data.gateways.length){b=b.data.gateways;var f;for(f=0;f<b.length;f++)if(b[f].index===a.gateway){var h=b[f];break}h?c(null,a):c(new d(e.NOT_FOUND,"gateway with index "+a.gateway+" not exists"))}else c(new d(e.NOT_FOUND,"gateway with index "+a.gateway+" not exists"));else c(new d(e.INVALID,"data is invalid"));else c(new d(e.INVALID,"address is invalid"));else c(new d(e.INVALID,"type is invalid"));else c(new d(e.INVALID,"gateway is invalid"));else c(new d(e.INVALID,"info is invalid"))},
updateDevice:function(a,b,c){var d=xnm.aio.hcgw.DM.DMError,e=xnm.aio.hcgw.DM.DMError.ERROR_CODE;if(a)if(a.address)if(a.type)if(a.data)if(b.data&&b.data.gateways&&0!==b.data.gateways.length){b=b.data.gateways;var f;for(f=0;f<b.length;f++)if(b[f].index===a.gateway){var h=b[f];break}h?c(null,a):c(new d(e.NOT_FOUND,"gateway with index "+a.gateway+" not exists"))}else c(new d(e.NOT_FOUND,"gateway with index "+a.gateway+" not exists"));else c(new d(e.INVALID,"data is invalid"));else c(new d(e.INVALID,"type is invalid"));
else c(new d(e.INVALID,"address is invalid"));else c(new d(e.INVALID,"info is invalid"))},deleteDevice:function(a,b,c){c()},applyUIFilter:function(a,b){var c=function(g,k){if("*"==g)return!0;for(var m=!1,l=0;l<g.length;l++)if(g[l]==k){m=!0;break}return m},d=function(g,k,m){var l=[];switch(m.catagory){case "command":g.command&&g.command.forEach(function(n){c(m.elems,n.type)&&(n=JSON.parse(JSON.stringify(n)),l.push(n))});break;case "status":g.status&&g.status.forEach(function(n){c(m.elems,n.type)&&
(n=JSON.parse(JSON.stringify(n)),l.push(n))})}return l},e=function(g,k){var m=[],l=null;switch(g.type){case "BS":l="heater"==g.data?xnm.aio.hcgw.DM.MAP.BS.heater:xnm.aio.hcgw.DM.MAP.BS.general;break;case "BS2":l=xnm.aio.hcgw.DM.MAP.BS2.general;break;case "ELRO":l=xnm.aio.hcgw.DM.MAP.ELRO.general;break;case "DY":case "DY2":l=xnm.aio.hcgw.DM.MAP.DY.shutter;break;case "IT":case "IT2":l="shutter"==g.data?xnm.aio.hcgw.DM.MAP.IT.shutter:"dimmer"==g.data?xnm.aio.hcgw.DM.MAP.IT.dimmer:xnm.aio.hcgw.DM.MAP.IT.general}l&&
(g=d(l,g,k),m=m.concat(g));return m};if(!a||null==a.type||"undefined"==typeof a.type)return null;var f=JSON.parse(JSON.stringify(a)),h=null;switch(b.catagory){case "command":h=e(a,b);f.command=h;break;case "status":h=e(a,b);f.status=h;break;default:return null}return h&&0!=h.length?f:null}};xnm.aio.hcgw.DM.DMError=function(a,b){this.code=a;this.message=b;this.stack=Error().stack};xnm.aio.hcgw.DM.DMError.prototype=Error();xnm.aio.hcgw.DM.DMError.prototype.name="HCGatewayDMError";
xnm.aio.hcgw.DM.DMError.prototype.code=0;xnm.aio.hcgw.DM.DMError.ERROR_CODE={DEFAULT:0,INDEX_EXISTS:1,ID_EXISTS:2,INVALID:3,UNKNOW:4,NOT_FOUND:5,IP_EXISTS:6};xnm.aio.hcgw.Handler.prototype.devicemanager=xnm.aio.hcgw.DM;xnm.aio.hcgw.Handler.prototype.registModule=function(a){a.register(xnm.aio.hcgw.DM.SYS,"hcgw")};
xnm.aio.hcgw.Handler.prototype.resolveGateway=function(a){if(!a.ip)return null;var b=new xnm.aio.hcgw.Gateway(a.ip);a.port&&(b.port=a.port);null!=a.mode&&"undefined"!=typeof a.mode&&(b.mode=a.mode);return b};xnm.aio.hcgw.Handler.prototype.getDeviceCommands=function(a,b){a=(a=xnm.aio.hcgw.DM.applyUIFilter(a,{catagory:"command",elems:"*"}))?a.command:[];b&&b(null,a)};xnm.aio.hcgw.Handler.prototype.doCommand=function(a,b,c,d){(a=this.resolveGateway(a))?a.executeCommandCreator(b,c,d):d&&d(Error("gateway json format invalid"))};
xnm.aio.hcgw.Handler.prototype.discover=function(a,b){this.discoverGateways(function(c){if(c){var d=[],e;for(e in c)if(c.hasOwnProperty(e)){var f=c[e].toJSON();f.vendor=a?a:"hc";d.push(f)}b&&b(null,d)}else b&&b(null,[])})};"undefined"!=typeof exports&&"undefined"!=typeof module&&module.exports&&(exports=module.exports=new xnm.aio.hcgw.Handler);
