Built motion from commit fade34fd.|2.5.41
[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 _0x4fa6=['phone','accountcode','updateStatusRealtime','[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/logger','../ami','redis','defaults','socket.io-emitter','trunks','agents','telephones','registryentry','syncRegistry','bind','devicestatechange','peerstatus','syncPeerStatus','syncRealtimeShow','resolve','then','actionSipShowRegistry','error','inspect','catch','actionSipPeerStatus','prototype','isNotNull','isUndefined','emit','username','state','debug','find','stateRegistry','merge','omit','event','actionid','format','name','trunk:save','[userReport][registry]','syncDeviceState','info','[userReport][devicestatechange]\x20device:%s\x20state:%s','device','[userReport][devicestatechange]','local','inuse','toLowerCase','toNumber','stateTime','[userReport][devicestatechange]\x20sending\x20telephone:save\x20event:','telephone:%s','telephone:save','[userReport][devicestatechange]\x20sending\x20trunk:save\x20event:','trunk:%s','Local','substring','indexOf'];(function(_0x39bdb5,_0x84bb49){var _0x3afcdf=function(_0x4dfbd7){while(--_0x4dfbd7){_0x39bdb5['push'](_0x39bdb5['shift']());}};_0x3afcdf(++_0x84bb49);}(_0x4fa6,0x9b));var _0x64fa=function(_0x39c734,_0x2f3e06){_0x39c734=_0x39c734-0x0;var _0xb2a2f9=_0x4fa6[_0x39c734];return _0xb2a2f9;};'use strict';var _=require('lodash');var moment=require(_0x64fa('0x0'));var util=require(_0x64fa('0x1'));var Redis=require(_0x64fa('0x2'));var BPromise=require(_0x64fa('0x3'));var config=require('../../../config/environment');var logger=require(_0x64fa('0x4'))('ami');var ami=require(_0x64fa('0x5'));config[_0x64fa('0x6')]=_[_0x64fa('0x7')](config[_0x64fa('0x6')],{'host':'localhost','port':0x18eb});var io=require(_0x64fa('0x8'))(new Redis(config[_0x64fa('0x6')]));function UserReport(_0x34babe){this[_0x64fa('0x9')]=_0x34babe['trunks'];this[_0x64fa('0xa')]=_0x34babe['agents'];this[_0x64fa('0xb')]=_0x34babe['telephones'];ami['on'](_0x64fa('0xc'),this[_0x64fa('0xd')]['bind'](this));ami['on']('registry',this[_0x64fa('0xd')][_0x64fa('0xe')](this));ami['on'](_0x64fa('0xf'),this['syncDeviceState']['bind'](this));ami['on'](_0x64fa('0x10'),this[_0x64fa('0x11')][_0x64fa('0xe')](this));return this[_0x64fa('0x12')]();}function showSipRegistrations(){Promise[_0x64fa('0x13')]()[_0x64fa('0x14')](ami[_0x64fa('0x15')]())['catch'](function(_0x5deef2){logger[_0x64fa('0x16')]('[userReport][sipshowregistry]',util[_0x64fa('0x17')](_0x5deef2,{'showHidden':![],'depth':null}));});}function listDeviceStates(){Promise[_0x64fa('0x13')]()[_0x64fa('0x14')](ami['actionDeviceStateList']())[_0x64fa('0x18')](function(_0x453f87){logger[_0x64fa('0x16')]('[userReport][devicestatelist]',util[_0x64fa('0x17')](_0x453f87,{'showHidden':![],'depth':null}));});}function showPeersStatus(){Promise[_0x64fa('0x13')]()[_0x64fa('0x14')](ami[_0x64fa('0x19')]())[_0x64fa('0x18')](function(_0x3ffb28){logger[_0x64fa('0x16')]('[userReport][sippeerstatus]',util[_0x64fa('0x17')](_0x3ffb28,{'showHidden':![],'depth':null}));});}UserReport[_0x64fa('0x1a')][_0x64fa('0x12')]=function(){setInterval(function(){BPromise[_0x64fa('0x13')]()[_0x64fa('0x14')](showSipRegistrations())[_0x64fa('0x14')](listDeviceStates())[_0x64fa('0x14')](showPeersStatus());},0x32c8);};UserReport[_0x64fa('0x1a')][_0x64fa('0x1b')]=function(_0x3e7ca2){return _0x3e7ca2!==null&&!_[_0x64fa('0x1c')](_0x3e7ca2);};UserReport[_0x64fa('0x1a')]['emit']=function(_0x525a0a,_0x51ab5b,_0xc5b727){io['to'](_0x525a0a)[_0x64fa('0x1d')](_0x51ab5b,_0xc5b727);};UserReport[_0x64fa('0x1a')][_0x64fa('0xd')]=function(_0x5f0a76){try{if(this['isNotNull'](_0x5f0a76)){logger['info']('[userReport][registry]\x20registry:%s\x20state:%s',_0x5f0a76[_0x64fa('0x1e')],_0x5f0a76[_0x64fa('0x1f')]);logger[_0x64fa('0x20')]('[userReport][registry]',util[_0x64fa('0x17')](_0x5f0a76,{'showHidden':![],'depth':null}));var _0xcd9c8=_[_0x64fa('0x21')](this[_0x64fa('0x9')],{'defaultuser':_0x5f0a76[_0x64fa('0x1e')]});if(_0xcd9c8){_0x5f0a76[_0x64fa('0x22')]=_0x5f0a76[_0x64fa('0x1f')];_[_0x64fa('0x23')](_0xcd9c8,_[_0x64fa('0x24')](_0x5f0a76,[_0x64fa('0x25'),_0x64fa('0x26'),'username',_0x64fa('0x1f')]));logger[_0x64fa('0x20')]('[userReport][registry]\x20sending\x20trunk:save\x20event:',util[_0x64fa('0x17')](_0xcd9c8,{'showHidden':![],'depth':null}));this[_0x64fa('0x1d')](util[_0x64fa('0x27')]('trunk:%s',_0xcd9c8[_0x64fa('0x28')]),_0x64fa('0x29'),_0xcd9c8);}}}catch(_0x20c979){logger[_0x64fa('0x16')](_0x64fa('0x2a'),util[_0x64fa('0x17')](_0x20c979,{'showHidden':![],'depth':null}));}};UserReport['prototype'][_0x64fa('0x2b')]=function(_0x383a36){try{if(this[_0x64fa('0x1b')](_0x383a36)){logger[_0x64fa('0x2c')](_0x64fa('0x2d'),_0x383a36[_0x64fa('0x2e')],_0x383a36[_0x64fa('0x1f')]);logger[_0x64fa('0x20')](_0x64fa('0x2f'),util[_0x64fa('0x17')](_0x383a36,{'showHidden':![],'depth':null}));if(this[_0x64fa('0xa')][_0x383a36[_0x64fa('0x2e')]]){if(!this[_0x64fa('0xa')][_0x383a36['device']][_0x64fa('0x30')]||_0x383a36['state']['toLowerCase']()==_0x64fa('0x31')){this[_0x64fa('0xa')][_0x383a36['device']][_0x64fa('0x30')]=![];if(this[_0x64fa('0xa')][_0x383a36[_0x64fa('0x2e')]][_0x64fa('0x1f')]!==_0x383a36['state'][_0x64fa('0x32')]()){this['agents'][_0x383a36['device']]['stateTime']=_[_0x64fa('0x33')](moment()[_0x64fa('0x27')]('x'));}this[_0x64fa('0xa')][_0x383a36[_0x64fa('0x2e')]][_0x64fa('0x1f')]=_0x383a36['state'][_0x64fa('0x32')]();this[_0x64fa('0xa')][_0x383a36[_0x64fa('0x2e')]]['updateStatusRealtime'](_0x383a36);}}else if(this[_0x64fa('0xb')][_0x383a36[_0x64fa('0x2e')]]){if(this[_0x64fa('0xb')][_0x383a36['device']]['state']!==_0x383a36[_0x64fa('0x1f')][_0x64fa('0x32')]()){this[_0x64fa('0xb')][_0x383a36[_0x64fa('0x2e')]][_0x64fa('0x34')]=_[_0x64fa('0x33')](moment()[_0x64fa('0x27')]('x'));}this['telephones'][_0x383a36['device']][_0x64fa('0x1f')]=_0x383a36[_0x64fa('0x1f')][_0x64fa('0x32')]();logger[_0x64fa('0x20')](_0x64fa('0x35'),util[_0x64fa('0x17')](this['telephones'][_0x383a36['device']],{'showHidden':![],'depth':null}));this[_0x64fa('0x1d')](util[_0x64fa('0x27')](_0x64fa('0x36'),this['telephones'][_0x383a36[_0x64fa('0x2e')]][_0x64fa('0x28')]),_0x64fa('0x37'),this[_0x64fa('0xb')][_0x383a36[_0x64fa('0x2e')]]);}else if(this['trunks'][_0x383a36[_0x64fa('0x2e')]]){if(this[_0x64fa('0x9')][_0x383a36[_0x64fa('0x2e')]][_0x64fa('0x1f')]!==_0x383a36[_0x64fa('0x1f')][_0x64fa('0x32')]()){this[_0x64fa('0x9')][_0x383a36[_0x64fa('0x2e')]]['stateTime']=_[_0x64fa('0x33')](moment()[_0x64fa('0x27')]('x'));}this[_0x64fa('0x9')][_0x383a36['device']][_0x64fa('0x1f')]=_0x383a36[_0x64fa('0x1f')][_0x64fa('0x32')]();logger[_0x64fa('0x20')](_0x64fa('0x38'),util[_0x64fa('0x17')](this[_0x64fa('0x9')][_0x383a36[_0x64fa('0x2e')]],{'showHidden':![],'depth':null}));this[_0x64fa('0x1d')](util[_0x64fa('0x27')](_0x64fa('0x39'),this['trunks'][_0x383a36[_0x64fa('0x2e')]][_0x64fa('0x28')]),_0x64fa('0x29'),this[_0x64fa('0x9')][_0x383a36[_0x64fa('0x2e')]]);}else if(_['startsWith'](_0x383a36[_0x64fa('0x2e')],_0x64fa('0x3a'))){var _0x57dc34=_0x383a36[_0x64fa('0x2e')];var _0x1354cc=_0x57dc34[_0x64fa('0x3b')](_0x57dc34[_0x64fa('0x3c')]('/')+0x1,_0x57dc34[_0x64fa('0x3c')]('@'));var _0xf4ee1e=_['find'](this['agents'],function(_0x460cda){return _0x460cda['mobile']==_0x1354cc||_0x460cda[_0x64fa('0x3d')]==_0x1354cc||_0x460cda[_0x64fa('0x3e')]==_0x1354cc;});if(_0xf4ee1e){if(_0xf4ee1e[_0x64fa('0x1f')]!==_0x383a36[_0x64fa('0x1f')]['toLowerCase']()){_0xf4ee1e[_0x64fa('0x34')]=_[_0x64fa('0x33')](moment()[_0x64fa('0x27')]('x'));}_0xf4ee1e[_0x64fa('0x1f')]=_0x383a36[_0x64fa('0x1f')][_0x64fa('0x32')]();_0xf4ee1e[_0x64fa('0x30')]=!![];_0xf4ee1e[_0x64fa('0x3f')](_0x383a36);}}}}catch(_0x13ae73){logger[_0x64fa('0x16')](_0x64fa('0x2f'),util[_0x64fa('0x17')](_0x13ae73,{'showHidden':![],'depth':null}));}};UserReport[_0x64fa('0x1a')][_0x64fa('0x11')]=function(_0x53af6b){try{if(this['isNotNull'](_0x53af6b)&&this[_0x64fa('0x1b')](_0x53af6b['peerstatus'])){logger['info'](_0x64fa('0x40'),_0x53af6b[_0x64fa('0x41')],_0x53af6b[_0x64fa('0x42')],_0x53af6b['peerstatus']);logger[_0x64fa('0x20')](_0x64fa('0x43'),util[_0x64fa('0x17')](_0x53af6b,{'showHidden':![],'depth':null}));if(this[_0x64fa('0xa')][_0x53af6b[_0x64fa('0x41')]]){if(!this[_0x64fa('0xa')][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x30')]||_0x53af6b[_0x64fa('0x10')][_0x64fa('0x32')]()=='registered'){this['agents'][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x30')]=![];this[_0x64fa('0xa')][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x44')]=_0x53af6b[_0x64fa('0x10')]['toLowerCase']();this[_0x64fa('0xa')][_0x53af6b['peer']]['address']=_0x53af6b[_0x64fa('0x42')];this['agents'][_0x53af6b[_0x64fa('0x41')]]['updateStatusRealtime'](_0x53af6b);}}else if(this['telephones'][_0x53af6b[_0x64fa('0x41')]]){this[_0x64fa('0xb')][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x44')]=_0x53af6b[_0x64fa('0x10')][_0x64fa('0x32')]();this[_0x64fa('0xb')][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x42')]=_0x53af6b[_0x64fa('0x42')];logger[_0x64fa('0x20')](_0x64fa('0x45'),util[_0x64fa('0x17')](this[_0x64fa('0xb')][_0x53af6b['peer']],{'showHidden':![],'depth':null}));this['emit'](util[_0x64fa('0x27')](_0x64fa('0x36'),this[_0x64fa('0xb')][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x28')]),_0x64fa('0x37'),this['telephones'][_0x53af6b[_0x64fa('0x41')]]);}else if(this[_0x64fa('0x9')][_0x53af6b[_0x64fa('0x41')]]){this[_0x64fa('0x9')][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x44')]=_0x53af6b[_0x64fa('0x10')][_0x64fa('0x32')]();this[_0x64fa('0x9')][_0x53af6b[_0x64fa('0x41')]]['address']=_0x53af6b[_0x64fa('0x42')];logger[_0x64fa('0x20')](_0x64fa('0x46'),util[_0x64fa('0x17')](this[_0x64fa('0x9')][_0x53af6b[_0x64fa('0x41')]],{'showHidden':![],'depth':null}));this[_0x64fa('0x1d')](util[_0x64fa('0x27')](_0x64fa('0x39'),this[_0x64fa('0x9')][_0x53af6b[_0x64fa('0x41')]][_0x64fa('0x28')]),_0x64fa('0x29'),this[_0x64fa('0x9')][_0x53af6b[_0x64fa('0x41')]]);}}}catch(_0x2cbfe6){logger['error'](_0x64fa('0x43'),util[_0x64fa('0x17')](_0x2cbfe6,{'showHidden':![],'depth':null}));}};module[_0x64fa('0x47')]=UserReport;