01a6b957988fe18a54134c769db3c3011c20dc8b
[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 _0x32f4=['name','trunk:save','device','inuse','local','toLowerCase','stateTime','toNumber','updateStatusRealtime','[userReport][devicestatechange]\x20sending\x20telephone:save\x20event:','telephone:%s','telephone:save','startsWith','Local','substring','mobile','phone','[userReport][devicestatechange]','[userReport][peerstatus]\x20peer:%s\x20address:%s\x20status:%s','peer','address','[userReport][peerstatus]','status','[userReport][peerstatus]\x20sending\x20telephone:save\x20event:','[userReport][peerstatus]\x20sending\x20trunk:save\x20event:','exports','moment','util','ioredis','bluebird','../../../config/environment','../../../config/logger','ami','../ami','defaults','localhost','socket.io-emitter','trunks','agents','telephones','registryentry','syncRegistry','bind','registry','devicestatechange','syncDeviceState','peerstatus','syncPeerStatus','resolve','then','actionSipShowRegistry','catch','error','[userReport][sipshowregistry]','inspect','[userReport][devicestatelist]','actionSipPeerStatus','[userReport][sippeerstatus]','prototype','syncRealtimeShow','isNotNull','emit','info','[userReport][registry]\x20registry:%s\x20state:%s','username','state','debug','stateRegistry','merge','event','[userReport][registry]\x20sending\x20trunk:save\x20event:','format','trunk:%s'];(function(_0xfae23b,_0x2be4ba){var _0x200640=function(_0x4ae258){while(--_0x4ae258){_0xfae23b['push'](_0xfae23b['shift']());}};_0x200640(++_0x2be4ba);}(_0x32f4,0x187));var _0x432f=function(_0x511a04,_0x68a9b0){_0x511a04=_0x511a04-0x0;var _0x35e67e=_0x32f4[_0x511a04];return _0x35e67e;};'use strict';var _=require('lodash');var moment=require(_0x432f('0x0'));var util=require(_0x432f('0x1'));var Redis=require(_0x432f('0x2'));var BPromise=require(_0x432f('0x3'));var config=require(_0x432f('0x4'));var logger=require(_0x432f('0x5'))(_0x432f('0x6'));var ami=require(_0x432f('0x7'));config['redis']=_[_0x432f('0x8')](config['redis'],{'host':_0x432f('0x9'),'port':0x18eb});var io=require(_0x432f('0xa'))(new Redis(config['redis']));function UserReport(_0x342fc6){this[_0x432f('0xb')]=_0x342fc6[_0x432f('0xb')];this['agents']=_0x342fc6[_0x432f('0xc')];this[_0x432f('0xd')]=_0x342fc6[_0x432f('0xd')];ami['on'](_0x432f('0xe'),this[_0x432f('0xf')][_0x432f('0x10')](this));ami['on'](_0x432f('0x11'),this['syncRegistry'][_0x432f('0x10')](this));ami['on'](_0x432f('0x12'),this[_0x432f('0x13')][_0x432f('0x10')](this));ami['on'](_0x432f('0x14'),this[_0x432f('0x15')][_0x432f('0x10')](this));return this['syncRealtimeShow']();}function showSipRegistrations(){Promise[_0x432f('0x16')]()[_0x432f('0x17')](ami[_0x432f('0x18')]())[_0x432f('0x19')](function(_0x2e38f5){logger[_0x432f('0x1a')](_0x432f('0x1b'),util[_0x432f('0x1c')](_0x2e38f5,{'showHidden':![],'depth':null}));});}function listDeviceStates(){Promise[_0x432f('0x16')]()[_0x432f('0x17')](ami['actionDeviceStateList']())[_0x432f('0x19')](function(_0x5ae6a0){logger['error'](_0x432f('0x1d'),util[_0x432f('0x1c')](_0x5ae6a0,{'showHidden':![],'depth':null}));});}function showPeersStatus(){Promise['resolve']()['then'](ami[_0x432f('0x1e')]())[_0x432f('0x19')](function(_0x984337){logger['error'](_0x432f('0x1f'),util[_0x432f('0x1c')](_0x984337,{'showHidden':![],'depth':null}));});}UserReport[_0x432f('0x20')][_0x432f('0x21')]=function(){setInterval(function(){BPromise[_0x432f('0x16')]()[_0x432f('0x17')](showSipRegistrations())[_0x432f('0x17')](listDeviceStates())[_0x432f('0x17')](showPeersStatus());},0x32c8);};UserReport[_0x432f('0x20')][_0x432f('0x22')]=function(_0x13dae9){return _0x13dae9!==null&&!_['isUndefined'](_0x13dae9);};UserReport[_0x432f('0x20')][_0x432f('0x23')]=function(_0x213e22,_0x585d40,_0x5c882a){io['to'](_0x213e22)[_0x432f('0x23')](_0x585d40,_0x5c882a);};UserReport[_0x432f('0x20')][_0x432f('0xf')]=function(_0x1725ff){try{if(this[_0x432f('0x22')](_0x1725ff)){logger[_0x432f('0x24')](_0x432f('0x25'),_0x1725ff[_0x432f('0x26')],_0x1725ff[_0x432f('0x27')]);logger[_0x432f('0x28')]('[userReport][registry]',util[_0x432f('0x1c')](_0x1725ff,{'showHidden':![],'depth':null}));var _0x2b3617=_['find'](this[_0x432f('0xb')],{'defaultuser':_0x1725ff['username']});if(_0x2b3617){_0x1725ff[_0x432f('0x29')]=_0x1725ff[_0x432f('0x27')];_[_0x432f('0x2a')](_0x2b3617,_['omit'](_0x1725ff,[_0x432f('0x2b'),'actionid','username','state']));logger[_0x432f('0x28')](_0x432f('0x2c'),util[_0x432f('0x1c')](_0x2b3617,{'showHidden':![],'depth':null}));this['emit'](util[_0x432f('0x2d')](_0x432f('0x2e'),_0x2b3617[_0x432f('0x2f')]),_0x432f('0x30'),_0x2b3617);}}}catch(_0x459a47){logger[_0x432f('0x1a')]('[userReport][registry]',util['inspect'](_0x459a47,{'showHidden':![],'depth':null}));}};UserReport[_0x432f('0x20')][_0x432f('0x13')]=function(_0x1bea3e){try{if(this[_0x432f('0x22')](_0x1bea3e)){logger['info']('[userReport][devicestatechange]\x20device:%s\x20state:%s',_0x1bea3e[_0x432f('0x31')],_0x1bea3e['state']);logger[_0x432f('0x28')]('[userReport][devicestatechange]',util[_0x432f('0x1c')](_0x1bea3e,{'showHidden':![],'depth':null}));if(this['agents'][_0x1bea3e['device']]){if(!this[_0x432f('0xc')][_0x1bea3e[_0x432f('0x31')]]['local']||_0x1bea3e['state']['toLowerCase']()==_0x432f('0x32')){this[_0x432f('0xc')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x33')]=![];if(this[_0x432f('0xc')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x27')]!==_0x1bea3e[_0x432f('0x27')][_0x432f('0x34')]()){this[_0x432f('0xc')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x35')]=_[_0x432f('0x36')](moment()[_0x432f('0x2d')]('x'));}this[_0x432f('0xc')][_0x1bea3e[_0x432f('0x31')]]['state']=_0x1bea3e['state'][_0x432f('0x34')]();this[_0x432f('0xc')][_0x1bea3e['device']][_0x432f('0x37')](_0x1bea3e);}}else if(this['telephones'][_0x1bea3e[_0x432f('0x31')]]){if(this['telephones'][_0x1bea3e['device']][_0x432f('0x27')]!==_0x1bea3e[_0x432f('0x27')]['toLowerCase']()){this[_0x432f('0xd')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x35')]=_['toNumber'](moment()[_0x432f('0x2d')]('x'));}this[_0x432f('0xd')][_0x1bea3e['device']]['state']=_0x1bea3e[_0x432f('0x27')][_0x432f('0x34')]();logger['debug'](_0x432f('0x38'),util[_0x432f('0x1c')](this[_0x432f('0xd')][_0x1bea3e[_0x432f('0x31')]],{'showHidden':![],'depth':null}));this[_0x432f('0x23')](util[_0x432f('0x2d')](_0x432f('0x39'),this[_0x432f('0xd')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x2f')]),_0x432f('0x3a'),this[_0x432f('0xd')][_0x1bea3e['device']]);}else if(this[_0x432f('0xb')][_0x1bea3e[_0x432f('0x31')]]){if(this[_0x432f('0xb')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x27')]!==_0x1bea3e['state'][_0x432f('0x34')]()){this[_0x432f('0xb')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x35')]=_[_0x432f('0x36')](moment()[_0x432f('0x2d')]('x'));}this[_0x432f('0xb')][_0x1bea3e[_0x432f('0x31')]][_0x432f('0x27')]=_0x1bea3e[_0x432f('0x27')][_0x432f('0x34')]();logger[_0x432f('0x28')]('[userReport][devicestatechange]\x20sending\x20trunk:save\x20event:',util[_0x432f('0x1c')](this[_0x432f('0xb')][_0x1bea3e[_0x432f('0x31')]],{'showHidden':![],'depth':null}));this[_0x432f('0x23')](util[_0x432f('0x2d')](_0x432f('0x2e'),this['trunks'][_0x1bea3e['device']][_0x432f('0x2f')]),_0x432f('0x30'),this[_0x432f('0xb')][_0x1bea3e[_0x432f('0x31')]]);}else if(_[_0x432f('0x3b')](_0x1bea3e[_0x432f('0x31')],_0x432f('0x3c'))){var _0x355506=_0x1bea3e[_0x432f('0x31')];var _0x1e60b0=_0x355506[_0x432f('0x3d')](_0x355506['indexOf']('/')+0x1,_0x355506['indexOf']('@'));var _0x2bf26a=_['find'](this['agents'],function(_0x4f1915){return _0x4f1915[_0x432f('0x3e')]==_0x1e60b0||_0x4f1915[_0x432f('0x3f')]==_0x1e60b0||_0x4f1915['accountcode']==_0x1e60b0;});if(_0x2bf26a){if(_0x2bf26a['state']!==_0x1bea3e[_0x432f('0x27')]['toLowerCase']()){_0x2bf26a['stateTime']=_[_0x432f('0x36')](moment()['format']('x'));}_0x2bf26a['state']=_0x1bea3e['state'][_0x432f('0x34')]();_0x2bf26a[_0x432f('0x33')]=!![];_0x2bf26a['updateStatusRealtime'](_0x1bea3e);}}}}catch(_0x480192){logger[_0x432f('0x1a')](_0x432f('0x40'),util[_0x432f('0x1c')](_0x480192,{'showHidden':![],'depth':null}));}};UserReport['prototype']['syncPeerStatus']=function(_0x57e788){try{if(this[_0x432f('0x22')](_0x57e788)&&this[_0x432f('0x22')](_0x57e788['peerstatus'])){logger[_0x432f('0x24')](_0x432f('0x41'),_0x57e788[_0x432f('0x42')],_0x57e788[_0x432f('0x43')],_0x57e788[_0x432f('0x14')]);logger[_0x432f('0x28')](_0x432f('0x44'),util[_0x432f('0x1c')](_0x57e788,{'showHidden':![],'depth':null}));if(this[_0x432f('0xc')][_0x57e788[_0x432f('0x42')]]){if(!this[_0x432f('0xc')][_0x57e788[_0x432f('0x42')]][_0x432f('0x33')]||_0x57e788['peerstatus']['toLowerCase']()=='registered'){this['agents'][_0x57e788[_0x432f('0x42')]]['local']=![];this[_0x432f('0xc')][_0x57e788[_0x432f('0x42')]][_0x432f('0x45')]=_0x57e788['peerstatus']['toLowerCase']();this[_0x432f('0xc')][_0x57e788['peer']][_0x432f('0x43')]=_0x57e788[_0x432f('0x43')];this['agents'][_0x57e788['peer']][_0x432f('0x37')](_0x57e788);}}else if(this[_0x432f('0xd')][_0x57e788[_0x432f('0x42')]]){this[_0x432f('0xd')][_0x57e788[_0x432f('0x42')]][_0x432f('0x45')]=_0x57e788[_0x432f('0x14')][_0x432f('0x34')]();this[_0x432f('0xd')][_0x57e788[_0x432f('0x42')]][_0x432f('0x43')]=_0x57e788[_0x432f('0x43')];logger[_0x432f('0x28')](_0x432f('0x46'),util[_0x432f('0x1c')](this[_0x432f('0xd')][_0x57e788[_0x432f('0x42')]],{'showHidden':![],'depth':null}));this[_0x432f('0x23')](util[_0x432f('0x2d')](_0x432f('0x39'),this[_0x432f('0xd')][_0x57e788[_0x432f('0x42')]][_0x432f('0x2f')]),'telephone:save',this[_0x432f('0xd')][_0x57e788['peer']]);}else if(this[_0x432f('0xb')][_0x57e788['peer']]){this[_0x432f('0xb')][_0x57e788['peer']][_0x432f('0x45')]=_0x57e788[_0x432f('0x14')]['toLowerCase']();this[_0x432f('0xb')][_0x57e788[_0x432f('0x42')]][_0x432f('0x43')]=_0x57e788['address'];logger['debug'](_0x432f('0x47'),util[_0x432f('0x1c')](this[_0x432f('0xb')][_0x57e788[_0x432f('0x42')]],{'showHidden':![],'depth':null}));this[_0x432f('0x23')](util[_0x432f('0x2d')](_0x432f('0x2e'),this['trunks'][_0x57e788['peer']][_0x432f('0x2f')]),'trunk:save',this[_0x432f('0xb')][_0x57e788[_0x432f('0x42')]]);}}}catch(_0xd12dc){logger[_0x432f('0x1a')](_0x432f('0x44'),util[_0x432f('0x1c')](_0xd12dc,{'showHidden':![],'depth':null}));}};module[_0x432f('0x48')]=UserReport;