#include "EslGateway.h" #include "FsProxy.h" #include #include #include #include #include #include #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 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 rows; boost::split(rows, body, boost::is_any_of("\n")); if (rows.size() < 3) return false; std::map mKeyVal; std::list 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 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 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 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 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 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); }