a1077410b4f4b70e8b0d1d9a1bce452d8d8bae91
[motion2.git] / server / services / ami / report / userReport.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x894a=['accountcode','peer','[userReport][peerstatus]','registered','status','address','[userReport][peerstatus]\x20sending\x20telephone:save\x20event:','telephone:save','[userReport][peerstatus]\x20sending\x20trunk:save\x20event:','lodash','moment','bluebird','../../../config/environment','../../../config/logger','ami','../ami','redis','defaults','localhost','socket.io-emitter','trunks','agents','telephones','registryentry','syncRegistry','bind','devicestatechange','peerstatus','syncPeerStatus','syncRealtimeShow','resolve','then','[userReport][sipshowregistry]','actionDeviceStateList','catch','error','[userReport][devicestatelist]','actionSipPeerStatus','[userReport][sippeerstatus]','inspect','prototype','isNotNull','isUndefined','emit','info','[userReport][registry]\x20registry:%s\x20state:%s','username','debug','[userReport][registry]','state','merge','omit','event','actionid','[userReport][registry]\x20sending\x20trunk:save\x20event:','format','trunk:%s','name','trunk:save','syncDeviceState','[userReport][devicestatechange]\x20device:%s\x20state:%s','device','[userReport][devicestatechange]','toLowerCase','local','toNumber','updateStatusRealtime','[userReport][devicestatechange]\x20sending\x20telephone:save\x20event:','telephone:%s','Local','indexOf','mobile'];(function(_0x44c768,_0x552a5e){var _0x55daf5=function(_0x4958e8){while(--_0x4958e8){_0x44c768['push'](_0x44c768['shift']());}};_0x55daf5(++_0x552a5e);}(_0x894a,0xe1));var _0xa894=function(_0x1cd6b4,_0x1b1a18){_0x1cd6b4=_0x1cd6b4-0x0;var _0x5012fa=_0x894a[_0x1cd6b4];return _0x5012fa;};'use strict';var _=require(_0xa894('0x0'));var moment=require(_0xa894('0x1'));var util=require('util');var Redis=require('ioredis');var BPromise=require(_0xa894('0x2'));var config=require(_0xa894('0x3'));var logger=require(_0xa894('0x4'))(_0xa894('0x5'));var ami=require(_0xa894('0x6'));config[_0xa894('0x7')]=_[_0xa894('0x8')](config[_0xa894('0x7')],{'host':_0xa894('0x9'),'port':0x18eb});var io=require(_0xa894('0xa'))(new Redis(config[_0xa894('0x7')]));function UserReport(_0x116dec){this[_0xa894('0xb')]=_0x116dec[_0xa894('0xb')];this[_0xa894('0xc')]=_0x116dec[_0xa894('0xc')];this['telephones']=_0x116dec[_0xa894('0xd')];ami['on'](_0xa894('0xe'),this[_0xa894('0xf')][_0xa894('0x10')](this));ami['on']('registry',this[_0xa894('0xf')][_0xa894('0x10')](this));ami['on'](_0xa894('0x11'),this['syncDeviceState'][_0xa894('0x10')](this));ami['on'](_0xa894('0x12'),this[_0xa894('0x13')][_0xa894('0x10')](this));return this[_0xa894('0x14')]();}function showSipRegistrations(){Promise[_0xa894('0x15')]()[_0xa894('0x16')](ami['actionSipShowRegistry']())['catch'](function(_0x238403){logger['error'](_0xa894('0x17'),util['inspect'](_0x238403,{'showHidden':![],'depth':null}));});}function listDeviceStates(){Promise['resolve']()['then'](ami[_0xa894('0x18')]())[_0xa894('0x19')](function(_0x554145){logger[_0xa894('0x1a')](_0xa894('0x1b'),util['inspect'](_0x554145,{'showHidden':![],'depth':null}));});}function showPeersStatus(){Promise[_0xa894('0x15')]()[_0xa894('0x16')](ami[_0xa894('0x1c')]())[_0xa894('0x19')](function(_0x4fd1e2){logger['error'](_0xa894('0x1d'),util[_0xa894('0x1e')](_0x4fd1e2,{'showHidden':![],'depth':null}));});}UserReport[_0xa894('0x1f')][_0xa894('0x14')]=function(){setInterval(function(){BPromise[_0xa894('0x15')]()[_0xa894('0x16')](showSipRegistrations())['then'](listDeviceStates())[_0xa894('0x16')](showPeersStatus());},0x32c8);};UserReport[_0xa894('0x1f')][_0xa894('0x20')]=function(_0x38515d){return _0x38515d!==null&&!_[_0xa894('0x21')](_0x38515d);};UserReport['prototype'][_0xa894('0x22')]=function(_0x2290b3,_0x3a07df,_0x23e028){io['to'](_0x2290b3)[_0xa894('0x22')](_0x3a07df,_0x23e028);};UserReport[_0xa894('0x1f')][_0xa894('0xf')]=function(_0x2f79be){try{if(this[_0xa894('0x20')](_0x2f79be)){logger[_0xa894('0x23')](_0xa894('0x24'),_0x2f79be[_0xa894('0x25')],_0x2f79be['state']);logger[_0xa894('0x26')](_0xa894('0x27'),util[_0xa894('0x1e')](_0x2f79be,{'showHidden':![],'depth':null}));var _0x2e8809=_['find'](this[_0xa894('0xb')],{'defaultuser':_0x2f79be[_0xa894('0x25')]});if(_0x2e8809){_0x2f79be['stateRegistry']=_0x2f79be[_0xa894('0x28')];_[_0xa894('0x29')](_0x2e8809,_[_0xa894('0x2a')](_0x2f79be,[_0xa894('0x2b'),_0xa894('0x2c'),'username',_0xa894('0x28')]));logger[_0xa894('0x26')](_0xa894('0x2d'),util[_0xa894('0x1e')](_0x2e8809,{'showHidden':![],'depth':null}));this['emit'](util[_0xa894('0x2e')](_0xa894('0x2f'),_0x2e8809[_0xa894('0x30')]),_0xa894('0x31'),_0x2e8809);}}}catch(_0x4d1b49){logger[_0xa894('0x1a')](_0xa894('0x27'),util[_0xa894('0x1e')](_0x4d1b49,{'showHidden':![],'depth':null}));}};UserReport[_0xa894('0x1f')][_0xa894('0x32')]=function(_0x4f2cfc){try{if(this[_0xa894('0x20')](_0x4f2cfc)){logger[_0xa894('0x23')](_0xa894('0x33'),_0x4f2cfc[_0xa894('0x34')],_0x4f2cfc[_0xa894('0x28')]);logger[_0xa894('0x26')](_0xa894('0x35'),util['inspect'](_0x4f2cfc,{'showHidden':![],'depth':null}));if(this[_0xa894('0xc')][_0x4f2cfc['device']]){if(!this[_0xa894('0xc')][_0x4f2cfc[_0xa894('0x34')]]['local']||_0x4f2cfc[_0xa894('0x28')][_0xa894('0x36')]()=='inuse'){this[_0xa894('0xc')][_0x4f2cfc['device']][_0xa894('0x37')]=![];if(this[_0xa894('0xc')][_0x4f2cfc[_0xa894('0x34')]][_0xa894('0x28')]!==_0x4f2cfc[_0xa894('0x28')][_0xa894('0x36')]()){this['agents'][_0x4f2cfc[_0xa894('0x34')]]['stateTime']=_[_0xa894('0x38')](moment()['format']('x'));}this['agents'][_0x4f2cfc[_0xa894('0x34')]][_0xa894('0x28')]=_0x4f2cfc[_0xa894('0x28')][_0xa894('0x36')]();this['agents'][_0x4f2cfc[_0xa894('0x34')]][_0xa894('0x39')](_0x4f2cfc);}}else if(this[_0xa894('0xd')][_0x4f2cfc[_0xa894('0x34')]]){if(this[_0xa894('0xd')][_0x4f2cfc[_0xa894('0x34')]][_0xa894('0x28')]!==_0x4f2cfc['state'][_0xa894('0x36')]()){this[_0xa894('0xd')][_0x4f2cfc[_0xa894('0x34')]]['stateTime']=_[_0xa894('0x38')](moment()[_0xa894('0x2e')]('x'));}this['telephones'][_0x4f2cfc[_0xa894('0x34')]][_0xa894('0x28')]=_0x4f2cfc[_0xa894('0x28')][_0xa894('0x36')]();logger[_0xa894('0x26')](_0xa894('0x3a'),util[_0xa894('0x1e')](this[_0xa894('0xd')][_0x4f2cfc['device']],{'showHidden':![],'depth':null}));this['emit'](util[_0xa894('0x2e')](_0xa894('0x3b'),this[_0xa894('0xd')][_0x4f2cfc[_0xa894('0x34')]][_0xa894('0x30')]),'telephone:save',this[_0xa894('0xd')][_0x4f2cfc[_0xa894('0x34')]]);}else if(this[_0xa894('0xb')][_0x4f2cfc[_0xa894('0x34')]]){if(this[_0xa894('0xb')][_0x4f2cfc[_0xa894('0x34')]]['state']!==_0x4f2cfc[_0xa894('0x28')][_0xa894('0x36')]()){this[_0xa894('0xb')][_0x4f2cfc[_0xa894('0x34')]]['stateTime']=_[_0xa894('0x38')](moment()[_0xa894('0x2e')]('x'));}this['trunks'][_0x4f2cfc['device']][_0xa894('0x28')]=_0x4f2cfc[_0xa894('0x28')][_0xa894('0x36')]();logger[_0xa894('0x26')]('[userReport][devicestatechange]\x20sending\x20trunk:save\x20event:',util[_0xa894('0x1e')](this[_0xa894('0xb')][_0x4f2cfc[_0xa894('0x34')]],{'showHidden':![],'depth':null}));this[_0xa894('0x22')](util[_0xa894('0x2e')](_0xa894('0x2f'),this[_0xa894('0xb')][_0x4f2cfc[_0xa894('0x34')]][_0xa894('0x30')]),_0xa894('0x31'),this[_0xa894('0xb')][_0x4f2cfc[_0xa894('0x34')]]);}else if(_['startsWith'](_0x4f2cfc[_0xa894('0x34')],_0xa894('0x3c'))){var _0x1a4a44=_0x4f2cfc[_0xa894('0x34')];var _0x398f76=_0x1a4a44['substring'](_0x1a4a44[_0xa894('0x3d')]('/')+0x1,_0x1a4a44['indexOf']('@'));var _0x314d11=_['find'](this[_0xa894('0xc')],function(_0x1e7965){return _0x1e7965[_0xa894('0x3e')]==_0x398f76||_0x1e7965['phone']==_0x398f76||_0x1e7965[_0xa894('0x3f')]==_0x398f76;});if(_0x314d11){if(_0x314d11[_0xa894('0x28')]!==_0x4f2cfc[_0xa894('0x28')]['toLowerCase']()){_0x314d11['stateTime']=_[_0xa894('0x38')](moment()['format']('x'));}_0x314d11[_0xa894('0x28')]=_0x4f2cfc[_0xa894('0x28')][_0xa894('0x36')]();_0x314d11[_0xa894('0x37')]=!![];_0x314d11['updateStatusRealtime'](_0x4f2cfc);}}}}catch(_0x50ea9f){logger[_0xa894('0x1a')](_0xa894('0x35'),util['inspect'](_0x50ea9f,{'showHidden':![],'depth':null}));}};UserReport[_0xa894('0x1f')][_0xa894('0x13')]=function(_0x74304f){try{if(this[_0xa894('0x20')](_0x74304f)&&this[_0xa894('0x20')](_0x74304f['peerstatus'])){logger[_0xa894('0x23')]('[userReport][peerstatus]\x20peer:%s\x20address:%s\x20status:%s',_0x74304f[_0xa894('0x40')],_0x74304f['address'],_0x74304f[_0xa894('0x12')]);logger[_0xa894('0x26')](_0xa894('0x41'),util[_0xa894('0x1e')](_0x74304f,{'showHidden':![],'depth':null}));if(this[_0xa894('0xc')][_0x74304f[_0xa894('0x40')]]){if(!this['agents'][_0x74304f[_0xa894('0x40')]][_0xa894('0x37')]||_0x74304f['peerstatus'][_0xa894('0x36')]()==_0xa894('0x42')){this[_0xa894('0xc')][_0x74304f[_0xa894('0x40')]][_0xa894('0x37')]=![];this[_0xa894('0xc')][_0x74304f[_0xa894('0x40')]][_0xa894('0x43')]=_0x74304f['peerstatus'][_0xa894('0x36')]();this['agents'][_0x74304f[_0xa894('0x40')]][_0xa894('0x44')]=_0x74304f['address'];this[_0xa894('0xc')][_0x74304f['peer']]['updateStatusRealtime'](_0x74304f);}}else if(this[_0xa894('0xd')][_0x74304f[_0xa894('0x40')]]){this[_0xa894('0xd')][_0x74304f['peer']][_0xa894('0x43')]=_0x74304f['peerstatus'][_0xa894('0x36')]();this[_0xa894('0xd')][_0x74304f[_0xa894('0x40')]]['address']=_0x74304f[_0xa894('0x44')];logger['debug'](_0xa894('0x45'),util[_0xa894('0x1e')](this[_0xa894('0xd')][_0x74304f['peer']],{'showHidden':![],'depth':null}));this[_0xa894('0x22')](util[_0xa894('0x2e')](_0xa894('0x3b'),this['telephones'][_0x74304f['peer']][_0xa894('0x30')]),_0xa894('0x46'),this[_0xa894('0xd')][_0x74304f[_0xa894('0x40')]]);}else if(this[_0xa894('0xb')][_0x74304f['peer']]){this[_0xa894('0xb')][_0x74304f['peer']][_0xa894('0x43')]=_0x74304f[_0xa894('0x12')][_0xa894('0x36')]();this[_0xa894('0xb')][_0x74304f[_0xa894('0x40')]][_0xa894('0x44')]=_0x74304f[_0xa894('0x44')];logger[_0xa894('0x26')](_0xa894('0x47'),util['inspect'](this[_0xa894('0xb')][_0x74304f[_0xa894('0x40')]],{'showHidden':![],'depth':null}));this['emit'](util[_0xa894('0x2e')]('trunk:%s',this['trunks'][_0x74304f['peer']][_0xa894('0x30')]),_0xa894('0x31'),this[_0xa894('0xb')][_0x74304f['peer']]);}}}catch(_0x16f5e1){logger['error'](_0xa894('0x41'),util[_0xa894('0x1e')](_0x16f5e1,{'showHidden':![],'depth':null}));}};module['exports']=UserReport;