text: var xnm=xnm||{};xnm.aio=xnm.aio||{};xnm.aio.minisafe=xnm.aio.minisafe||{};var Logger=require("x.logger.js");
  xnm.aio.minisafe.Importer=function(a,b){this._ip=a;this._port=12345;b&&(this._port=b);this.setBaseAddress=!0;this._timeout=5E3;this._devicesToImport=[];this._importedDevices=[];this.onNotification=this._importPassword=this._importIP=null;a=require("xnm.aio.gateway.js");b=a.devicemanager.getSystem({sys:"aio",type:"ENOCEAN"}).DM.V6_DEVICES;this._AIODEVICES={};for(var c in a.devicemanager.getSystem({sys:"aio",type:"ENOCEAN"}).DM.V6_DEVICES)this._AIODEVICES[b[c].vendor]=b[c].subtypes;this._logger=require("x.logger.js").getLogger("xnm.aio.minisafe")};
  xnm.aio.minisafe.Importer.prototype._getMiniSafeConfiguration=function(a){var b=require("http").request({host:this._ip,port:this._port,path:"/MediolaExport",method:"GET",timeout:this._timeout},function(b){var c="";b.setEncoding("utf8");b.on("data",function(a){c+=a});b.on("end",function(){var b=null;try{b=JSON.parse(c)}catch(f){}a&&(null!=b?a(null,b):a(Error("invalid data")),a=null)})});b.on("error",function(b){a&&(a(b),a=null)});b.setTimeout(this._timeout,function(){b.abort()});b.end()};
  xnm.aio.minisafe.Importer.prototype._getBaseAddress=function(a){var b=4294967295,c;for(c in a){var d=a[c],e;for(e in d.transmissionInformation)if(d.transmissionInformation[e].transmissionID&&8==d.transmissionInformation[e].transmissionID.length){var f=parseInt(d.transmissionInformation[e].transmissionID,16);f<b&&(b=f)}}return 4294967295==b?null:b.toString(16).toUpperCase()};xnm.aio.minisafe.Importer.prototype._getVendor=function(a){var b="general";"eltako"==a.manufacturer&&(b=a.manufacturer);return b};
  xnm.aio.minisafe.Importer.prototype._compareType=function(a,b){var c=a.physicalDevice,d=null,e;for(e in a.deviceInformation)if(a.deviceInformation[e].eep){d=a.deviceInformation[e].eep;break}return b.id.toLowerCase()==c.toLowerCase()||d&&b.data.toLowerCase()==d.toLowerCase()||0==c.toLowerCase().indexOf(b.id.toLowerCase())||0==b.id.toLowerCase().indexOf(c.toLowerCase())?!0:!1};
  xnm.aio.minisafe.Importer.prototype._getData=function(a){var b={type:"ENOCEAN",data:null,vendor:this._getVendor(a)};if(a.deviceInformation)if(1==a.deviceInformation.length)a.deviceInformation[0].deviceid&&(b.adr=a.deviceInformation[0].deviceid);else{var c=[],d;for(d in a.deviceInformation)a.deviceInformation[d].deviceid&&c.push(a.deviceInformation[d].deviceid);b.adr=JSON.stringify(c)}if(a.transmissionInformation)if(1==a.transmissionInformation.length)a.transmissionInformation[0].transmissionID&&(b.senderID=
  a.transmissionInformation[0].transmissionID);else{c=[];for(var e in a.transmissionInformation)a.transmissionInformation[e].transmissionID&&c.push(a.transmissionInformation[e].transmissionID);b.senderID=JSON.stringify(c)}if(e=this._AIODEVICES[b.vendor])for(var f in e)if(1==this._compareType(a,e[f])){b.data=e[f].data;break}if(null==b.data&&(e=this._AIODEVICES.general))for(f in e)if(1==this._compareType(a,e[f])){b.data=e[f].data;b.vendor="general";break}return null==b.data?null:b};
  xnm.aio.minisafe.Importer.prototype._setBaseAddress=function(a,b){a={type:"ENOCEAN",baseAdr:a};var c=new (require("xnm.aio.gateway.js").Gateway)(this._importIP);c.isV5=!0;c.timeout=2E3;c.password=this._importPassword;c.executeRequestV5("setConfig",a,function(a){a.error?b(!1):b(!0)})};
  xnm.aio.minisafe.Importer.prototype._setDeviceData=function(a,b){var c=this._getData(a);c?(a=new (require("xnm.aio.gateway.js").Gateway)(this._importIP),a.isV5=!0,a.timeout=2E3,a.password=this._importPassword,a.executeRequestV5("addSensor",c,function(a){setTimeout(function(){b(a)},100)})):(this._logger.log("trace","Failed to import: "+a.manufacturer+" "+a.physicalDevice),setTimeout(function(){b(null)},0))};
  xnm.aio.minisafe.Importer.prototype._importNextDevice=function(a){var b=this;if(0<this._devicesToImport.length){var c=this._devicesToImport.pop();this._setDeviceData(c,function(d){if(d&&a)if(d.data){if(Array.isArray(d.data))for(var e in d.data)d.data[e].location=c.location,d.data[e].name=c.friendlyId,0<e&&(d.data[e].name+="-"+e),0<e&&b._importedDevices.push(d.data[e]);else d.data.location=c.location,d.data.name=c.friendlyId,b._importedDevices.push(d.data);a(!0)}else b._logger.log("trace","Failed to set: "+
  c.manufacturer+" "+c.physicalDevice);b._importNextDevice(a)})}else a(!1)};xnm.aio.minisafe.Importer.prototype._importDevices=function(a,b){this._importedDevices=[];var c=this;this._devicesToImport=a;this._importNextDevice(function(a){0==a&&b(null,c._importedDevices)})};
  xnm.aio.minisafe.Importer.prototype.importTo=function(a,b,c){this._importIP=a;this._importPassword=b;var d=this;this._getMiniSafeConfiguration(function(a,b){a?(d._logger.log("trace",a),c&&(c("failed to get configuration"),c=null)):b.devices&&(a=d._getBaseAddress(b.devices),d._logger.log("trace","Base address: "+a),null!=a&&1==d.setBaseAddress?d._setBaseAddress(a,function(a){1==a?d._importDevices(b.devices,c):c("failed to set base address")}):d._importDevices(b.devices,c))})};
  xnm.aio.minisafe.Handler=function(){var a=function(){};a.prototype.Importer=xnm.aio.minisafe.Importer;return a}();"undefined"!=typeof exports&&"undefined"!=typeof module&&module.exports&&(exports=module.exports=new xnm.aio.minisafe.Handler);
  