| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967 |
- #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 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());
- });
- 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) {
- 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);
- }
- }
- }
- 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;
- 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::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)
- {
- 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);
- }
- 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)"; // 空闲不接电话/话后处理
- 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();
- 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 Exten = __getExtenByAgent(TargetAgentID);
- bool ret = __setState(TargetAgentID, true);
- {
- std::unique_lock<std::mutex>lock(m_AgentLock);
- auto it = m_MapAgent.find(Exten);
- if (it != m_MapAgent.end())
- it->second->setState(AGENT_STATE_FREE);
- 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 {
- }
- // 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_mpAgentDetailHdl.erase(conID);
- 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;
|