| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- #include "EslGateway.h"
- #include "FsProxy.h"
- #include <thread>
- #include <iostream>
- #include <functional>
- #include <regex>
- #include <boost/algorithm/string.hpp>
- #include <boost/format.hpp>
- #include "Config.h"
- #include "Log.h"
- using Format = boost::format;
- //#pragma comment(lib,"libesl.a")
- CEslGateway::CEslGateway(CFsProxy * pParent) :m_pParent(pParent), m_Stop(true), m_EslHdl4Listen({ {0} }), m_EslHdl4Send({ {0} })
- {
- }
- CEslGateway::~CEslGateway()
- {
- }
- bool CEslGateway::start(void)
- {
- CConfig *cfg = CConfig::GetInstance();
- LOG_INFO(("{EslGateway}: 处理客户端连接, ip = %s,port=%d,pwd=%s"), cfg->fsAddr().c_str(), cfg->fsPort(), cfg->fsPwd().c_str());
- esl_connect(&m_EslHdl4Listen, cfg->fsAddr().c_str(), cfg->fsPort(), NULL, cfg->fsPwd().c_str());
- if (!m_EslHdl4Listen.connected)
- return false;
- LOG_INFO_S(("{EslGateway}: 处理客户端连接m_EslHdl4Listen OK"));
- esl_events(&m_EslHdl4Listen, ESL_EVENT_TYPE_PLAIN,
- "BACKGROUND_JOB CHANNEL_CREATE CHANNEL_PROGRESS CHANNEL_PROGRESS_MEDIA CHANNEL_ANSWER CHANNEL_HANGUP_COMPLETE CHANNEL_HOLD CHANNEL_UNHOLD DTMF CUSTOM sofia::register sofia::unregister callcenter::info");
- LOG_INFO(("{EslGateway}: 处理客户端连接, ip = %s,port=%d,pwd=%s"), cfg->fsAddr().c_str(), cfg->fsPort(), cfg->fsPwd().c_str());
- esl_connect(&m_EslHdl4Send, cfg->fsAddr().c_str(), cfg->fsPort(), NULL, cfg->fsPwd().c_str());
- if (!m_EslHdl4Send.connected)
- {
- esl_disconnect(&m_EslHdl4Listen);
- return false;
- }
- m_Stop = false;
- std::thread(std::bind(&CEslGateway::__eventThread,this)).detach();
- return true;
- }
- void CEslGateway::stop(void)
- {
- if (!m_Stop)
- {
- m_Stop = true;
- esl_disconnect(&m_EslHdl4Listen);
- esl_disconnect(&m_EslHdl4Send);
- }
- }
- bool CEslGateway::hangupAll(void)
- {
- return esl_send_recv(&m_EslHdl4Send, ESL_CMD_HANGUPALL) == ESL_SUCCESS;
- }
- bool CEslGateway::scanExten(void)
- {
- if (esl_send_recv(&m_EslHdl4Send, ESL_CMD_SCAN_INTERNAL) != ESL_SUCCESS)
- return false;
- if (m_EslHdl4Send.last_sr_event == nullptr || m_EslHdl4Send.last_sr_event->body == nullptr)
- return false;
- std::string body = m_EslHdl4Send.last_sr_event->body;
- std::list<std::string> rows;
- boost::split(rows, body, boost::is_any_of("\n"));
- while (!rows.empty())
- {
- std::string var = rows.front();
- rows.pop_front();
- std::regex space("\t| ");
- var = std::regex_replace(var, space, "");
- std::regex reg("IP:(.*)");
- if (std::regex_match(var, reg))
- {
- std::string extenNo, extenIP;
- extenIP = std::regex_replace(var, reg, "$1");
- while (!rows.empty())
- {
- var = rows.front();
- rows.pop_front();
- std::regex space("\t| ");
- var = std::regex_replace(var, space, "");
- reg = std::regex("Auth-User:(.*)");
- if (std::regex_match(var, reg))
- {
- extenNo = std::regex_replace(var, reg, "$1");
- if (!extenNo.empty() && !extenIP.empty())
- {
- LOG_DEBUG("分机注册:%s %s", extenNo.c_str(), extenIP.c_str());
- if(m_pParent)
- {
- m_pParent->onEslExtenReg(atoi(extenNo.c_str()), extenIP);
- }
- }
- break;
- }
- }
- }
- }
- return true;
- }
- bool CEslGateway::getAgentState(std::string AgentID, AgentDetail &agent)
- {
- Format fmt("api callcenter_config agent list %1%");
- fmt %AgentID;
- if (esl_send_recv(&m_EslHdl4Send, fmt.str().c_str()) != ESL_SUCCESS)
- return false;
- if (m_EslHdl4Send.last_sr_event == nullptr || m_EslHdl4Send.last_sr_event->body == nullptr)
- return false;
- std::string body = m_EslHdl4Send.last_sr_event->body;
- std::vector<std::string> rows;
- boost::split(rows, body, boost::is_any_of("\n"));
- if (rows.size() < 3)
- return false;
- std::map<std::string, std::string> mKeyVal;
- std::list<std::string> heards,contents;
- std::string heard = std::move(rows[0]);
- std::string content = std::move(rows[1]);
- boost::split(heards, heard, boost::is_any_of("|"));
- boost::split(contents, content, boost::is_any_of("|"));
- if(heards.size()!=contents.size())
- return false;
- while (!heards.empty())
- {
- mKeyVal[std::move(heards.front())] = std::move(contents.front());
- heards.pop_front();
- contents.pop_front();
- }
- //AgentDetail agent;
- auto name = mKeyVal.find("name");
- if (name != mKeyVal.end())
- agent.name = name->second;
- auto system = mKeyVal.find("system");
- if (system != mKeyVal.end())
- agent.system = system->second;
- auto uuid = mKeyVal.find("uuid");
- if (uuid != mKeyVal.end())
- agent.uuid = uuid->second;
- auto type = mKeyVal.find("type");
- if (type != mKeyVal.end())
- agent.type = type->second;
- auto contact = mKeyVal.find("contact");
- if (contact != mKeyVal.end())
- agent.contact = contact->second;
- auto status = mKeyVal.find("status");
- if (status != mKeyVal.end())
- agent.status = status->second;
- auto state = mKeyVal.find("state");
- if (state != mKeyVal.end())
- agent.state = state->second;
- return true;
- }
- bool CEslGateway::getConferenceMemberId(std::string MeetingId, std::string ChanId, std::string & MemberId)
- {
- std::string cmd;
- cmd = boost::str(Format("api conference %s list") % MeetingId);
- if (esl_send_recv(&m_EslHdl4Send, cmd.c_str()) != ESL_SUCCESS)
- return false;
- if (m_EslHdl4Send.last_sr_event && m_EslHdl4Send.last_sr_event->body) {
- std::vector<std::string> vs;
- boost::split(vs, m_EslHdl4Send.last_sr_event->body, boost::algorithm::is_any_of("\n"));
- for (auto v : vs) {
- if (v.find(ChanId) != std::string::npos) {
- std::vector<std::string> des;
- boost::split(des, v, boost::algorithm::is_any_of(";"));
- if (!des.empty()) {
- MemberId = des[0].c_str();
- return true;
- }
- break;
- }
- }
- }
- return false;
- }
- bool CEslGateway::delAgentAll(void)
- {
- LOG_DEBUG("清空FreeSWITCH中已签入坐席");
- Format fmt("api callcenter_config agent list");
- if (esl_send_recv(&m_EslHdl4Send, fmt.str().c_str()) != ESL_SUCCESS)
- return false;
- if (m_EslHdl4Send.last_sr_event == nullptr || m_EslHdl4Send.last_sr_event->body == nullptr)
- return false;
- std::string body = m_EslHdl4Send.last_sr_event->body;
- std::list<std::string> rows;
- boost::split(rows, body, boost::is_any_of("\n"));
- if (rows.size() < 3)
- return false;
- rows.pop_front();
- rows.pop_back();
- rows.pop_back();
- for (std::string &var : rows)
- {
- std::list<std::string> contents;
- boost::split(contents, var, boost::is_any_of("|"));
- std::string name = contents.front();
- delAgent(name);
- }
- return false;
- }
- bool CEslGateway::delAgent(std::string AgentID)
- {
- bool ret = true;
- std::string cmd;
- cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
- ret &= sendCmd(cmd);
- cmd = boost::str(Format("bgapi callcenter_config tier del ZXZ %1% ") % AgentID);
- ret &= sendCmd(cmd);
- return ret;
- }
- bool CEslGateway::delAgent(std::string AgentID, std::list<std::string> Groups)
- {
- bool ret = true;
- std::string cmd;
- cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
- ret &= sendCmd(cmd);
- for (std::string &var : Groups)
- {
- cmd = boost::str(Format("bgapi callcenter_config tier del %2% %1% ") % AgentID %var);
- ret &= sendCmd(cmd);
- }
- return ret;
- }
- bool CEslGateway::sendCmd(string pCmd)
- {
- LOG_DEBUG_S(pCmd);
- esl_status_t Status = esl_send_recv(&m_EslHdl4Send, pCmd.c_str());
- return Status == ESL_SUCCESS;
- }
- bool CEslGateway::execute(esl_handle_t * pHandle, const char * App, const char * Param)
- {
- if (pHandle == nullptr) return false;
- esl_status_t Status = esl_execute(pHandle, App, Param, NULL);
- return Status == ESL_SUCCESS;
- }
- bool CEslGateway::Execte(const char * App, const char * Param, const char * ChanId)
- {
- if (App == nullptr) return false;
- esl_status_t Status = esl_execute(&m_EslHdl4Send, App, Param, ChanId);
- return Status == ESL_SUCCESS;
- }
- void CEslGateway::__eventThread(void)
- {
- esl_event_t* pEvent = NULL;
- esl_handle_t* pListenHandle = &m_EslHdl4Listen;
- // 下面的设置保证执行同时多个APP时,后面的不会抢占前面的
- pListenHandle->event_lock = 1;
- while (!m_Stop)
- {
- // esl_recv_event会阻塞线程,其第二个参数为1,表示优先检查内部缓存列表,防止遗漏事件。
- if (esl_recv_event(pListenHandle, 1, NULL) == ESL_SUCCESS)
- {
- pEvent = pListenHandle->last_ievent;
- if (pEvent != NULL)
- __onEslEvent(pEvent);
- }
- else
- {
- // 连接中断
- __onEslDisconnect();
- return;
- }
- }
- }
- void CEslGateway::__onEslEvent(esl_event_t * pEvent)
- {
- /*char *pContent = new char[2048];
- esl_event_serialize(pEvent, &pContent, ESL_TRUE);
- LOG_DEBUG_S(pContent);
- FS_LINK_DELETE(pContent);*/
- switch ((uint32_t)pEvent->event_id)
- {
- case ESL_EVENT_BACKGROUND_JOB:
- __onEslEvtBgJobDone(pEvent);
- break;
- case ESL_EVENT_CHANNEL_CREATE:
- case ESL_EVENT_CHANNEL_ANSWER:
- case ESL_EVENT_CHANNEL_BRIDGE:
- case ESL_EVENT_CHANNEL_PROGRESS:
- case ESL_EVENT_CHANNEL_PROGRESS_MEDIA:
- case ESL_EVENT_CHANNEL_HANGUP_COMPLETE:
- __onEslEvtChanEvent(pEvent);
- break;
- case ESL_EVENT_DTMF:
- __onEslEvtDtmf(pEvent);
- break;
- case ESL_EVENT_CHANNEL_HOLD:
- __onEslEvtHold(pEvent, EVENT_HOLD);
- break;
- case ESL_EVENT_CHANNEL_UNHOLD:
- __onEslEvtHold(pEvent, EVENT_UNHOLD);
- break;
- case ESL_EVENT_CUSTOM:
- __onEslEvtCustom(pEvent);
- break;
- }
- }
- void CEslGateway::__onEslDisconnect(void)
- {
- LOG_WARN_S("{EslGateway}: 监听通道事件时连接中断");
- m_Stop = true;
- m_pParent->onEslDisconnect();
- }
- void CEslGateway::__onEslEvtBgJobDone(esl_event_t * pEvent)
- {
- BG_JOB_NOTIFY Notify;
- memset(&Notify, '\0', sizeof(BG_JOB_NOTIFY));
- char* HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_JOB_UUID);
- if (HeaderValue != nullptr)
- Notify.JobInstance = atol(HeaderValue);
- LOG_DEBUG("JobCmd[%ld]-[%s]", Notify.JobInstance, HeaderValue);
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_JOB_CMD);
- Notify.JobCmd = HeaderValue;
- HeaderValue = esl_event_get_body(pEvent);
- if (HeaderValue != nullptr)
- Notify.JobBody = HeaderValue;
- m_pParent->onEslEvtBgJobDone(&Notify);
-
- }
- void CEslGateway::__onEslEvtChanEvent(esl_event_t * pEvent)
- {
- CHAN_EVENT_NOTIFY Notify;
- char* HeaderValue = nullptr;
- // 获取事件ID
- Notify.EventId = (CHAN_EVENT_ID)pEvent->event_id;
- // 获取通道方向
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DIRECTION);
-
- if (strcmp(HeaderValue, ESL_HDR_DIRECTION_INBOUND) == 0)
- Notify.Direction = CALL_DIRECTION_INBOUND;
- else
- Notify.Direction = CALL_DIRECTION_OUTBOUND;
- // 获取会话ID
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
- Notify.CallId = HeaderValue;
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CCID);
- if (HeaderValue != nullptr)
- Notify.CcId = HeaderValue;
- // 获取通道ID
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
- Notify.ChanId = HeaderValue;
- // 获取主被叫
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLER);
- if(HeaderValue !=nullptr)
- Notify.Caller = HeaderValue;
- else
- {
- Format fmt("主叫号码未获取到,EventId = %d ,CallId = %s,ChanId = %s,Direction = %d,CcId = %s ");
- fmt %Notify.EventId % Notify.CallId % Notify.CallId % Notify.Direction % Notify.CcId;
- LOG_WARN_S(fmt.str());
- }
-
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLEE);
- if (HeaderValue != nullptr)
- {
- Notify.Callee = HeaderValue;
- regex reg("\\d+");
- if (!regex_match(HeaderValue, reg))
- {
- LOG_WARN("被叫号码为非存数字,软电话可能需要处理,分机号: %s", HeaderValue);
- HeaderValue = esl_event_get_header(pEvent, "variable_dialed_user"); //判断被叫号码是不是软电话,软电话获取的被叫不是真实注册的分机号,是随机字符串
- LOG_WARN("被叫号码为非存数字,软电话可能需要处理,分机号: %s", HeaderValue);
- }
- if(HeaderValue!=nullptr)
- Notify.Callee = HeaderValue;
- }
- else
- {
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DEST_NUM);
- if (HeaderValue != nullptr)
- Notify.Callee = HeaderValue;
- }
- // 获取操作类型 区分自动外呼
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_OP_TYPE);
- if (HeaderValue != nullptr)
- {
- LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_OP_TYPE, HeaderValue);
- Notify.CallType = atoi(HeaderValue);
- }
- // 获取自动外呼任务id ,
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_TASK_ID);
- if (HeaderValue != nullptr)
- {
- LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_TASK_ID, HeaderValue);
- Notify.TaskId = atol(HeaderValue);
- }
- // 代码外呼,自动外呼时,主叫create事件时就得到正确的被叫
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_MALL_CALL_CALLEE);
- if (HeaderValue != nullptr)
- {
- LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_MALL_CALL_CALLEE, HeaderValue);
- Notify.Called = HeaderValue;
- }
-
- // 获取挂机原因
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_HANGUP_CAUSE);
- if (HeaderValue != nullptr)
- Notify.HangupCause = HeaderValue;
- else
- {
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALL_FAILED_CAUSE);
- if (HeaderValue != nullptr)
- Notify.HangupCause = HeaderValue;
- }
- // 获取通道操作标识
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_OP_INSTANCE);
- if (HeaderValue != nullptr)
- {
- LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_OP_INSTANCE,HeaderValue);
- Notify.ChanOpInstance = atol(HeaderValue);
- }
- Format fmt("FreeSWITCH事件,EventId = %d ,CallId = %s,ChanId = %s,Direction = %d ,caller = %s,callee = %s , op_type = %d,op_instance = %ld,task_id = %ld,call_called = %s");
- fmt %Notify.EventId % Notify.CallId % Notify.ChanId % Notify.Direction % Notify.Caller % Notify.Callee % Notify.CallType % Notify.ChanOpInstance %Notify.TaskId %Notify.Called;
- LOG_DEBUG(fmt.str());
- m_pParent->onEslEvtChannel(&Notify);
- }
- void CEslGateway::__onEslEvtCustom(esl_event_t * pEvent)
- {
- char* HeaderValue = nullptr;
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_SUBCLASS);
- if (strcmp(HeaderValue, ESL_HDR_SUBCLASS_SIP_REG) == 0) // 分机注册事件
- {
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_EXTEN_NO);
- uint32_t ExtenNo = 0;
- sscanf(HeaderValue, "%u", &ExtenNo);
- m_pParent->onEslExtenReg(ExtenNo,"");
- }
- else if (strcmp(HeaderValue, ESL_HDR_SUBCLASS_SIP_UNREG) == 0) // 分机注销事件
- {
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_EXTEN_NO);
- uint32_t ExtenNo = 0;
- if (HeaderValue != NULL) // jssip软电话注册后,刷新页面得到的HeaderValue为空,导致sscanf_s失败
- {
- sscanf(HeaderValue, "%u", &ExtenNo);
- m_pParent->onEslExtenUnreg(ExtenNo);
- }
- }
- else if (strcmp(HeaderValue, "callcenter::info") == 0) {
- const char* queueName = esl_event_get_header(pEvent, "CC-Queue");
- const char* callsWaitingStr = esl_event_get_header(pEvent, "CC-Count");
- if (callsWaitingStr) {
- LOG_INFO("队列[%s]当前排队人数[%s]", queueName, callsWaitingStr);
- m_pParent->onCallcenterQueue(queueName, callsWaitingStr);
- }
- char *pContent = new char[2048];
- esl_event_serialize(pEvent, &pContent, ESL_TRUE);
- LOG_DEBUG_S(pContent);
- FS_LINK_DELETE(pContent);
- }
- }
- void CEslGateway::__onEslEvtDtmf(esl_event_t * pEvent)
- {
- DTMF_NOTIFY Notify;
- char* HeaderValue = NULL;
- // 获取会话ID
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
- Notify.CallId = HeaderValue;
- // 获取通道ID
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
- Notify.ChanId = HeaderValue;
- // 获取DTMF
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DTMF);
- Notify.DTMF = HeaderValue;
- m_pParent->onEslEvtDtmf(&Notify);
- }
- void CEslGateway::__onEslEvtHold(esl_event_t * pEvent, EVENT_HOLD_TYPE EvtType)
- {
- HOLD_NOTIFY Notify;
- char* HeaderValue = NULL;
- // 获取会话ID
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
- Notify.CallId = HeaderValue;
- // 获取通道ID
- HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
- Notify.ChanId = HeaderValue;
- // 获取保持事件类型
- Notify.EvtType = EvtType;
- m_pParent->onEslEvtHold(&Notify);
- }
|