text: var util=require("util"),EventEmitter=require("events");require("x.logger.js").setLevel("debug","x.hub.peripheralman");var x=x||{};x.hub=x.hub||{};x.hub.peripheralman=x.hub.peripheralman||{};
  x.hub.peripheralman.PeripheralManager=function(){var e=function(){this._logger=require("x.logger.js").getLogger("x.hub.peripheralman.PeripheralManager");this._peripherals=[]};e.FILE_NAME="pm.json";e.prototype.init=function(a){var b=this;this._load(function(d){d?a&&a(d):(global&&"A20"!=global.PLATFORM&&"PI2"!=global.PLATFORM&&"MAC"!=global.PLATFORM||"MAC"!=global.PLATFORM&&b._initUdevMonitor(),a&&a())})};e.prototype.getUSBDevices=function(){return this._peripherals};e.prototype._load=function(a){var b=
  this,d=require("fs-extra"),c=require("x.hub.fileman.js").fileManager,g=require("path"),c=c.getUserRootDataPath(),f=g.resolve(c,e.FILE_NAME);d.ensureFile(f,function(d){d?a&&a(d):b._loadData(f,function(d,c){d?a&&a(d):(b._peripherals=c,a&&a())})})};e.prototype._loadData=function(a,b){require("fs").readFile(a,"utf8",function(a,c){if(a)b&&b(a);else if(c)try{var e=JSON.parse(c);b&&b(null,e)}catch(f){b&&b(f)}else b&&b(null,[])})};e.prototype._initUdevMonitor=function(){var a=require("udev").monitor();a.on("add",
  this._onAdd.bind(this));a.on("remove",this._onRemove.bind(this));a.on("change",this._onChange.bind(this));var a=this._listSerialDev(),b=this;a&&a.forEach(function(a){if(a=b._udev2dev(a))console.log("find dev",a),b._onFindUsbDevice(a)})};e.prototype._listSerialDev=function(){return global&&"A20"!=global.PLATFORM&&"PI2"!=global.PLATFORM&&"MAC"!=global.PLATFORM?null:require("udev").list("tty")};e.prototype._isSameDev=function(a,b){for(var d in a)if(a.hasOwnProperty(d)&&a[d]!=b[d])return!1;for(d in b)if(b.hasOwnProperty(d)&&
  a[d]!=b[d])return!1;return!0};e.prototype._onAdd=function(a){this._logger.log("trace","add: "+JSON.stringify(a));(a=this._udev2dev(a))&&this._onFindUsbDevice(a)};e.prototype._onRemove=function(a){this._logger.log("trace","remove: "+JSON.stringify(a));(a=this._udev2dev(a))&&this._onRemoveUsbDevice(a)};e.prototype._onChange=function(a){this._logger.log("trace","changed: "+JSON.stringify(a))};e.prototype._udev2dev=function(a){if(!a||"usb"!=a.ID_BUS||!a.DEVNAME)return null;var b={},d;for(d in a)a.hasOwnProperty(d)&&
  "ID"==d.substr(0,2)&&(b[d]=a[d]);a={port:a.DEVNAME,info:b};"EnOcean_GmbH"==b.ID_VENDOR&&b.ID_USB_DRIVER?a.type="enocean":"0658"==b.ID_VENDOR&&b.ID_USB_DRIVER?a.type="zwave":"mediola_IopOS"==b.ID_VENDOR&&"IopZigbee_USB_CDC"==b.ID_MODEL&&(a.type="zigbee");return a};e.prototype._onFindUsbDevice=function(a){for(var b=this._peripherals.length,d=!1,c=null;b;)if(b--,c=this._peripherals[b],this._isSameDev(c.info,a.info)){d=!0;c.port=a.port;break}d||(c=a,this._peripherals.push(c));a=require("x.hub.eventbus.js");
  switch(c.type){case "enocean":a.emit("x.hub.peripheralman.ADD_ENOCEAN",c);break;case "zwave":a.emit("x.hub.peripheralman.ADD_ZWAVE",c);break;case "zigbee":a.emit("x.hub.peripheralman.ADD_ZIGBEE",c)}c&&c.info&&c.info.ID_USB_DRIVER&&a.emit("x.hub.peripheralman.ADD_SEIRAL",c)};e.prototype._onRemoveUsbDevice=function(a){for(var b=this._peripherals.length,d=!1,c=null;b;)if(b--,c=this._peripherals[b],this._isSameDev(c.info,a.info)){d=!0;break}d&&this._peripherals.splice(b,1);a=require("x.hub.eventbus.js");
  switch(c.type){case "enocean":a.emit("x.hub.peripheralman.REMOVE_ENOCEAN",c);break;case "zwave":a.emit("x.hub.peripheralman.REMOVE_ZWAVE",c);break;case "zigbee":a.emit("x.hub.peripheralman.REMOVE_ZIGBEE",c)}c&&c.info&&c.info.ID_USB_DRIVER&&a.emit("x.hub.peripheralman.REMOVE_SEIRAL",c)};return e}();
  x.hub.peripheralman.Handler=function(){var e=function(){this._logger=require("x.logger.js").getLogger("x.hub.peripheralman.Handler");this.peripheralManager=new x.hub.peripheralman.PeripheralManager};e.prototype.PeripheralManager=x.hub.peripheralman.PeripheralManager;e.prototype.init=function(a,b,d){this.peripheralManager.init(d)};e.prototype.getUSBDevices=function(a){var b=this.peripheralManager.getUSBDevices();a&&a({data:b})};e.prototype.getUSBDevicesShortInfo=function(a){for(var b=this.peripheralManager.getUSBDevices(),
  d=[],c=0;c<b.length;c++){var e=b[c];e.type&&d.push({port:e.port,type:e.type,model:e.info?e.info.ID_MODEL:null,serial:e.info?e.info.ID_SERIAL:null,vendor:e.info?e.info.ID_VENDOR:null})}a&&a({data:d})};return e}();module.exports=new x.hub.peripheralman.Handler;
  