linux版本中间件

FsProxy.cpp 66KB

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