linux版本中间件

FsProxy.cpp 62KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  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. lineType = std::string("外线");
  140. }
  141. std::string state;
  142. switch (chanState)
  143. {
  144. case CHAN_LOGIC_STATE_DISABLED: // 不可用
  145. state = "不可用";
  146. break;
  147. case CHAN_LOGIC_STATE_FREE: // 空闲
  148. state = "空闲";
  149. break;
  150. case CHAN_LOGIC_STATE_INIT: // 摘机等待拨号
  151. state = "摘机等待拨号";
  152. break;
  153. case CHAN_LOGIC_STATE_DIALING: // 拨号
  154. state = "拨号";
  155. break;
  156. case CHAN_LOGIC_STATE_RING_BACK: // 呼出振铃
  157. state = "呼出振铃";
  158. break;
  159. case CHAN_LOGIC_STATE_ALERTING: // 来电振铃
  160. state = "来电振铃";
  161. break;
  162. case CHAN_LOGIC_STATE_TALKING: // 通话中
  163. state = "通话中";
  164. break;
  165. case CHAN_LOGIC_STATE_STANDBY: // 中继通道可响应APP(此状态仅用作DevLink侧,不可传至CTI)
  166. break;
  167. case CHAN_LOGIC_STATE_HELD: // 通话保持中(保持状态的使用须与原通道状态按位或和与)
  168. state = "通话保持中";
  169. break;
  170. }
  171. Format fmt("lineType = %s[%s],lineState = %s,chanState = %d");
  172. fmt % lineType % chanNo % state % chanState;
  173. LOG_DEBUG_S(fmt.str());
  174. if (devType == DEV_RES_TYPE_EXT) {
  175. map<std::string, Agent*>::iterator it;
  176. std::unique_lock<std::mutex>lock(m_AgentLock);
  177. it = m_MapAgent.find(chanNo);
  178. if (it != m_MapAgent.end()) {
  179. if (it->second == nullptr) {
  180. LOG_WARN("查到分机[%s]对应坐席空", chanNo.c_str());
  181. return;
  182. }
  183. if (chanState == CHAN_LOGIC_STATE_FREE) {
  184. if (it->second->isRepose()) {
  185. it->second->setState(AGENT_STATE_REPOSE);
  186. state = it->second->state_s();
  187. }
  188. else {
  189. if (it->second->state() == AGENT_STATE_BUSY) {
  190. __setStatePostProcessing(it->second->id()); // 在fs中设置坐席话后处理
  191. it->second->setState(AGENT_STATE_POST_PROCESSING);
  192. state = it->second->state_s();
  193. }
  194. else {
  195. it->second->setState(AGENT_STATE_FREE);
  196. }
  197. }
  198. }
  199. else if (chanState == CHAN_LOGIC_STATE_TALKING) {
  200. it->second->setState(AGENT_STATE_BUSY);
  201. }
  202. else if (chanState == CHAN_LOGIC_STATE_RING_BACK || chanState == CHAN_LOGIC_STATE_ALERTING) {
  203. if (AGENT_STATE_REQUESTED == it->second->state()) { // 当前状态已通知过,不在重复通知
  204. return;
  205. }
  206. it->second->setState(AGENT_STATE_REQUESTED);
  207. }
  208. if (!state.empty()) { // 过滤掉保持状态
  209. std::string data = creatJson("LineState", state);
  210. m_Server.sendMsg(it->second->hdl(), data);
  211. data = creatJson("Monitor", state, it->second->id(), chanNo);
  212. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  213. }
  214. __agentDetail(lock.release()); // 通知坐席详情变化
  215. }
  216. else {
  217. LOG_WARN("线路状态通知失败,找不到分机[%s]对应的坐席", chanNo.c_str());
  218. }
  219. }
  220. else if (devType == DEV_RES_TYPE_TRUNK) {
  221. if (CHAN_LOGIC_STATE_ALERTING == chanState) {
  222. __agentDetailTrunkChange(1); // 计算呼入数量
  223. }
  224. else if (CHAN_LOGIC_STATE_FREE == chanState) {
  225. __agentDetailTrunkChange(-1);
  226. }
  227. }
  228. }
  229. void CFsProxy::onChanPoor(Session * pSession, PCHAN_EVENT_NOTIFY pNotify)
  230. {
  231. LOG_INFO_S(boost::str(Format("空闲通道枯竭,主动挂断,ChanId = %s ,caller = %s") % pNotify->ChanId % pNotify->Caller).c_str());
  232. __kill(FS_LINK_JOBID_INVALID, pNotify->ChanId);
  233. }
  234. void CFsProxy::onCallcenterQueue(const std::string & strQueueName, const std::string & strQueueCount)
  235. {
  236. std::unique_lock<std::shared_mutex>lock(m_mutQueryCount);
  237. m_QueryCount[strQueueName] = strQueueCount;
  238. Json::Value root;
  239. Json::StreamWriterBuilder jsrocd;
  240. root["Type"] = "WaitCount";
  241. root["Result"] = true;
  242. std::int32_t nTotal = 0;
  243. for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
  244. root[it->first] = it->second;
  245. try {
  246. auto n = stoi(it->second);
  247. nTotal += n;
  248. }
  249. catch (const std::exception& e) {
  250. LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
  251. }
  252. }
  253. root["Total"] = nTotal;
  254. auto strJson = Json::writeString(jsrocd, root);
  255. // 发送给需要的坐席
  256. for (auto it = m_subWaitCount.begin(); it != m_subWaitCount.end();) {
  257. auto bOk = m_Server.sendMsg(*it, strJson);
  258. if (bOk) {
  259. ++it;
  260. }
  261. else {
  262. it = m_subWaitCount.erase(it);
  263. }
  264. }
  265. }
  266. void CFsProxy::onEslDisconnect(void)
  267. {
  268. // esl 断开重连
  269. m_Gateway.stop();
  270. __freeSession();
  271. __freeExten();
  272. __freeTrunkChan();
  273. LOG_WARN_S("ESL断开重新初始化...");
  274. while (!__init())
  275. {
  276. LOG_WARN_S("ESL重连中...")
  277. this_thread::sleep_for(std::chrono::seconds(5));
  278. }
  279. LOG_INFO_S("ESL重新初始化成功...");
  280. }
  281. void CFsProxy::onEslExtenReg(uint32_t ExtenNo, string ExtenIp)
  282. {
  283. ChanExten* pExten = getExten(ExtenNo);
  284. if (pExten == nullptr)
  285. {
  286. __addExten(ExtenNo);
  287. }
  288. else
  289. {
  290. if (pExten->isVoid()) // 若被丢弃,则改变丢弃状态
  291. pExten->discard(false);
  292. }
  293. }
  294. void CFsProxy::onEslExtenUnreg(uint32_t ExtenNo)
  295. {
  296. std::string agentId;
  297. {
  298. std::unique_lock<std::mutex>lock(m_AgentLock);
  299. auto it = m_MapAgent.find(std::to_string(ExtenNo));
  300. if (it != m_MapAgent.end())
  301. {
  302. agentId = it->second->id();
  303. it->second->removeAgent("分机掉线");
  304. //m_Gateway.delAgent(it->second->id()); // callcenter模块中删除
  305. m_Gateway.delAgent(it->second->id(), it->second->groups());
  306. m_Server.sendMsg(it->second->hdl(), creatJson("Logout", "分机掉线"));
  307. m_MapAgent.erase(it); // 从签入列表中删除
  308. }
  309. }
  310. if (!agentId.empty())
  311. {
  312. std::string data = creatJson("Monitor", "签出", agentId, std::to_string(ExtenNo));
  313. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  314. }
  315. __delExten(ExtenNo);
  316. }
  317. void CFsProxy::onEslEvtBgJobDone(PBG_JOB_NOTIFY pNotify)
  318. {
  319. COperationReactor::GetInstance()->onEslEvtBgJobDone(pNotify);
  320. }
  321. void CFsProxy::onEslEvtChannel(PCHAN_EVENT_NOTIFY pNotify)
  322. {
  323. Session* pSession = __getSession(pNotify);
  324. if (pSession == nullptr)
  325. {
  326. Format fmt("{FsProxy}: 通道事件,没有找到会话,EslEventId=%d,ChanId[%s],CallId[%s],Caller[%s],Callee[%s],CcId[%s]");
  327. fmt % pNotify->EventId % pNotify->ChanId % pNotify->CallId % pNotify->Caller % pNotify->Callee % pNotify->CcId;
  328. LOG_WARN_S(fmt.str());
  329. return;
  330. }
  331. pSession->onChanEvent(pNotify);
  332. if (pSession->isVoid())
  333. __delSession(pSession->id());
  334. }
  335. void CFsProxy::onEslEvtDtmf(PDTMF_NOTIFY pNotify)
  336. {
  337. }
  338. void CFsProxy::onEslEvtHold(PHOLD_NOTIFY pNotify)
  339. {
  340. if (pNotify == nullptr) return;
  341. Session* pSession = __getSession(pNotify->CallId);
  342. if (pSession != nullptr)
  343. pSession->onChanHold(pNotify);
  344. }
  345. bool CFsProxy::send2Agent(std::string ExtenNo, std::string Data)
  346. {
  347. std::unique_lock<std::mutex>lock(m_AgentLock);
  348. auto it = m_MapAgent.find(ExtenNo);
  349. if (it != m_MapAgent.end())
  350. {
  351. if (m_Server.sendMsg(it->second->hdl(), Data))
  352. {
  353. LOG_DEBUG("成功发送到坐席[%s]-分机[%s],msg[%s]", it->second->id().c_str(), ExtenNo.c_str(), Data.c_str());
  354. return true;
  355. }
  356. }
  357. Format fmt("坐席通知失败,找不到分机对应的坐席,分机[%s],msg[%s]");
  358. fmt % ExtenNo % Data;
  359. LOG_WARN_S(fmt.str());
  360. return false;
  361. }
  362. ChanExten * CFsProxy::getExten(uint32_t ExtenNo)
  363. {
  364. auto it = m_MapChanExt.find(ExtenNo);
  365. if (it != m_MapChanExt.end()) return it->second;
  366. return nullptr;
  367. }
  368. ChanTrunk * CFsProxy::getTrunk(uint32_t TrunkNo)
  369. {
  370. if (TrunkNo <= 0 || TrunkNo > m_ArrayTrunk.size())
  371. return nullptr;
  372. return m_ArrayTrunk[--TrunkNo];
  373. }
  374. void CFsProxy::delChan(VirtualChan * pChan)
  375. {
  376. if (pChan->type() == DEV_RES_TYPE_EXT)
  377. __delExten(pChan->no());
  378. }
  379. ChanTrunk * CFsProxy::getFreeTrunk(void)
  380. {
  381. for (size_t i = 0; i < m_ArrayTrunk.size(); ++i)
  382. {
  383. ChanTrunk* pTrunk = m_ArrayTrunk[i];
  384. if (pTrunk != nullptr)
  385. {
  386. if (pTrunk->isFree())
  387. {
  388. return pTrunk;
  389. }
  390. }
  391. }
  392. LOG_INFO_S("没有空闲中继通道");
  393. return nullptr;
  394. }
  395. VirtualChan * CFsProxy::getAssoChanInSession(VirtualChan * pChan)
  396. {
  397. Session* pSession = __getSession(pChan->sessionId());
  398. if (pSession != nullptr)
  399. return pSession->getAssoChan(pChan);
  400. else
  401. return nullptr;
  402. }
  403. VirtualChan * CFsProxy::getBusyChan(string ChanId)
  404. {
  405. auto it = m_MapBusyChan.find(ChanId);
  406. if (it != m_MapBusyChan.end())
  407. return it->second;
  408. return nullptr;
  409. }
  410. void CFsProxy::regBusyChan(VirtualChan * pChan)
  411. {
  412. m_MapBusyChan[pChan->chanId()] = pChan;
  413. }
  414. void CFsProxy::unregBusyChan(VirtualChan * pChan)
  415. {
  416. m_MapBusyChan.erase(pChan->chanId());
  417. }
  418. std::string CFsProxy::getAgentByExten(std::string ExtenNo)
  419. {
  420. std::unique_lock<std::mutex>lock(m_AgentLock);
  421. auto it = m_MapAgent.find(ExtenNo);
  422. if (it->second == nullptr) return "";
  423. return it->second->id();
  424. }
  425. std::string CFsProxy::getAgentByExten(std::string ExtenNo, std::string &Groups)
  426. {
  427. std::unique_lock<std::mutex>lock(m_AgentLock);
  428. auto it = m_MapAgent.find(ExtenNo);
  429. if (it->second == nullptr) return "";
  430. Groups = it->second->group();
  431. return it->second->id();
  432. }
  433. std::string CFsProxy::getExtenByAgent(std::string AgentId)
  434. {
  435. std::unique_lock<std::mutex>lock(m_AgentLock);
  436. auto it = m_MapAgent.begin();
  437. while (it != m_MapAgent.end())
  438. {
  439. if (it->second->id() == AgentId)
  440. return it->first;
  441. ++it;
  442. }
  443. return "";
  444. }
  445. bool CFsProxy::ExtenCall(long JobId, VirtualChan * pChan, std::string CallerNum, std::string CalleeNum)
  446. {
  447. /*Format fmt("bgapi originate {origination_caller_id_number=%lu }user/%lu %s XML %s\r\n%s: %ld");
  448. fmt % pChan->no() % pChan->no() % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;*/
  449. std::string called;
  450. if (getExten(atoi(CalleeNum.c_str())) == nullptr)
  451. {
  452. std::string prefix = CConfig::GetInstance()->gateWayPrefix();
  453. called = CalleeNum.substr(prefix.length(), CalleeNum.length() - prefix.length());
  454. }
  455. else
  456. {
  457. called = CalleeNum;
  458. }
  459. Format fmt("bgapi originate {origination_caller_id_number=%lu,%s=%s}user/%lu %s XML %s\r\n%s: %ld");
  460. fmt % pChan->no() % ESL_VAR_MALL_CALL_CALLEE %called % pChan->no() % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  461. return m_Gateway.sendCmd(fmt.str());
  462. }
  463. bool CFsProxy::PredictionCall(long JobId, std::string CallerNum, std::string CalleeNum)
  464. {
  465. std::string CallString;
  466. if (!m_CallStringMaker.makeCallString(CallerNum, CalleeNum, CallString))
  467. return false;
  468. std::string EslCmd;
  469. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld")
  470. % CallerNum % ESL_VAR_OP_INSTANCE % JobId % CallString % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId);
  471. return m_Gateway.sendCmd(EslCmd);
  472. }
  473. bool CFsProxy::AutoCall(long JobId, std::string CallerNum, std::string CalleeNum, int OpType, long TaskId)
  474. {
  475. std::string CallString;
  476. std::string Caller;
  477. if (!m_CallStringMaker.makeCallString(Caller, CallerNum, CallString))
  478. return false;
  479. std::string EslCmd;
  480. /*EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld,%s=%ld}%s %s XML %s\r\n%s: %ld")
  481. % CallerNum % ESL_VAR_OP_TYPE % OpType % ESL_VAR_TASK_ID % TaskId % CallString % CalleeNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId);*/
  482. std::string called;
  483. if (getExten(atoi(CalleeNum.c_str())) == nullptr)
  484. {
  485. std::string prefix = CConfig::GetInstance()->gateWayPrefix();
  486. if (CalleeNum.substr(0, prefix.length()) == prefix)
  487. called = CalleeNum.substr(prefix.length(), CalleeNum.length() - prefix.length());
  488. }
  489. else
  490. {
  491. called = CalleeNum;
  492. }
  493. 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")
  494. % 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);
  495. return m_Gateway.sendCmd(EslCmd);
  496. }
  497. bool CFsProxy::kill(long JobId, VirtualChan * pChan)
  498. {
  499. return __kill(JobId, pChan->chanId());
  500. }
  501. bool CFsProxy::kill(long JobId, std::string ChanId)
  502. {
  503. return __kill(JobId, ChanId);
  504. }
  505. /*****************************************************************
  506. **【函数名称】 consult
  507. **【函数功能】 协商呼叫
  508. **【参数】
  509. **【返回值】
  510. ****************************************************************/
  511. bool CFsProxy::consult(long JobId, VirtualChan * pChan, std::string DestNum)
  512. {
  513. std::string CallerNum;
  514. std::string CallString;
  515. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
  516. return false;
  517. std::string EslCmd;
  518. EslCmd = boost::str(Format("bgapi uuid_broadcast %s att_xfer::%s\r\n%s: %ld") % pChan->chanId() % CallString % ESL_HEADER_JOB_UUID % JobId);
  519. return m_Gateway.sendCmd(EslCmd);
  520. }
  521. /*****************************************************************
  522. **【函数名称】 insert
  523. **【函数功能】 强插
  524. **【参数】
  525. **【返回值】
  526. ****************************************************************/
  527. bool CFsProxy::insert(long JobId, VirtualChan * pChan, std::string DestSessionId)
  528. {
  529. std::string EslCmd;
  530. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &three_way(%s)\r\n%s: %ld") %
  531. pChan->no() % pChan->no() % DestSessionId% ESL_HEADER_JOB_UUID% JobId);
  532. return m_Gateway.sendCmd(EslCmd);
  533. }
  534. /*****************************************************************
  535. **【函数名称】 intercept
  536. **【函数功能】 强截
  537. **【参数】
  538. **【返回值】
  539. ****************************************************************/
  540. bool CFsProxy::intercept(long JobId, VirtualChan * pChan, std::string DestChanId)
  541. {
  542. std::string EslCmd;
  543. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &intercept(%s)\r\n%s: %ld") %
  544. pChan->no() % pChan->no() % DestChanId % ESL_HEADER_JOB_UUID % JobId);
  545. return m_Gateway.sendCmd(EslCmd);
  546. }
  547. /*****************************************************************
  548. **【函数名称】 listen
  549. **【函数功能】 监听
  550. **【参数】
  551. **【返回值】
  552. ****************************************************************/
  553. bool CFsProxy::listen(long JobId, VirtualChan * pChan, std::string DestChanId)
  554. {
  555. std::string EslCmd;
  556. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%lu}user/%lu &eavesdrop(%s)\r\n%s: %ld") %
  557. pChan->no() % pChan->no() % DestChanId% ESL_HEADER_JOB_UUID% JobId);
  558. return m_Gateway.sendCmd(EslCmd);
  559. }
  560. bool CFsProxy::playAgentNo(VirtualChan * pChan)
  561. {
  562. std::string filePath;
  563. std::string welcomeWords;
  564. /*Format fmt("您好, %s 号话务员为您服务.");
  565. fmt % pChan->no();
  566. if (!tts.TextToAudio(fmt.str(), filePath))*/
  567. std::string agent = getAgentByExten(std::to_string(pChan->no()));
  568. if (agent.empty())
  569. welcomeWords = (boost::str(Format("您好, %s 号话机为您服务.") % pChan->no()));
  570. else
  571. welcomeWords = (boost::str(Format("您好, %s 号话务员为您服务.") % __num2text(agent)));
  572. if (!tts.TextToAudio(welcomeWords, filePath))
  573. {
  574. LOG_WARN("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败", pChan->no());
  575. return false;
  576. }
  577. //std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 暂停50ms,防止太快导致分机放音失败
  578. //Format EslCmd("bgapi uuid_broadcast %s %s both");
  579. Format EslCmd("bgapi sched_broadcast +1 %s playback::%s both");
  580. EslCmd % pChan->chanId() % filePath;
  581. return m_Gateway.sendCmd(EslCmd.str());
  582. }
  583. // 播报坐席工号
  584. bool CFsProxy::playAgentNo(long JobId, VirtualChan * pChan)
  585. {
  586. std::string filePath;
  587. Format fmt("%ld 话务员为您服务");
  588. fmt % pChan->no();
  589. if (!tts.TextToAudio(fmt.str(), filePath))
  590. {
  591. LOG_WARN("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败", pChan->no());
  592. return false;
  593. }
  594. std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 暂停50ms,防止太快导致分机放音失败
  595. Format EslCmd("bgapi uuid_broadcast %s %s both\r\n%s: %ld");
  596. EslCmd % pChan->chanId() % filePath % ESL_HEADER_JOB_UUID % JobId;
  597. return m_Gateway.sendCmd(EslCmd.str());
  598. }
  599. bool CFsProxy::meeting(long JobId, std::string CallerNum, std::string DestNum, std::string MeetingId)
  600. {
  601. std::string CallString;
  602. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
  603. return false;
  604. LOG_INFO(("{FsProxy}: 三方会议呼叫字符串: %s"), CallString.c_str());
  605. std::string EslCmd;
  606. EslCmd = boost::str(Format("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld")
  607. % CallerNum % ESL_VAR_OP_INSTANCE % JobId % CallString % MeetingId % CConfig::GetInstance()->meetingContext() % ESL_HEADER_JOB_UUID % JobId);
  608. return m_Gateway.sendCmd(EslCmd);
  609. }
  610. bool CFsProxy::muteOn(long JobId, VirtualChan * pChan)
  611. {
  612. std::string EslCmd;
  613. EslCmd = boost::str(Format("bgapi uuid_audio %s start write mute -4\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  614. return m_Gateway.sendCmd(EslCmd);
  615. }
  616. bool CFsProxy::muteOff(long JobId, VirtualChan * pChan)
  617. {
  618. std::string EslCmd;
  619. EslCmd = boost::str(Format("bgapi uuid_audio %s stop write mute -4\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  620. return m_Gateway.sendCmd(EslCmd);
  621. }
  622. bool CFsProxy::holdon(long JobId, VirtualChan * pChan)
  623. {
  624. std::string EslCmd;
  625. EslCmd = boost::str(Format("bgapi uuid_hold %s\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  626. return m_Gateway.sendCmd(EslCmd);
  627. }
  628. bool CFsProxy::takeBack(long JobId, VirtualChan * pChan)
  629. {
  630. std::string EslCmd;
  631. EslCmd = boost::str(Format("bgapi uuid_hold off %s\r\n%s: %ld") % pChan->chanId() % ESL_HEADER_JOB_UUID % JobId);
  632. return m_Gateway.sendCmd(EslCmd);
  633. }
  634. bool CFsProxy::record(long JobId, VirtualChan * pChan, std::string RcdFile)
  635. {
  636. std::string EslCmd;
  637. EslCmd = boost::str(Format("bgapi uuid_record %s start %s\r\n%s: %ld") % pChan->chanId() % RcdFile % ESL_HEADER_JOB_UUID % JobId);
  638. return m_Gateway.sendCmd(EslCmd);
  639. }
  640. bool CFsProxy::turnIvr(long JobId, VirtualChan * pChan)
  641. {
  642. Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
  643. EslCmd % pChan->chanId() % "turnmyd" % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  644. return m_Gateway.sendCmd(EslCmd.str());
  645. //m_Gateway.Execte("ivr", "myd", pChan->chanId().c_str());
  646. }
  647. bool CFsProxy::transfer(long JobId, VirtualChan * pChan, std::string DestNum)
  648. {
  649. /*Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
  650. EslCmd % pChan->chanId() % DestNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  651. return m_Gateway.sendCmd(EslCmd.str());*/
  652. //{%s = %ld} % ESL_VAR_OP_INSTANCE % JobId
  653. std::string Param;
  654. Param = boost::str(Format("{%s=%ld}") % ESL_VAR_OP_INSTANCE % JobId);
  655. m_Gateway.Execte(ESL_APP_SET, Param.c_str(), pChan->chanId().c_str());
  656. Format EslCmd("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld");
  657. EslCmd % pChan->chanId() % DestNum % CConfig::GetInstance()->extContext() % ESL_HEADER_JOB_UUID % JobId;
  658. return m_Gateway.sendCmd(EslCmd.str());
  659. }
  660. bool CFsProxy::transfer2Context(long JobId, std::string DestChanId, std::string Exten, std::string Context, bool BothSide)
  661. {
  662. std::string EslCmd;
  663. if (BothSide)
  664. EslCmd = boost::str(Format("bgapi uuid_transfer %s -both %s xml %s\r\n%s: %ld") % DestChanId % Exten % Context % ESL_HEADER_JOB_UUID % JobId);
  665. else
  666. EslCmd = boost::str(Format("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld") % DestChanId % Exten % Context % ESL_HEADER_JOB_UUID % JobId);
  667. return m_Gateway.sendCmd(EslCmd);
  668. }
  669. bool CFsProxy::setChannelVariable(long JobId, std::string DestChanId, std::string VarName, std::string Content)
  670. {
  671. std::string EslCmd;
  672. Format fmt("bgapi uuid_setvar %s %s %s\r\n%s: %ld");
  673. fmt % DestChanId % VarName % Content % ESL_HEADER_JOB_UUID % JobId;
  674. EslCmd = fmt.str();
  675. bool ret = m_Gateway.sendCmd(EslCmd);
  676. return ret;
  677. }
  678. bool CFsProxy::ExecteApp(std::string App, std::string Param, std::string ChanId)
  679. {
  680. return m_Gateway.Execte(App.c_str(), Param.c_str(), ChanId.c_str());
  681. }
  682. bool CFsProxy::sendDtmf(long JobId, std::string DestChanId, std::string Content)
  683. {
  684. std::string EslCmd;
  685. Format fmt("bgapi uuid_send_dtmf %s %s\r\n%s: %ld");
  686. fmt %DestChanId %Content %ESL_HEADER_JOB_UUID % JobId;
  687. EslCmd = fmt.str();
  688. bool ret = m_Gateway.sendCmd(EslCmd);
  689. return ret;
  690. }
  691. bool CFsProxy::getConferenceMemberId(std::string MeetingId, std::string ChanId, std::string & MemberId)
  692. {
  693. return m_Gateway.getConferenceMemberId(MeetingId, ChanId, MemberId);
  694. }
  695. bool CFsProxy::setConferenceExitSound(std::string MeetingId, bool isPlay)
  696. {
  697. std::string EslCmd;
  698. Format fmt("bgapi conference %s exit_sound %s");
  699. if (isPlay) {
  700. fmt %MeetingId %"on";
  701. }
  702. else {
  703. fmt %MeetingId %"off";
  704. }
  705. EslCmd = fmt.str();
  706. LOG_INFO("{CFsProxy}: 设置会议人员离开是否播放声音[%s]", EslCmd.c_str());
  707. bool ret = m_Gateway.sendCmd(EslCmd);
  708. return ret;
  709. }
  710. bool CFsProxy::setConferenceIsMute(long JobId, std::string MeetingId, std::string MemberId, bool isMute)
  711. {
  712. std::string EslCmd;
  713. if (isMute)
  714. {
  715. EslCmd = (Format("bgapi conference %s deaf %s") % MeetingId % MemberId).str();
  716. m_Gateway.sendCmd(EslCmd);
  717. EslCmd = (Format("bgapi conference %s mute %s\r\n%s: %ld") % MeetingId % MemberId % ESL_HEADER_JOB_UUID % JobId).str();
  718. }
  719. else
  720. {
  721. EslCmd = (Format("bgapi conference %s undeaf %s") % MeetingId % MemberId).str();
  722. m_Gateway.sendCmd(EslCmd);
  723. EslCmd = (Format("bgapi conference %s stop all %s") % MeetingId % MemberId).str();
  724. m_Gateway.sendCmd(EslCmd);
  725. EslCmd = (Format("bgapi conference %s unmute %s\r\n%s: %ld") % MeetingId % MemberId % ESL_HEADER_JOB_UUID % JobId).str();
  726. }
  727. LOG_ERROR("{CFsProxy}: 设置会议人员是否静音[%s]", EslCmd.c_str());
  728. bool ret = m_Gateway.sendCmd(EslCmd);
  729. return ret;
  730. }
  731. std::string CFsProxy::creatJson(std::string type, std::string result)
  732. {
  733. // Json::Value root;
  734. // Json::StreamWriterBuilder jsrocd;
  735. // root["Type"] = type;
  736. // root["Result"] = result;
  737. // return Json::writeString(jsrocd, root);
  738. // jsoncpp中文乱码
  739. Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\" }");
  740. fmt %type %result;
  741. return fmt.str();
  742. }
  743. std::string CFsProxy::creatJson(std::string type, bool result)
  744. {
  745. Json::Value root;
  746. Json::StreamWriterBuilder jsrocd;
  747. std::unique_ptr<Json::StreamWriter> write(jsrocd.newStreamWriter());
  748. Json::OStringStream os;
  749. root["Type"] = type;
  750. root["Result"] = result;
  751. write->write(root, &os);
  752. return os.str();
  753. }
  754. std::string CFsProxy::creatJson(std::string type, bool result, std::string desc)
  755. {
  756. Json::Value root;
  757. Json::StreamWriterBuilder jsrocd;
  758. std::unique_ptr<Json::StreamWriter> write(jsrocd.newStreamWriter());
  759. Json::OStringStream os;
  760. root["Type"] = type;
  761. root["Result"] = result;
  762. root["Desc"] = desc;
  763. write->write(root, &os);
  764. return os.str();
  765. }
  766. std::string CFsProxy::creatJson(std::string type, std::string result, std::string agentId)
  767. {
  768. Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\",\"AgentID\":\"%s\" }");
  769. fmt %type %result %agentId;
  770. return fmt.str();
  771. }
  772. std::string CFsProxy::creatJson(std::string type, std::string result, std::string agentId, std::string extenNo)
  773. {
  774. Format fmt("{ \"Type\":\"%s\",\"Result\":\"%s\",\"AgentID\":\"%s\",\"ExtenNo\":\"%s\" }");
  775. fmt %type %result %agentId %extenNo;
  776. return fmt.str();
  777. }
  778. std::string CFsProxy::creatJsonIncoming(std::string callid, std::string caller, std::string callee, std::string trunkNumber, int incomintType)
  779. {
  780. Json::Value root;
  781. Json::StreamWriterBuilder jsrocd;
  782. root["Type"] = "Incoming";
  783. root["Result"] = true;
  784. root["CallID"] = callid; // callid
  785. root["Number"] = caller; // 主叫
  786. root["CalleeNumber"] = callee;
  787. root["TrunkNumber"] = trunkNumber; // 中继号
  788. root["IncomintType"] = incomintType; // 来电类型,来电,外呼,自动外呼
  789. return Json::writeString(jsrocd, root);
  790. }
  791. std::string CFsProxy::creatJsonRecord(std::string callid, std::string recordPath)
  792. {
  793. Json::Value root;
  794. Json::StreamWriterBuilder jsrocd;
  795. root["Type"] = "RecordPath";
  796. root["Result"] = true;
  797. root["CallID"] = callid; // callid
  798. root["RecPath"] = recordPath;
  799. return Json::writeString(jsrocd, root);
  800. }
  801. bool CFsProxy::__init()
  802. {
  803. __initTrunkChan();
  804. if (m_Gateway.start())
  805. {
  806. m_Gateway.hangupAll();
  807. static bool del = m_Gateway.delAgentAll(); // 使用static 保证delagentall运行后在fs连接断开后重连成功后不会再一次执行
  808. boost::ignore_unused(del); // 取消del不使用的警告
  809. if (m_Gateway.scanExten())
  810. {
  811. LOG_INFO_S("{FsProxy}: FreeSWITCH代理初始化成功");
  812. return true;
  813. }
  814. else
  815. {
  816. LOG_ERROR_S("{FsProxy}: FreeSWITCH代理初始化失败, 扫描分机资源失败");
  817. return false;
  818. }
  819. }
  820. else
  821. {
  822. LOG_ERROR_S("{FsProxy}: FreeSWITCH代理初始化失败, ESL网关启动失败");
  823. return false;
  824. }
  825. }
  826. uint32_t CFsProxy::__transLogicState2CtiState(DEV_RES_TYPE ChanType, CHAN_LOGIC_STATE State)
  827. {
  828. if (ChanType == DEV_RES_TYPE_EXT)
  829. {
  830. uint32_t HoldMask = State & HELD_STATE_IND_MASK;
  831. switch (State & HELD_STATE_FILTER_MASK)
  832. {
  833. case CHAN_LOGIC_STATE_DISABLED: return INNER_STATE_DISABLED; // 不可用
  834. case CHAN_LOGIC_STATE_FREE: return INNER_STATE_FREE | HoldMask; // 空闲
  835. case CHAN_LOGIC_STATE_INIT: return INNER_STATE_INIT | HoldMask; // 摘机等待拨号
  836. case CHAN_LOGIC_STATE_DIALING: return INNER_STATE_DIALING | HoldMask; // 拨号
  837. case CHAN_LOGIC_STATE_RING_BACK: return INNER_STATE_RING_BACK | HoldMask; // 呼出振铃
  838. case CHAN_LOGIC_STATE_ALERTING: return INNER_STATE_ALERTING | HoldMask; // 来电振铃
  839. case CHAN_LOGIC_STATE_TALKING: return INNER_STATE_TALKING | HoldMask; // 通话中
  840. default:
  841. return INNER_STATE_DISABLED;
  842. }
  843. }
  844. else
  845. {
  846. switch (State)
  847. {
  848. case CHAN_LOGIC_STATE_DISABLED: return TRUNK_STATE_DISABLED; // 不可用
  849. case CHAN_LOGIC_STATE_FREE: return TRUNK_STATE_FREE; // 空闲
  850. case CHAN_LOGIC_STATE_DIALING: return TRUNK_STATE_DIALING; // 拨号
  851. case CHAN_LOGIC_STATE_RING_BACK: return TRUNK_STATE_RING_BACK; // 呼出振铃
  852. case CHAN_LOGIC_STATE_ALERTING: return TRUNK_STATE_ALERTING; // 来电振铃
  853. case CHAN_LOGIC_STATE_STANDBY:
  854. case CHAN_LOGIC_STATE_TALKING: return TRUNK_STATE_TALKING; // 通话中
  855. default:
  856. return TRUNK_STATE_UNKNOWN;
  857. }
  858. }
  859. }
  860. void CFsProxy::__addExten(uint32_t ExtenNo)
  861. {
  862. ChanExten* pExten = new ChanExten(this, ExtenNo);
  863. m_MapChanExt[ExtenNo] = pExten;
  864. pExten->regist();
  865. }
  866. void CFsProxy::__delExten(uint32_t ExtenNo)
  867. {
  868. auto it = m_MapChanExt.find(ExtenNo);
  869. if (it != m_MapChanExt.end())
  870. {
  871. if (it->second->isFree())
  872. {
  873. FS_LINK_DELETE(it->second);
  874. m_MapChanExt.erase(it);
  875. }
  876. else
  877. {
  878. it->second->discard(true);
  879. }
  880. }
  881. }
  882. void CFsProxy::__freeExten(void)
  883. {
  884. auto it = m_MapChanExt.begin();
  885. while (it != m_MapChanExt.end())
  886. {
  887. FS_LINK_DELETE(it->second);
  888. ++it;
  889. }
  890. m_MapChanExt.clear();
  891. }
  892. void CFsProxy::__initTrunkChan(void)
  893. {
  894. /*for (int i = 1; i <= CConfig::GetInstance()->trunkCount(); ++i)*/
  895. for (int i = 1; i <= SoftAuth::GetInstance()->trunkNum(); ++i)
  896. {
  897. ChanTrunk* pTrunk = new ChanTrunk(this, i);
  898. m_ArrayTrunk.emplace_back(pTrunk);
  899. pTrunk->regist();
  900. }
  901. }
  902. void CFsProxy::__freeTrunkChan(void)
  903. {
  904. ChanTrunk* pTrunk = nullptr;
  905. for (size_t i = 0; i < m_ArrayTrunk.size(); ++i)
  906. {
  907. pTrunk = m_ArrayTrunk[i];
  908. FS_LINK_DELETE(pTrunk);
  909. }
  910. m_ArrayTrunk.clear();
  911. }
  912. Session * CFsProxy::__getSessionByChanId(const string & ChanId)
  913. {
  914. auto it = m_MapSession.begin();
  915. while (it != m_MapSession.end())
  916. {
  917. if (it->second->isContainChanId(ChanId))
  918. return it->second;
  919. ++it;
  920. }
  921. return nullptr;
  922. }
  923. Session * CFsProxy::__getSession(string SessionId)
  924. {
  925. auto it = m_MapSession.find(SessionId);
  926. if (it != m_MapSession.end())
  927. return it->second;
  928. return nullptr;
  929. }
  930. Session * CFsProxy::__getSession(PCHAN_EVENT_NOTIFY pNotify)
  931. {
  932. Session* pSession = nullptr;
  933. pSession = __getSession(pNotify->CallId);
  934. if (pSession == nullptr)
  935. {
  936. if (!pNotify->CcId.empty())
  937. {
  938. LOG_INFO(boost::str(Format("CallId查不到,CcId不为空,使用CcId代替CallId进一步查找Session,CallId[%s],CcId[%s]") % pNotify->CallId % pNotify->CcId).c_str());
  939. pSession = __getSession(pNotify->CcId);
  940. if (pSession != nullptr) return pSession;
  941. LOG_WARN(boost::str(Format("CallId,CcId都查不到session,CallId[%s],CcId[%s]") % pNotify->CallId % pNotify->CcId).c_str());
  942. }
  943. LOG_INFO(boost::str(Format("CallId查不到,使用ChanId代替CallId进一步查找Session,CallId[%s],ChanId[%s]") % pNotify->CallId % pNotify->ChanId).c_str());
  944. pSession = __getSessionByChanId(pNotify->ChanId);
  945. if (pSession != nullptr) return pSession;
  946. LOG_WARN(boost::str(Format("CallId,ChanId都查不到session,CallId[%s],ChanId[%s]") % pNotify->CallId % pNotify->ChanId).c_str());
  947. pSession = new Session(this, pNotify->CallId);
  948. pSession->prepare(pNotify);
  949. m_MapSession[pNotify->CallId] = pSession;
  950. Format fmt("{CFsProxy}:Add Session[%s],ChanId[%s],EventId[%d]");
  951. fmt % pSession->id() % pNotify->ChanId % pNotify->EventId;
  952. LOG_INFO_S(fmt.str());
  953. }
  954. return pSession;
  955. }
  956. void CFsProxy::__delSession(std::string SessionId)
  957. {
  958. if (SessionId.empty())
  959. return;
  960. auto it = m_MapSession.find(SessionId);
  961. if (it != m_MapSession.end())
  962. {
  963. Format fmt("{FsProxy}: Delete Session[%s]");
  964. fmt % it->second->id();
  965. LOG_INFO_S(fmt.str());
  966. FS_LINK_DELETE(it->second);
  967. m_MapSession.erase(it);
  968. }
  969. }
  970. void CFsProxy::__freeSession(void)
  971. {
  972. auto it = m_MapSession.begin();
  973. while (it != m_MapSession.end())
  974. {
  975. FS_LINK_DELETE(it->second);
  976. ++it;
  977. }
  978. m_MapSession.clear();
  979. }
  980. bool CFsProxy::__kill(long JobId, string ChanId)
  981. {
  982. string EslCmd;
  983. if (JobId == FS_LINK_JOBID_INVALID)
  984. EslCmd = boost::str(Format("bgapi uuid_kill %s") % ChanId);
  985. else
  986. EslCmd = boost::str(Format("bgapi uuid_kill %s\r\n%s: %ld") % ChanId % ESL_HEADER_JOB_UUID % JobId);
  987. return m_Gateway.sendCmd(EslCmd);
  988. }
  989. std::string CFsProxy::__getExtenByAgent(std::string agentId)
  990. {
  991. std::unique_lock<std::mutex>lock(m_AgentLock);
  992. auto it = m_MapAgent.begin();
  993. while (it != m_MapAgent.end())
  994. {
  995. if (it->second->id() == agentId)
  996. {
  997. return it->first;
  998. }
  999. ++it;
  1000. }
  1001. return "";
  1002. }
  1003. bool CFsProxy::__login(std::string AgentID, std::string ExtenNo)
  1004. {
  1005. bool ret = true;
  1006. std::string cmd;
  1007. cmd = boost::str(Format("bgapi callcenter_config agent add %1% Callback") % AgentID); // 添加座席
  1008. ret &= m_Gateway.sendCmd(cmd);
  1009. cmd = boost::str(Format("bgapi callcenter_config agent set contact %1% [call_timeout=30]user/%2%") % AgentID % ExtenNo); // 设置呼叫字符串
  1010. ret &= m_Gateway.sendCmd(cmd);
  1011. cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available ") % AgentID); // 座席登录后默认空闲
  1012. ret &= m_Gateway.sendCmd(cmd);
  1013. cmd = boost::str(Format("bgapi callcenter_config agent set state %1% Waiting") % AgentID); // 座席登录后默认空闲
  1014. ret &= m_Gateway.sendCmd(cmd);
  1015. cmd = boost::str(Format("bgapi callcenter_config agent set max_no_answer %1% 0") % AgentID); // 0禁用
  1016. ret &= m_Gateway.sendCmd(cmd);
  1017. //cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% 20") % AgentID); // 话后处理时长
  1018. cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% %2%") % AgentID % CConfig::GetInstance()->postProcessTime()); // 话后处理时长
  1019. ret &= m_Gateway.sendCmd(cmd);
  1020. cmd = boost::str(Format("bgapi callcenter_config agent set reject_delay_time %1% 0") % AgentID);
  1021. ret &= m_Gateway.sendCmd(cmd);
  1022. cmd = boost::str(Format("bgapi allcenter_config agent set busy_delay_time %1% 0") % AgentID);
  1023. ret &= m_Gateway.sendCmd(cmd);
  1024. cmd = boost::str(Format("bgapi callcenter_config tier add support@default %1% 1 1") % AgentID); // 添加梯队 到队列等价于坐席组
  1025. ret &= m_Gateway.sendCmd(cmd);
  1026. return ret;
  1027. }
  1028. bool CFsProxy::__login(std::string AgentID, std::string ExtenNo, std::list<std::string> Group)
  1029. {
  1030. bool ret = true;
  1031. std::string cmd;
  1032. cmd = boost::str(Format("bgapi callcenter_config agent add %1% Callback") % AgentID); // 添加座席
  1033. ret &= m_Gateway.sendCmd(cmd);
  1034. cmd = boost::str(Format("bgapi callcenter_config agent set contact %1% [call_timeout=30]user/%2%") % AgentID % ExtenNo); // 设置呼叫字符串
  1035. ret &= m_Gateway.sendCmd(cmd);
  1036. cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available ") % AgentID); // 座席登录后默认空闲
  1037. //cmd = boost::str(Format("bgapi callcenter_config agent set status %1% Available (On Demand)") % AgentID); // 座席登录后默认空闲
  1038. ret &= m_Gateway.sendCmd(cmd);
  1039. cmd = boost::str(Format("bgapi callcenter_config agent set state %1% Waiting") % AgentID); // 座席登录后默认空闲
  1040. ret &= m_Gateway.sendCmd(cmd);
  1041. cmd = boost::str(Format("bgapi callcenter_config agent set max_no_answer %1% 0") % AgentID); // 0禁用
  1042. ret &= m_Gateway.sendCmd(cmd);
  1043. cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% 2") % AgentID); // 话后处理时长,2秒
  1044. //cmd = boost::str(Format("bgapi callcenter_config agent set wrap_up_time %1% %2%") % AgentID %CConfig::GetInstance()->postProcessTime());
  1045. ret &= m_Gateway.sendCmd(cmd);
  1046. cmd = boost::str(Format("bgapi callcenter_config agent set reject_delay_time %1% 0") % AgentID);
  1047. ret &= m_Gateway.sendCmd(cmd);
  1048. cmd = boost::str(Format("bgapi allcenter_config agent set busy_delay_time %1% 0") % AgentID);
  1049. ret &= m_Gateway.sendCmd(cmd);
  1050. for (std::string &var : Group)
  1051. {
  1052. cmd = boost::str(Format("bgapi callcenter_config tier add %2% %1% 1 1") % AgentID %var); // 添加梯队 到队列等价于坐席组
  1053. ret &= m_Gateway.sendCmd(cmd);
  1054. }
  1055. return ret;
  1056. }
  1057. bool CFsProxy::__logout(std::string AgentID, std::string AgentExten)
  1058. {
  1059. bool ret = true;
  1060. std::string cmd;
  1061. cmd = boost::str(Format("bgapi callcenter_config agent del %1% ") % AgentID);
  1062. ret &= m_Gateway.sendCmd(cmd);
  1063. /*cmd = boost::str(Format("bgapi callcenter_config tier del support@default %1% ") % AgentID);
  1064. ret &= m_Gateway.sendCmd(cmd);*/
  1065. std::unique_lock<std::mutex>lock(m_AgentLock);
  1066. auto it = m_MapAgent.find(AgentExten);
  1067. if (ret && it != m_MapAgent.end())
  1068. {
  1069. for (std::string &var : it->second->groups())
  1070. {
  1071. cmd = boost::str(Format("bgapi callcenter_config tier del %2% %1% ") % AgentID %var);
  1072. ret &= m_Gateway.sendCmd(cmd);
  1073. }
  1074. it->second->removeAgent(AgentID);
  1075. FS_LINK_DELETE(it->second);
  1076. m_MapAgent.erase(it);
  1077. }
  1078. return ret;
  1079. }
  1080. bool CFsProxy::__freeAgent()
  1081. {
  1082. std::unique_lock<std::mutex>lock(m_AgentLock);
  1083. auto it = m_MapAgent.begin();
  1084. while (it != m_MapAgent.end())
  1085. {
  1086. // __logout(it->second->id());
  1087. //FS_LINK_DELETE(it->second);
  1088. Agent* agent = it->second;
  1089. FS_LINK_DELETE(agent);
  1090. m_MapAgent.erase(it++);
  1091. }
  1092. return false;
  1093. }
  1094. bool CFsProxy::__setState(std::string AgentID, bool isFree)
  1095. {
  1096. Format fmt("bgapi callcenter_config agent set status %1% '%2%'");
  1097. if (isFree)
  1098. fmt %AgentID %"Available"; // 置闲
  1099. //fmt %AgentID %"Available (On Demand)"; // 置闲
  1100. else
  1101. fmt %AgentID %"On Break"; // 置忙
  1102. //if (isFree)
  1103. //{
  1104. // Format fmt("bgapi callcenter_config agent set state %1% '%2%'");
  1105. // fmt %AgentID %"Waiting"; // 置闲时改为可进电话
  1106. //}
  1107. return m_Gateway.sendCmd(fmt.str());
  1108. }
  1109. bool CFsProxy::__setStatePostProcessing(std::string AgentID)
  1110. {
  1111. Format fmt("bgapi callcenter_config agent set status %1% '%2%'");
  1112. fmt %AgentID %"Available (On Demand)"; // 空闲不接电话/话后处理
  1113. return m_Gateway.sendCmd(fmt.str());
  1114. }
  1115. //std::string text[] = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
  1116. std::string text[] = { "零","一","二","三","四","五","六","七","八","九" };
  1117. std::string CFsProxy::__num2text(std::string data)
  1118. {
  1119. std::string word;
  1120. try
  1121. {
  1122. for (std::size_t i = 0; i < data.length(); i++)
  1123. {
  1124. switch (data.at(i))
  1125. {
  1126. case '0':
  1127. word += text[0];
  1128. break;
  1129. case '1':
  1130. word += text[1];
  1131. break;
  1132. case '2':
  1133. word += text[2];
  1134. break;
  1135. case '3':
  1136. word += text[3];
  1137. break;
  1138. case '4':
  1139. word += text[4];
  1140. break;
  1141. case '5':
  1142. word += text[5];
  1143. break;
  1144. case '6':
  1145. word += text[6];
  1146. break;
  1147. case '7':
  1148. word += text[7];
  1149. case '8':
  1150. word += text[8];
  1151. break;
  1152. case '9':
  1153. word += text[9];
  1154. break;
  1155. default:
  1156. word += data.at(i);
  1157. break;
  1158. }
  1159. }
  1160. }
  1161. catch (const std::exception& e)
  1162. {
  1163. std::string str;
  1164. str = e.what() + data;
  1165. LOG_ERROR_S(str);
  1166. word = data;
  1167. }
  1168. return word;
  1169. }
  1170. void CFsProxy::__recvMsgFun(websocketpp::connection_hdl hdl, string msg)
  1171. {
  1172. const long conID = reinterpret_cast<long>(hdl.lock().get());
  1173. try
  1174. {
  1175. Json::Value root;
  1176. Json::CharReaderBuilder builder;
  1177. std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  1178. std::string errs;
  1179. bool ret = reader->parse(msg.c_str(), msg.c_str() + msg.length(), &root, &errs);
  1180. if (!ret || !errs.empty())
  1181. {
  1182. if (errs.empty()) errs = "空";
  1183. Format fmt("Json 解析错误,errs = %s ,msg = %s");
  1184. fmt %errs %msg;
  1185. LOG_WARN_S(fmt.str());
  1186. return;
  1187. }
  1188. if (!root.isMember("Type"))
  1189. {
  1190. Format fmt("该条Json消息格式不能识别 ,msg = %s");
  1191. fmt %msg;
  1192. LOG_WARN_S(fmt.str());
  1193. return;
  1194. }
  1195. LOG_DEBUG_S(msg);
  1196. const string type = root["Type"].asString();
  1197. const string AgentID = root["AgentID"].asString();
  1198. const string AgentExten = root["AgentExten"].asString();
  1199. uint32_t ExtenNo = 0;
  1200. if (sscanf(AgentExten.c_str(), "%u", &ExtenNo) != 1)
  1201. {
  1202. Format fmt("分机号为非UInt,msg = %s");
  1203. fmt % msg;
  1204. LOG_WARN_S(fmt.str());
  1205. m_Server.sendMsg(hdl, fmt.str());
  1206. return;
  1207. }
  1208. if ("Login" == type) { // 签入
  1209. if (SoftAuth::GetInstance()->acdNum() <= (int)m_MapAgent.size())
  1210. {
  1211. Format fmt("ACD授权数量已达上限,Agent = %s,Exten = %d");
  1212. fmt % AgentID % AgentExten;
  1213. LOG_INFO_S(fmt.str());
  1214. m_Server.sendMsg(hdl, creatJson(type, std::string("签入失败,ACD授权数量已达上限")));
  1215. return;
  1216. }
  1217. if (getExten(ExtenNo) == nullptr)
  1218. {
  1219. Format fmt("分机不存在,Agent = %s,Exten = %d");
  1220. fmt % AgentID % AgentExten;
  1221. LOG_INFO_S(fmt.str());
  1222. m_Server.sendMsg(hdl, creatJson(type, std::string("分机号不存在")));
  1223. return;
  1224. }
  1225. // 判断 坐席,分机是否已被签入
  1226. std::unique_lock<std::mutex>lock(m_AgentLock);
  1227. auto it = m_MapAgent.begin();
  1228. while (it != m_MapAgent.end()) {
  1229. if (it->second->id() == AgentID) {
  1230. Format fmt("坐席已被绑定,Agent = %s,Exten = %s");
  1231. fmt % AgentID % AgentExten;
  1232. LOG_INFO_S(fmt.str());
  1233. m_Server.sendMsg(hdl, creatJson(type, std::string("坐席已被绑定")));
  1234. return;
  1235. }
  1236. else if (it->first == AgentExten) {
  1237. Format fmt("分机已被绑定,Agent = %s,Exten = %s");
  1238. fmt % AgentID % AgentExten;
  1239. LOG_INFO_S(fmt.str());
  1240. m_Server.sendMsg(hdl, creatJson(type, std::string("分机已被绑定")));
  1241. return;
  1242. }
  1243. ++it;
  1244. }
  1245. std::string AgentGroup = root["AgentGroup"].asString();
  1246. Agent *agent = new Agent(AgentID, AgentExten, AgentGroup);
  1247. agent->hdl() = hdl;
  1248. m_MapAgent[AgentExten] = agent; // 分机号作为key
  1249. if (!__login(AgentID, AgentExten, agent->groups())) {
  1250. m_MapAgent.erase(AgentExten);
  1251. Format fmt("座席签入失败,Agent = %s,Exten = %d, %s");
  1252. fmt % AgentID % AgentExten % hdl.lock().get();
  1253. LOG_INFO_S(fmt.str());
  1254. m_Server.sendMsg(hdl, creatJson(type, false));
  1255. return;
  1256. }
  1257. Format fmt("座席签入成功,Agent = %s,Exten = %d,AgentGroup = %s %s");
  1258. fmt % AgentID % AgentExten % AgentGroup % hdl.lock().get();
  1259. LOG_INFO_S(fmt.str());
  1260. m_Server.sendMsg(hdl, creatJson(type, true));
  1261. __agentDetail(lock.release()); // 通知坐席详情变化
  1262. std::string data = creatJson("Monitor", "空闲", AgentID, AgentExten);
  1263. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1264. }
  1265. else if ("Logout" == type) { // 签出
  1266. bool ret = __logout(AgentID, AgentExten);
  1267. Format fmt("座席签出[%d],Agent = %s,Exten = %d, %s");
  1268. fmt %ret % AgentID % AgentExten % hdl.lock().get();
  1269. LOG_INFO_S(fmt.str());
  1270. m_Server.sendMsg(hdl, creatJson(type, ret));
  1271. __agentDetail(); // 通知坐席详情变化
  1272. std::string data = creatJson("Monitor", "签出", AgentID, AgentExten);
  1273. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1274. }
  1275. else if ("SayBusy" == type) { // 置忙
  1276. bool ret = __setState(AgentID, false);
  1277. std::unique_lock<std::mutex>lock(m_AgentLock);
  1278. auto it = m_MapAgent.find(AgentExten);
  1279. if (it != m_MapAgent.end())
  1280. it->second->setState(AGENT_STATE_REPOSE);
  1281. __agentDetail(lock.release()); // 通知坐席详情变化
  1282. m_Server.sendMsg(hdl, creatJson(type, ret));
  1283. std::string data = creatJson("Monitor", "小休", AgentID, AgentExten);
  1284. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1285. }
  1286. else if ("SayFree" == type) { // 置闲
  1287. bool ret = __setState(AgentID, true);
  1288. std::unique_lock<std::mutex>lock(m_AgentLock);
  1289. auto it = m_MapAgent.find(AgentExten);
  1290. if (it != m_MapAgent.end())
  1291. it->second->setState(AGENT_STATE_FREE);
  1292. __agentDetail(lock.release()); // 通知坐席详情变化
  1293. m_Server.sendMsg(hdl, creatJson(type, ret));
  1294. std::string data = creatJson("Monitor", "空闲", AgentID, AgentExten);
  1295. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1296. }
  1297. else if ("ForceAgentState" == type) { // 强制更改座席状态
  1298. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1299. const string Exten = __getExtenByAgent(TargetAgentID);
  1300. bool ret = __setState(TargetAgentID, true);
  1301. {
  1302. std::unique_lock<std::mutex>lock(m_AgentLock);
  1303. auto it = m_MapAgent.find(Exten);
  1304. if (it != m_MapAgent.end())
  1305. it->second->setState(AGENT_STATE_FREE);
  1306. else
  1307. ret = false;
  1308. __agentDetail(lock.release()); // 通知坐席详情变化
  1309. }
  1310. LineOpParam param;
  1311. param.szParam1 = AgentID; // 主控坐席id
  1312. param.szParam2 = TargetAgentID; // 被叫坐席id
  1313. param.nParam1 = AgentExten; // 主控分机
  1314. param.szParam3 = Exten; // 被控分机
  1315. param.nParam2 = ret;
  1316. ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_AGENT_MONI_FORCE_STATE, ExtenNo, &param);
  1317. m_Server.sendMsg(hdl, creatJson(type, ret));
  1318. }
  1319. else if ("Hold" == type) { // 保持
  1320. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HOLD, ExtenNo, nullptr);
  1321. if (!ret)
  1322. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1323. }
  1324. else if ("Retrieve" == type) { // 接回保持
  1325. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TAKEBACK, ExtenNo, nullptr);
  1326. if (!ret)
  1327. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1328. }
  1329. else if ("MakeCall" == type) { // 外呼
  1330. const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
  1331. LineOpParam param;
  1332. param.szParam1 = AgentExten;
  1333. param.szParam2 = DestinationNumber;
  1334. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MAKE_CALL, ExtenNo, &param);
  1335. if (!ret)
  1336. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1337. }
  1338. else if ("Transfer" == type) { // 转移
  1339. const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
  1340. LineOpParam param;
  1341. param.szParam1 = AgentExten;
  1342. param.szParam2 = DestinationNumber;
  1343. auto it = m_MapAgent.find(DestinationNumber);
  1344. if (it != m_MapAgent.end())
  1345. {
  1346. if (it->second->isRepose())
  1347. {
  1348. Format fmt("坐席[%s]-[%s]转移失败,目标[%s][%s]小休中");
  1349. fmt %AgentID %AgentExten %it->second->id() % DestinationNumber;
  1350. LOG_WARN_S(fmt.str());
  1351. m_Server.sendMsg(hdl, creatJson(type, false));
  1352. return;
  1353. }
  1354. }
  1355. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TRANSFER, ExtenNo, &param);
  1356. if (!ret)
  1357. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1358. }
  1359. else if ("Meeting" == type) { // 会议
  1360. const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
  1361. {
  1362. auto it = m_MapAgent.find(DestinationNumber);
  1363. if (it != m_MapAgent.end())
  1364. {
  1365. if (it->second->isRepose())
  1366. {
  1367. Format fmt("坐席[%s]-[%s]操作会议失败,目标[%s][%s]小休中");
  1368. fmt %AgentID %AgentExten %it->second->id() % DestinationNumber;
  1369. LOG_WARN_S(fmt.str());
  1370. m_Server.sendMsg(hdl, creatJson(type, false));
  1371. return;
  1372. }
  1373. }
  1374. }
  1375. auto it = getExten(ExtenNo);
  1376. std::string meetingID;
  1377. if (it != nullptr)
  1378. {
  1379. meetingID = it->sessionId();
  1380. }
  1381. LOG_DEBUG("会议ID[%s]", meetingID.c_str());
  1382. LineOpParam param;
  1383. param.nParam1 = meetingID; // 会议id
  1384. param.szParam1 = AgentExten;
  1385. param.szParam2 = DestinationNumber;
  1386. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_CONFERENCE, ExtenNo, &param);
  1387. if (!ret)
  1388. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1389. }
  1390. else if ("MultiMeeting" == type) { // 多步会议
  1391. const string DestinationNumber = root["DestinationNumber"].asString(); // 目标号码
  1392. {
  1393. auto it = m_MapAgent.find(DestinationNumber);
  1394. if (it != m_MapAgent.end())
  1395. {
  1396. if (it->second->isRepose())
  1397. {
  1398. Format fmt("坐席[%s]-[%s]操作多步会议失败,目标[%s][%s]小休中");
  1399. fmt %AgentID %AgentExten %it->second->id() % DestinationNumber;
  1400. LOG_WARN_S(fmt.str());
  1401. m_Server.sendMsg(hdl, creatJson(type, false));
  1402. return;
  1403. }
  1404. }
  1405. }
  1406. auto it = getExten(ExtenNo);
  1407. std::string meetingID;
  1408. if (it != nullptr)
  1409. {
  1410. meetingID = it->sessionId();
  1411. }
  1412. LOG_DEBUG("多步会议ID[%s]", meetingID.c_str());
  1413. LineOpParam param;
  1414. param.nParam1 = meetingID; // 会议id
  1415. param.szParam1 = AgentExten;
  1416. param.szParam2 = DestinationNumber;
  1417. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MULTI_CONFERENCE, ExtenNo, &param);
  1418. if (!ret)
  1419. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1420. }
  1421. else if ("MultiTakeback" == type) { // 多步会议接回
  1422. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MULTI_CONFERENCE_TAKEBACK, ExtenNo, nullptr);
  1423. if (!ret)
  1424. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1425. }
  1426. else if ("DropCall" == type) { // 挂机
  1427. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HANG_UP, ExtenNo, nullptr);
  1428. if (!ret)
  1429. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1430. }
  1431. else if ("Break" == type) { // 强拆
  1432. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1433. const string Exten = __getExtenByAgent(TargetAgentID);
  1434. LineOpParam param;
  1435. param.szParam1 = AgentExten;
  1436. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_HANG_UP, atoi(Exten.c_str()), &param);
  1437. if (!ret)
  1438. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1439. }
  1440. else if ("Listen" == type) { // 监听
  1441. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1442. const string Exten = __getExtenByAgent(TargetAgentID);
  1443. LineOpParam param;
  1444. param.szParam1 = AgentExten;
  1445. param.szParam2 = Exten;
  1446. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_LISTEN, ExtenNo, &param);
  1447. if (!ret)
  1448. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1449. }
  1450. else if ("Insert" == type) { // 强插
  1451. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1452. const string Exten = __getExtenByAgent(TargetAgentID);
  1453. LineOpParam param;
  1454. param.szParam1 = AgentExten;
  1455. param.szParam2 = Exten;
  1456. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_INSERT, ExtenNo, &param);
  1457. if (!ret)
  1458. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1459. }
  1460. else if ("Instead" == type) { // 代接
  1461. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1462. const string Exten = __getExtenByAgent(TargetAgentID);
  1463. LineOpParam param;
  1464. param.szParam1 = AgentExten;
  1465. param.szParam2 = Exten;
  1466. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_INSTEAD, ExtenNo, &param);
  1467. if (!ret)
  1468. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1469. }
  1470. else if ("Intercept" == type) { // 强截
  1471. const string TargetAgentID = root["TargetAgentID"].asString(); // 目标号码
  1472. const string Exten = __getExtenByAgent(TargetAgentID);
  1473. LineOpParam param;
  1474. param.szParam1 = AgentExten;
  1475. param.szParam2 = Exten;
  1476. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_GRAB, ExtenNo, &param);
  1477. if (!ret)
  1478. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1479. }
  1480. else if ("Record" == type) { // 录音
  1481. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_RECORD, ExtenNo, nullptr);
  1482. if (!ret)
  1483. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1484. }
  1485. else if ("MuteOn" == type) { // 静音
  1486. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MUTE_BEGIN, ExtenNo, nullptr);
  1487. if (!ret)
  1488. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1489. }
  1490. else if ("MuteOff" == type) { // 取消静音
  1491. bool ret = COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_MUTE_END, ExtenNo, nullptr);
  1492. if (!ret)
  1493. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1494. }
  1495. else if ("Monitor" == type) { // 监控
  1496. m_MonitAgents.addAgent(AgentID, hdl);
  1497. m_Server.sendMsg(hdl, creatJson(type, true));
  1498. std::unique_lock<std::mutex>lock(m_AgentLock);
  1499. auto it = m_MapAgent.begin();
  1500. while (it != m_MapAgent.end())
  1501. {
  1502. std::string state = it->second->state_s();
  1503. std::string agentId = it->second->id();
  1504. std::string data = creatJson("Monitor", state, agentId, it->second->assoExten());
  1505. m_Server.sendMsg(hdl, data);
  1506. ++it;
  1507. }
  1508. }
  1509. else if ("MonitorCancel" == type) { // 取消监控
  1510. m_MonitAgents.delAgent(AgentID);
  1511. m_Server.sendMsg(hdl, creatJson(type, true));
  1512. }
  1513. else if ("TurnIvr" == type) { // 转满意度
  1514. COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_TURNIVR, ExtenNo, nullptr);
  1515. }
  1516. else if ("Heart" == type) { // 心跳
  1517. m_Server.sendMsg(hdl, msg);
  1518. }
  1519. else if ("AutoCall" == type) { // 自动外呼
  1520. //m_AutoCall.addTask(); // 从数据库刷新任务
  1521. m_Server.sendMsg(hdl, creatJson(type, true));
  1522. }
  1523. else if ("GetAgentDetail" == type) {
  1524. auto data = m_AgentDetail.to_string();
  1525. m_Server.sendMsg(hdl, data);
  1526. m_mpAgentDetailHdl[conID] = hdl;
  1527. }
  1528. else if ("WaitCount" == type) {
  1529. std::unique_lock<std::shared_mutex>lock(m_mutQueryCount);
  1530. auto it = std::find_if(m_subWaitCount.begin(), m_subWaitCount.end(), [=](websocketpp::connection_hdl hdltmp) {return hdltmp.lock() == hdl.lock(); });
  1531. if (it == m_subWaitCount.end()) { // 第一次发送则保存
  1532. m_subWaitCount.emplace_back(hdl); // 保存订阅排队数量的坐席
  1533. }
  1534. Json::Value root;
  1535. Json::StreamWriterBuilder jsrocd;
  1536. root["Type"] = "WaitCount";
  1537. root["Result"] = true;
  1538. std::int32_t nTotal = 0;
  1539. for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
  1540. root[it->first] = it->second;
  1541. try {
  1542. auto n = stoi(it->second);
  1543. nTotal += n;
  1544. }
  1545. catch (const std::exception& e) {
  1546. LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
  1547. }
  1548. }
  1549. lock.unlock();
  1550. root["Total"] = nTotal;
  1551. auto data = Json::writeString(jsrocd, root);
  1552. m_Server.sendMsg(hdl, data);
  1553. }
  1554. else if ("SendDtmf" == type){
  1555. std::string Dtmf = root["Dtmf"].asString();
  1556. std::unique_lock<std::mutex>lock(m_AgentLock);
  1557. auto it = m_MapAgent.find(AgentExten);
  1558. if (it == m_MapAgent.end()) { // 坐席未签入
  1559. lock.unlock();
  1560. m_Server.sendMsg(hdl, creatJson(type, false,"坐席未签入"));
  1561. return;
  1562. }
  1563. if (AGENT_STATE_BUSY != it->second->state()) {
  1564. lock.unlock();
  1565. m_Server.sendMsg(hdl, creatJson(type, false, "坐席未通话"));
  1566. return;
  1567. }
  1568. auto pChanIter = m_MapChanExt.find(ExtenNo);
  1569. if (pChanIter == m_MapChanExt.end()) {
  1570. return;
  1571. }
  1572. auto pChan = pChanIter->second;
  1573. auto pSession = __getSessionByChanId(pChan->chanId());
  1574. auto pFinalChan = pSession->getAssoFinalChan();
  1575. if (CHAN_LOGIC_STATE_TALKING != pFinalChan->state() || !pFinalChan->isInMeeting()) {
  1576. m_Server.sendMsg(hdl, creatJson(type, false, "目标线路未通话或未在会议中"));
  1577. return;
  1578. }
  1579. LineOpParam param;
  1580. param.szParam1 = AgentExten;
  1581. param.szParam2 = std::to_string(pFinalChan->no());
  1582. param.szParam3 = pFinalChan->chanId();
  1583. param.szDtmf = Dtmf; // 按键
  1584. bool ret = COperationReactor::GetInstance()->procOperation(ExtenNo, LINE_OP_SEND_DTMF, pFinalChan->no(), &param);
  1585. if (!ret)
  1586. m_Server.sendMsg(hdl, creatJson(type, std::string("操作失败")));
  1587. }
  1588. else {
  1589. }
  1590. // try 结束
  1591. }
  1592. catch (const Json::LogicError& e)
  1593. {
  1594. Format fmt("格式发送错误[%s];msg:[%s]");
  1595. fmt %e.what() % msg;
  1596. m_Server.sendMsg(hdl, fmt.str());
  1597. }
  1598. catch (const std::exception&)
  1599. {
  1600. }
  1601. }
  1602. void CFsProxy::__closeFun(websocketpp::connection_hdl hdl)
  1603. {
  1604. const long conID = reinterpret_cast<long>(hdl.lock().get());
  1605. m_mpAgentDetailHdl.erase(conID);
  1606. std::string agentId;
  1607. std::string extenNo;
  1608. std::unique_lock<std::mutex>lock(m_AgentLock);
  1609. auto it = m_MapAgent.begin();
  1610. while (it != m_MapAgent.end())
  1611. {
  1612. if (!(it->second->hdl().expired()))
  1613. {
  1614. auto sp = it->second->hdl().lock();
  1615. stringstream agent_hdl;
  1616. agent_hdl << sp.get();
  1617. stringstream local_hdl;
  1618. local_hdl << hdl.lock().get();
  1619. if (agent_hdl.str() == local_hdl.str())
  1620. {
  1621. agentId = it->second->id(); // 保存坐席id
  1622. extenNo = it->second->assoExten(); // 分机号
  1623. Format fmt("座席掉线,agentId = %s,extenNo = %s,IP = %s");
  1624. fmt %it->second->id() % it->second->assoExten() % local_hdl.str();
  1625. LOG_WARN_S(fmt.str());
  1626. //m_Gateway.delAgent(agentId);
  1627. m_Gateway.delAgent(agentId, it->second->groups());
  1628. it->second->removeAgent("坐席掉线");
  1629. FS_LINK_DELETE(it->second);
  1630. m_MapAgent.erase(it);
  1631. break;
  1632. }
  1633. }
  1634. ++it;
  1635. }
  1636. __agentDetail(lock.release());
  1637. if (!agentId.empty())
  1638. {
  1639. std::string data = creatJson("Monitor", "签出", agentId, extenNo);
  1640. m_MonitAgents.loopAgent(m_Server, data); // 通知班长坐席监控
  1641. m_MonitAgents.delAgent(agentId);
  1642. }
  1643. }
  1644. void CFsProxy::__doAutoTask(Task task)
  1645. {
  1646. switch (task.type)
  1647. {
  1648. case VOICE_CALL: // 语音外呼
  1649. {
  1650. __voicCall(task);
  1651. }
  1652. break;
  1653. case AGENT_CALL: // 指定坐席外呼 set 在当前 channel 上设置变量,而 export 在(a-leg 和 b-leg) 两个 channel 上都设置
  1654. {
  1655. LineOpParam param;
  1656. param.nParam1 = task.agent; // 坐席号
  1657. param.nParam2 = task.type; // 自动外呼类型
  1658. param.nParam3 = task.id; // 自动外呼任务id
  1659. param.szParam1 = getExtenByAgent(task.agent); // 坐席对应的分机号
  1660. param.szParam2 = task.number;
  1661. if (param.szParam1.empty()) // 如果坐席对应分机不存在说明坐席未登陆,不进行呼叫
  1662. {
  1663. return;
  1664. }
  1665. auto pChan = getExten(std::atoi(param.szParam1.c_str()));
  1666. if (pChan == nullptr || pChan->state() != CHAN_LOGIC_STATE_FREE)
  1667. {
  1668. return;
  1669. }
  1670. COperationReactor::GetInstance()->procOperation(opInstance++, LINE_OP_AUTO_CALL, atoi(param.szParam1.c_str()), &param);
  1671. }
  1672. break;
  1673. case AUTO_CALL: // 系统自动分配坐席外呼
  1674. break;
  1675. default:
  1676. break;
  1677. }
  1678. if (task.id != 0)
  1679. m_AutoCall.updateTask(std::to_string(task.id)); // 更新任务
  1680. }
  1681. bool CFsProxy::__voicCall(Task & task)
  1682. {
  1683. std::string content = task.content;
  1684. std::string filePath;
  1685. if (!tts.TextToAudio(content, filePath))
  1686. {
  1687. LOG_WARN_S("文本转语音失败");
  1688. return false;
  1689. };
  1690. std::string callee = task.number;
  1691. std::string caller;
  1692. std::string CallString;
  1693. m_CallStringMaker.makeCallString(caller, callee, CallString);
  1694. Format fmt("bgapi originate {origination_caller_id_number=%s,%s=%ld,%s=%s,%s=%ld}%s VOICECALL XML %s\r\n%s: %ld");
  1695. fmt %caller % ESL_VAR_OP_TYPE % task.type %"filePath" %filePath % ESL_VAR_TASK_ID % task.id % CallString % "AutoCall" % ESL_HEADER_JOB_UUID % task.uniqueId();
  1696. return m_Gateway.sendCmd(fmt.str());
  1697. }
  1698. void CFsProxy::__agentDetail()
  1699. {
  1700. m_AgentDetail.resetAgent();
  1701. std::unique_lock<std::mutex>lock(m_AgentLock);
  1702. m_AgentDetail.setAgentOnlineCount((std::int32_t)m_MapAgent.size());
  1703. Agent* pAgent = nullptr;
  1704. for (auto it = m_MapAgent.begin(); it != m_MapAgent.end(); ++it) {
  1705. pAgent = it->second;
  1706. if (pAgent) {
  1707. auto state = pAgent->state();
  1708. switch (state)
  1709. {
  1710. case AGENT_STATE_FREE:
  1711. m_AgentDetail.setAgentFreeCount(1);
  1712. break;
  1713. case AGENT_STATE_BUSY:
  1714. m_AgentDetail.setAgentSpeakCount(1);
  1715. break;
  1716. case AGENT_STATE_REPOSE:
  1717. m_AgentDetail.setAgentReposeCount(1);
  1718. break;
  1719. default:
  1720. break;
  1721. }
  1722. }
  1723. }
  1724. // 群发坐席详情
  1725. for (auto it = m_mpAgentDetailHdl.begin(); it != m_mpAgentDetailHdl.end(); ++it) {
  1726. m_Server.sendMsg(it->second, m_AgentDetail.to_string());
  1727. }
  1728. }
  1729. void CFsProxy::__agentDetail(std::mutex * p_mtx, bool islock)
  1730. {
  1731. m_AgentDetail.resetAgent();
  1732. m_AgentDetail.setAgentOnlineCount((std::int32_t)m_MapAgent.size());
  1733. Agent* pAgent = nullptr;
  1734. for (auto it = m_MapAgent.begin(); it != m_MapAgent.end(); ++it) {
  1735. pAgent = it->second;
  1736. if (pAgent) {
  1737. auto state = pAgent->state();
  1738. switch (state)
  1739. {
  1740. case AGENT_STATE_FREE:
  1741. m_AgentDetail.setAgentFreeCount(1);
  1742. break;
  1743. case AGENT_STATE_BUSY:
  1744. m_AgentDetail.setAgentSpeakCount(1);
  1745. break;
  1746. case AGENT_STATE_REPOSE:
  1747. m_AgentDetail.setAgentReposeCount(1);
  1748. break;
  1749. default:
  1750. break;
  1751. }
  1752. }
  1753. }
  1754. if (islock) {
  1755. p_mtx->lock();
  1756. }
  1757. else {
  1758. p_mtx->unlock();
  1759. }
  1760. // 群发坐席详情
  1761. for (auto it = m_mpAgentDetailHdl.begin(); it != m_mpAgentDetailHdl.end(); ++it) {
  1762. m_Server.sendMsg(it->second, m_AgentDetail.to_string());
  1763. }
  1764. }
  1765. void CFsProxy::__agentDetailTrunkChange(const std::int32_t& nCount)
  1766. {
  1767. m_AgentDetail.setTrunkCallInCount(nCount);
  1768. // 群发坐席详情
  1769. for (auto it = m_mpAgentDetailHdl.begin(); it != m_mpAgentDetailHdl.end(); ++it) {
  1770. m_Server.sendMsg(it->second, m_AgentDetail.to_string());
  1771. }
  1772. }
  1773. std::string CFsProxy::__queryCount(const bool & bIsLock)
  1774. {
  1775. Json::Value root;
  1776. Json::StreamWriterBuilder jsrocd;
  1777. root["Type"] = "WaitCount";
  1778. root["Result"] = true;
  1779. std::int32_t nTotal = 0;
  1780. if (bIsLock) {
  1781. std::shared_lock<std::shared_mutex>lock(m_mutQueryCount);
  1782. for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
  1783. root[it->first] = it->second;
  1784. try {
  1785. auto n = stoi(it->second);
  1786. nTotal += n;
  1787. }
  1788. catch (const std::exception& e) {
  1789. LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
  1790. }
  1791. }
  1792. lock.unlock();
  1793. }
  1794. else {
  1795. for (auto it = m_QueryCount.begin(); it != m_QueryCount.end(); ++it) {
  1796. root[it->first] = it->second;
  1797. try {
  1798. auto n = stoi(it->second);
  1799. nTotal += n;
  1800. }
  1801. catch (const std::exception& e) {
  1802. LOG_ERROR("坐席组[%s]数量[%s]转换失败:[%s]", it->first.c_str(), it->second.c_str(), e.what());
  1803. }
  1804. }
  1805. }
  1806. //m_WaitCounter
  1807. root["Total"] = nTotal;
  1808. auto strJson = Json::writeString(jsrocd, root);
  1809. return strJson;
  1810. }
  1811. CFsProxy CFsProxy::instance;