linux版本中间件

EslGateway.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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");
  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::delAgentAll(void)
  150. {
  151. LOG_DEBUG("清空FreeSWITCH中已签入坐席");
  152. Format fmt("api callcenter_config agent list");
  153. if (esl_send_recv(&m_EslHdl4Send, fmt.str().c_str()) != ESL_SUCCESS)
  154. return false;
  155. if (m_EslHdl4Send.last_sr_event == nullptr || m_EslHdl4Send.last_sr_event->body == nullptr)
  156. return false;
  157. std::string body = m_EslHdl4Send.last_sr_event->body;
  158. std::list<std::string> rows;
  159. boost::split(rows, body, boost::is_any_of("\n"));
  160. if (rows.size() < 3)
  161. return false;
  162. rows.pop_front();
  163. rows.pop_back();
  164. rows.pop_back();
  165. for (std::string &var : rows)
  166. {
  167. std::list<std::string> contents;
  168. boost::split(contents, var, boost::is_any_of("|"));
  169. std::string name = contents.front();
  170. delAgent(name);
  171. }
  172. return false;
  173. }
  174. bool CEslGateway::delAgent(std::string AgentID)
  175. {
  176. bool ret = true;
  177. std::string cmd;
  178. cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
  179. ret &= sendCmd(cmd);
  180. cmd = boost::str(Format("bgapi callcenter_config tier del support@default %1% ") % AgentID);
  181. ret &= sendCmd(cmd);
  182. return ret;
  183. }
  184. bool CEslGateway::delAgent(std::string AgentID, std::list<std::string> Groups)
  185. {
  186. bool ret = true;
  187. std::string cmd;
  188. cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
  189. ret &= sendCmd(cmd);
  190. for (std::string &var : Groups)
  191. {
  192. cmd = boost::str(Format("bgapi callcenter_config tier del %2% %1% ") % AgentID %var);
  193. ret &= sendCmd(cmd);
  194. }
  195. return ret;
  196. }
  197. bool CEslGateway::sendCmd(string pCmd)
  198. {
  199. LOG_DEBUG_S(pCmd);
  200. esl_status_t Status = esl_send_recv(&m_EslHdl4Send, pCmd.c_str());
  201. return Status == ESL_SUCCESS;
  202. }
  203. bool CEslGateway::execute(esl_handle_t * pHandle, const char * App, const char * Param)
  204. {
  205. if (pHandle == nullptr) return false;
  206. esl_status_t Status = esl_execute(pHandle, App, Param, NULL);
  207. return Status == ESL_SUCCESS;
  208. }
  209. bool CEslGateway::Execte(const char * App, const char * Param, const char * ChanId)
  210. {
  211. if (App == nullptr) return false;
  212. esl_status_t Status = esl_execute(&m_EslHdl4Send, App, Param, ChanId);
  213. return Status == ESL_SUCCESS;
  214. }
  215. void CEslGateway::__eventThread(void)
  216. {
  217. esl_event_t* pEvent = NULL;
  218. esl_handle_t* pListenHandle = &m_EslHdl4Listen;
  219. // 下面的设置保证执行同时多个APP时,后面的不会抢占前面的
  220. pListenHandle->event_lock = 1;
  221. while (!m_Stop)
  222. {
  223. // esl_recv_event会阻塞线程,其第二个参数为1,表示优先检查内部缓存列表,防止遗漏事件。
  224. if (esl_recv_event(pListenHandle, 1, NULL) == ESL_SUCCESS)
  225. {
  226. pEvent = pListenHandle->last_ievent;
  227. if (pEvent != NULL)
  228. __onEslEvent(pEvent);
  229. }
  230. else
  231. {
  232. // 连接中断
  233. __onEslDisconnect();
  234. return;
  235. }
  236. }
  237. }
  238. void CEslGateway::__onEslEvent(esl_event_t * pEvent)
  239. {
  240. /*char *pContent = new char[2048];
  241. esl_event_serialize(pEvent, &pContent, ESL_TRUE);
  242. LOG_DEBUG_S(pContent);
  243. FS_LINK_DELETE(pContent);*/
  244. switch ((uint32_t)pEvent->event_id)
  245. {
  246. case ESL_EVENT_BACKGROUND_JOB:
  247. __onEslEvtBgJobDone(pEvent);
  248. break;
  249. case ESL_EVENT_CHANNEL_CREATE:
  250. case ESL_EVENT_CHANNEL_ANSWER:
  251. case ESL_EVENT_CHANNEL_BRIDGE:
  252. case ESL_EVENT_CHANNEL_PROGRESS:
  253. case ESL_EVENT_CHANNEL_PROGRESS_MEDIA:
  254. case ESL_EVENT_CHANNEL_HANGUP_COMPLETE:
  255. __onEslEvtChanEvent(pEvent);
  256. break;
  257. case ESL_EVENT_DTMF:
  258. __onEslEvtDtmf(pEvent);
  259. break;
  260. case ESL_EVENT_CHANNEL_HOLD:
  261. __onEslEvtHold(pEvent, EVENT_HOLD);
  262. break;
  263. case ESL_EVENT_CHANNEL_UNHOLD:
  264. __onEslEvtHold(pEvent, EVENT_UNHOLD);
  265. break;
  266. case ESL_EVENT_CUSTOM:
  267. __onEslEvtCustom(pEvent);
  268. break;
  269. }
  270. }
  271. void CEslGateway::__onEslDisconnect(void)
  272. {
  273. LOG_WARN_S("{EslGateway}: 监听通道事件时连接中断");
  274. m_Stop = true;
  275. m_pParent->onEslDisconnect();
  276. }
  277. void CEslGateway::__onEslEvtBgJobDone(esl_event_t * pEvent)
  278. {
  279. //try
  280. {
  281. BG_JOB_NOTIFY Notify;
  282. memset(&Notify, '\0', sizeof(BG_JOB_NOTIFY));
  283. char* HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_JOB_UUID);
  284. if (HeaderValue != nullptr)
  285. Notify.JobInstance = atol(HeaderValue);
  286. LOG_DEBUG("JobCmd[%ld]-[%s]", Notify.JobInstance, HeaderValue);
  287. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_JOB_CMD);
  288. Notify.JobCmd = HeaderValue;
  289. HeaderValue = esl_event_get_body(pEvent);
  290. if (HeaderValue != nullptr)
  291. Notify.JobBody = HeaderValue;
  292. m_pParent->onEslEvtBgJobDone(&Notify);
  293. }
  294. //catch (const std::exception& e)
  295. {
  296. // LOG_ERROR_S(e.what());
  297. }
  298. }
  299. void CEslGateway::__onEslEvtChanEvent(esl_event_t * pEvent)
  300. {
  301. CHAN_EVENT_NOTIFY Notify;
  302. char* HeaderValue = nullptr;
  303. // 获取事件ID
  304. Notify.EventId = (CHAN_EVENT_ID)pEvent->event_id;
  305. // 获取通道方向
  306. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DIRECTION);
  307. if (strcmp(HeaderValue, ESL_HDR_DIRECTION_INBOUND) == 0)
  308. Notify.Direction = CALL_DIRECTION_INBOUND;
  309. else
  310. Notify.Direction = CALL_DIRECTION_OUTBOUND;
  311. // 获取会话ID
  312. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
  313. Notify.CallId = HeaderValue;
  314. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CCID);
  315. if (HeaderValue != nullptr)
  316. Notify.CcId = HeaderValue;
  317. // 获取通道ID
  318. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
  319. Notify.ChanId = HeaderValue;
  320. // 获取主被叫
  321. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLER);
  322. if(HeaderValue !=nullptr)
  323. Notify.Caller = HeaderValue;
  324. else
  325. {
  326. Format fmt("主叫号码未获取到,EventId = %d ,CallId = %s,ChanId = %s,Direction = %d,CcId = %s ");
  327. fmt %Notify.EventId % Notify.CallId % Notify.CallId % Notify.Direction % Notify.CcId;
  328. LOG_WARN_S(fmt.str());
  329. }
  330. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLEE);
  331. if (HeaderValue != nullptr)
  332. {
  333. regex reg("\\d+");
  334. if (!regex_match(HeaderValue, reg))
  335. {
  336. LOG_WARN("被叫号码为非存数字,软电话可能需要处理,分机号: %s", HeaderValue);
  337. HeaderValue = esl_event_get_header(pEvent, "variable_dialed_user"); //判断被叫号码是不是软电话,软电话获取的被叫不是真实注册的分机号,是随机字符串
  338. LOG_WARN("被叫号码为非存数字,软电话可能需要处理,分机号: %s", HeaderValue);
  339. }
  340. Notify.Callee = HeaderValue;
  341. }
  342. else
  343. {
  344. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DEST_NUM);
  345. if (HeaderValue != nullptr)
  346. Notify.Callee = HeaderValue;
  347. }
  348. // 获取操作类型 区分自动外呼
  349. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_OP_TYPE);
  350. if (HeaderValue != nullptr)
  351. {
  352. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_OP_TYPE, HeaderValue);
  353. Notify.CallType = atoi(HeaderValue);
  354. }
  355. // 获取自动外呼任务id ,
  356. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_TASK_ID);
  357. if (HeaderValue != nullptr)
  358. {
  359. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_TASK_ID, HeaderValue);
  360. Notify.TaskId = atol(HeaderValue);
  361. }
  362. // 代码外呼,自动外呼时,主叫create事件时就得到正确的被叫
  363. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_MALL_CALL_CALLEE);
  364. if (HeaderValue != nullptr)
  365. {
  366. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_MALL_CALL_CALLEE, HeaderValue);
  367. Notify.Called = HeaderValue;
  368. }
  369. // 获取挂机原因
  370. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_HANGUP_CAUSE);
  371. if (HeaderValue != nullptr)
  372. Notify.HangupCause = HeaderValue;
  373. else
  374. {
  375. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALL_FAILED_CAUSE);
  376. if (HeaderValue != nullptr)
  377. Notify.HangupCause = HeaderValue;
  378. }
  379. // 获取通道操作标识
  380. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHAN_OP_INSTANCE);
  381. if (HeaderValue != nullptr)
  382. {
  383. LOG_DEBUG("%s=%s", ESL_HEADER_CHAN_OP_INSTANCE,HeaderValue);
  384. Notify.ChanOpInstance = atol(HeaderValue);
  385. }
  386. 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");
  387. fmt %Notify.EventId % Notify.CallId % Notify.ChanId % Notify.Direction % Notify.Caller % Notify.Callee % Notify.CallType % Notify.ChanOpInstance %Notify.TaskId %Notify.Called;
  388. LOG_DEBUG(fmt.str());
  389. m_pParent->onEslEvtChannel(&Notify);
  390. }
  391. void CEslGateway::__onEslEvtCustom(esl_event_t * pEvent)
  392. {
  393. char* HeaderValue = nullptr;
  394. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_SUBCLASS);
  395. if (strcmp(HeaderValue, ESL_HDR_SUBCLASS_SIP_REG) == 0) // 分机注册事件
  396. {
  397. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_EXTEN_NO);
  398. uint32_t ExtenNo = 0;
  399. sscanf(HeaderValue, "%u", &ExtenNo);
  400. m_pParent->onEslExtenReg(ExtenNo,"");
  401. }
  402. else if (strcmp(HeaderValue, ESL_HDR_SUBCLASS_SIP_UNREG) == 0) // 分机注销事件
  403. {
  404. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_EXTEN_NO);
  405. uint32_t ExtenNo = 0;
  406. if (HeaderValue != NULL) // jssip软电话注册后,刷新页面得到的HeaderValue为空,导致sscanf_s失败
  407. {
  408. sscanf(HeaderValue, "%u", &ExtenNo);
  409. m_pParent->onEslExtenUnreg(ExtenNo);
  410. }
  411. }
  412. }
  413. void CEslGateway::__onEslEvtDtmf(esl_event_t * pEvent)
  414. {
  415. DTMF_NOTIFY Notify;
  416. char* HeaderValue = NULL;
  417. // 获取会话ID
  418. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
  419. Notify.CallId = HeaderValue;
  420. // 获取通道ID
  421. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
  422. Notify.ChanId = HeaderValue;
  423. // 获取DTMF
  424. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_DTMF);
  425. Notify.DTMF = HeaderValue;
  426. m_pParent->onEslEvtDtmf(&Notify);
  427. }
  428. void CEslGateway::__onEslEvtHold(esl_event_t * pEvent, EVENT_HOLD_TYPE EvtType)
  429. {
  430. HOLD_NOTIFY Notify;
  431. char* HeaderValue = NULL;
  432. // 获取会话ID
  433. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CALLID);
  434. Notify.CallId = HeaderValue;
  435. // 获取通道ID
  436. HeaderValue = esl_event_get_header(pEvent, ESL_HEADER_CHANID);
  437. Notify.ChanId = HeaderValue;
  438. // 获取保持事件类型
  439. Notify.EvtType = EvtType;
  440. m_pParent->onEslEvtHold(&Notify);
  441. }