linux版本中间件

FsProxy.cpp 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. #include "FsProxy.h"
  2. #include "EslGateway.h"
  3. #include "Config.h"
  4. #include "ChanTrunk.h"
  5. #include "ChanExten.h"
  6. #include "Session.h"
  7. #include "Config.h"
  8. #include "OperationReactor.h"
  9. #include "JsonStringMaker.h"
  10. #include <string>
  11. #include <string.h>
  12. #include "SoftAuth.h"
  13. CFsProxy::CFsProxy() : m_Gateway(this)
  14. {
  15. }
  16. CFsProxy::~CFsProxy()
  17. {
  18. release();
  19. }
  20. bool CFsProxy::init()
  21. {
  22. CConfig *cfg = CConfig::GetInstance();
  23. tts.Init(cfg->ttsPath()); // 初始化tts路径
  24. if (!JdbcHelper::GetInstance()->init(cfg->dbAddr(), cfg->dbUser(), cfg->dbPwd(), cfg->dbDatabase()))
  25. {
  26. LOG_ERROR("程序运行失败,数据库驱动加载失败");
  27. return false;
  28. }
  29. if (JdbcHelper::GetInstance()->jdbc_connect(false))
  30. {
  31. LOG_ERROR("程序运行失败,数据库连接失败");
  32. return false;
  33. }
  34. LOG_INFO("数据库连接成功。");
  35. //std::string sql = "update agent set exten = ''";
  36. std::string sql = "delete from agent ";
  37. JdbcHelper::GetInstance()->jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
  38. }, [sql](sql::SQLException &e) {
  39. Format fmt("Sql执行失败,错误信息:[%s],Sql[%s]");
  40. fmt % e.what() % sql;
  41. LOG_ERROR(fmt.str().c_str());
  42. });
  43. if (!m_Server.init(cfg->wsPort()))
  44. {
  45. LOG_ERROR("程序运行失败,请检查端口[%d]是否已被占用", cfg->wsPort());
  46. return false;
  47. }
  48. LOG_INFO("websocket服务打开成功,占用端口[%d]", cfg->wsPort());
  49. m_Server.setCallbackRecvMsg(std::bind(&CFsProxy::__recvMsgFun, this, std::placeholders::_1, std::placeholders::_2));
  50. m_Server.setCallbackClose(std::bind(&CFsProxy::__closeFun, this, std::placeholders::_1));
  51. m_AutoCall.setDetalTaskFun(std::bind(&CFsProxy::__doAutoTask,this, std::placeholders::_1));
  52. m_AutoCall.startTask();
  53. return __init();
  54. }
  55. void CFsProxy::release(void)
  56. {
  57. m_Server.stop();
  58. m_AutoCall.stopTask();
  59. m_Gateway.delAgentAll();
  60. m_Gateway.stop();
  61. __freeAgent();
  62. __freeSession();
  63. __freeExten();
  64. __freeTrunkChan();
  65. JdbcHelper::GetInstance()->jdbc_close_connect();
  66. }
  67. void CFsProxy::run(void)
  68. {
  69. m_Server.run();
  70. }
  71. /*****************************************************************
  72. **【函数名称】 onChanRegist
  73. **【函数功能】 通道注册的处理函数
  74. **【参数】
  75. **【返回值】
  76. ****************************************************************/
  77. void CFsProxy::onChanRegist(DEV_RES_TYPE ChanType, uint32_t ChanNo, CHAN_LOGIC_STATE ChanState)
  78. {
  79. Format fmt("通道注册: ChanType = %d,ChanNo = %d,ChanState = %d");
  80. fmt % ChanType % ChanNo % ChanState;
  81. LOG_INFO_S(fmt);
  82. }
  83. /*****************************************************************
  84. **【函数名称】 onExtenDestroy
  85. **【函数功能】 分机销毁的处理函数
  86. **【参数】
  87. **【返回值】
  88. ****************************************************************/
  89. void CFsProxy::onExtenDestroy(uint32_t ExtenNo)
  90. {
  91. }
  92. /*****************************************************************
  93. **【函数名称】 onExtenDirectOp
  94. **【函数功能】 分机直接操作启动
  95. **【参数】 OpType 启动的操作类型
  96. pHostChan 触发事件的通道
  97. pNotify 触发操作事件内容
  98. **【返回值】
  99. ****************************************************************/
  100. void CFsProxy::onExtenDirectOp(DEV_OP OpType, VirtualChan * pHostChan, PCHAN_EVENT_NOTIFY pNotify)
  101. {
  102. switch ((int)OpType)
  103. {
  104. case DEV_OP_CALL_OUT:
  105. {
  106. if (!COperationReactor::GetInstance()->onExtenCallFromDev(pHostChan, pNotify))
  107. __kill(FS_LINK_JOBID_INVALID, pHostChan->chanId());
  108. }
  109. break;
  110. } // end switch
  111. }
  112. /*****************************************************************
  113. **【函数名称】 onChanStateUpdate
  114. **【函数功能】 通道状态更新处理
  115. **【参数】
  116. **【返回值】
  117. *****************************************************************/
  118. void CFsProxy::onChanStateUpdate(long OpInstance, VirtualChan * pChan)
  119. {
  120. if (OpInstance != FS_LINK_INSTANCE_INVALID && pChan != nullptr)
  121. COperationReactor::GetInstance()->onEslEvtChanState(OpInstance, pChan);
  122. if (pChan == nullptr)
  123. return;
  124. // 发送通道/线路状态到坐席
  125. DEV_RES_TYPE devType = pChan->type(); // 通道类型
  126. std::string chanNo = std::to_string(pChan->no()); // 通道号 中继号或分机号
  127. //CHAN_LOGIC_STATE
  128. uint32_t chanState = pChan->state(); // 通道状态
  129. if ((pChan->state() & HELD_STATE_IND_MASK) == INNER_STATE_HELD)
  130. {
  131. chanState = (pChan->state() & HELD_STATE_IND_MASK);
  132. }
  133. std::string lineType = "未知";
  134. if (devType == DEV_RES_TYPE_EXT)
  135. {
  136. lineType = std::string("内线");
  137. }
  138. else if (devType == DEV_RES_TYPE_TRUNK)
  139. {
  140. lineType = std::string("外线");
  141. }
  142. std::string state;
  143. switch (chanState)
  144. {
  145. case CHAN_LOGIC_STATE_DISABLED: // 不可用
  146. state = "不可用";
  147. break;
  148. case CHAN_LOGIC_STATE_FREE: // 空闲
  149. state = "空闲";
  150. break;
  151. case CHAN_LOGIC_STATE_INIT: // 摘机等待拨号
  152. state = "摘机等待拨号";
  153. break;
  154. case CHAN_LOGIC_STATE_DIALING: // 拨号
  155. state = "拨号";
  156. break;
  157. case CHAN_LOGIC_STATE_RING_BACK: // 呼出振铃
  158. state = "呼出振铃";
  159. break;
  160. case CHAN_LOGIC_STATE_ALERTING: // 来电振铃
  161. state = "来电振铃";
  162. break;
  163. case CHAN_LOGIC_STATE_TALKING: // 通话中
  164. state = "通话中";
  165. break;
  166. case CHAN_LOGIC_STATE_STANDBY: // 中继通道可响应APP(此状态仅用作DevLink侧,不可传至CTI)
  167. break;
  168. case CHAN_LOGIC_STATE_HELD: // 通话保持中(保持状态的使用须与原通道状态按位或和与)
  169. state = "通话保持中";
  170. break;
  171. }
  172. Format fmt("lineType = %s[%s],lineState = %s,chanState = %d");
  173. fmt % lineType % chanNo % state % chanState;
  174. LOG_DEBUG_S(fmt.str())
  175. if (devType == DEV_RES_TYPE_EXT)
  176. {
  177. map<std::string, Agent*>::iterator it;
  178. {
  179. std::unique_lock<std::mutex>lock(m_AgentLock);
  180. it = m_MapAgent.find(chanNo);
  181. }
  182. if (it != m_MapAgent.end())
  183. {
  184. if (it->second == nullptr)
  185. {
  186. LOG_DEBUG("查不到分机[%s]", chanNo.c_str());
  187. }
  188. else
  189. {
  190. LOG_DEBUG("查到分机[%s]", chanNo.c_str());
  191. }
  192. if (chanState == CHAN_LOGIC_STATE_FREE && !it->second->isRepose())
  193. it->second->setState(AGENT_STATE_FREE);
  194. else if (chanState == CHAN_LOGIC_STATE_TALKING)
  195. it->second->setState(AGENT_STATE_BUSY);
  196. else if(chanState == CHAN_LOGIC_STATE_RING_BACK || chanState == CHAN_LOGIC_STATE_ALERTING)
  197. it->second->setState(AGENT_STATE_REQUESTED);
  198. if (it->second->isRepose())
  199. {
  200. state = "小休";
  201. }
  202. if (!state.empty()) // 过滤掉保持状态
  203. {
  204. std::string data = creatJson("LineState", state);
  205. m_Server.sendMsg(it->second->hdl(), data);
  206. data = creatJson("Monitor", state, it->second->id());
  207. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  208. }
  209. // 坐席空闲且没有置忙 执行外呼
  210. if (it->second->state() == AGENT_STATE_FREE && !it->second->isRepose())
  211. {
  212. LOG_DEBUG_S("进自动外呼等待");
  213. m_AutoCall.waitTask(it->second->id());
  214. LOG_DEBUG_S("出自动外呼等待");
  215. }
  216. }
  217. else
  218. {
  219. LOG_WARN("线路状态通知失败,找不到分机[%s]对应的坐席", chanNo.c_str());
  220. }
  221. }
  222. }
  223. void CFsProxy::onChanPoor(Session * pSession, PCHAN_EVENT_NOTIFY pNotify)
  224. {
  225. LOG_INFO_S(boost::str(Format("空闲通道枯竭,主动挂断,ChanId = %s ,caller = %s") % pNotify->ChanId % pNotify->Caller).c_str());
  226. __kill(FS_LINK_JOBID_INVALID, pNotify->ChanId);
  227. }
  228. void CFsProxy::onEslDisconnect(void)
  229. {
  230. // esl 断开重连
  231. m_Gateway.stop();
  232. __freeSession();
  233. __freeExten();
  234. __freeTrunkChan();
  235. LOG_WARN_S("ESL断开重新初始化...");
  236. while (!__init())
  237. {
  238. LOG_WARN_S("ESL重连中...")
  239. this_thread::sleep_for(std::chrono::seconds(5));
  240. }
  241. LOG_INFO_S("ESL重新初始化成功...");
  242. }
  243. void CFsProxy::onEslExtenReg(uint32_t ExtenNo, string ExtenIp)
  244. {
  245. ChanExten* pExten = getExten(ExtenNo);
  246. if (pExten == nullptr)
  247. {
  248. __addExten(ExtenNo);
  249. }
  250. else
  251. {
  252. if (pExten->isVoid()) // 若被丢弃,则改变丢弃状态
  253. pExten->discard(false);
  254. }
  255. }
  256. void CFsProxy::onEslExtenUnreg(uint32_t ExtenNo)
  257. {
  258. std::string agentId;
  259. {
  260. std::unique_lock<std::mutex>lock(m_AgentLock);
  261. auto it = m_MapAgent.find(std::to_string(ExtenNo));
  262. if (it != m_MapAgent.end())
  263. {
  264. agentId = it->second->id();
  265. it->second->removeAgent("分机掉线");
  266. //m_Gateway.delAgent(it->second->id()); // callcenter模块中删除
  267. m_Gateway.delAgent(it->second->id(), it->second->groups());
  268. m_Server.sendMsg(it->second->hdl(), creatJson("Logout", "分机掉线"));
  269. m_MapAgent.erase(it); // 从签入列表中删除
  270. }
  271. }
  272. if (!agentId.empty())
  273. {
  274. std::string data = creatJson("Monitor", "签出", agentId);
  275. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  276. }
  277. __delExten(ExtenNo);
  278. }
  279. void CFsProxy::onEslEvtBgJobDone(PBG_JOB_NOTIFY pNotify)
  280. {
  281. COperationReactor::GetInstance()->onEslEvtBgJobDone(pNotify);
  282. }
  283. void CFsProxy::onEslEvtChannel(PCHAN_EVENT_NOTIFY pNotify)
  284. {
  285. Session* pSession = __getSession(pNotify);
  286. if (pSession == nullptr)
  287. {
  288. Format fmt("{FsProxy}: 通道事件,没有找到会话,EslEventId=%d,ChanId[%s],CallId[%s],Caller[%s],Callee[%s],CcId[%s]");
  289. fmt % pNotify->EventId % pNotify->ChanId % pNotify->CallId % pNotify->Caller % pNotify->Callee % pNotify->CcId;
  290. LOG_WARN_S(fmt.str());
  291. return;
  292. }
  293. pSession->onChanEvent(pNotify);
  294. if (pSession->isVoid())
  295. __delSession(pSession->id());
  296. }
  297. void CFsProxy::onEslEvtDtmf(PDTMF_NOTIFY pNotify)
  298. {
  299. }
  300. void CFsProxy::onEslEvtHold(PHOLD_NOTIFY pNotify)
  301. {
  302. if (pNotify == nullptr) return;
  303. Session* pSession = __getSession(pNotify->CallId);
  304. if (pSession != nullptr)
  305. pSession->onChanHold(pNotify);
  306. }
  307. bool CFsProxy::send2Agent(std::string ExtenNo, std::string Data)
  308. {
  309. std::unique_lock<std::mutex>lock(m_AgentLock);
  310. auto it = m_MapAgent.find(ExtenNo);
  311. if (it != m_MapAgent.end())
  312. {
  313. if (m_Server.sendMsg(it->second->hdl(), Data))
  314. {
  315. LOG_DEBUG("成功发送到坐席[%s]-分机[%s],msg[%s]", it->second->id().c_str(), ExtenNo.c_str(), Data.c_str());
  316. return true;
  317. }
  318. }
  319. Format fmt("坐席通知失败,找不到分机对应的坐席,分机[%s],msg[%s]");
  320. fmt % ExtenNo % Data;
  321. LOG_WARN_S(fmt.str());
  322. return false;
  323. }
  324. ChanExten * CFsProxy::getExten(uint32_t ExtenNo)
  325. {
  326. auto it = m_MapChanExt.find(ExtenNo);
  327. if (it != m_MapChanExt.end()) return it->second;
  328. return nullptr;
  329. }
  330. ChanTrunk * CFsProxy::getTrunk(uint32_t TrunkNo)
  331. {
  332. if (TrunkNo <= 0 || TrunkNo > m_ArrayTrunk.size())
  333. return nullptr;
  334. return m_ArrayTrunk[--TrunkNo];
  335. }
  336. void CFsProxy::delChan(VirtualChan * pChan)
  337. {
  338. if (pChan->type() == DEV_RES_TYPE_EXT)
  339. __delExten(pChan->no());
  340. }
  341. ChanTrunk * CFsProxy::getFreeTrunk(void)
  342. {
  343. for (size_t i = 0; i < m_ArrayTrunk.size(); ++i)
  344. {
  345. ChanTrunk* pTrunk = m_ArrayTrunk[i];
  346. if (pTrunk != nullptr)
  347. {
  348. if (pTrunk->isFree())
  349. {
  350. return pTrunk;
  351. }
  352. }
  353. }
  354. LOG_INFO_S("没有空闲中继通道");
  355. return nullptr;
  356. }
  357. VirtualChan * CFsProxy::getAssoChanInSession(VirtualChan * pChan)
  358. {
  359. Session* pSession = __getSession(pChan->sessionId());
  360. if (pSession != nullptr)
  361. return pSession->getAssoChan(pChan);
  362. else
  363. return nullptr;
  364. }
  365. VirtualChan * CFsProxy::getBusyChan(string ChanId)
  366. {
  367. auto it = m_MapBusyChan.find(ChanId);
  368. if (it != m_MapBusyChan.end())
  369. return it->second;
  370. return nullptr;
  371. }
  372. void CFsProxy::regBusyChan(VirtualChan * pChan)
  373. {
  374. m_MapBusyChan[pChan->chanId()] = pChan;
  375. }
  376. void CFsProxy::unregBusyChan(VirtualChan * pChan)
  377. {
  378. m_MapBusyChan.erase(pChan->chanId());
  379. }
  380. std::string CFsProxy::getAgentByExten(std::string ExtenNo)
  381. {
  382. std::unique_lock<std::mutex>lock(m_AgentLock);
  383. auto it = m_MapAgent.find(ExtenNo);
  384. if (it->second == nullptr) return "";
  385. return it->second->id();
  386. }
  387. std::string CFsProxy::getAgentByExten(std::string ExtenNo, std::string &Groups)
  388. {
  389. std::unique_lock<std::mutex>lock(m_AgentLock);
  390. auto it = m_MapAgent.find(ExtenNo);
  391. if (it->second == nullptr) return "";
  392. Groups = it->second->group();
  393. return it->second->id();
  394. }
  395. std::string CFsProxy::getExtenByAgent(std::string AgentId)
  396. {
  397. std::unique_lock<std::mutex>lock(m_AgentLock);
  398. auto it = m_MapAgent.begin();
  399. while (it != m_MapAgent.end())
  400. {
  401. if (it->second->id() == AgentId)
  402. return it->first;
  403. ++it;
  404. }
  405. return "";
  406. }
  407. bool CFsProxy::ExtenCall(long JobId, VirtualChan * pChan, std::string CallerNum, std::string CalleeNum)
  408. {
  409. /*Format fmt("bgapi originate {origination_caller_id_number=%lu }user/%lu %s XML %s\r\n%s: %ld");
  410. fmt % pChan->no() % pChan->no() % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;*/
  411. std::string called;
  412. if (getExten(atoi(CalleeNum.c_str())) == nullptr)
  413. {
  414. std::string prefix = CConfig::GetInstance()->gateWayPrefix();
  415. called = CalleeNum.substr(prefix.length(), CalleeNum.length()- prefix.length());
  416. }
  417. else
  418. {
  419. called = CalleeNum;
  420. }
  421. Format fmt("bgapi originate {origination_caller_id_number=%lu,%s=%s}user/%lu %s XML %s\r\n%s: %ld");
  422. fmt % pChan->no() % ESL_VAR_MALL_CALL_CALLEE %called % pChan->no() % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  423. return m_Gateway.sendCmd(fmt.str());
  424. }
  425. bool CFsProxy::PredictionCall(long JobId, std::string CallerNum, std::string CalleeNum)
  426. {
  427. std::string CallString;
  428. if (!m_CallStringMaker.makeCallString(CallerNum, CalleeNum, CallString))
  429. return false;
  430. std::string EslCmd;
  431. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld")
  432. %CallerNum % ESL_VAR_OP_INSTANCE % JobId % CallString % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId);
  433. return m_Gateway.sendCmd(EslCmd);
  434. }
  435. bool CFsProxy::AutoCall(long JobId, std::string CallerNum, std::string CalleeNum, int OpType, long TaskId)
  436. {
  437. std::string CallString;
  438. std::string Caller;
  439. if (!m_CallStringMaker.makeCallString(Caller, CallerNum, CallString))
  440. return false;
  441. std::string EslCmd;
  442. /*EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld,%s=%ld}%s %s XML %s\r\n%s: %ld")
  443. % CallerNum % ESL_VAR_OP_TYPE % OpType % ESL_VAR_TASK_ID % TaskId % CallString % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId);*/
  444. std::string called;
  445. if (getExten(atoi(CalleeNum.c_str())) == nullptr)
  446. {
  447. std::string prefix = CConfig::GetInstance()->gateWayPrefix();
  448. called = CalleeNum.substr(prefix.length(), CalleeNum.length() - prefix.length());
  449. }
  450. else
  451. {
  452. called = CalleeNum;
  453. }
  454. 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")
  455. % 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);
  456. return m_Gateway.sendCmd(EslCmd);
  457. }
  458. bool CFsProxy::kill(long JobId, VirtualChan * pChan)
  459. {
  460. return __kill(JobId, pChan->chanId());
  461. }
  462. /*****************************************************************
  463. **【函数名称】 consult
  464. **【函数功能】 协商呼叫
  465. **【参数】
  466. **【返回值】
  467. ****************************************************************/
  468. bool CFsProxy::consult(long JobId, VirtualChan * pChan, std::string DestNum)
  469. {
  470. std::string CallerNum;
  471. std::string CallString;
  472. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
  473. return false;
  474. std::string EslCmd;
  475. EslCmd = boost::str(Format("bgapi uuid_broadcast %s att_xfer::%s\r\n%s: %ld") % pChan->chanId() % CallString % ESL_HEADER_JOB_UUID % JobId);
  476. return m_Gateway.sendCmd(EslCmd);
  477. }
  478. /*****************************************************************
  479. **【函数名称】 insert
  480. **【函数功能】 强插
  481. **【参数】
  482. **【返回值】
  483. ****************************************************************/
  484. bool CFsProxy::insert(long JobId, VirtualChan * pChan, std::string DestSessionId)
  485. {
  486. std::string EslCmd;
  487. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &three_way(%s)\r\n%s: %ld") %
  488. pChan->no() % pChan->no() % DestSessionId% ESL_HEADER_JOB_UUID% JobId);
  489. return m_Gateway.sendCmd(EslCmd);
  490. }
  491. /*****************************************************************
  492. **【函数名称】 intercept
  493. **【函数功能】 强截
  494. **【参数】
  495. **【返回值】
  496. ****************************************************************/
  497. bool CFsProxy::intercept(long JobId, VirtualChan * pChan, std::string DestChanId)
  498. {
  499. std::string EslCmd;
  500. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &intercept(%s)\r\n%s: %ld") %
  501. pChan->no() % pChan->no() % DestChanId % ESL_HEADER_JOB_UUID % JobId);
  502. return m_Gateway.sendCmd(EslCmd);
  503. }
  504. /*****************************************************************
  505. **【函数名称】 listen
  506. **【函数功能】 监听
  507. **【参数】
  508. **【返回值】
  509. ****************************************************************/
  510. bool CFsProxy::listen(long JobId, VirtualChan * pChan, std::string DestChanId)
  511. {
  512. std::string EslCmd;
  513. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &eavesdrop(%s)\r\n%s: %ld") %
  514. pChan->no() % pChan->no() % DestChanId% ESL_HEADER_JOB_UUID% JobId);
  515. return m_Gateway.sendCmd(EslCmd);
  516. }
  517. bool CFsProxy::playAgentNo(VirtualChan * pChan)
  518. {
  519. std::string filePath;
  520. std::string welcomeWords;
  521. /*Format fmt("您好, %s 号话务员为您服务.");
  522. fmt % pChan->no();
  523. if (!tts.TextToAudio(fmt.str(), filePath))*/
  524. std::string agent = getAgentByExten(std::to_string(pChan->no()));
  525. if (agent.empty())
  526. welcomeWords = (boost::str(Format("您好, %s 号话机为您服务.") % pChan->no()));
  527. else
  528. welcomeWords = (boost::str(Format("您好, %s 号话务员为您服务.") % agent));
  529. if (!tts.TextToAudio(welcomeWords, filePath))
  530. {
  531. LOG_WARN("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败", pChan->no());
  532. return false;
  533. }
  534. std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 暂停50ms,防止太快导致分机放音失败
  535. Format EslCmd("bgapi uuid_broadcast %s %s both");
  536. EslCmd % pChan->chanId() % filePath;
  537. return m_Gateway.sendCmd(EslCmd.str());
  538. }
  539. // 播报坐席工号
  540. bool CFsProxy::playAgentNo(long JobId, VirtualChan * pChan)
  541. {
  542. std::string filePath;
  543. Format fmt("%ld 话务员为您服务");
  544. fmt % pChan->no();
  545. if (!tts.TextToAudio(fmt.str(), filePath))
  546. {
  547. LOG_WARN("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败", pChan->no());
  548. return false;
  549. }
  550. std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 暂停50ms,防止太快导致分机放音失败
  551. Format EslCmd("bgapi uuid_broadcast %s %s both\r\n%s: %ld");
  552. EslCmd % pChan->chanId() % filePath % ESL_HEADER_JOB_UUID % JobId;
  553. return m_Gateway.sendCmd(EslCmd.str());
  554. }
  555. bool CFsProxy::meeting(long JobId, std::string CallerNum, std::string DestNum, std::string MeetingId)
  556. {
  557. std::string CallString;
  558. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
  559. return false;
  560. LOG_INFO(("{FsProxy}: 三方会议呼叫字符串: %s"), CallString.c_str());
  561. std::string EslCmd;
  562. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld")
  563. % CallerNum % ESL_VAR_OP_INSTANCE % JobId % CallString % MeetingId % CConfig::GetInstance()->meetingContext() % ESL_HEADER_JOB_UUID % JobId);
  564. return m_Gateway.sendCmd(EslCmd);
  565. }
  566. bool CFsProxy::muteOn(long JobId, VirtualChan * pChan)
  567. {
  568. std::string EslCmd;
  569. EslCmd = boost::str(Format("bgapi uuid_audio %s start write mute -4\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  570. return m_Gateway.sendCmd(EslCmd);
  571. }
  572. bool CFsProxy::muteOff(long JobId, VirtualChan * pChan)
  573. {
  574. std::string EslCmd;
  575. EslCmd = boost::str(Format("bgapi uuid_audio %s stop write mute -4\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  576. return m_Gateway.sendCmd(EslCmd);
  577. }
  578. bool CFsProxy::holdon(long JobId, VirtualChan * pChan)
  579. {
  580. std::string EslCmd;
  581. EslCmd = boost::str(Format("bgapi uuid_hold %s\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  582. return m_Gateway.sendCmd(EslCmd);
  583. }
  584. bool CFsProxy::takeBack(long JobId, VirtualChan * pChan)
  585. {
  586. std::string EslCmd;
  587. EslCmd = boost::str(Format("bgapi uuid_hold off %s\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  588. return m_Gateway.sendCmd(EslCmd);
  589. }
  590. bool CFsProxy::record(long JobId, VirtualChan * pChan, std::string RcdFile)
  591. {
  592. std::string EslCmd;
  593. EslCmd = boost::str(Format("bgapi uuid_record %s start %s\r\n%s: %ld") % pChan->chanId() % RcdFile % ESL_HEADER_JOB_UUID % JobId);
  594. return m_Gateway.sendCmd(EslCmd);
  595. }
  596. bool CFsProxy::turnIvr(long JobId, VirtualChan * pChan)
  597. {
  598. Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
  599. EslCmd % pChan->chanId() % "turnmyd" % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  600. return m_Gateway.sendCmd(EslCmd.str());
  601. //m_Gateway.Execte("ivr", "myd", pChan->chanId().c_str());
  602. }
  603. bool CFsProxy::transfer(long JobId, VirtualChan * pChan, std::string DestNum)
  604. {
  605. /*Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
  606. EslCmd % pChan->chanId() % DestNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  607. return m_Gateway.sendCmd(EslCmd.str());*/
  608. //{%s = %ld} % ESL_VAR_OP_INSTANCE % JobId
  609. std::string Param;
  610. Param = boost::str(Format("{%s=%ld}") % ESL_VAR_OP_INSTANCE % JobId);
  611. m_Gateway.Execte(ESL_APP_SET, Param.c_str(), pChan->chanId().c_str());
  612. Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
  613. EslCmd % pChan->chanId() % DestNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  614. return m_Gateway.sendCmd(EslCmd.str());
  615. }
  616. bool CFsProxy::transfer2Context(long JobId, std::string DestChanId, std::string Exten, std::string Context, bool BothSide)
  617. {
  618. std::string EslCmd;
  619. if (BothSide)
  620. EslCmd = boost::str(Format("bgapi uuid_transfer %s -both %s xml %s\r\n%s: %ld") % DestChanId % Exten % Context % ESL_HEADER_JOB_UUID % JobId);
  621. else
  622. EslCmd = boost::str(Format("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld") % DestChanId % Exten % Context % ESL_HEADER_JOB_UUID % JobId);
  623. return m_Gateway.sendCmd(EslCmd);
  624. }
  625. std::string CFsProxy::creatJson(std::string type, std::string result)
  626. {
  627. // Json::Value root;
  628. // Json::StreamWriterBuilder jsrocd;
  629. // root["Type"] = type;
  630. // root["Result"] = result;
  631. // return Json::writeString(jsrocd, root);
  632. // jsoncpp中文乱码
  633. Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\" }");
  634. fmt %type %result;
  635. return fmt.str();
  636. }
  637. std::string CFsProxy::creatJson(std::string type, bool result)
  638. {
  639. Json::Value root;
  640. Json::StreamWriterBuilder jsrocd;
  641. std::unique_ptr<Json::StreamWriter> write(jsrocd.newStreamWriter());
  642. Json::OStringStream os;
  643. root["Type"] = type;
  644. root["Result"] = result;
  645. write->write(root, &os);
  646. return os.str();
  647. }
  648. std::string CFsProxy::creatJson(std::string type, std::string result, std::string agentId)
  649. {
  650. Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\",\"AgentID\":\"%s\" }");
  651. fmt %type %result %agentId;
  652. return fmt.str();
  653. }
  654. std::string CFsProxy::creatJsonIncoming(std::string callid, std::string caller, std::string callee, std::string trunkNumber, int incomintType)
  655. {
  656. Json::Value root;
  657. Json::StreamWriterBuilder jsrocd;
  658. root["Type"] = "Incoming";
  659. root["Result"] = true;
  660. root["CallID"] = callid; // callid
  661. root["Number"] = caller; // 主叫
  662. root["CalleeNumber"] = callee;
  663. root["TrunkNumber"] = trunkNumber; // 中继号
  664. root["IncomintType"] = incomintType; // 来电类型,来电,外呼,自动外呼
  665. return Json::writeString(jsrocd, root);
  666. }
  667. bool CFsProxy::__init()
  668. {
  669. __initTrunkChan();
  670. if (m_Gateway.start())
  671. {
  672. m_Gateway.hangupAll();
  673. static bool del = m_Gateway.delAgentAll(); // 使用static 保证delagentall运行后在fs连接断开后重连成功后不会再一次执行
  674. boost::ignore_unused(del); // 取消del不使用的警告
  675. if (m_Gateway.scanExten())
  676. {
  677. LOG_INFO_S("{FsProxy}: FreeSWITCH代理初始化成功");
  678. return true;
  679. }
  680. else
  681. {
  682. LOG_ERROR_S("{FsProxy}: FreeSWITCH代理初始化失败, 扫描分机资源失败");
  683. return false;
  684. }
  685. }
  686. else
  687. {
  688. LOG_ERROR_S("{FsProxy}: FreeSWITCH代理初始化失败, ESL网关启动失败");
  689. return false;
  690. }
  691. }
  692. uint32_t CFsProxy::__transLogicState2CtiState(DEV_RES_TYPE ChanType, CHAN_LOGIC_STATE State)
  693. {
  694. if (ChanType == DEV_RES_TYPE_EXT)
  695. {
  696. uint32_t HoldMask = State & HELD_STATE_IND_MASK;
  697. switch (State & HELD_STATE_FILTER_MASK)
  698. {
  699. case CHAN_LOGIC_STATE_DISABLED: return INNER_STATE_DISABLED; // 不可用
  700. case CHAN_LOGIC_STATE_FREE: return INNER_STATE_FREE | HoldMask; // 空闲
  701. case CHAN_LOGIC_STATE_INIT: return INNER_STATE_INIT | HoldMask; // 摘机等待拨号
  702. case CHAN_LOGIC_STATE_DIALING: return INNER_STATE_DIALING | HoldMask; // 拨号
  703. case CHAN_LOGIC_STATE_RING_BACK: return INNER_STATE_RING_BACK | HoldMask; // 呼出振铃
  704. case CHAN_LOGIC_STATE_ALERTING: return INNER_STATE_ALERTING | HoldMask; // 来电振铃
  705. case CHAN_LOGIC_STATE_TALKING: return INNER_STATE_TALKING | HoldMask; // 通话中
  706. default:
  707. return INNER_STATE_DISABLED;
  708. }
  709. }
  710. else
  711. {
  712. switch (State)
  713. {
  714. case CHAN_LOGIC_STATE_DISABLED: return TRUNK_STATE_DISABLED; // 不可用
  715. case CHAN_LOGIC_STATE_FREE: return TRUNK_STATE_FREE; // 空闲
  716. case CHAN_LOGIC_STATE_DIALING: return TRUNK_STATE_DIALING; // 拨号
  717. case CHAN_LOGIC_STATE_RING_BACK: return TRUNK_STATE_RING_BACK; // 呼出振铃
  718. case CHAN_LOGIC_STATE_ALERTING: return TRUNK_STATE_ALERTING; // 来电振铃
  719. case CHAN_LOGIC_STATE_STANDBY:
  720. case CHAN_LOGIC_STATE_TALKING: return TRUNK_STATE_TALKING; // 通话中
  721. default:
  722. return TRUNK_STATE_UNKNOWN;
  723. }
  724. }
  725. }
  726. void CFsProxy::__addExten(uint32_t ExtenNo)
  727. {
  728. ChanExten* pExten = new ChanExten(this, ExtenNo);
  729. m_MapChanExt[ExtenNo] = pExten;
  730. pExten->regist();
  731. }
  732. void CFsProxy::__delExten(uint32_t ExtenNo)
  733. {
  734. auto it = m_MapChanExt.find(ExtenNo);
  735. if (it != m_MapChanExt.end())
  736. {
  737. if (it->second->isFree())
  738. {
  739. FS_LINK_DELETE(it->second);
  740. m_MapChanExt.erase(it);
  741. }
  742. else
  743. {
  744. it->second->discard(true);
  745. }
  746. }
  747. }
  748. void CFsProxy::__freeExten(void)
  749. {
  750. auto it = m_MapChanExt.begin();
  751. while (it != m_MapChanExt.end())
  752. {
  753. FS_LINK_DELETE(it->second);
  754. ++it;
  755. }
  756. m_MapChanExt.clear();
  757. }
  758. void CFsProxy::__initTrunkChan(void)
  759. {
  760. /*for (int i = 1; i <= CConfig::GetInstance()->trunkCount(); ++i)*/
  761. for (int i = 1; i <= SoftAuth::GetInstance()->trunkNum(); ++i)
  762. {
  763. ChanTrunk* pTrunk = new ChanTrunk(this, i);
  764. m_ArrayTrunk.emplace_back(pTrunk);
  765. pTrunk->regist();
  766. }
  767. }
  768. void CFsProxy::__freeTrunkChan(void)
  769. {
  770. ChanTrunk* pTrunk = nullptr;
  771. for (size_t i = 0; i < m_ArrayTrunk.size(); ++i)
  772. {
  773. pTrunk = m_ArrayTrunk[i];
  774. FS_LINK_DELETE(pTrunk);
  775. }
  776. m_ArrayTrunk.clear();
  777. }
  778. Session * CFsProxy::__getSession(string SessionId)
  779. {
  780. auto it = m_MapSession.find(SessionId);
  781. if (it != m_MapSession.end())
  782. return it->second;
  783. return nullptr;
  784. }
  785. Session * CFsProxy::__getSession(PCHAN_EVENT_NOTIFY pNotify)
  786. {
  787. Session* pSession = nullptr;
  788. pSession = __getSession(pNotify->CallId);
  789. if (pSession == nullptr)
  790. {
  791. if (!pNotify->CcId.empty())
  792. {
  793. LOG_INFO(boost::str(Format("Callid查不到,CcId不为空,使用CcId代替CallId进一步查找Session,CallId[%s],CcId[%s]") % pNotify->CallId % pNotify->CcId).c_str());
  794. pSession = __getSession(pNotify->CcId);
  795. if (pSession != nullptr) return pSession;
  796. LOG_WARN(boost::str(Format("CallId,CcId都查不到session,CallId[%s],CcId[%s]") % pNotify->CallId % pNotify->CcId).c_str());
  797. }
  798. /*if (pNotify->EventId == CHANNEL_EVENT_HANGUP_COMPLETE)
  799. {
  800. LOG_WARN("{CFsProxy}:接收到的挂机事件在会话中找不到,不再创建新会话, CallId[%s], CcId[%s]", pNotify->CallId.c_str(), pNotify->CcId.c_str());
  801. return nullptr;
  802. }*/
  803. pSession = new Session(this, pNotify->CallId);
  804. pSession->prepare(pNotify);
  805. m_MapSession[pNotify->CallId] = pSession;
  806. Format fmt("{CFsProxy}:Add Session[%s],ChanId[%s],EventId[%d]");
  807. fmt % pSession->id() % pNotify->ChanId % pNotify->EventId;
  808. LOG_INFO_S(fmt.str());
  809. }
  810. return pSession;
  811. }
  812. void CFsProxy::__delSession(std::string SessionId)
  813. {
  814. if (SessionId.empty())
  815. return;
  816. auto it = m_MapSession.find(SessionId);
  817. if (it != m_MapSession.end())
  818. {
  819. Format fmt("{FsProxy}: Delete Session[%s]");
  820. fmt % it->second->id();
  821. LOG_INFO_S(fmt.str());
  822. FS_LINK_DELETE(it->second);
  823. m_MapSession.erase(it);
  824. }
  825. }
  826. void CFsProxy::__freeSession(void)
  827. {
  828. auto it = m_MapSession.begin();
  829. while (it != m_MapSession.end())
  830. {
  831. FS_LINK_DELETE(it->second);
  832. ++it;
  833. }
  834. m_MapSession.clear();
  835. }
  836. bool CFsProxy::__kill(long JobId, string ChanId)
  837. {
  838. string EslCmd;
  839. if (JobId == FS_LINK_JOBID_INVALID)
  840. EslCmd = boost::str(Format("bgapi uuid_kill %s") % ChanId);
  841. else
  842. EslCmd = boost::str(Format("bgapi uuid_kill %s\r\n%s: %ld") % ChanId % ESL_HEADER_JOB_UUID % JobId);
  843. return m_Gateway.sendCmd(EslCmd);
  844. }
  845. std::string CFsProxy::__getExtenByAgent(std::string agentId)
  846. {
  847. std::unique_lock<std::mutex>lock(m_AgentLock);
  848. auto it = m_MapAgent.begin();
  849. while (it != m_MapAgent.end())
  850. {
  851. if (it->second->id() == agentId)
  852. {
  853. return it->first;
  854. }
  855. ++it;
  856. }
  857. return "";
  858. }
  859. bool CFsProxy::__login(std::string AgentID, std::string ExtenNo)
  860. {
  861. bool ret = true;
  862. std::string cmd;
  863. cmd = boost::str(Format("bgapi callcenter_config agent add %1% Callback") % AgentID); // 添加座席
  864. ret &= m_Gateway.sendCmd(cmd);
  865. cmd = boost::str(Format("bgapi callcenter_config agent set contact %1% [call_timeout=30]user/%2%") % AgentID % ExtenNo); // 设置呼叫字符串
  866. ret &= m_Gateway.sendCmd(cmd);
  867. cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available ") % AgentID); // 座席登录后默认空闲
  868. ret &= m_Gateway.sendCmd(cmd);
  869. cmd = boost::str(Format("bgapi callcenter_config agent set state %1% Waiting") % AgentID); // 座席登录后默认空闲
  870. ret &= m_Gateway.sendCmd(cmd);
  871. cmd = boost::str(Format("bgapi callcenter_config agent set max_no_answer %1% 0") % AgentID); // 0禁用
  872. ret &= m_Gateway.sendCmd(cmd);
  873. cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% 20") % AgentID); // 话后处理时长
  874. ret &= m_Gateway.sendCmd(cmd);
  875. cmd = boost::str(Format("bgapi callcenter_config agent set reject_delay_time %1% 0") % AgentID);
  876. ret &= m_Gateway.sendCmd(cmd);
  877. cmd = boost::str(Format("bgapi allcenter_config agent set busy_delay_time %1% 0") % AgentID);
  878. ret &= m_Gateway.sendCmd(cmd);
  879. cmd = boost::str(Format("bgapi callcenter_config tier add support@default %1% 1 1") % AgentID); // 添加梯队 到队列等价于坐席组
  880. ret &= m_Gateway.sendCmd(cmd);
  881. return ret;
  882. }
  883. bool CFsProxy::__login(std::string AgentID, std::string ExtenNo, std::list<std::string> Group)
  884. {
  885. bool ret = true;
  886. std::string cmd;
  887. cmd = boost::str(Format("bgapi callcenter_config agent add %1% Callback") % AgentID); // 添加座席
  888. ret &= m_Gateway.sendCmd(cmd);
  889. cmd = boost::str(Format("bgapi callcenter_config agent set contact %1% [call_timeout=30]user/%2%") % AgentID % ExtenNo); // 设置呼叫字符串
  890. ret &= m_Gateway.sendCmd(cmd);
  891. cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available ") % AgentID); // 座席登录后默认空闲
  892. ret &= m_Gateway.sendCmd(cmd);
  893. cmd = boost::str(Format("bgapi callcenter_config agent set state %1% Waiting") % AgentID); // 座席登录后默认空闲
  894. ret &= m_Gateway.sendCmd(cmd);
  895. cmd = boost::str(Format("bgapi callcenter_config agent set max_no_answer %1% 0") % AgentID); // 0禁用
  896. ret &= m_Gateway.sendCmd(cmd);
  897. //cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% 20") % AgentID); // 话后处理时长
  898. cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% %2%") % AgentID %CConfig::GetInstance()->postProcessTime());
  899. ret &= m_Gateway.sendCmd(cmd);
  900. cmd = boost::str(Format("bgapi callcenter_config agent set reject_delay_time %1% 0") % AgentID);
  901. ret &= m_Gateway.sendCmd(cmd);
  902. cmd = boost::str(Format("bgapi allcenter_config agent set busy_delay_time %1% 0") % AgentID);
  903. ret &= m_Gateway.sendCmd(cmd);
  904. for (std::string &var : Group)
  905. {
  906. cmd = boost::str(Format("bgapi callcenter_config tier add %2% %1% 1 1") % AgentID %var); // 添加梯队 到队列等价于坐席组
  907. ret &= m_Gateway.sendCmd(cmd);
  908. }
  909. return ret;
  910. }
  911. bool CFsProxy::__logout(std::string AgentID, std::string AgentExten)
  912. {
  913. bool ret = true;
  914. std::string cmd;
  915. cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
  916. ret &= m_Gateway.sendCmd(cmd);
  917. /*cmd = boost::str(Format("bgapi callcenter_config tier del support@default %1% ") % AgentID);
  918. ret &= m_Gateway.sendCmd(cmd);*/
  919. std::unique_lock<std::mutex>lock(m_AgentLock);
  920. auto it = m_MapAgent.find(AgentExten);
  921. if (ret && it != m_MapAgent.end())
  922. {
  923. for (std::string &var : it->second->groups())
  924. {
  925. cmd = boost::str(Format("bgapi callcenter_config tier del %2% %1% ") % AgentID %var);
  926. ret &= m_Gateway.sendCmd(cmd);
  927. }
  928. it->second->removeAgent(AgentID);
  929. FS_LINK_DELETE(it->second);
  930. m_MapAgent.erase(it);
  931. }
  932. return ret;
  933. }
  934. bool CFsProxy::__freeAgent()
  935. {
  936. std::unique_lock<std::mutex>lock(m_AgentLock);
  937. auto it = m_MapAgent.begin();
  938. while (it != m_MapAgent.end())
  939. {
  940. // __logout(it->second->id());
  941. //FS_LINK_DELETE(it->second);
  942. Agent* agent = it->second;
  943. FS_LINK_DELETE(agent);
  944. m_MapAgent.erase(it++);
  945. }
  946. return false;
  947. }
  948. bool CFsProxy::__setState(std::string AgentID, bool isFree)
  949. {
  950. Format fmt("bgapi callcenter_config agent set status %1% '%2%'");
  951. if (isFree)
  952. fmt %AgentID %"Available"; // 置闲
  953. else
  954. fmt %AgentID %"On Break"; // 置忙
  955. return m_Gateway.sendCmd(fmt.str());
  956. }
  957. void CFsProxy::__recvMsgFun(websocketpp::connection_hdl hdl, string msg)
  958. {
  959. Json::Value root;
  960. Json::CharReaderBuilder builder;
  961. std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  962. std::string errs;
  963. bool ret = reader->parse(msg.c_str(), msg.c_str() + msg.length(), &root, &errs);
  964. if (!ret || !errs.empty())
  965. {
  966. if (errs.empty()) errs = "空";
  967. Format fmt("Json 解析错误,errs = %s ,msg = %s");
  968. fmt %errs %msg;
  969. LOG_WARN_S(fmt.str());
  970. return;
  971. }
  972. if (!root.isMember("Type"))
  973. {
  974. Format fmt("该条Json消息格式不能识别 ,msg = %s");
  975. fmt %msg;
  976. LOG_WARN_S(fmt.str());
  977. return;
  978. }
  979. LOG_DEBUG_S(msg);
  980. const string type = root["Type"].asString();
  981. const string AgentID = root["AgentID"].asString();
  982. const string AgentExten = root["AgentExten"].asString();
  983. uint32_t ExtenNo = 0;
  984. if (sscanf(AgentExten.c_str(), "%u", &ExtenNo) != 1)
  985. {
  986. Format fmt("分机号为非UInt,msg = %s");
  987. fmt % msg;
  988. LOG_WARN_S(fmt.str());
  989. m_Server.sendMsg(hdl, fmt.str());
  990. return;
  991. }
  992. if ("Login" == type) { // 签入
  993. if (SoftAuth::GetInstance()->acdNum() <= m_MapAgent.size())
  994. {
  995. Format fmt("ACD授权数量已达上限,Agent = %s,Exten = %d");
  996. fmt % AgentID % AgentExten;
  997. LOG_INFO_S(fmt.str());
  998. m_Server.sendMsg(hdl, creatJson(type, std::string("签入失败,ACD授权数量已达上限")));
  999. return;
  1000. }
  1001. if (getExten(ExtenNo) == nullptr)
  1002. {
  1003. Format fmt("分机不存在,Agent = %s,Exten = %d");
  1004. fmt % AgentID % AgentExten;
  1005. LOG_INFO_S(fmt.str());
  1006. m_Server.sendMsg(hdl, creatJson(type, std::string("分机号不存在")));
  1007. return;
  1008. }
  1009. auto it = m_MapAgent.find(AgentExten);
  1010. if (it != m_MapAgent.end())
  1011. {
  1012. Format fmt("分机已被绑定,Agent = %s,Exten = %s");
  1013. fmt % AgentID % AgentExten;
  1014. LOG_INFO_S(fmt.str());
  1015. m_Server.sendMsg(hdl, creatJson(type, std::string("分机已被绑定")));
  1016. return;
  1017. }
  1018. std::string AgentGroup = root["AgentGroup"].asString();
  1019. Agent *agent = new Agent(AgentID, AgentExten, AgentGroup);
  1020. agent->hdl() = hdl;
  1021. {
  1022. std::unique_lock<std::mutex>lock(m_AgentLock);
  1023. m_MapAgent[AgentExten] = agent; // 分机号作为key
  1024. }
  1025. if (!__login(AgentID, AgentExten, agent->groups()))
  1026. {
  1027. m_MapAgent.erase(AgentExten);
  1028. Format fmt("座席签入失败,Agent = %s,Exten = %d, %s");
  1029. fmt % AgentID % AgentExten % hdl.lock().get();
  1030. LOG_INFO_S(fmt.str());
  1031. m_Server.sendMsg(hdl, creatJson(type, false));
  1032. return;
  1033. }
  1034. Format fmt("座席签入成功,Agent = %s,Exten = %d,AgentGroup = %s %s");
  1035. fmt % AgentID % AgentExten % AgentGroup % hdl.lock().get();
  1036. LOG_INFO_S(fmt.str());
  1037. m_Server.sendMsg(hdl, creatJson(type, true));
  1038. }
  1039. else if ("Logout" == type) { // 签出
  1040. bool ret = __logout(AgentID, AgentExten);
  1041. Format fmt("座席签出[%d],Agent = %s,Exten = %d, %s");
  1042. fmt %ret % AgentID % AgentExten % hdl.lock().get();
  1043. LOG_INFO_S(fmt.str());
  1044. m_Server.sendMsg(hdl, creatJson(type, ret));
  1045. std::string data = creatJson("Monitor", "签出", AgentID);
  1046. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1047. }
  1048. else if ("SayBusy" == type) { // 置忙
  1049. bool ret = __setState(AgentID, false);
  1050. std::unique_lock<std::mutex>lock(m_AgentLock);
  1051. auto it = m_MapAgent.find(AgentExten);
  1052. if (it != m_MapAgent.end())
  1053. it->second->setState(AGENT_STATE_REPOSE);
  1054. m_Server.sendMsg(hdl, creatJson(type, ret));
  1055. std::string data = creatJson("Monitor", "小休", AgentID);
  1056. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1057. }
  1058. else if ("SayFree" == type) { // 置闲
  1059. bool ret = __setState(AgentID, true);
  1060. std::unique_lock<std::mutex>lock(m_AgentLock);
  1061. auto it = m_MapAgent.find(AgentExten);
  1062. if (it != m_MapAgent.end())
  1063. it->second->setState(AGENT_STATE_FREE);
  1064. m_Server.sendMsg(hdl, creatJson(type, ret));
  1065. std::string data = creatJson("Monitor", "空闲", AgentID);
  1066. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1067. }
  1068. else if ("Hold" == type) { // 保持
  1069. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HOLD, ExtenNo, nullptr);
  1070. if (!ret)
  1071. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1072. }
  1073. else if ("Retrieve" == type) { // 接回保持
  1074. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TAKEBACK, ExtenNo, nullptr);
  1075. if (!ret)
  1076. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1077. }
  1078. else if ("MakeCall" == type) { // 外呼
  1079. const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
  1080. LineOpParam param;
  1081. param.szParam1 = AgentExten;
  1082. param.szParam2 = DestinationNumber;
  1083. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MAKE_CALL, ExtenNo, &param);
  1084. if (!ret)
  1085. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1086. }
  1087. else if ("Transfer" == type) { // 转移
  1088. const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
  1089. LineOpParam param;
  1090. param.szParam1 = AgentExten;
  1091. param.szParam2 = DestinationNumber;
  1092. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TRANSFER, ExtenNo, &param);
  1093. if (!ret)
  1094. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1095. }
  1096. else if ("Meeting" == type) { // 会议
  1097. const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
  1098. auto it = getExten(ExtenNo);
  1099. std::string meetingID;
  1100. if (it != nullptr)
  1101. {
  1102. meetingID = it->sessionId();
  1103. }
  1104. LOG_DEBUG("会议ID[%s]", meetingID.c_str());
  1105. LineOpParam param;
  1106. param.nParam1 = meetingID; // 会议id
  1107. param.szParam1 = AgentExten;
  1108. param.szParam2 = DestinationNumber;
  1109. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_CONFERENCE, ExtenNo, &param);
  1110. if (!ret)
  1111. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1112. }
  1113. else if ("DropCall" == type) { // 挂机
  1114. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HANG_UP, ExtenNo, nullptr);
  1115. if (!ret)
  1116. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1117. }
  1118. else if ("Break" == type) { // 强拆
  1119. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1120. const string Exten = __getExtenByAgent(TargetAgentID);
  1121. LineOpParam param;
  1122. param.szParam1 = AgentExten;
  1123. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HANG_UP, atoi(Exten.c_str()), &param);
  1124. if (!ret)
  1125. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1126. }
  1127. else if ("Listen" == type) { // 监听
  1128. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1129. const string Exten = __getExtenByAgent(TargetAgentID);
  1130. LineOpParam param;
  1131. param.szParam1 = AgentExten;
  1132. param.szParam2 = Exten;
  1133. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_LISTEN, ExtenNo, &param);
  1134. if (!ret)
  1135. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1136. }
  1137. else if ("Insert" == type) { // 强插
  1138. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1139. const string Exten = __getExtenByAgent(TargetAgentID);
  1140. LineOpParam param;
  1141. param.szParam1 = AgentExten;
  1142. param.szParam2 = Exten;
  1143. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_INSERT, ExtenNo, &param);
  1144. if (!ret)
  1145. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1146. }
  1147. else if ("Instead" == type) { // 代接
  1148. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1149. const string Exten = __getExtenByAgent(TargetAgentID);
  1150. LineOpParam param;
  1151. param.szParam1 = AgentExten;
  1152. param.szParam2 = Exten;
  1153. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_INSTEAD, ExtenNo, &param);
  1154. if (!ret)
  1155. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1156. }
  1157. else if ("Intercept" == type) { // 强截
  1158. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1159. const string Exten = __getExtenByAgent(TargetAgentID);
  1160. LineOpParam param;
  1161. param.szParam1 = AgentExten;
  1162. param.szParam2 = Exten;
  1163. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_GRAB, ExtenNo, &param);
  1164. if (!ret)
  1165. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1166. }
  1167. else if ("Record" == type) { // 录音
  1168. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_RECORD, ExtenNo, nullptr);
  1169. if (!ret)
  1170. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1171. }
  1172. else if ("MuteOn" == type) { // 静音
  1173. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MUTE_BEGIN, ExtenNo, nullptr);
  1174. if (!ret)
  1175. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1176. }
  1177. else if ("MuteOff" == type) { // 取消静音
  1178. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MUTE_END, ExtenNo, nullptr);
  1179. if (!ret)
  1180. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1181. }
  1182. else if ("Monitor" == type) { // 监控
  1183. m_MonitAgents.addAgent(AgentID, hdl);
  1184. m_Server.sendMsg(hdl, creatJson(type, true));
  1185. std::unique_lock<std::mutex>lock(m_AgentLock);
  1186. auto it = m_MapAgent.begin();
  1187. while (it != m_MapAgent.end())
  1188. {
  1189. std::string state = it->second->state_s();
  1190. std::string agentId = it->second->id();
  1191. std::string data = creatJson("Monitor", state, agentId);
  1192. m_Server.sendMsg(hdl, data);
  1193. ++it;
  1194. }
  1195. }
  1196. else if ("MonitorCancel" == type) { // 取消监控
  1197. m_MonitAgents.delAgent(AgentID);
  1198. m_Server.sendMsg(hdl, creatJson(type, true));
  1199. }
  1200. else if ("TurnIvr" == type) { // 转满意度
  1201. COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TURNIVR, ExtenNo, nullptr);
  1202. }
  1203. else if ("AutoCall" == type) { // 自动外呼
  1204. m_AutoCall.addTask(); // 从数据库刷新任务
  1205. m_Server.sendMsg(hdl, creatJson(type, true));
  1206. }
  1207. else if ("Heart" == type) { // 心跳
  1208. m_Server.sendMsg(hdl, msg);
  1209. }
  1210. }
  1211. void CFsProxy::__closeFun(websocketpp::connection_hdl hdl)
  1212. {
  1213. std::string agentId;
  1214. std::unique_lock<std::mutex>lock(m_AgentLock);
  1215. auto it = m_MapAgent.begin();
  1216. while (it != m_MapAgent.end())
  1217. {
  1218. if (!(it->second->hdl().expired()))
  1219. {
  1220. auto sp = it->second->hdl().lock();
  1221. stringstream agent_hdl;
  1222. agent_hdl << sp.get();
  1223. stringstream local_hdl;
  1224. local_hdl << hdl.lock().get();
  1225. if (agent_hdl.str() == local_hdl.str())
  1226. {
  1227. agentId = it->second->id(); // 保存坐席id
  1228. Format fmt("座席掉线,agentId = %s,extenNo = %s,IP = %s");
  1229. fmt %it->second->id() % it->second->assoExten() % local_hdl.str();
  1230. LOG_WARN_S(fmt.str());
  1231. //m_Gateway.delAgent(agentId);
  1232. m_Gateway.delAgent(agentId,it->second->groups());
  1233. it->second->removeAgent("坐席掉线");
  1234. FS_LINK_DELETE(it->second);
  1235. m_MapAgent.erase(it);
  1236. break;
  1237. }
  1238. }
  1239. ++it;
  1240. }
  1241. if (!agentId.empty())
  1242. {
  1243. std::string data = creatJson("Monitor", "签出", agentId);
  1244. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1245. m_MonitAgents.delAgent(agentId);
  1246. }
  1247. }
  1248. void CFsProxy::__doAutoTask(Task & task)
  1249. {
  1250. switch (task.type)
  1251. {
  1252. case VOICE_CALL: // 语音外呼
  1253. {
  1254. __voicCall(task);
  1255. }
  1256. break;
  1257. case AGENT_CALL: // 指定坐席外呼 set 在当前 channel 上设置变量,而 export 在(a-leg 和 b-leg) 两个 channel 上都设置
  1258. {
  1259. LineOpParam param;
  1260. param.nParam1 = task.agent; // 坐席号
  1261. param.nParam2 = task.type; // 自动外呼类型
  1262. param.nParam3 = task.id; // 自动外呼任务id
  1263. param.szParam1 = getExtenByAgent(task.agent); // 坐席对应的分机号
  1264. param.szParam2 = task.number;
  1265. COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_AUTO_CALL, atoi(param.szParam1.c_str()), &param);
  1266. }
  1267. break;
  1268. case AUTO_CALL: // 系统自动分配坐席外呼
  1269. break;
  1270. default:
  1271. break;
  1272. }
  1273. if (task.id != 0)
  1274. m_AutoCall.updateTask(std::to_string(task.id)); // 更新任务
  1275. }
  1276. bool CFsProxy::__voicCall(Task & task)
  1277. {
  1278. std::string content = task.content;
  1279. std::string filePath;
  1280. if (!tts.TextToAudio(content, filePath))
  1281. {
  1282. LOG_WARN_S("文本转语音失败");
  1283. return false;
  1284. };
  1285. std::string callee = task.number;
  1286. std::string caller;
  1287. std::string CallString;
  1288. m_CallStringMaker.makeCallString(caller, callee, CallString);
  1289. Format fmt("bgapi originate {origination_caller_id_number=%s,%s=%ld,%s=%s,%s=%ld}%s VOICECALL XML %s\r\n%s: %ld");
  1290. fmt %caller % ESL_VAR_OP_TYPE % task.type %"filePath" %filePath % ESL_VAR_TASK_ID % task.id % CallString % "AutoCall" % ESL_HEADER_JOB_UUID % task.uniqueId();
  1291. return m_Gateway.sendCmd(fmt.str());
  1292. }
  1293. CFsProxy CFsProxy::instance;