linux版本中间件

FsProxy.cpp 67KB

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