| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140 |
- #include "FsProxy.h"
- #include "EslGateway.h"
- #include "Config.h"
- #include "ChanTrunk.h"
- #include "ChanExten.h"
- #include "Session.h"
- #include "Config.h"
- #include "OperationReactor.h"
- #include "JsonStringMaker.h"
- #include <string>
- #include <string.h>
- #include "SoftAuth.h"
- CFsProxy::CFsProxy() : m_Gateway(this)
- {
- }
- CFsProxy::~CFsProxy()
- {
- release();
- }
- bool CFsProxy::init()
- {
- CConfig *cfg = CConfig::GetInstance();
- tts.Init(cfg->ttsPath()); // 初始化tts路径
- if (!JdbcHelper::GetInstance()->init(cfg->dbAddr(), cfg->dbUser(), cfg->dbPwd(), cfg->dbDatabase()))
- {
- LOG_ERROR("程序运行失败,数据库驱动加载失败");
- return false;
- }
- if (JdbcHelper::GetInstance()->jdbc_connect(false))
- {
- LOG_ERROR("程序运行失败,数据库连接失败");
- return false;
- }
- LOG_INFO("数据库连接成功。");
- //std::string sql = "update agent set exten = ''";
- std::string sql = "delete from rep_agent ";
- JdbcHelper::GetInstance()->jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
- }, [sql](sql::SQLException &e) {
- Format fmt("Sql执行失败,错误信息:[%s],Sql[%s]");
- fmt % e.what() % sql;
- LOG_ERROR(fmt.str().c_str());
- });
- m_VideoOffice.initVideoExten();
- m_DepttelOffice.InitDepttelext();
- if (!m_Server.init(cfg->wsPort()))
- {
- LOG_ERROR("程序运行失败,请检查端口[%d]是否已被占用", cfg->wsPort());
- return false;
- }
- LOG_INFO("websocket服务打开成功,占用端口[%d]", cfg->wsPort());
- m_Server.setCallbackRecvMsg(std::bind(&CFsProxy::__recvMsgFun, this, std::placeholders::_1, std::placeholders::_2));
- m_Server.setCallbackClose(std::bind(&CFsProxy::__closeFun, this, std::placeholders::_1));
- m_AutoCall.setDetalTaskFun(std::bind(&CFsProxy::__doAutoTask, this, std::placeholders::_1));
- //m_AutoCall.startTask();
- return __init();
- }
- void CFsProxy::release(void)
- {
- m_Server.stop();
- m_AutoCall.stopTask();
- m_Gateway.delAgentAll();
- m_Gateway.stop();
- __freeAgent();
- __freeSession();
- __freeExten();
- __freeTrunkChan();
- JdbcHelper::GetInstance()->jdbc_close_connect();
- }
- void CFsProxy::run(void)
- {
- m_Server.run();
- }
- /*****************************************************************
- **【函数名称】 onChanRegist
- **【函数功能】 通道注册的处理函数
- **【参数】
- **【返回值】
- ****************************************************************/
- void CFsProxy::onChanRegist(DEV_RES_TYPE ChanType, uint32_t ChanNo, CHAN_LOGIC_STATE ChanState)
- {
- Format fmt("通道注册: ChanType = %d,ChanNo = %d,ChanState = %d");
- fmt % ChanType % ChanNo % ChanState;
- LOG_INFO_S(fmt);
- }
- /*****************************************************************
- **【函数名称】 onExtenDestroy
- **【函数功能】 分机销毁的处理函数
- **【参数】
- **【返回值】
- ****************************************************************/
- void CFsProxy::onExtenDestroy(uint32_t ExtenNo)
- {
- }
- /*****************************************************************
- **【函数名称】 onExtenDirectOp
- **【函数功能】 分机直接操作启动
- **【参数】 OpType 启动的操作类型
- pHostChan 触发事件的通道
- pNotify 触发操作事件内容
- **【返回值】
- ****************************************************************/
- void CFsProxy::onExtenDirectOp(DEV_OP OpType, VirtualChan * pHostChan, PCHAN_EVENT_NOTIFY pNotify)
- {
- switch ((int)OpType)
- {
- case DEV_OP_CALL_OUT:
- {
- if (!COperationReactor::GetInstance()->onExtenCallFromDev(pHostChan, pNotify))
- __kill(FS_LINK_JOBID_INVALID, pHostChan->chanId());
- }
- break;
- } // end switch
- }
- /*****************************************************************
- **【函数名称】 onChanStateUpdate
- **【函数功能】 通道状态更新处理
- **【参数】
- **【返回值】
- *****************************************************************/
- void CFsProxy::onChanStateUpdate(long OpInstance, VirtualChan * pChan)
- {
- if (OpInstance != FS_LINK_INSTANCE_INVALID && pChan != nullptr)
- COperationReactor::GetInstance()->onEslEvtChanState(OpInstance, pChan);
- if (pChan == nullptr)
- return;
- // 发送通道/线路状态到坐席
- DEV_RES_TYPE devType = pChan->type(); // 通道类型
- std::string chanNo = std::to_string(pChan->no()); // 通道号 中继号或分机号
- //CHAN_LOGIC_STATE
- uint32_t chanState = pChan->state(); // 通道状态
- if ((pChan->state() & HELD_STATE_IND_MASK) == INNER_STATE_HELD)
- {
- chanState = (pChan->state() & HELD_STATE_IND_MASK);
- }
- std::string lineType = "未知";
- if (devType == DEV_RES_TYPE_EXT)
- {
- lineType = std::string("内线");
- }
- else if (devType == DEV_RES_TYPE_TRUNK) {
- lineType = std::string("外线");
- }
- std::string state;
- switch (chanState)
- {
- case CHAN_LOGIC_STATE_DISABLED: // 不可用
- state = "不可用";
- break;
- case CHAN_LOGIC_STATE_FREE: // 空闲
- state = "空闲";
- break;
- case CHAN_LOGIC_STATE_INIT: // 摘机等待拨号
- state = "摘机等待拨号";
- break;
- case CHAN_LOGIC_STATE_DIALING: // 拨号
- state = "拨号";
- break;
- case CHAN_LOGIC_STATE_RING_BACK: // 呼出振铃
- state = "呼出振铃";
- break;
- case CHAN_LOGIC_STATE_ALERTING: // 来电振铃
- state = "来电振铃";
- break;
- case CHAN_LOGIC_STATE_TALKING: // 通话中
- state = "通话中";
- break;
- case CHAN_LOGIC_STATE_STANDBY: // 中继通道可响应APP(此状态仅用作DevLink侧,不可传至CTI)
- break;
- case CHAN_LOGIC_STATE_HELD: // 通话保持中(保持状态的使用须与原通道状态按位或和与)
- state = "通话保持中";
- break;
- }
- Format fmt("lineType = %s[%s],lineState = %s,chanState = %d");
- fmt % lineType % chanNo % state % chanState;
- LOG_DEBUG_S(fmt.str());
- if (devType == DEV_RES_TYPE_EXT) {
- // 更新督办分机状态
- if (m_DepttelOffice.UpdateDepttelext(pChan->no(), chanState)) {
- return;
- }
- }
-
- if (devType == DEV_RES_TYPE_EXT) {
- map<std::string, Agent*>::iterator it;
- std::unique_lock<std::mutex>lock(m_AgentLock);
- it = m_MapAgent.find(chanNo);
- if (it != m_MapAgent.end()) {
- if (it->second == nullptr) {
- LOG_WARN("查到分机[%s]对应坐席空", chanNo.c_str());
- return;
- }
- if (chanState == CHAN_LOGIC_STATE_FREE) {
-
- if (it->second->isRepose()) {
- it->second->setState(AGENT_STATE_REPOSE);
- state = it->second->state_s();
- }
- else {
- if (it->second->state() == AGENT_STATE_BUSY) {
- __setStatePostProcessing(it->second->id()); // 在fs中设置坐席话后处理
- it->second->setState(AGENT_STATE_POST_PROCESSING);
- state = it->second->state_s();
- }
- else {
- it->second->setState(AGENT_STATE_FREE);
- }
- }
- // 发送挂机事件
- std::string data = creatJson("DropCall", "挂机");
- m_Server.sendMsg(it->second->hdl(), data);
- }
- else if (chanState == CHAN_LOGIC_STATE_TALKING) {
- it->second->setState(AGENT_STATE_BUSY);
- }
- else if (chanState == CHAN_LOGIC_STATE_RING_BACK || chanState == CHAN_LOGIC_STATE_ALERTING) {
- if (AGENT_STATE_REQUESTED == it->second->state()) { // 当前状态已通知过,不在重复通知
- return;
- }
- it->second->setState(AGENT_STATE_REQUESTED);
- }
- if (!state.empty()) { // 过滤掉保持状态
- std::string data = creatJson("LineState", state);
- m_Server.sendMsg(it->second->hdl(), data);
- data = creatJson("Monitor", state, it->second->id(), chanNo);
- m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
- }
- __agentDetail(lock.release()); // 通知坐席详情变化
- }
- else {
- LOG_WARN("线路状态通知失败,找不到分机[%s]对应的坐席", chanNo.c_str());
- }
- }
- else if (devType == DEV_RES_TYPE_TRUNK) {
- if (CHAN_LOGIC_STATE_ALERTING == chanState) {
- __agentDetailTrunkChange(1); // 计算呼入数量
- }
- else if (CHAN_LOGIC_STATE_FREE == chanState) {
- __agentDetailTrunkChange(-1);
- }
- }
- }
- void CFsProxy::onChanPoor(Session * pSession, PCHAN_EVENT_NOTIFY pNotify)
- {
- LOG_INFO_S(boost::str(Format("空闲通道枯竭,主动挂断,ChanId = %s ,caller = %s") % pNotify->ChanId % pNotify->Caller).c_str());
- __kill(FS_LINK_JOBID_INVALID, pNotify->ChanId);
- }
- void CFsProxy::onCallcenterQueue(const std::string & strQueueName, const std::string & strQueueCount)
- {
- std::unique_lock<std::shared_mutex>lock(m_mutQueryCount);
- m_QueryCount[strQueueName] = strQueueCount;
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- root["Type"] = "WaitCount";
- root["Result"] = true;
- std::int32_t nTotal = 0;
- for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
- root[it->first] = it->second;
- try {
- auto n = stoi(it->second);
- nTotal += n;
- }
- catch (const std::exception& e) {
- LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
- }
- }
- root["Total"] = nTotal;
- auto strJson = Json::writeString(jsrocd, root);
- // 发送给需要的坐席
- for (auto it = m_subWaitCount.begin(); it != m_subWaitCount.end();) {
- auto bOk = m_Server.sendMsg(*it, strJson);
- if (bOk) {
- ++it;
- }
- else {
- it = m_subWaitCount.erase(it);
- }
- }
- }
- void CFsProxy::onEslDisconnect(void)
- {
- // esl 断开重连
- m_Gateway.stop();
- __freeSession();
- __freeExten();
- __freeTrunkChan();
- LOG_WARN_S("ESL断开重新初始化...");
- while (!__init())
- {
- LOG_WARN_S("ESL重连中...")
- this_thread::sleep_for(std::chrono::seconds(5));
- }
- LOG_INFO_S("ESL重新初始化成功...");
- }
- void CFsProxy::onEslExtenReg(uint32_t ExtenNo, string ExtenIp)
- {
- ChanExten* pExten = getExten(ExtenNo);
- if (pExten == nullptr)
- {
- __addExten(ExtenNo);
- }
- else
- {
- if (pExten->isVoid()) // 若被丢弃,则改变丢弃状态
- pExten->discard(false);
- }
- }
- void CFsProxy::onEslExtenUnreg(uint32_t ExtenNo)
- {
- std::string agentId;
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(std::to_string(ExtenNo));
- if (it != m_MapAgent.end())
- {
- agentId = it->second->id();
- it->second->removeAgent("分机掉线");
- //m_Gateway.delAgent(it->second->id()); // callcenter模块中删除
- m_Gateway.delAgent(it->second->id(), it->second->groups());
- m_Server.sendMsg(it->second->hdl(), creatJson("Logout", "分机掉线"));
- m_MapAgent.erase(it); // 从签入列表中删除
- }
- }
- if (!agentId.empty())
- {
- std::string data = creatJson("Monitor", "签出", agentId, std::to_string(ExtenNo));
- m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
- }
- __delExten(ExtenNo);
- }
- void CFsProxy::onEslEvtBgJobDone(PBG_JOB_NOTIFY pNotify)
- {
- COperationReactor::GetInstance()->onEslEvtBgJobDone(pNotify);
- }
- void CFsProxy::onEslEvtChannel(PCHAN_EVENT_NOTIFY pNotify)
- {
- Session* pSession = __getSession(pNotify);
- if (pSession == nullptr)
- {
- Format fmt("{FsProxy}: 通道事件,没有找到会话,EslEventId=%d,ChanId[%s],CallId[%s],Caller[%s],Callee[%s],CcId[%s]");
- fmt % pNotify->EventId % pNotify->ChanId % pNotify->CallId % pNotify->Caller % pNotify->Callee % pNotify->CcId;
- LOG_WARN_S(fmt.str());
- return;
- }
- pSession->onChanEvent(pNotify);
- if (pSession->isVoid())
- __delSession(pSession->id());
- }
- void CFsProxy::onEslEvtDtmf(PDTMF_NOTIFY pNotify)
- {
- }
- void CFsProxy::onEslEvtHold(PHOLD_NOTIFY pNotify)
- {
- if (pNotify == nullptr) return;
- Session* pSession = __getSession(pNotify->CallId);
- if (pSession != nullptr)
- pSession->onChanHold(pNotify);
- }
- bool CFsProxy::send2Agent(std::string ExtenNo, std::string Data)
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(ExtenNo);
- if (it != m_MapAgent.end())
- {
- if (m_Server.sendMsg(it->second->hdl(), Data))
- {
- LOG_DEBUG("成功发送到坐席[%s]-分机[%s],msg[%s]", it->second->id().c_str(), ExtenNo.c_str(), Data.c_str());
- return true;
- }
- }
- Format fmt("坐席通知失败,找不到分机对应的坐席,分机[%s],msg[%s]");
- fmt % ExtenNo % Data;
- LOG_WARN_S(fmt.str());
- return false;
- }
- ChanExten * CFsProxy::getExten(uint32_t ExtenNo)
- {
- auto it = m_MapChanExt.find(ExtenNo);
- if (it != m_MapChanExt.end()) return it->second;
- return nullptr;
- }
- ChanTrunk * CFsProxy::getTrunk(uint32_t TrunkNo)
- {
- if (TrunkNo <= 0 || TrunkNo > m_ArrayTrunk.size())
- return nullptr;
- return m_ArrayTrunk[--TrunkNo];
- }
- void CFsProxy::delChan(VirtualChan * pChan)
- {
- if (pChan->type() == DEV_RES_TYPE_EXT)
- __delExten(pChan->no());
- }
- ChanTrunk * CFsProxy::getFreeTrunk(void)
- {
- for (size_t i = 0; i < m_ArrayTrunk.size(); ++i)
- {
- ChanTrunk* pTrunk = m_ArrayTrunk[i];
- if (pTrunk != nullptr)
- {
- if (pTrunk->isFree())
- {
- return pTrunk;
- }
- }
- }
- LOG_INFO_S("没有空闲中继通道");
- return nullptr;
- }
- VirtualChan * CFsProxy::getAssoChanInSession(VirtualChan * pChan)
- {
- Session* pSession = __getSession(pChan->sessionId());
- if (pSession != nullptr)
- return pSession->getAssoChan(pChan);
- else
- return nullptr;
- }
- VirtualChan * CFsProxy::getBusyChan(string ChanId)
- {
- auto it = m_MapBusyChan.find(ChanId);
- if (it != m_MapBusyChan.end())
- return it->second;
- return nullptr;
- }
- void CFsProxy::regBusyChan(VirtualChan * pChan)
- {
- m_MapBusyChan[pChan->chanId()] = pChan;
- }
- void CFsProxy::unregBusyChan(VirtualChan * pChan)
- {
- m_MapBusyChan.erase(pChan->chanId());
- }
- std::string CFsProxy::getAgentByExten(std::string ExtenNo)
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(ExtenNo);
- if (it->second == nullptr) return "";
- return it->second->id();
- }
- std::string CFsProxy::getAgentByExten(std::string ExtenNo, std::string &Groups)
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(ExtenNo);
- if (it->second == nullptr) return "";
- Groups = it->second->group();
- return it->second->id();
- }
- std::string CFsProxy::getExtenByAgent(std::string AgentId)
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.begin();
- while (it != m_MapAgent.end())
- {
- if (it->second->id() == AgentId)
- return it->first;
- ++it;
- }
- return "";
- }
- bool CFsProxy::ExtenCall(long JobId, VirtualChan * pChan, std::string CallerNum, std::string CalleeNum)
- {
- /*Format fmt("bgapi originate {origination_caller_id_number=%lu }user/%lu %s XML %s\r\n%s: %ld");
- fmt % pChan->no() % pChan->no() % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;*/
- std::string called;
- if (getExten(atoi(CalleeNum.c_str())) == nullptr)
- {
- std::string prefix = CConfig::GetInstance()->gateWayPrefix();
- called = CalleeNum.substr(prefix.length(), CalleeNum.length() - prefix.length());
- }
- else
- {
- called = CalleeNum;
- }
- Format fmt("bgapi originate {origination_caller_id_number=%lu,%s=%s}user/%lu %s XML %s\r\n%s: %ld");
- fmt % pChan->no() % ESL_VAR_MALL_CALL_CALLEE %called % pChan->no() % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
- return m_Gateway.sendCmd(fmt.str());
- }
- bool CFsProxy::PredictionCall(long JobId, std::string CallerNum, std::string CalleeNum)
- {
- std::string CallString;
- if (!m_CallStringMaker.makeCallString(CallerNum, CalleeNum, CallString))
- return false;
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld")
- % CallerNum % ESL_VAR_OP_INSTANCE % JobId % CallString % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::AutoCall(long JobId, std::string CallerNum, std::string CalleeNum, int OpType, long TaskId)
- {
- std::string CallString;
- std::string Caller;
- if (!m_CallStringMaker.makeCallString(Caller, CallerNum, CallString))
- return false;
- std::string EslCmd;
- /*EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld,%s=%ld}%s %s XML %s\r\n%s: %ld")
- % CallerNum % ESL_VAR_OP_TYPE % OpType % ESL_VAR_TASK_ID % TaskId % CallString % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId);*/
- std::string called;
- if (getExten(atoi(CalleeNum.c_str())) == nullptr)
- {
- std::string prefix = CConfig::GetInstance()->gateWayPrefix();
- if (CalleeNum.substr(0, prefix.length()) == prefix)
- called = CalleeNum.substr(prefix.length(), CalleeNum.length() - prefix.length());
- }
- else
- {
- called = CalleeNum;
- }
- EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld,%s=%ld,,%s=%s}%s %s XML %s\r\n%s: %ld")
- % CallerNum % ESL_VAR_OP_TYPE % OpType % ESL_VAR_TASK_ID % TaskId % ESL_VAR_MALL_CALL_CALLEE %called % CallString % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::kill(long JobId, VirtualChan * pChan)
- {
- return __kill(JobId, pChan->chanId());
- }
- bool CFsProxy::kill(long JobId, std::string ChanId)
- {
- return __kill(JobId, ChanId);
- }
- /*****************************************************************
- **【函数名称】 consult
- **【函数功能】 协商呼叫
- **【参数】
- **【返回值】
- ****************************************************************/
- bool CFsProxy::consult(long JobId, VirtualChan * pChan, std::string DestNum)
- {
- std::string CallerNum;
- std::string CallString;
- if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
- return false;
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi uuid_broadcast %s att_xfer::%s\r\n%s: %ld") % pChan->chanId() % CallString % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- /*****************************************************************
- **【函数名称】 insert
- **【函数功能】 强插
- **【参数】
- **【返回值】
- ****************************************************************/
- bool CFsProxy::insert(long JobId, VirtualChan * pChan, std::string DestSessionId)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &three_way(%s)\r\n%s: %ld") %
- pChan->no() % pChan->no() % DestSessionId% ESL_HEADER_JOB_UUID% JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- /*****************************************************************
- **【函数名称】 intercept
- **【函数功能】 强截
- **【参数】
- **【返回值】
- ****************************************************************/
- bool CFsProxy::intercept(long JobId, VirtualChan * pChan, std::string DestChanId)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &intercept(%s)\r\n%s: %ld") %
- pChan->no() % pChan->no() % DestChanId % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- /*****************************************************************
- **【函数名称】 listen
- **【函数功能】 监听
- **【参数】
- **【返回值】
- ****************************************************************/
- bool CFsProxy::listen(long JobId, VirtualChan * pChan, std::string DestChanId)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &eavesdrop(%s)\r\n%s: %ld") %
- pChan->no() % pChan->no() % DestChanId% ESL_HEADER_JOB_UUID% JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::playAgentNo(VirtualChan * pChan)
- {
- std::string filePath;
- std::string welcomeWords;
- /*Format fmt("您好, %s 号话务员为您服务.");
- fmt % pChan->no();
- if (!tts.TextToAudio(fmt.str(), filePath))*/
- std::string agent = getAgentByExten(std::to_string(pChan->no()));
- if (agent.empty())
- welcomeWords = (boost::str(Format("您好, %s 号话机为您服务.") % pChan->no()));
- else
- welcomeWords = (boost::str(Format("您好, %s 号话务员为您服务.") % __num2text(agent)));
- if (!tts.TextToAudio(welcomeWords, filePath))
- {
- LOG_WARN("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败", pChan->no());
- return false;
- }
- //std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 暂停50ms,防止太快导致分机放音失败
- //Format EslCmd("bgapi uuid_broadcast %s %s both");
- Format EslCmd("bgapi sched_broadcast +1 %s playback::%s both");
- EslCmd % pChan->chanId() % filePath;
- return m_Gateway.sendCmd(EslCmd.str());
- }
- // 播报坐席工号
- bool CFsProxy::playAgentNo(long JobId, VirtualChan * pChan)
- {
- std::string filePath;
- Format fmt("%ld 话务员为您服务");
- fmt % pChan->no();
- if (!tts.TextToAudio(fmt.str(), filePath))
- {
- LOG_WARN("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败", pChan->no());
- return false;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 暂停50ms,防止太快导致分机放音失败
- Format EslCmd("bgapi uuid_broadcast %s %s both\r\n%s: %ld");
- EslCmd % pChan->chanId() % filePath % ESL_HEADER_JOB_UUID % JobId;
- return m_Gateway.sendCmd(EslCmd.str());
- }
- bool CFsProxy::meeting(long JobId, std::string CallerNum, std::string DestNum, std::string MeetingId)
- {
- std::string CallString;
- if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
- return false;
- //getGwIP()
- LOG_INFO(("{FsProxy}: 三方会议呼叫字符串: %s"), CallString.c_str());
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld")
- % CallerNum % ESL_VAR_OP_INSTANCE % JobId % CallString % MeetingId % CConfig::GetInstance()->meetingContext() % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::muteOn(long JobId, VirtualChan * pChan)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi uuid_audio %s start write mute -4\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::muteOff(long JobId, VirtualChan * pChan)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi uuid_audio %s stop write mute -4\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::holdon(long JobId, VirtualChan * pChan)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi uuid_hold %s\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::takeBack(long JobId, VirtualChan * pChan)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi uuid_hold off %s\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::record(long JobId, VirtualChan * pChan, std::string RcdFile)
- {
- std::string EslCmd;
- EslCmd = boost::str(Format("bgapi uuid_record %s start %s\r\n%s: %ld") % pChan->chanId() % RcdFile % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::turnIvr(long JobId, VirtualChan * pChan)
- {
- Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
- EslCmd % pChan->chanId() % "turnmyd" % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
- return m_Gateway.sendCmd(EslCmd.str());
- //m_Gateway.Execte("ivr", "myd", pChan->chanId().c_str());
- }
- bool CFsProxy::transfer(long JobId, VirtualChan * pChan, std::string DestNum)
- {
- /*Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
- EslCmd % pChan->chanId() % DestNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
- return m_Gateway.sendCmd(EslCmd.str());*/
- //{%s = %ld} % ESL_VAR_OP_INSTANCE % JobId
- std::string Param;
- Param = boost::str(Format("{%s=%ld}") % ESL_VAR_OP_INSTANCE % JobId);
- m_Gateway.Execte(ESL_APP_SET, Param.c_str(), pChan->chanId().c_str());
- Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
- EslCmd % pChan->chanId() % DestNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
- return m_Gateway.sendCmd(EslCmd.str());
- }
- bool CFsProxy::transfer2Context(long JobId, std::string DestChanId, std::string Exten, std::string Context, bool BothSide)
- {
- std::string EslCmd;
- if (BothSide)
- EslCmd = boost::str(Format("bgapi uuid_transfer %s -both %s xml %s\r\n%s: %ld") % DestChanId % Exten % Context % ESL_HEADER_JOB_UUID % JobId);
- else
- EslCmd = boost::str(Format("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld") % DestChanId % Exten % Context % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- bool CFsProxy::setChannelVariable(long JobId, std::string DestChanId, std::string VarName, std::string Content)
- {
- std::string EslCmd;
- Format fmt("bgapi uuid_setvar %s %s %s\r\n%s: %ld");
- fmt % DestChanId % VarName % Content % ESL_HEADER_JOB_UUID % JobId;
- EslCmd = fmt.str();
- bool ret = m_Gateway.sendCmd(EslCmd);
- return ret;
- }
- bool CFsProxy::ExecteApp(std::string App, std::string Param, std::string ChanId)
- {
- return m_Gateway.Execte(App.c_str(), Param.c_str(), ChanId.c_str());
- }
- bool CFsProxy::sendDtmf(long JobId, std::string DestChanId, std::string Content)
- {
- std::string EslCmd;
- Format fmt("bgapi uuid_send_dtmf %s %s\r\n%s: %ld");
- fmt %DestChanId %Content %ESL_HEADER_JOB_UUID % JobId;
- EslCmd = fmt.str();
- bool ret = m_Gateway.sendCmd(EslCmd);
- return ret;
- }
- bool CFsProxy::getConferenceMemberId(std::string MeetingId, std::string ChanId, std::string & MemberId)
- {
- return m_Gateway.getConferenceMemberId(MeetingId, ChanId, MemberId);
- }
- bool CFsProxy::setConferenceExitSound(std::string MeetingId, bool isPlay)
- {
- std::string EslCmd;
- Format fmt("bgapi conference %s exit_sound %s");
- if (isPlay) {
- fmt %MeetingId %"on";
- }
- else {
- fmt %MeetingId %"off";
- }
- EslCmd = fmt.str();
- LOG_INFO("{CFsProxy}: 设置会议人员离开是否播放声音[%s]", EslCmd.c_str());
- bool ret = m_Gateway.sendCmd(EslCmd);
- return ret;
- }
- bool CFsProxy::setConferenceIsMute(long JobId, std::string MeetingId, std::string MemberId, bool isMute)
- {
- std::string EslCmd;
- if (isMute)
- {
- EslCmd = (Format("bgapi conference %s deaf %s") % MeetingId % MemberId).str();
- m_Gateway.sendCmd(EslCmd);
- EslCmd = (Format("bgapi conference %s mute %s\r\n%s: %ld") % MeetingId % MemberId % ESL_HEADER_JOB_UUID % JobId).str();
- }
- else
- {
- EslCmd = (Format("bgapi conference %s undeaf %s") % MeetingId % MemberId).str();
- m_Gateway.sendCmd(EslCmd);
- EslCmd = (Format("bgapi conference %s stop all %s") % MeetingId % MemberId).str();
- m_Gateway.sendCmd(EslCmd);
- EslCmd = (Format("bgapi conference %s unmute %s\r\n%s: %ld") % MeetingId % MemberId % ESL_HEADER_JOB_UUID % JobId).str();
- }
- LOG_ERROR("{CFsProxy}: 设置会议人员是否静音[%s]", EslCmd.c_str());
- bool ret = m_Gateway.sendCmd(EslCmd);
- return ret;
- }
- std::string CFsProxy::getGwIP(const std::string & SipAccount)
- {
- std::string cmd = "api sofia_contact " + SipAccount;
- auto strGwIP = m_Gateway.getCmdResult(cmd);
- if (!strGwIP.empty())
- {
- LOG_INFO("网关信息: %s",strGwIP.c_str());
- size_t Index = strGwIP.find_first_of('@');
- if (Index != std::string::npos)
- {
- /*strGwIp = strGwIp.substr(Index);
- CallString = "sofia/internal/";
- CallString += DestNum;
- CallString += strGwIp.c_str();*/
- return strGwIP.substr(Index);
- }
- }
- return "";
- }
- std::string CFsProxy::creatJson(std::string type, std::string result)
- {
- // Json::Value root;
- // Json::StreamWriterBuilder jsrocd;
- // root["Type"] = type;
- // root["Result"] = result;
- // return Json::writeString(jsrocd, root);
- // jsoncpp中文乱码
- Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\" }");
- fmt %type %result;
- return fmt.str();
- }
- std::string CFsProxy::creatJson(std::string type, bool result)
- {
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- std::unique_ptr<Json::StreamWriter> write(jsrocd.newStreamWriter());
- Json::OStringStream os;
- root["Type"] = type;
- root["Result"] = result;
- write->write(root, &os);
- return os.str();
- }
- std::string CFsProxy::creatJson(std::string type, bool result, std::string desc)
- {
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- std::unique_ptr<Json::StreamWriter> write(jsrocd.newStreamWriter());
- Json::OStringStream os;
- root["Type"] = type;
- root["Result"] = result;
- root["Desc"] = desc;
- write->write(root, &os);
- return os.str();
- }
- std::string CFsProxy::creatJson(std::string type, std::string result, std::string agentId)
- {
- Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\",\"AgentID\":\"%s\" }");
- fmt %type %result %agentId;
- return fmt.str();
- }
- std::string CFsProxy::creatJson(std::string type, std::string result, std::string agentId, std::string extenNo)
- {
- Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\",\"AgentID\":\"%s\",\"ExtenNo\":\"%s\" }");
- fmt %type %result %agentId %extenNo;
- return fmt.str();
- }
- std::string CFsProxy::creatJsonIncoming(std::string callid, std::string caller, std::string callee, std::string trunkNumber, int incomintType)
- {
- if (incomintType == 1) { // 视频通话,来电弹屏将来电分机号替换为相应手机号
- auto it = m_ExtPhone.find(atoi(caller.c_str()));
- if (it != m_ExtPhone.end()) {
- caller = it->second;
- }
- }
-
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- root["Type"] = "Incoming";
- root["Result"] = true;
- root["CallID"] = callid; // callid
- root["Number"] = caller; // 主叫
- root["CalleeNumber"] = callee;
- root["TrunkNumber"] = trunkNumber; // 中继号
- root["IncomintType"] = incomintType; // 来电类型,来电,外呼,自动外呼
- return Json::writeString(jsrocd, root);
- }
- std::string CFsProxy::creatJsonRecord(std::string callid, std::string recordPath)
- {
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- root["Type"] = "RecordPath";
- root["Result"] = true;
- root["CallID"] = callid; // callid
- root["RecPath"] = recordPath;
- return Json::writeString(jsrocd, root);
- }
- std::string CFsProxy::creatJson2(std::string type, bool result)
- {
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- Json::Value person;
- person["Type"] = type;
- person["Result"] = result;
- root.append(person);
- return Json::writeString(jsrocd, root);
- }
- std::string CFsProxy::creatJson2(std::string type, bool result, int extenID)
- {
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- Json::Value person;
- person["Type"] = type;
- person["Result"] = result;
- person["AgentExten"] = extenID;
- root.append(person);
- return Json::writeString(jsrocd, root);
- }
- std::string CFsProxy::creatJson2(std::string type, bool result, int extenID, std::string pwd)
- {
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- Json::Value person;
- person["Type"] = type;
- person["Result"] = result;
- person["AgentExten"] = extenID;
- person["ExtenPwd"] = pwd;
- root.append(person);
- return Json::writeString(jsrocd, root);
- }
- bool CFsProxy::__init()
- {
- __initTrunkChan();
- if (m_Gateway.start())
- {
- m_Gateway.hangupAll();
- static bool del = m_Gateway.delAgentAll(); // 使用static 保证delagentall运行后在fs连接断开后重连成功后不会再一次执行
- boost::ignore_unused(del); // 取消del不使用的警告
- if (m_Gateway.scanExten())
- {
- LOG_INFO_S("{FsProxy}: FreeSWITCH代理初始化成功");
- return true;
- }
- else
- {
- LOG_ERROR_S("{FsProxy}: FreeSWITCH代理初始化失败, 扫描分机资源失败");
- return false;
- }
- }
- else
- {
- LOG_ERROR_S("{FsProxy}: FreeSWITCH代理初始化失败, ESL网关启动失败");
- return false;
- }
- }
- uint32_t CFsProxy::__transLogicState2CtiState(DEV_RES_TYPE ChanType, CHAN_LOGIC_STATE State)
- {
- if (ChanType == DEV_RES_TYPE_EXT)
- {
- uint32_t HoldMask = State & HELD_STATE_IND_MASK;
- switch (State & HELD_STATE_FILTER_MASK)
- {
- case CHAN_LOGIC_STATE_DISABLED: return INNER_STATE_DISABLED; // 不可用
- case CHAN_LOGIC_STATE_FREE: return INNER_STATE_FREE | HoldMask; // 空闲
- case CHAN_LOGIC_STATE_INIT: return INNER_STATE_INIT | HoldMask; // 摘机等待拨号
- case CHAN_LOGIC_STATE_DIALING: return INNER_STATE_DIALING | HoldMask; // 拨号
- case CHAN_LOGIC_STATE_RING_BACK: return INNER_STATE_RING_BACK | HoldMask; // 呼出振铃
- case CHAN_LOGIC_STATE_ALERTING: return INNER_STATE_ALERTING | HoldMask; // 来电振铃
- case CHAN_LOGIC_STATE_TALKING: return INNER_STATE_TALKING | HoldMask; // 通话中
- default:
- return INNER_STATE_DISABLED;
- }
- }
- else
- {
- switch (State)
- {
- case CHAN_LOGIC_STATE_DISABLED: return TRUNK_STATE_DISABLED; // 不可用
- case CHAN_LOGIC_STATE_FREE: return TRUNK_STATE_FREE; // 空闲
- case CHAN_LOGIC_STATE_DIALING: return TRUNK_STATE_DIALING; // 拨号
- case CHAN_LOGIC_STATE_RING_BACK: return TRUNK_STATE_RING_BACK; // 呼出振铃
- case CHAN_LOGIC_STATE_ALERTING: return TRUNK_STATE_ALERTING; // 来电振铃
- case CHAN_LOGIC_STATE_STANDBY:
- case CHAN_LOGIC_STATE_TALKING: return TRUNK_STATE_TALKING; // 通话中
- default:
- return TRUNK_STATE_UNKNOWN;
- }
- }
- }
- void CFsProxy::__addExten(uint32_t ExtenNo)
- {
- ChanExten* pExten = new ChanExten(this, ExtenNo);
- m_MapChanExt[ExtenNo] = pExten;
- pExten->regist();
- }
- void CFsProxy::__delExten(uint32_t ExtenNo)
- {
- auto it = m_MapChanExt.find(ExtenNo);
- if (it != m_MapChanExt.end())
- {
- if (it->second->isFree())
- {
- FS_LINK_DELETE(it->second);
- m_MapChanExt.erase(it);
- }
- else
- {
- it->second->discard(true);
- }
- }
- }
- void CFsProxy::__freeExten(void)
- {
- auto it = m_MapChanExt.begin();
- while (it != m_MapChanExt.end())
- {
- FS_LINK_DELETE(it->second);
- ++it;
- }
- m_MapChanExt.clear();
- }
- void CFsProxy::__initTrunkChan(void)
- {
- /*for (int i = 1; i <= CConfig::GetInstance()->trunkCount(); ++i)*/
- for (int i = 1; i <= SoftAuth::GetInstance()->trunkNum(); ++i)
- {
- ChanTrunk* pTrunk = new ChanTrunk(this, i);
- m_ArrayTrunk.emplace_back(pTrunk);
- pTrunk->regist();
- }
- }
- void CFsProxy::__freeTrunkChan(void)
- {
- ChanTrunk* pTrunk = nullptr;
- for (size_t i = 0; i < m_ArrayTrunk.size(); ++i)
- {
- pTrunk = m_ArrayTrunk[i];
- FS_LINK_DELETE(pTrunk);
- }
- m_ArrayTrunk.clear();
- }
- Session * CFsProxy::__getSessionByChanId(const string & ChanId)
- {
- auto it = m_MapSession.begin();
- while (it != m_MapSession.end())
- {
- if (it->second->isContainChanId(ChanId))
- return it->second;
- ++it;
- }
- return nullptr;
- }
- Session * CFsProxy::__getSession(string SessionId)
- {
- auto it = m_MapSession.find(SessionId);
- if (it != m_MapSession.end())
- return it->second;
- return nullptr;
- }
- Session * CFsProxy::__getSession(PCHAN_EVENT_NOTIFY pNotify)
- {
- Session* pSession = nullptr;
- pSession = __getSession(pNotify->CallId);
- if (pSession == nullptr)
- {
- if (!pNotify->CcId.empty())
- {
- LOG_INFO(boost::str(Format("CallId查不到,CcId不为空,使用CcId代替CallId进一步查找Session,CallId[%s],CcId[%s]") % pNotify->CallId % pNotify->CcId).c_str());
- pSession = __getSession(pNotify->CcId);
- if (pSession != nullptr) return pSession;
- LOG_WARN(boost::str(Format("CallId,CcId都查不到session,CallId[%s],CcId[%s]") % pNotify->CallId % pNotify->CcId).c_str());
- }
- LOG_INFO(boost::str(Format("CallId查不到,使用ChanId代替CallId进一步查找Session,CallId[%s],ChanId[%s]") % pNotify->CallId % pNotify->ChanId).c_str());
- pSession = __getSessionByChanId(pNotify->ChanId);
- if (pSession != nullptr) return pSession;
- LOG_WARN(boost::str(Format("CallId,ChanId都查不到session,CallId[%s],ChanId[%s]") % pNotify->CallId % pNotify->ChanId).c_str());
- pSession = new Session(this, pNotify->CallId);
- pSession->prepare(pNotify);
- m_MapSession[pNotify->CallId] = pSession;
- Format fmt("{CFsProxy}:Add Session[%s],ChanId[%s],EventId[%d]");
- fmt % pSession->id() % pNotify->ChanId % pNotify->EventId;
- LOG_INFO_S(fmt.str());
- }
- return pSession;
- }
- void CFsProxy::__delSession(std::string SessionId)
- {
- if (SessionId.empty())
- return;
- auto it = m_MapSession.find(SessionId);
- if (it != m_MapSession.end())
- {
- Format fmt("{FsProxy}: Delete Session[%s]");
- fmt % it->second->id();
- LOG_INFO_S(fmt.str());
- FS_LINK_DELETE(it->second);
- m_MapSession.erase(it);
- }
- }
- void CFsProxy::__freeSession(void)
- {
- auto it = m_MapSession.begin();
- while (it != m_MapSession.end())
- {
- FS_LINK_DELETE(it->second);
- ++it;
- }
- m_MapSession.clear();
- }
- bool CFsProxy::__kill(long JobId, string ChanId)
- {
- string EslCmd;
- if (JobId == FS_LINK_JOBID_INVALID)
- EslCmd = boost::str(Format("bgapi uuid_kill %s") % ChanId);
- else
- EslCmd = boost::str(Format("bgapi uuid_kill %s\r\n%s: %ld") % ChanId % ESL_HEADER_JOB_UUID % JobId);
- return m_Gateway.sendCmd(EslCmd);
- }
- std::string CFsProxy::__getExtenByAgent(std::string agentId)
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.begin();
- while (it != m_MapAgent.end())
- {
- if (it->second->id() == agentId)
- {
- return it->first;
- }
- ++it;
- }
- return "";
- }
- bool CFsProxy::__login(std::string AgentID, std::string ExtenNo)
- {
- bool ret = true;
- std::string cmd;
- cmd = boost::str(Format("bgapi callcenter_config agent add %1% Callback") % AgentID); // 添加座席
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set contact %1% [call_timeout=30]user/%2%") % AgentID % ExtenNo); // 设置呼叫字符串
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available ") % AgentID); // 座席登录后默认空闲
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set state %1% Waiting") % AgentID); // 座席登录后默认空闲
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set max_no_answer %1% 0") % AgentID); // 0禁用
- ret &= m_Gateway.sendCmd(cmd);
- //cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% 20") % AgentID); // 话后处理时长
- cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% %2%") % AgentID % CConfig::GetInstance()->postProcessTime()); // 话后处理时长
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set reject_delay_time %1% 0") % AgentID);
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi allcenter_config agent set busy_delay_time %1% 0") % AgentID);
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config tier add support@default %1% 1 1") % AgentID); // 添加梯队 到队列等价于坐席组
- ret &= m_Gateway.sendCmd(cmd);
- return ret;
- }
- bool CFsProxy::__login(std::string AgentID, std::string ExtenNo, std::list<std::string> Group)
- {
- bool ret = true;
- std::string cmd;
- cmd = boost::str(Format("bgapi callcenter_config agent add %1% Callback") % AgentID); // 添加座席
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set contact %1% [call_timeout=30]user/%2%") % AgentID % ExtenNo); // 设置呼叫字符串
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available ") % AgentID); // 座席登录后默认空闲
- //cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available (On Demand)") % AgentID); // 座席登录后默认空闲
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set state %1% Waiting") % AgentID); // 座席登录后默认空闲
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set max_no_answer %1% 0") % AgentID); // 0禁用
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% 2") % AgentID); // 话后处理时长,2秒
- //cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% %2%") % AgentID %CConfig::GetInstance()->postProcessTime());
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config agent set reject_delay_time %1% 0") % AgentID);
- ret &= m_Gateway.sendCmd(cmd);
- cmd = boost::str(Format("bgapi allcenter_config agent set busy_delay_time %1% 0") % AgentID);
- ret &= m_Gateway.sendCmd(cmd);
- for (std::string &var : Group)
- {
- cmd = boost::str(Format("bgapi callcenter_config tier add %2% %1% 1 1") % AgentID %var); // 添加梯队 到队列等价于坐席组
- ret &= m_Gateway.sendCmd(cmd);
- }
- return ret;
- }
- bool CFsProxy::__logout(std::string AgentID, std::string AgentExten)
- {
- bool ret = true;
- std::string cmd;
- cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
- ret &= m_Gateway.sendCmd(cmd);
- /*cmd = boost::str(Format("bgapi callcenter_config tier del support@default %1% ") % AgentID);
- ret &= m_Gateway.sendCmd(cmd);*/
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(AgentExten);
- if (ret && it != m_MapAgent.end())
- {
- for (std::string &var : it->second->groups())
- {
- cmd = boost::str(Format("bgapi callcenter_config tier del %2% %1% ") % AgentID %var);
- ret &= m_Gateway.sendCmd(cmd);
- }
- it->second->removeAgent(AgentID);
- FS_LINK_DELETE(it->second);
- m_MapAgent.erase(it);
- }
- return ret;
- }
- bool CFsProxy::__freeAgent()
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.begin();
- while (it != m_MapAgent.end())
- {
- // __logout(it->second->id());
- //FS_LINK_DELETE(it->second);
- Agent* agent = it->second;
- FS_LINK_DELETE(agent);
- m_MapAgent.erase(it++);
- }
- return false;
- }
- bool CFsProxy::__setState(std::string AgentID, bool isFree)
- {
- Format fmt("bgapi callcenter_config agent set status %1% '%2%'");
- if (isFree)
- fmt %AgentID %"Available"; // 置闲
- //fmt %AgentID %"Available (On Demand)"; // 置闲
- else
- fmt %AgentID %"On Break"; // 置忙
- //if (isFree)
- //{
- // Format fmt("bgapi callcenter_config agent set state %1% '%2%'");
- // fmt %AgentID %"Waiting"; // 置闲时改为可进电话
- //}
- return m_Gateway.sendCmd(fmt.str());
- }
- bool CFsProxy::__setStatePostProcessing(std::string AgentID)
- {
- Format fmt("bgapi callcenter_config agent set status %1% '%2%'");
- //fmt %AgentID %"Available (On Demand)"; // 空闲不接电话/话后处理
- fmt %AgentID %"On Break";
- return m_Gateway.sendCmd(fmt.str());
- }
- //std::string text[] = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
- std::string text[] = { "零","一","二","三","四","五","六","七","八","九" };
- std::string CFsProxy::__num2text(std::string data)
- {
- std::string word;
- try
- {
- for (std::size_t i = 0; i < data.length(); i++)
- {
- switch (data.at(i))
- {
- case '0':
- word += text[0];
- break;
- case '1':
- word += text[1];
- break;
- case '2':
- word += text[2];
- break;
- case '3':
- word += text[3];
- break;
- case '4':
- word += text[4];
- break;
- case '5':
- word += text[5];
- break;
- case '6':
- word += text[6];
- break;
- case '7':
- word += text[7];
- case '8':
- word += text[8];
- break;
- case '9':
- word += text[9];
- break;
- default:
- word += data.at(i);
- break;
- }
- }
- }
- catch (const std::exception& e)
- {
- std::string str;
- str = e.what() + data;
- LOG_ERROR_S(str);
- word = data;
- }
- return word;
- }
- void CFsProxy::__recvMsgFun(websocketpp::connection_hdl hdl, string msg)
- {
- const long conID = reinterpret_cast<long>(hdl.lock().get());
- try
- {
- Json::Value root;
- Json::CharReaderBuilder builder;
- std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
- std::string errs;
- bool ret = reader->parse(msg.c_str(), msg.c_str() + msg.length(), &root, &errs);
- if (!ret || !errs.empty())
- {
- if (errs.empty()) errs = "空";
- Format fmt("Json 解析错误,errs = %s ,msg = %s");
- fmt %errs %msg;
- LOG_WARN_S(fmt.str());
- return;
- }
- if (!root.isMember("Type"))
- {
- Format fmt("该条Json消息格式不能识别 ,msg = %s");
- fmt %msg;
- LOG_WARN_S(fmt.str());
- return;
- }
- LOG_DEBUG_S(msg);
- const string type = root["Type"].asString();
- if (type == "Auth") {
- auto data = root["Data"].asString();
- auto res = m_ToeknList.Auth(conID, data);
- m_Server.sendMsg(hdl, creatJson(type, res));
- if (!res) {
- m_Server.close(hdl);
- LOG_WARN("验证失败,断开链接");
- }
- return;
- }
- if (!m_ToeknList.exists(conID)) {
- //m_Server.close(hdl);
- LOG_WARN("未进行验证,断开链接");
- }
- const string AgentID = root["AgentID"].asString();
- const string AgentExten = root["AgentExten"].asString();
- uint32_t ExtenNo = 0;
- if (sscanf(AgentExten.c_str(), "%u", &ExtenNo) != 1)
- {
- Format fmt("分机号为非UInt,msg = %s");
- fmt % msg;
- LOG_WARN_S(fmt.str());
- m_Server.sendMsg(hdl, fmt.str());
- return;
- }
- if ("Login" == type) { // 签入
- if (SoftAuth::GetInstance()->acdNum() <= (int)m_MapAgent.size())
- {
- Format fmt("ACD授权数量已达上限,Agent = %s,Exten = %d");
- fmt % AgentID % AgentExten;
- LOG_INFO_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, std::string("签入失败,ACD授权数量已达上限")));
- return;
- }
- if (getExten(ExtenNo) == nullptr)
- {
- Format fmt("分机不存在,Agent = %s,Exten = %d");
- fmt % AgentID % AgentExten;
- LOG_INFO_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, std::string("分机号不存在")));
- return;
- }
- // 判断 坐席,分机是否已被签入
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.begin();
- while (it != m_MapAgent.end()) {
- if (it->second->id() == AgentID) {
- Format fmt("坐席已被绑定,Agent = %s,Exten = %s");
- fmt % AgentID % AgentExten;
- LOG_INFO_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, std::string("坐席已被绑定")));
- return;
- }
- else if (it->first == AgentExten) {
- Format fmt("分机已被绑定,Agent = %s,Exten = %s");
- fmt % AgentID % AgentExten;
- LOG_INFO_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, std::string("分机已被绑定")));
- return;
- }
- ++it;
- }
- std::string AgentGroup = root["AgentGroup"].asString();
- Agent *agent = new Agent(AgentID, AgentExten, AgentGroup);
- agent->hdl() = hdl;
- m_MapAgent[AgentExten] = agent; // 分机号作为key
- if (!__login(AgentID, AgentExten, agent->groups())) {
- m_MapAgent.erase(AgentExten);
- Format fmt("座席签入失败,Agent = %s,Exten = %d, %s");
- fmt % AgentID % AgentExten % hdl.lock().get();
- LOG_INFO_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, false));
- return;
- }
- Format fmt("座席签入成功,Agent = %s,Exten = %d,AgentGroup = %s %s");
- fmt % AgentID % AgentExten % AgentGroup % hdl.lock().get();
- LOG_INFO_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, true));
- __agentDetail(lock.release()); // 通知坐席详情变化
- std::string data = creatJson("Monitor", "空闲", AgentID, AgentExten);
- m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
- }
- else if ("Logout" == type) { // 签出
- bool ret = __logout(AgentID, AgentExten);
- Format fmt("座席签出[%d],Agent = %s,Exten = %d, %s");
- fmt %ret % AgentID % AgentExten % hdl.lock().get();
- LOG_INFO_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, ret));
- __agentDetail(); // 通知坐席详情变化
- std::string data = creatJson("Monitor", "签出", AgentID, AgentExten);
- m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
- }
- else if ("SayBusy" == type) { // 置忙
- bool ret = __setState(AgentID, false);
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(AgentExten);
- if (it != m_MapAgent.end())
- it->second->setState(AGENT_STATE_REPOSE);
- __agentDetail(lock.release()); // 通知坐席详情变化
- m_Server.sendMsg(hdl, creatJson(type, ret));
- std::string data = creatJson("Monitor", "小休", AgentID, AgentExten);
- m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
- }
- else if ("SayFree" == type) { // 置闲
- bool ret = __setState(AgentID, true);
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(AgentExten);
- if (it != m_MapAgent.end())
- it->second->setState(AGENT_STATE_FREE);
- __agentDetail(lock.release()); // 通知坐席详情变化
- m_Server.sendMsg(hdl, creatJson(type, ret));
- std::string data = creatJson("Monitor", "空闲", AgentID, AgentExten);
- m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
- }
- else if ("ForceAgentState" == type) { // 强制更改座席状态
- const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
- const string TargetAgentState = root["TargetAgentState"].asString();
- const string Exten = __getExtenByAgent(TargetAgentID);
-
- bool ret = __setState(TargetAgentID, TargetAgentState == "2"); {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(Exten);
- if (it != m_MapAgent.end())
- it->second->setState(TargetAgentState == "2" ? AGENT_STATE_FREE : AGENT_STATE_REPOSE);
- else
- ret = false;
- __agentDetail(lock.release()); // 通知坐席详情变化
- }
- LineOpParam param;
- param.szParam1 = AgentID; // 主控坐席id
- param.szParam2 = TargetAgentID; // 被叫坐席id
- param.nParam1 = AgentExten; // 主控分机
- param.szParam3 = Exten; // 被控分机
- param.nParam2 = ret;
- ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_AGENT_MONI_FORCE_STATE, ExtenNo, ¶m);
- m_Server.sendMsg(hdl, creatJson(type, ret));
- }
- else if ("Hold" == type) { // 保持
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HOLD, ExtenNo, nullptr);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Retrieve" == type) { // 接回保持
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TAKEBACK, ExtenNo, nullptr);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("MakeCall" == type) { // 外呼
- const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
- LineOpParam param;
- param.szParam1 = AgentExten;
- param.szParam2 = DestinationNumber;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MAKE_CALL, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Transfer" == type) { // 转移
- const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
- LineOpParam param;
- param.szParam1 = AgentExten;
- param.szParam2 = DestinationNumber;
- auto it = m_MapAgent.find(DestinationNumber);
- if (it != m_MapAgent.end())
- {
- if (it->second->isRepose())
- {
- Format fmt("坐席[%s]-[%s]转移失败,目标[%s][%s]小休中");
- fmt %AgentID %AgentExten %it->second->id() % DestinationNumber;
- LOG_WARN_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, false));
- return;
- }
- }
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TRANSFER, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Meeting" == type) { // 会议
- const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
- {
- auto it = m_MapAgent.find(DestinationNumber);
- if (it != m_MapAgent.end())
- {
- if (it->second->isRepose())
- {
- Format fmt("坐席[%s]-[%s]操作会议失败,目标[%s][%s]小休中");
- fmt %AgentID %AgentExten %it->second->id() % DestinationNumber;
- LOG_WARN_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, false));
- return;
- }
- }
- }
- auto it = getExten(ExtenNo);
- std::string meetingID;
- if (it != nullptr)
- {
- meetingID = it->sessionId();
- }
- LOG_DEBUG("会议ID[%s]", meetingID.c_str());
- LineOpParam param;
- param.nParam1 = meetingID; // 会议id
- param.szParam1 = AgentExten;
- param.szParam2 = DestinationNumber;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_CONFERENCE, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("MultiMeeting" == type) { // 多步会议
- const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
- {
- auto it = m_MapAgent.find(DestinationNumber);
- if (it != m_MapAgent.end())
- {
- if (it->second->isRepose())
- {
- Format fmt("坐席[%s]-[%s]操作多步会议失败,目标[%s][%s]小休中");
- fmt %AgentID %AgentExten %it->second->id() % DestinationNumber;
- LOG_WARN_S(fmt.str());
- m_Server.sendMsg(hdl, creatJson(type, false));
- return;
- }
- }
- }
- auto it = getExten(ExtenNo);
- std::string meetingID;
- if (it != nullptr)
- {
- meetingID = it->sessionId();
- }
- LOG_DEBUG("多步会议ID[%s]", meetingID.c_str());
- LineOpParam param;
- param.nParam1 = meetingID; // 会议id
- param.szParam1 = AgentExten;
- param.szParam2 = DestinationNumber;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MULTI_CONFERENCE, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("MultiTakeback" == type) { // 多步会议接回
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MULTI_CONFERENCE_TAKEBACK, ExtenNo, nullptr);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("DropCall" == type) { // 挂机
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HANG_UP, ExtenNo, nullptr);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Break" == type) { // 强拆
- const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
- const string Exten = __getExtenByAgent(TargetAgentID);
- LineOpParam param;
- param.szParam1 = AgentExten;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HANG_UP, atoi(Exten.c_str()), ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Listen" == type) { // 监听
- const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
- const string Exten = __getExtenByAgent(TargetAgentID);
- LineOpParam param;
- param.szParam1 = AgentExten;
- param.szParam2 = Exten;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_LISTEN, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Insert" == type) { // 强插
- const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
- const string Exten = __getExtenByAgent(TargetAgentID);
- LineOpParam param;
- param.szParam1 = AgentExten;
- param.szParam2 = Exten;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_INSERT, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Instead" == type) { // 代接
- const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
- const string Exten = __getExtenByAgent(TargetAgentID);
- LineOpParam param;
- param.szParam1 = AgentExten;
- param.szParam2 = Exten;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_INSTEAD, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Intercept" == type) { // 强截
- const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
- const string Exten = __getExtenByAgent(TargetAgentID);
- LineOpParam param;
- param.szParam1 = AgentExten;
- param.szParam2 = Exten;
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_GRAB, ExtenNo, ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Record" == type) { // 录音
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_RECORD, ExtenNo, nullptr);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("MuteOn" == type) { // 静音
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MUTE_BEGIN, ExtenNo, nullptr);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("MuteOff" == type) { // 取消静音
- bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MUTE_END, ExtenNo, nullptr);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- else if ("Monitor" == type) { // 监控
- m_MonitAgents.addAgent(AgentID, hdl);
- m_Server.sendMsg(hdl, creatJson(type, true));
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.begin();
- while (it != m_MapAgent.end())
- {
- std::string state = it->second->state_s();
- std::string agentId = it->second->id();
- std::string data = creatJson("Monitor", state, agentId, it->second->assoExten());
- m_Server.sendMsg(hdl, data);
- ++it;
- }
- }
- else if ("MonitorCancel" == type) { // 取消监控
- m_MonitAgents.delAgent(AgentID);
- m_Server.sendMsg(hdl, creatJson(type, true));
- }
- else if ("TurnIvr" == type) { // 转满意度
- COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TURNIVR, ExtenNo, nullptr);
- }
- else if ("Heart" == type) { // 心跳
- m_Server.sendMsg(hdl, msg);
- }
- else if ("AutoCall" == type) { // 自动外呼
- //m_AutoCall.addTask(); // 从数据库刷新任务
- m_Server.sendMsg(hdl, creatJson(type, true));
- }
- else if ("GetAgentDetail" == type) {
- auto data = m_AgentDetail.to_string();
- m_Server.sendMsg(hdl, data);
- m_mpAgentDetailHdl[conID] = hdl;
- }
- else if ("WaitCount" == type) {
- std::unique_lock<std::shared_mutex>lock(m_mutQueryCount);
- auto it = std::find_if(m_subWaitCount.begin(), m_subWaitCount.end(), [=](websocketpp::connection_hdl hdltmp) {return hdltmp.lock() == hdl.lock(); });
- if (it == m_subWaitCount.end()) { // 第一次发送则保存
- m_subWaitCount.emplace_back(hdl); // 保存订阅排队数量的坐席
- }
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- root["Type"] = "WaitCount";
- root["Result"] = true;
- std::int32_t nTotal = 0;
- for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
- root[it->first] = it->second;
- try {
- auto n = stoi(it->second);
- nTotal += n;
- }
- catch (const std::exception& e) {
- LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
- }
- }
- lock.unlock();
- root["Total"] = nTotal;
- auto data = Json::writeString(jsrocd, root);
- m_Server.sendMsg(hdl, data);
- }
- else if ("SendDtmf" == type){
- std::string Dtmf = root["Dtmf"].asString();
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(AgentExten);
- if (it == m_MapAgent.end()) { // 坐席未签入
- lock.unlock();
- m_Server.sendMsg(hdl, creatJson(type, false,"坐席未签入"));
- return;
- }
- if (AGENT_STATE_BUSY != it->second->state()) {
- lock.unlock();
- m_Server.sendMsg(hdl, creatJson(type, false, "坐席未通话"));
- return;
- }
- auto pChanIter = m_MapChanExt.find(ExtenNo);
- if (pChanIter == m_MapChanExt.end()) {
- return;
- }
- auto pChan = pChanIter->second;
- auto pSession = __getSessionByChanId(pChan->chanId());
- auto pFinalChan = pSession->getAssoFinalChan();
- if (CHAN_LOGIC_STATE_TALKING != pFinalChan->state() || !pFinalChan->isInMeeting()) {
- m_Server.sendMsg(hdl, creatJson(type, false, "目标线路未通话或未在会议中"));
- return;
- }
- LineOpParam param;
- param.szParam1 = AgentExten;
- param.szParam2 = std::to_string(pFinalChan->no());
- param.szParam3 = pFinalChan->chanId();
- param.szDtmf = Dtmf; // 按键
- bool ret = COperationReactor::GetInstance()->procOperation(ExtenNo, LINE_OP_SEND_DTMF, pFinalChan->no(), ¶m);
- if (!ret)
- m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
- }
- // 以下是视频接口
- else if ("VideoReqExten" == type) { // 请求可以注册的分机号
- uint32_t extenId = 0;
- std::string pwd;
- auto ret = m_VideoOffice.findVideoExten(extenId, pwd);
- if (ret) {
- auto agent = AgentID;
- ret = m_VideoExtenHdl.add(ExtenNo, agent, hdl);
- if (!ret) {
- m_VideoOffice.recoverVideoExten(extenId);
- }
- }
- auto data = creatJson2("VideoReqExten", ret, extenId, pwd);
- m_Server.sendMsg(hdl, data);
- }
- else if ("VideoReqBindExtPhone" == type) { // 绑定分机号-手机号关系
- const string DestinationNumber = root["DestinationNumber"].asString();
- m_ExtPhone[ExtenNo] = DestinationNumber;
- auto data = creatJson2(type, true);
- m_Server.sendMsg(hdl, data);
- }
- else if ("VideoReqIdeAgent" == type) { // 请求空闲坐席
- std::unique_lock<std::mutex>lock(m_AgentLock);
- bool ret = false;
- uint32_t exten = 0;
- for (auto it = m_MapAgent.begin(); it != m_MapAgent.end(); ++it) {
- if (it->second->VideoLock()) {
- exten = (uint32_t)stoul(it->first);
- ret = true;
- break;
- }
- }
- lock.unlock();
- auto data = creatJson2(type, ret, exten);
- m_Server.sendMsg(hdl, data);
- ret = m_VideoExtenHdl.bindReqAgentExten(ExtenNo, exten);
- if (!ret && exten != 0) {
- lock.lock();
- auto it = m_MapAgent.find(std::to_string(exten));
- if (ret && it != m_MapAgent.end()) {
- it->second->CancelVideoLock();
- }
- lock.unlock();
- }
- }
- else if ("VideoDesExten" == type) { // 销毁/回收请求的分机号
- m_VideoOffice.recoverVideoExten(ExtenNo);
- std::string data = creatJson2(type, true);
- m_Server.sendMsg(hdl, data);
- }
- else if ("VideoReqBindAgent" == type) { //督办专员 保存
- auto agent = AgentID;
- auto ret = m_VideoExtenHdl.add(ExtenNo, agent,hdl);
- std::string data = creatJson2(type, ret);
- m_Server.sendMsg(hdl, data);
- }
- else if ("GetExtenList" == type) { // 获取督办部门状态
- auto data = m_DepttelOffice.GetAllDeptTelext();
- m_Server.sendMsg(hdl, data);
- }
- else {
- }
- // try 结束
- }
- catch (const Json::LogicError& e)
- {
- Format fmt("格式发送错误[%s];msg:[%s]");
- fmt %e.what() % msg;
- m_Server.sendMsg(hdl, fmt.str());
- }
- catch (const std::exception&)
- {
- }
- }
- void CFsProxy::__closeFun(websocketpp::connection_hdl hdl)
- {
- const long conID = reinterpret_cast<long>(hdl.lock().get());
- m_ToeknList.remove(conID);
- m_mpAgentDetailHdl.erase(conID);
- /*视频处理*/
- auto pair = m_VideoExtenHdl.del(hdl);
- if (pair.first > 0) {
- m_VideoOffice.recoverVideoExten(pair.first, true);
- m_DepttelOffice.UpdateDepttelext(pair.first, 0);
- if (pair.second > 0) {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(std::to_string(pair.second));
- it->second->CancelVideoLock();
- }
- return;
- }
-
- std::string agentId;
- std::string extenNo;
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.begin();
- while (it != m_MapAgent.end())
- {
- if (!(it->second->hdl()).expired())
- {
- auto sp = it->second->hdl().lock();
- stringstream agent_hdl;
- agent_hdl << sp.get();
- stringstream local_hdl;
- local_hdl << hdl.lock().get();
- if (agent_hdl.str() == local_hdl.str())
- {
- agentId = it->second->id(); // 保存坐席id
- extenNo = it->second->assoExten(); // 分机号
- Format fmt("座席掉线,agentId = %s,extenNo = %s,IP = %s");
- fmt %it->second->id() % it->second->assoExten() % local_hdl.str();
- LOG_WARN_S(fmt.str());
- //m_Gateway.delAgent(agentId);
- m_Gateway.delAgent(agentId, it->second->groups());
- it->second->removeAgent("坐席掉线");
- FS_LINK_DELETE(it->second);
- m_MapAgent.erase(it);
- break;
- }
- }
- ++it;
- }
- __agentDetail(lock.release());
- if (!agentId.empty())
- {
- std::string data = creatJson("Monitor", "签出", agentId, extenNo);
- m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
- m_MonitAgents.delAgent(agentId);
- }
- }
- void CFsProxy::__doAutoTask(Task task)
- {
- switch (task.type)
- {
- case VOICE_CALL: // 语音外呼
- {
- __voicCall(task);
- }
- break;
- case AGENT_CALL: // 指定坐席外呼 set 在当前 channel 上设置变量,而 export 在(a-leg 和 b-leg) 两个 channel 上都设置
- {
- LineOpParam param;
- param.nParam1 = task.agent; // 坐席号
- param.nParam2 = task.type; // 自动外呼类型
- param.nParam3 = task.id; // 自动外呼任务id
- param.szParam1 = getExtenByAgent(task.agent); // 坐席对应的分机号
- param.szParam2 = task.number;
- if (param.szParam1.empty()) // 如果坐席对应分机不存在说明坐席未登陆,不进行呼叫
- {
- return;
- }
- auto pChan = getExten(std::atoi(param.szParam1.c_str()));
- if (pChan == nullptr || pChan->state() != CHAN_LOGIC_STATE_FREE)
- {
- return;
- }
- COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_AUTO_CALL, atoi(param.szParam1.c_str()), ¶m);
- }
- break;
- case AUTO_CALL: // 系统自动分配坐席外呼
- break;
- default:
- break;
- }
- if (task.id != 0)
- m_AutoCall.updateTask(std::to_string(task.id)); // 更新任务
- }
- bool CFsProxy::__voicCall(Task & task)
- {
- std::string content = task.content;
- std::string filePath;
- if (!tts.TextToAudio(content, filePath))
- {
- LOG_WARN_S("文本转语音失败");
- return false;
- };
- std::string callee = task.number;
- std::string caller;
- std::string CallString;
- m_CallStringMaker.makeCallString(caller, callee, CallString);
- Format fmt("bgapi originate {origination_caller_id_number=%s,%s=%ld,%s=%s,%s=%ld}%s VOICECALL XML %s\r\n%s: %ld");
- fmt %caller % ESL_VAR_OP_TYPE % task.type %"filePath" %filePath % ESL_VAR_TASK_ID % task.id % CallString % "AutoCall" % ESL_HEADER_JOB_UUID % task.uniqueId();
- return m_Gateway.sendCmd(fmt.str());
- }
- void CFsProxy::__agentDetail()
- {
- m_AgentDetail.resetAgent();
- std::unique_lock<std::mutex>lock(m_AgentLock);
- m_AgentDetail.setAgentOnlineCount((std::int32_t)m_MapAgent.size());
- Agent* pAgent = nullptr;
- for (auto it = m_MapAgent.begin(); it != m_MapAgent.end(); ++it) {
- pAgent = it->second;
- if (pAgent) {
- auto state = pAgent->state();
- switch (state)
- {
- case AGENT_STATE_FREE:
- m_AgentDetail.setAgentFreeCount(1);
- break;
- case AGENT_STATE_BUSY:
- m_AgentDetail.setAgentSpeakCount(1);
- break;
- case AGENT_STATE_REPOSE:
- m_AgentDetail.setAgentReposeCount(1);
- break;
- default:
- break;
- }
- }
- }
- // 群发坐席详情
- for (auto it = m_mpAgentDetailHdl.begin(); it != m_mpAgentDetailHdl.end(); ++it) {
- m_Server.sendMsg(it->second, m_AgentDetail.to_string());
- }
- }
- void CFsProxy::__agentDetail(std::mutex * p_mtx, bool islock)
- {
- m_AgentDetail.resetAgent();
- m_AgentDetail.setAgentOnlineCount((std::int32_t)m_MapAgent.size());
- Agent* pAgent = nullptr;
- for (auto it = m_MapAgent.begin(); it != m_MapAgent.end(); ++it) {
- pAgent = it->second;
- if (pAgent) {
- auto state = pAgent->state();
- switch (state)
- {
- case AGENT_STATE_FREE:
- m_AgentDetail.setAgentFreeCount(1);
- break;
- case AGENT_STATE_BUSY:
- m_AgentDetail.setAgentSpeakCount(1);
- break;
- case AGENT_STATE_REPOSE:
- m_AgentDetail.setAgentReposeCount(1);
- break;
- default:
- break;
- }
- }
- }
- if (islock) {
- p_mtx->lock();
- }
- else {
- p_mtx->unlock();
- }
- // 群发坐席详情
- for (auto it = m_mpAgentDetailHdl.begin(); it != m_mpAgentDetailHdl.end(); ++it) {
- m_Server.sendMsg(it->second, m_AgentDetail.to_string());
- }
- }
- void CFsProxy::__agentDetailTrunkChange(const std::int32_t& nCount)
- {
- m_AgentDetail.setTrunkCallInCount(nCount);
- // 群发坐席详情
- for (auto it = m_mpAgentDetailHdl.begin(); it != m_mpAgentDetailHdl.end(); ++it) {
- m_Server.sendMsg(it->second, m_AgentDetail.to_string());
- }
- }
- std::string CFsProxy::__queryCount(const bool & bIsLock)
- {
- Json::Value root;
- Json::StreamWriterBuilder jsrocd;
- root["Type"] = "WaitCount";
- root["Result"] = true;
- std::int32_t nTotal = 0;
- if (bIsLock) {
- std::shared_lock<std::shared_mutex>lock(m_mutQueryCount);
- for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
- root[it->first] = it->second;
- try {
- auto n = stoi(it->second);
- nTotal += n;
- }
- catch (const std::exception& e) {
- LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
- }
- }
- lock.unlock();
- }
- else {
- for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
- root[it->first] = it->second;
- try {
- auto n = stoi(it->second);
- nTotal += n;
- }
- catch (const std::exception& e) {
- LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
- }
- }
- }
- //m_WaitCounter
- root["Total"] = nTotal;
- auto strJson = Json::writeString(jsrocd, root);
- return strJson;
- }
- CFsProxy CFsProxy::instance;
|