linux版本中间件

EslGateway.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include "EslGateway.h"
  2. #include "FsProxy.h"
  3. #include <thread>
  4. #include <iostream>
  5. #include <functional>
  6. #include <regex>
  7. #include <boost/algorithm/string.hpp>
  8. #include <boost/format.hpp>
  9. #include "Config.h"
  10. #include "Log.h"
  11. using Format = boost::format;
  12. //#pragma comment(lib,"libesl.a")
  13. CEslGateway::CEslGateway(CFsProxy * pParent) :m_pParent(pParent), m_Stop(true), m_EslHdl4Listen({ {0} }), m_EslHdl4Send({ {0} })
  14. {
  15. }
  16. CEslGateway::~CEslGateway()
  17. {
  18. }
  19. bool CEslGateway::start(void)
  20. {
  21. CConfig *cfg = CConfig::GetInstance();
  22. LOG_INFO(("{EslGateway}: 处理客户端连接, ip = %s,port=%d,pwd=%s"), cfg->fsAddr().c_str(), cfg->fsPort(), cfg->fsPwd().c_str());
  23. esl_connect(&m_EslHdl4Listen, cfg->fsAddr().c_str(), cfg->fsPort(), NULL, cfg->fsPwd().c_str());
  24. if (!m_EslHdl4Listen.connected)
  25. return false;
  26. LOG_INFO_S(("{EslGateway}: 处理客户端连接m_EslHdl4Listen OK"));
  27. esl_events(&m_EslHdl4Listen, ESL_EVENT_TYPE_PLAIN,
  28. "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");
  29. LOG_INFO(("{EslGateway}: 处理客户端连接, ip = %s,port=%d,pwd=%s"), cfg->fsAddr().c_str(), cfg->fsPort(), cfg->fsPwd().c_str());
  30. esl_connect(&m_EslHdl4Send, cfg->fsAddr().c_str(), cfg->fsPort(), NULL, cfg->fsPwd().c_str());
  31. if (!m_EslHdl4Send.connected)
  32. {
  33. esl_disconnect(&m_EslHdl4Listen);
  34. return false;
  35. }
  36. m_Stop = false;
  37. std::thread(std::bind(&CEslGateway::__eventThread,this)).detach();
  38. return true;
  39. }
  40. void CEslGateway::stop(void)
  41. {
  42. if (!m_Stop)
  43. {
  44. m_Stop = true;
  45. esl_disconnect(&m_EslHdl4Listen);
  46. esl_disconnect(&m_EslHdl4Send);
  47. }
  48. }
  49. bool CEslGateway::hangupAll(void)
  50. {
  51. return esl_send_recv(&m_EslHdl4Send, ESL_CMD_HANGUPALL) == ESL_SUCCESS;
  52. }
  53. bool CEslGateway::scanExten(void)
  54. {
  55. if (esl_send_recv(&m_EslHdl4Send, ESL_CMD_SCAN_INTERNAL) != ESL_SUCCESS)
  56. return false;
  57. if (m_EslHdl4Send.last_sr_event == nullptr || m_EslHdl4Send.last_sr_event->body == nullptr)
  58. return false;
  59. std::string body = m_EslHdl4Send.last_sr_event->body;
  60. std::list<std::string> rows;
  61. boost::split(rows, body, boost::is_any_of("\n"));
  62. while (!rows.empty())
  63. {
  64. std::string var = rows.front();
  65. rows.pop_front();
  66. std::regex space("\t| ");
  67. var = std::regex_replace(var, space, "");
  68. std::regex reg("IP:(.*)");
  69. if (std::regex_match(var, reg))
  70. {
  71. std::string extenNo, extenIP;
  72. extenIP = std::regex_replace(var, reg, "$1");
  73. while (!rows.empty())
  74. {
  75. var = rows.front();
  76. rows.pop_front();
  77. std::regex space("\t| ");
  78. var = std::regex_replace(var, space, "");
  79. reg = std::regex("Auth-User:(.*)");
  80. if (std::regex_match(var, reg))
  81. {
  82. extenNo = std::regex_replace(var, reg, "$1");
  83. if (!extenNo.empty() && !extenIP.empty())
  84. {
  85. LOG_DEBUG("分机注册:%s %s", extenNo.c_str(), extenIP.c_str());
  86. if(m_pParent)
  87. {
  88. m_pParent->onEslExtenReg(atoi(extenNo.c_str()), extenIP);
  89. }
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. return true;
  97. }
  98. bool CEslGateway::getAgentState(std::string AgentID, AgentDetail &agent)
  99. {
  100. Format fmt("api callcenter_config agent list %1%");
  101. fmt %AgentID;
  102. if (esl_send_recv(&m_EslHdl4Send, fmt.str().c_str()) != ESL_SUCCESS)
  103. return false;
  104. if (m_EslHdl4Send.last_sr_event == nullptr || m_EslHdl4Send.last_sr_event->body == nullptr)
  105. return false;
  106. std::string body = m_EslHdl4Send.last_sr_event->body;
  107. std::vector<std::string> rows;
  108. boost::split(rows, body, boost::is_any_of("\n"));
  109. if (rows.size() < 3)
  110. return false;
  111. std::map<std::string, std::string> mKeyVal;
  112. std::list<std::string> heards,contents;
  113. std::string heard = std::move(rows[0]);
  114. std::string content = std::move(rows[1]);
  115. boost::split(heards, heard, boost::is_any_of("|"));
  116. boost::split(contents, content, boost::is_any_of("|"));
  117. if(heards.size()!=contents.size())
  118. return false;
  119. while (!heards.empty())
  120. {
  121. mKeyVal[std::move(heards.front())] = std::move(contents.front());
  122. heards.pop_front();
  123. contents.pop_front();
  124. }
  125. //AgentDetail agent;
  126. auto name = mKeyVal.find("name");
  127. if (name != mKeyVal.end())
  128. agent.name = name->second;
  129. auto system = mKeyVal.find("system");
  130. if (system != mKeyVal.end())
  131. agent.system = system->second;
  132. auto uuid = mKeyVal.find("uuid");
  133. if (uuid != mKeyVal.end())
  134. agent.uuid = uuid->second;
  135. auto type = mKeyVal.find("type");
  136. if (type != mKeyVal.end())
  137. agent.type = type->second;
  138. auto contact = mKeyVal.find("contact");
  139. if (contact != mKeyVal.end())
  140. agent.contact = contact->second;
  141. auto status = mKeyVal.find("status");
  142. if (status != mKeyVal.end())
  143. agent.status = status->second;
  144. auto state = mKeyVal.find("state");
  145. if (state != mKeyVal.end())
  146. agent.state = state->second;
  147. return true;
  148. }
  149. bool CEslGateway::getConferenceMemberId(std::string MeetingId, std::string ChanId, std::string & MemberId)
  150. {
  151. std::string cmd;
  152. cmd = boost::str(Format("api conference %s list") % MeetingId);
  153. if (esl_send_recv(&m_EslHdl4Send, cmd.c_str()) != ESL_SUCCESS)
  154. return false;
  155. if (m_EslHdl4Send.last_sr_event && m_EslHdl4Send.last_sr_event->body) {
  156. std::vector<std::string> vs;
  157. boost::split(vs, m_EslHdl4Send.last_sr_event->body, boost::algorithm::is_any_of("\n"));
  158. for (auto v : vs) {
  159. if (v.find(ChanId) != std::string::npos) {
  160. std::vector<std::string> des;
  161. boost::split(des, v, boost::algorithm::is_any_of(";"));
  162. if (!des.empty()) {
  163. MemberId = des[0].c_str();
  164. return true;
  165. }
  166. break;
  167. }
  168. }
  169. }
  170. return false;
  171. }
  172. bool CEslGateway::delAgentAll(void)
  173. {
  174. LOG_DEBUG("清空FreeSWITCH中已签入坐席");
  175. Format fmt("api callcenter_config agent list");
  176. if (esl_send_recv(&m_EslHdl4Send, fmt.str().c_str()) != ESL_SUCCESS)
  177. return false;
  178. if (m_EslHdl4Send.last_sr_event == nullptr || m_EslHdl4Send.last_sr_event->body == nullptr)
  179. return false;
  180. std::string body = m_EslHdl4Send.last_sr_event->body;
  181. std::list<std::string> rows;
  182. boost::split(rows, body, boost::is_any_of("\n"));
  183. if (rows.size() < 3)
  184. return false;
  185. rows.pop_front();
  186. rows.pop_back();
  187. rows.pop_back();
  188. for (std::string &var : rows)
  189. {
  190. std::list<std::string> contents;
  191. boost::split(contents, var, boost::is_any_of("|"));
  192. std::string name = contents.front();
  193. delAgent(name);
  194. }
  195. return false;
  196. }
  197. bool CEslGateway::delAgent(std::string AgentID)
  198. {
  199. bool ret = true;
  200. std::string cmd;
  201. cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
  202. ret &= sendCmd(cmd);
  203. cmd = boost::str(Format("bgapi callcenter_config tier del ZXZ %1% ") % AgentID);
  204. ret &= sendCmd(cmd);
  205. return ret;
  206. }
  207. bool CEslGateway::delAgent(std::string AgentID, std::list<std::string> Groups)
  208. {
  209. bool ret = true;
  210. std::string cmd;
  211. cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
  212. ret &= sendCmd(cmd);
  213. for (std::string &var : Groups)
  214. {
  215. cmd = boost::str(Format("bgapi callcenter_config tier del %2% %1% ") % AgentID %var);
  216. ret &= sendCmd(cmd);
  217. }
  218. return ret;
  219. }
  220. bool CEslGateway::sendCmd(string pCmd)
  221. {
  222. LOG_DEBUG_S(pCmd);
  223. esl_status_t Status = esl_send_recv(&m_EslHdl4Send, pCmd.c_str());
  224. return Status == ESL_SUCCESS;
  225. }
  226. bool CEslGateway::execute(esl_handle_t * pHandle, const char * App, const char * Param)
  227. {
  228. if (pHandle == nullptr) return false;
  229. esl_status_t Status = esl_execute(pHandle, App, Param, NULL);
  230. return Status == ESL_SUCCESS;
  231. }
  232. bool CEslGateway::Execte(const char * App, const char * Param, const char * ChanId)
  233. {
  234. if (App == nullptr) return false;
  235. esl_status_t Status = esl_execute(&m_EslHdl4Send, App, Param, ChanId);
  236. return Status == ESL_SUCCESS;
  237. }
  238. void CEslGateway::__eventThread(void)
  239. {
  240. esl_event_t* pEvent = NULL;
  241. esl_handle_t* pListenHandle = &m_EslHdl4Listen;
  242. // 下面的设置保证执行同时多个APP时,后面的不会抢占前面的
  243. pListenHandle->event_lock = 1;
  244. while (!m_Stop)
  245. {
  246. // esl_recv_event会阻塞线程,其第二个参数为1,表示优先检查内部缓存列表,防止遗漏事件。
  247. if (esl_recv_event(pListenHandle, 1, NULL) == ESL_SUCCESS)
  248. {
  249. pEvent = pListenHandle->last_ievent;
  250. if (pEvent != NULL)
  251. __onEslEvent(pEvent);
  252. }
  253. else
  254. {
  255. // 连接中断
  256. __onEslDisconnect();
  257. return;
  258. }
  259. }
  260. }
  261. void CEslGateway::__onEslEvent(esl_event_t * pEvent)
  262. {
  263. /*char *pContent = new char[2048];
  264. esl_event_serialize(pEvent, &pContent, ESL_TRUE);
  265. LOG_DEBUG_S(pContent);
  266. FS_LINK_DELETE(pContent);*/
  267. switch ((uint32_t)pEvent->event_id)
  268. {
  269. case ESL_EVENT_BACKGROUND_JOB:
  270. __onEslEvtBgJobDone(pEvent);
  271. break;
  272. case ESL_EVENT_CHANNEL_CREATE:
  273. case ESL_EVENT_CHANNEL_ANSWER:
  274. case ESL_EVENT_CHANNEL_BRIDGE:
  275. case ESL_EVENT_CHANNEL_PROGRESS:
  276. case ESL_EVENT_CHANNEL_PROGRESS_MEDIA:
  277. case ESL_EVENT_CHANNEL_HANGUP_COMPLETE:
  278. __onEslEvtChanEvent(pEvent);
  279. break;
  280. case ESL_EVENT_DTMF:
  281. __onEslEvtDtmf(pEvent);
  282. break;
  283. case ESL_EVENT_CHANNEL_HOLD:
  284. __onEslEvtHold(pEvent, EVENT_HOLD);
  285. break;
  286. case ESL_EVENT_CHANNEL_UNHOLD:
  287. __onEslEvtHold(pEvent, EVENT_UNHOLD);
  288. break;
  289. case ESL_EVENT_CUSTOM:
  290. __onEslEvtCustom(pEvent);
  291. break;
  292. }
  293. }
  294. void CEslGateway::__onEslDisconnect(void)
  295. {
  296. LOG_WARN_S("{EslGateway}: 监听通道事件时连接中断");
  297. m_Stop = true;
  298. m_pParent->onEslDisconnect();
  299. }
  300. void CEslGateway::__onEslEvtBgJobDone(esl_event_t * pEvent)
  301. {
  302. BG_JOB_NOTIFY Notify;
  303. memset(&Notify, '\0', sizeof(BG_JOB_NOTIFY));
  304. char* HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_JOB_UUID);
  305. if (HeaderValue != nullptr)
  306. Notify.JobInstance = atol(HeaderValue);
  307. LOG_DEBUG("JobCmd[%ld]-[%s]", Notify.JobInstance, HeaderValue);
  308. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_JOB_CMD);
  309. Notify.JobCmd = HeaderValue;
  310. HeaderValue = esl_event_get_body(pEvent);
  311. if (HeaderValue != nullptr)
  312. Notify.JobBody = HeaderValue;
  313. m_pParent->onEslEvtBgJobDone(&Notify);
  314. }
  315. void CEslGateway::__onEslEvtChanEvent(esl_event_t * pEvent)
  316. {
  317. CHAN_EVENT_NOTIFY Notify;
  318. char* HeaderValue = nullptr;
  319. // 获取事件ID
  320. Notify.EventId = (CHAN_EVENT_ID)pEvent->event_id;
  321. // 获取通道方向
  322. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DIRECTION);
  323. if (strcmp(HeaderValue, ESL_HDR_DIRECTION_INBOUND) == 0)
  324. Notify.Direction = CALL_DIRECTION_INBOUND;
  325. else
  326. Notify.Direction = CALL_DIRECTION_OUTBOUND;
  327. // 获取会话ID
  328. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
  329. Notify.CallId = HeaderValue;
  330. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CCID);
  331. if (HeaderValue != nullptr)
  332. Notify.CcId = HeaderValue;
  333. // 获取通道ID
  334. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
  335. Notify.ChanId = HeaderValue;
  336. // 获取主被叫
  337. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLER);
  338. if(HeaderValue !=nullptr)
  339. Notify.Caller = HeaderValue;
  340. else
  341. {
  342. Format fmt("主叫号码未获取到,EventId = %d ,CallId = %s,ChanId = %s,Direction = %d,CcId = %s ");
  343. fmt %Notify.EventId % Notify.CallId % Notify.CallId % Notify.Direction % Notify.CcId;
  344. LOG_WARN_S(fmt.str());
  345. }
  346. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLEE);
  347. if (HeaderValue != nullptr)
  348. {
  349. Notify.Callee = HeaderValue;
  350. regex reg("\\d+");
  351. if (!regex_match(HeaderValue, reg))
  352. {
  353. LOG_WARN("被叫号码为非存数字,软电话可能需要处理,分机号: %s", HeaderValue);
  354. HeaderValue = esl_event_get_header(pEvent, "variable_dialed_user"); //判断被叫号码是不是软电话,软电话获取的被叫不是真实注册的分机号,是随机字符串
  355. LOG_WARN("被叫号码为非存数字,软电话可能需要处理,分机号: %s", HeaderValue);
  356. }
  357. if(HeaderValue!=nullptr)
  358. Notify.Callee = HeaderValue;
  359. }
  360. else
  361. {
  362. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DEST_NUM);
  363. if (HeaderValue != nullptr)
  364. Notify.Callee = HeaderValue;
  365. }
  366. // 获取操作类型 区分自动外呼
  367. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_OP_TYPE);
  368. if (HeaderValue != nullptr)
  369. {
  370. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_OP_TYPE, HeaderValue);
  371. Notify.CallType = atoi(HeaderValue);
  372. }
  373. // 获取自动外呼任务id ,
  374. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_TASK_ID);
  375. if (HeaderValue != nullptr)
  376. {
  377. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_TASK_ID, HeaderValue);
  378. Notify.TaskId = atol(HeaderValue);
  379. }
  380. // 代码外呼,自动外呼时,主叫create事件时就得到正确的被叫
  381. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_MALL_CALL_CALLEE);
  382. if (HeaderValue != nullptr)
  383. {
  384. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_MALL_CALL_CALLEE, HeaderValue);
  385. Notify.Called = HeaderValue;
  386. }
  387. // 获取挂机原因
  388. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_HANGUP_CAUSE);
  389. if (HeaderValue != nullptr)
  390. Notify.HangupCause = HeaderValue;
  391. else
  392. {
  393. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALL_FAILED_CAUSE);
  394. if (HeaderValue != nullptr)
  395. Notify.HangupCause = HeaderValue;
  396. }
  397. // 获取通道操作标识
  398. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_OP_INSTANCE);
  399. if (HeaderValue != nullptr)
  400. {
  401. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_OP_INSTANCE,HeaderValue);
  402. Notify.ChanOpInstance = atol(HeaderValue);
  403. }
  404. 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");
  405. fmt %Notify.EventId % Notify.CallId % Notify.ChanId % Notify.Direction % Notify.Caller % Notify.Callee % Notify.CallType % Notify.ChanOpInstance %Notify.TaskId %Notify.Called;
  406. LOG_DEBUG(fmt.str());
  407. m_pParent->onEslEvtChannel(&Notify);
  408. }
  409. void CEslGateway::__onEslEvtCustom(esl_event_t * pEvent)
  410. {
  411. char* HeaderValue = nullptr;
  412. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_SUBCLASS);
  413. if (strcmp(HeaderValue, ESL_HDR_SUBCLASS_SIP_REG) == 0) // 分机注册事件
  414. {
  415. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_EXTEN_NO);
  416. uint32_t ExtenNo = 0;
  417. sscanf(HeaderValue, "%u", &ExtenNo);
  418. m_pParent->onEslExtenReg(ExtenNo,"");
  419. }
  420. else if (strcmp(HeaderValue, ESL_HDR_SUBCLASS_SIP_UNREG) == 0) // 分机注销事件
  421. {
  422. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_EXTEN_NO);
  423. uint32_t ExtenNo = 0;
  424. if (HeaderValue != NULL) // jssip软电话注册后,刷新页面得到的HeaderValue为空,导致sscanf_s失败
  425. {
  426. sscanf(HeaderValue, "%u", &ExtenNo);
  427. m_pParent->onEslExtenUnreg(ExtenNo);
  428. }
  429. }
  430. else if (strcmp(HeaderValue, "callcenter::info") == 0) {
  431. const char* queueName = esl_event_get_header(pEvent, "CC-Queue");
  432. const char* callsWaitingStr = esl_event_get_header(pEvent, "CC-Count");
  433. if (callsWaitingStr) {
  434. LOG_INFO("队列[%s]当前排队人数[%s]", queueName, callsWaitingStr);
  435. m_pParent->onCallcenterQueue(queueName, callsWaitingStr);
  436. }
  437. char *pContent = new char[2048];
  438. esl_event_serialize(pEvent, &pContent, ESL_TRUE);
  439. LOG_DEBUG_S(pContent);
  440. FS_LINK_DELETE(pContent);
  441. }
  442. }
  443. void CEslGateway::__onEslEvtDtmf(esl_event_t * pEvent)
  444. {
  445. DTMF_NOTIFY Notify;
  446. char* HeaderValue = NULL;
  447. // 获取会话ID
  448. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
  449. Notify.CallId = HeaderValue;
  450. // 获取通道ID
  451. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
  452. Notify.ChanId = HeaderValue;
  453. // 获取DTMF
  454. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DTMF);
  455. Notify.DTMF = HeaderValue;
  456. m_pParent->onEslEvtDtmf(&Notify);
  457. }
  458. void CEslGateway::__onEslEvtHold(esl_event_t * pEvent, EVENT_HOLD_TYPE EvtType)
  459. {
  460. HOLD_NOTIFY Notify;
  461. char* HeaderValue = NULL;
  462. // 获取会话ID
  463. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
  464. Notify.CallId = HeaderValue;
  465. // 获取通道ID
  466. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
  467. Notify.ChanId = HeaderValue;
  468. // 获取保持事件类型
  469. Notify.EvtType = EvtType;
  470. m_pParent->onEslEvtHold(&Notify);
  471. }