b82507e6b8febf13eb0ef8dc097e7b4ff47f293f
[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 _0xef2a=['localhost','trunks','telephones','registryentry','syncRegistry','bind','registry','syncPeerStatus','resolve','then','actionSipShowRegistry','catch','error','[userReport][sipshowregistry]','[userReport][devicestatelist]','inspect','[userReport][sippeerstatus]','prototype','isNotNull','isUndefined','emit','info','[userReport][registry]\x20registry:%s\x20state:%s','username','state','[userReport][registry]','find','stateRegistry','merge','omit','event','actionid','debug','[userReport][registry]\x20sending\x20trunk:save\x20event:','trunk:%s','name','trunk:save','[userReport][devicestatechange]\x20device:%s\x20state:%s','device','[userReport][devicestatechange]','agents','local','toLowerCase','stateTime','toNumber','format','updateStatusRealtime','[userReport][devicestatechange]\x20sending\x20telephone:save\x20event:','startsWith','Local','substring','indexOf','phone','peerstatus','address','[userReport][peerstatus]','peer','registered','status','[userReport][peerstatus]\x20sending\x20telephone:save\x20event:','exports','lodash','moment','util','ioredis','bluebird','../../../config/environment','../../../config/logger','ami','redis','defaults'];(function(_0x279d37,_0x14de3f){var _0x530173=function(_0x37fc79){while(--_0x37fc79){_0x279d37['push'](_0x279d37['shift']());}};_0x530173(++_0x14de3f);}(_0xef2a,0x1a0));var _0xaef2=function(_0x5228cd,_0x268079){_0x5228cd=_0x5228cd-0x0;var _0x184e51=_0xef2a[_0x5228cd];return _0x184e51;};'use strict';var _=require(_0xaef2('0x0'));var moment=require(_0xaef2('0x1'));var util=require(_0xaef2('0x2'));var Redis=require(_0xaef2('0x3'));var BPromise=require(_0xaef2('0x4'));var config=require(_0xaef2('0x5'));var logger=require(_0xaef2('0x6'))(_0xaef2('0x7'));var ami=require('../ami');config[_0xaef2('0x8')]=_[_0xaef2('0x9')](config['redis'],{'host':_0xaef2('0xa'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xaef2('0x8')]));function UserReport(_0x50be70){this[_0xaef2('0xb')]=_0x50be70['trunks'];this['agents']=_0x50be70['agents'];this['telephones']=_0x50be70[_0xaef2('0xc')];ami['on'](_0xaef2('0xd'),this[_0xaef2('0xe')][_0xaef2('0xf')](this));ami['on'](_0xaef2('0x10'),this[_0xaef2('0xe')][_0xaef2('0xf')](this));ami['on']('devicestatechange',this['syncDeviceState'][_0xaef2('0xf')](this));ami['on']('peerstatus',this[_0xaef2('0x11')][_0xaef2('0xf')](this));return this['syncRealtimeShow']();}function showSipRegistrations(){Promise[_0xaef2('0x12')]()[_0xaef2('0x13')](ami[_0xaef2('0x14')]())[_0xaef2('0x15')](function(_0x2deaf1){logger[_0xaef2('0x16')](_0xaef2('0x17'),util['inspect'](_0x2deaf1,{'showHidden':![],'depth':null}));});}function listDeviceStates(){Promise[_0xaef2('0x12')]()[_0xaef2('0x13')](ami['actionDeviceStateList']())[_0xaef2('0x15')](function(_0x55e95b){logger['error'](_0xaef2('0x18'),util[_0xaef2('0x19')](_0x55e95b,{'showHidden':![],'depth':null}));});}function showPeersStatus(){Promise[_0xaef2('0x12')]()[_0xaef2('0x13')](ami['actionSipPeerStatus']())['catch'](function(_0x22e732){logger[_0xaef2('0x16')](_0xaef2('0x1a'),util['inspect'](_0x22e732,{'showHidden':![],'depth':null}));});}UserReport[_0xaef2('0x1b')]['syncRealtimeShow']=function(){setInterval(function(){BPromise[_0xaef2('0x12')]()[_0xaef2('0x13')](showSipRegistrations())[_0xaef2('0x13')](listDeviceStates())[_0xaef2('0x13')](showPeersStatus());},0x32c8);};UserReport[_0xaef2('0x1b')][_0xaef2('0x1c')]=function(_0x2a2038){return _0x2a2038!==null&&!_[_0xaef2('0x1d')](_0x2a2038);};UserReport[_0xaef2('0x1b')][_0xaef2('0x1e')]=function(_0x1ca561,_0x46c5c7,_0xaba580){io['to'](_0x1ca561)[_0xaef2('0x1e')](_0x46c5c7,_0xaba580);};UserReport[_0xaef2('0x1b')][_0xaef2('0xe')]=function(_0x4f0984){try{if(this[_0xaef2('0x1c')](_0x4f0984)){logger[_0xaef2('0x1f')](_0xaef2('0x20'),_0x4f0984[_0xaef2('0x21')],_0x4f0984[_0xaef2('0x22')]);logger['debug'](_0xaef2('0x23'),util[_0xaef2('0x19')](_0x4f0984,{'showHidden':![],'depth':null}));var _0x5d8c80=_[_0xaef2('0x24')](this[_0xaef2('0xb')],{'registry':_0x4f0984[_0xaef2('0x21')]});if(_0x5d8c80){_0x4f0984[_0xaef2('0x25')]=_0x4f0984[_0xaef2('0x22')];_[_0xaef2('0x26')](_0x5d8c80,_[_0xaef2('0x27')](_0x4f0984,[_0xaef2('0x28'),_0xaef2('0x29'),'username',_0xaef2('0x22')]));logger[_0xaef2('0x2a')](_0xaef2('0x2b'),util['inspect'](_0x5d8c80,{'showHidden':![],'depth':null}));this[_0xaef2('0x1e')](util['format'](_0xaef2('0x2c'),_0x5d8c80[_0xaef2('0x2d')]),_0xaef2('0x2e'),_0x5d8c80);}}}catch(_0x3d5054){logger['error'](_0xaef2('0x23'),util[_0xaef2('0x19')](_0x3d5054,{'showHidden':![],'depth':null}));}};UserReport['prototype']['syncDeviceState']=function(_0x46346e){try{if(this[_0xaef2('0x1c')](_0x46346e)){logger[_0xaef2('0x1f')](_0xaef2('0x2f'),_0x46346e[_0xaef2('0x30')],_0x46346e[_0xaef2('0x22')]);logger[_0xaef2('0x2a')](_0xaef2('0x31'),util['inspect'](_0x46346e,{'showHidden':![],'depth':null}));if(this[_0xaef2('0x32')][_0x46346e['device']]){if(!this['agents'][_0x46346e[_0xaef2('0x30')]][_0xaef2('0x33')]||_0x46346e[_0xaef2('0x22')][_0xaef2('0x34')]()=='inuse'){this['agents'][_0x46346e[_0xaef2('0x30')]][_0xaef2('0x33')]=![];if(this['agents'][_0x46346e[_0xaef2('0x30')]][_0xaef2('0x22')]!==_0x46346e['state'][_0xaef2('0x34')]()){this[_0xaef2('0x32')][_0x46346e[_0xaef2('0x30')]][_0xaef2('0x35')]=_[_0xaef2('0x36')](moment()[_0xaef2('0x37')]('x'));}this[_0xaef2('0x32')][_0x46346e['device']]['state']=_0x46346e[_0xaef2('0x22')]['toLowerCase']();this[_0xaef2('0x32')][_0x46346e['device']][_0xaef2('0x38')](_0x46346e);}}else if(this[_0xaef2('0xc')][_0x46346e[_0xaef2('0x30')]]){if(this[_0xaef2('0xc')][_0x46346e[_0xaef2('0x30')]][_0xaef2('0x22')]!==_0x46346e[_0xaef2('0x22')][_0xaef2('0x34')]()){this[_0xaef2('0xc')][_0x46346e['device']]['stateTime']=_[_0xaef2('0x36')](moment()[_0xaef2('0x37')]('x'));}this[_0xaef2('0xc')][_0x46346e[_0xaef2('0x30')]][_0xaef2('0x22')]=_0x46346e[_0xaef2('0x22')][_0xaef2('0x34')]();logger[_0xaef2('0x2a')](_0xaef2('0x39'),util['inspect'](this[_0xaef2('0xc')][_0x46346e[_0xaef2('0x30')]],{'showHidden':![],'depth':null}));this[_0xaef2('0x1e')](util[_0xaef2('0x37')]('telephone:%s',this[_0xaef2('0xc')][_0x46346e['device']][_0xaef2('0x2d')]),'telephone:save',this[_0xaef2('0xc')][_0x46346e[_0xaef2('0x30')]]);}else if(this[_0xaef2('0xb')][_0x46346e[_0xaef2('0x30')]]){if(this[_0xaef2('0xb')][_0x46346e['device']][_0xaef2('0x22')]!==_0x46346e['state'][_0xaef2('0x34')]()){this[_0xaef2('0xb')][_0x46346e['device']][_0xaef2('0x35')]=_[_0xaef2('0x36')](moment()['format']('x'));}this[_0xaef2('0xb')][_0x46346e[_0xaef2('0x30')]]['state']=_0x46346e['state']['toLowerCase']();logger[_0xaef2('0x2a')]('[userReport][devicestatechange]\x20sending\x20trunk:save\x20event:',util[_0xaef2('0x19')](this[_0xaef2('0xb')][_0x46346e[_0xaef2('0x30')]],{'showHidden':![],'depth':null}));this['emit'](util[_0xaef2('0x37')]('trunk:%s',this[_0xaef2('0xb')][_0x46346e[_0xaef2('0x30')]][_0xaef2('0x2d')]),_0xaef2('0x2e'),this[_0xaef2('0xb')][_0x46346e[_0xaef2('0x30')]]);}else if(_[_0xaef2('0x3a')](_0x46346e[_0xaef2('0x30')],_0xaef2('0x3b'))){var _0x599fb6=_0x46346e['device'];var _0x59699d=_0x599fb6[_0xaef2('0x3c')](_0x599fb6[_0xaef2('0x3d')]('/')+0x1,_0x599fb6[_0xaef2('0x3d')]('@'));var _0x24f706=_['find'](this[_0xaef2('0x32')],function(_0x1bd4e3){return _0x1bd4e3['mobile']==_0x59699d||_0x1bd4e3[_0xaef2('0x3e')]==_0x59699d||_0x1bd4e3['accountcode']==_0x59699d;});if(_0x24f706){if(_0x24f706['state']!==_0x46346e['state']['toLowerCase']()){_0x24f706[_0xaef2('0x35')]=_[_0xaef2('0x36')](moment()[_0xaef2('0x37')]('x'));}_0x24f706[_0xaef2('0x22')]=_0x46346e[_0xaef2('0x22')][_0xaef2('0x34')]();_0x24f706[_0xaef2('0x33')]=!![];_0x24f706['updateStatusRealtime'](_0x46346e);}}}}catch(_0x272cbb){logger['error'](_0xaef2('0x31'),util['inspect'](_0x272cbb,{'showHidden':![],'depth':null}));}};UserReport['prototype'][_0xaef2('0x11')]=function(_0x5e4dd7){try{if(this[_0xaef2('0x1c')](_0x5e4dd7)&&this[_0xaef2('0x1c')](_0x5e4dd7[_0xaef2('0x3f')])){logger[_0xaef2('0x1f')]('[userReport][peerstatus]\x20peer:%s\x20address:%s\x20status:%s',_0x5e4dd7['peer'],_0x5e4dd7[_0xaef2('0x40')],_0x5e4dd7[_0xaef2('0x3f')]);logger[_0xaef2('0x2a')](_0xaef2('0x41'),util[_0xaef2('0x19')](_0x5e4dd7,{'showHidden':![],'depth':null}));if(this[_0xaef2('0x32')][_0x5e4dd7[_0xaef2('0x42')]]){if(!this[_0xaef2('0x32')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x33')]||_0x5e4dd7[_0xaef2('0x3f')][_0xaef2('0x34')]()==_0xaef2('0x43')){this['agents'][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x33')]=![];this[_0xaef2('0x32')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x44')]=_0x5e4dd7[_0xaef2('0x3f')][_0xaef2('0x34')]();this['agents'][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x40')]=_0x5e4dd7[_0xaef2('0x40')];this[_0xaef2('0x32')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x38')](_0x5e4dd7);}}else if(this[_0xaef2('0xc')][_0x5e4dd7[_0xaef2('0x42')]]){this[_0xaef2('0xc')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x44')]=_0x5e4dd7[_0xaef2('0x3f')][_0xaef2('0x34')]();this[_0xaef2('0xc')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x40')]=_0x5e4dd7[_0xaef2('0x40')];logger[_0xaef2('0x2a')](_0xaef2('0x45'),util[_0xaef2('0x19')](this[_0xaef2('0xc')][_0x5e4dd7[_0xaef2('0x42')]],{'showHidden':![],'depth':null}));this[_0xaef2('0x1e')](util[_0xaef2('0x37')]('telephone:%s',this[_0xaef2('0xc')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x2d')]),'telephone:save',this[_0xaef2('0xc')][_0x5e4dd7['peer']]);}else if(this[_0xaef2('0xb')][_0x5e4dd7[_0xaef2('0x42')]]){this[_0xaef2('0xb')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x44')]=_0x5e4dd7[_0xaef2('0x3f')]['toLowerCase']();this[_0xaef2('0xb')][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x40')]=_0x5e4dd7[_0xaef2('0x40')];logger[_0xaef2('0x2a')]('[userReport][peerstatus]\x20sending\x20trunk:save\x20event:',util[_0xaef2('0x19')](this[_0xaef2('0xb')][_0x5e4dd7['peer']],{'showHidden':![],'depth':null}));this['emit'](util[_0xaef2('0x37')]('trunk:%s',this['trunks'][_0x5e4dd7[_0xaef2('0x42')]][_0xaef2('0x2d')]),_0xaef2('0x2e'),this[_0xaef2('0xb')][_0x5e4dd7[_0xaef2('0x42')]]);}}}catch(_0x23d977){logger[_0xaef2('0x16')](_0xaef2('0x41'),util[_0xaef2('0x19')](_0x23d977,{'showHidden':![],'depth':null}));}};module[_0xaef2('0x46')]=UserReport;