中间件底层,websocket

AgentOffice.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include "StdAfx.h"
  2. #include "AgentOffice.h"
  3. #include "Agent.h"
  4. #include "AcdCore.h"
  5. #include "ExtenCtrl.h"
  6. #include "MsgCenter.h"
  7. CAgentOffice::CAgentOffice(void)
  8. {
  9. }
  10. CAgentOffice::~CAgentOffice(void)
  11. {
  12. }
  13. /*****************************************************************
  14. **【函数名称】 getAgentById
  15. **【函数功能】 根据工号查找座席对象
  16. **【参数】 AgentId: 要查找的座席工号
  17. **【返回值】 对应工号的座席对象
  18. ****************************************************************/
  19. CAgent* CAgentOffice::getAgentById( UINT AgentId )
  20. {
  21. CAgent* pAgent = NULL;
  22. m_AgentMap.Lookup(AgentId, pAgent);
  23. return pAgent;
  24. }
  25. /*****************************************************************
  26. **【函数名称】 getAgentByExt
  27. **【函数功能】 根据分机号查找座席对象
  28. **【参数】
  29. **【返回值】 对应工号的座席对象
  30. ****************************************************************/
  31. CAgent* CAgentOffice::getAgentByExt( UINT AgentId, UINT ExtenId )
  32. {
  33. CAgent* pAgent = NULL;
  34. if(m_AgentMap.Lookup(AgentId, pAgent))
  35. {
  36. if(pAgent->assoExten() == ExtenId)
  37. return pAgent;
  38. }
  39. return NULL;
  40. }
  41. /*****************************************************************
  42. **【函数名称】 getAgentByIp
  43. **【函数功能】 根据工号及IP查找座席对象
  44. **【参数】 AgentId: 要查找的座席工号
  45. AgentIp: 座席IP
  46. **【返回值】 对应工号及IP的座席对象
  47. ****************************************************************/
  48. CAgent* CAgentOffice::getAgentByIp( UINT AgentId, const CString& AgentIp )
  49. {
  50. CAgent* pAgent = NULL;
  51. if(m_AgentMap.Lookup(AgentId, pAgent))
  52. {
  53. if(pAgent->ip() == AgentIp)
  54. return pAgent;
  55. }
  56. return NULL;
  57. }
  58. /*****************************************************************
  59. **【函数名称】 getAgentInfo
  60. **【函数功能】 获取坐席信息
  61. **【参数】
  62. **【返回值】
  63. ****************************************************************/
  64. BOOL CAgentOffice::getAgentInfo( Agent2Show& Info )
  65. {
  66. CAgent* pAgent = NULL;
  67. if(m_AgentMap.Lookup(Info.AgentId, pAgent))
  68. {
  69. Info.ExtId = pAgent->assoExten();
  70. Info.GroupNo = pAgent->group();
  71. Info.AgentType = pAgent->type();
  72. Info.State = pAgent->state();
  73. return TRUE;
  74. }
  75. return FALSE;
  76. }
  77. /*****************************************************************
  78. **【函数名称】 close
  79. **【函数功能】 释放资源
  80. **【参数】
  81. **【返回值】
  82. ****************************************************************/
  83. void CAgentOffice::close( void )
  84. {
  85. clearAgent();
  86. }
  87. /*****************************************************************
  88. **【函数名称】 clearAgent
  89. **【函数功能】 清理所有座席
  90. **【参数】
  91. **【返回值】
  92. ****************************************************************/
  93. void CAgentOffice::clearAgent( void )
  94. {
  95. UINT Key;
  96. CAgent* pAgent = NULL;
  97. POSITION Pos = m_AgentMap.GetStartPosition();
  98. // 清除所有座席
  99. while (Pos != NULL)
  100. {
  101. m_AgentMap.GetNextAssoc(Pos, Key, pAgent);
  102. ASSERT(pAgent != NULL);
  103. if (pAgent)
  104. {
  105. CAcdCore::GetInstance().getQueueMgr().delAgent(pAgent, true); // 从排队队列中删除
  106. pAgent->release(0);
  107. delete pAgent;
  108. pAgent = NULL;
  109. }
  110. }
  111. m_AgentMap.RemoveAll();
  112. }
  113. /*****************************************************************
  114. **【函数名称】 insertAgent
  115. **【函数功能】 座席状态变化的对应处理
  116. **【参数】 a_AgentID 座席工号
  117. a_ExtID 分机号
  118. a_GroupID 座席组号
  119. a_AgentType 座席类型
  120. a_TimeOverWork 话后处理时长
  121. a_ExtStatus 关联的分机状态
  122. **【返回值】
  123. ****************************************************************/
  124. BOOL CAgentOffice::insertAgent( UINT a_AgentID, UINT a_ExtID, LPCTSTR a_GroupID, UINT a_AgentType, UINT a_TimePostProcessing, INNER_STATE a_ExtStatus )
  125. {
  126. // 查找是否存在当前座席
  127. if(isAgentExisted(a_AgentID))
  128. return FALSE;
  129. // 保存对象
  130. CAgent* pAgent = new CAgent(a_AgentID, a_ExtID, a_GroupID, a_AgentType, a_TimePostProcessing);
  131. m_AgentMap.SetAt(pAgent->id(), pAgent); // 座席添加到全体座席索引
  132. CAcdCore::GetInstance().getQueueMgr().addAgent(pAgent);
  133. pAgent->onAssoExtStatusUpdated(a_ExtStatus);// 设置座席当前状态
  134. return TRUE;
  135. }
  136. /*****************************************************************
  137. **【函数名称】 isAgentExisted
  138. **【函数功能】 指定工号的座席是否存在
  139. **【参数】 a_AgentId: 要查找的座席工号
  140. **【返回值】
  141. ****************************************************************/
  142. BOOL CAgentOffice::isAgentExisted( UINT a_AgentId )
  143. {
  144. return getAgentById(a_AgentId) == NULL ? FALSE : TRUE;
  145. }
  146. /*****************************************************************
  147. **【函数名称】 isAgentExisted
  148. **【函数功能】 指定工号及IP的座席是否存在
  149. **【参数】 a_AgentId: 要查找的座席工号
  150. a_AgentIp: 要查找的座席IP
  151. **【返回值】
  152. ****************************************************************/
  153. BOOL CAgentOffice::isAgentExisted( UINT a_AgentId, const CString& a_AgentIp )
  154. {
  155. return getAgentByIp(a_AgentId, a_AgentIp) == NULL ? FALSE : TRUE;
  156. }
  157. /*****************************************************************
  158. **【函数名称】 removeAgent
  159. **【函数功能】 删除指定座席
  160. **【参数】 a_AgentID: 座席工号
  161. a_HostAgent: 主控制坐席工号
  162. **【返回值】
  163. ****************************************************************/
  164. BOOL CAgentOffice::removeAgent( UINT a_AgentID, UINT a_HostAgent )
  165. {
  166. CAgent* pAgent = NULL;
  167. if(m_AgentMap.Lookup(a_AgentID, pAgent))
  168. {
  169. CAcdCore::GetInstance().getQueueMgr().delAgent(pAgent);
  170. m_AgentMap.RemoveKey(a_AgentID); // 从全体座席索引中删除
  171. pAgent->release(a_HostAgent);
  172. delete pAgent;
  173. pAgent = NULL;
  174. return TRUE;
  175. }
  176. return FALSE;
  177. }
  178. /*****************************************************************
  179. **【函数名称】 getAgentAssoExten
  180. **【函数功能】 查找座席对应的分机号
  181. **【参数】 a_AgentID: 座席工号
  182. **【返回值】 该座席关联的分机号
  183. ****************************************************************/
  184. UINT CAgentOffice::getAgentAssoExten( UINT a_AgentID )
  185. {
  186. CAgent* pAgent = getAgentById(a_AgentID);
  187. return pAgent == NULL ? 0 : pAgent->assoExten();
  188. }
  189. /*****************************************************************
  190. **【函数名称】 getAgentGroup
  191. **【函数功能】 查找座席对应的类型
  192. **【参数】 a_AgentID: 座席工号
  193. **【返回值】 该座席关联的类型
  194. ****************************************************************/
  195. UINT CAgentOffice::getAgentType( UINT a_AgentID )
  196. {
  197. CAgent* pAgent = getAgentById(a_AgentID);
  198. return pAgent == NULL ? 0 : pAgent->type();
  199. }
  200. CString CAgentOffice::getAgentGroup(UINT a_AgentID)
  201. {
  202. CAgent* pAgent = getAgentById(a_AgentID);
  203. return pAgent == NULL ? "" : pAgent->group();
  204. }
  205. /*****************************************************************
  206. **【函数名称】 getAgentState
  207. **【函数功能】 查找指定座席的当前状态
  208. **【参数】 a_AgentID: 座席工号
  209. **【返回值】 该座席当前状态
  210. ****************************************************************/
  211. UINT CAgentOffice::getAgentState( UINT a_AgentID )
  212. {
  213. CAgent* pAgent = getAgentById(a_AgentID);
  214. return pAgent == NULL ? AGENT_STATE_UNKNOWN : pAgent->state();
  215. }
  216. /*****************************************************************
  217. **【函数名称】 isAgentRepose
  218. **【函数功能】 查找指定座席是否是置忙状态
  219. **【参数】 a_AgentID: 座席工号
  220. **【返回值】
  221. ****************************************************************/
  222. BOOL CAgentOffice::isAgentRepose( UINT a_AgentID )
  223. {
  224. CAgent* pAgent = getAgentById(a_AgentID);
  225. return pAgent == NULL ? FALSE : pAgent->isRepose();
  226. }
  227. /*****************************************************************
  228. **【函数名称】 lockAgent
  229. **【函数功能】 临时对座席进行锁定
  230. **【参数】 a_AgentId: 座席工号
  231. **【返回值】
  232. ****************************************************************/
  233. BOOL CAgentOffice::lockAgent( UINT a_AgentId )
  234. {
  235. CAgent* pAgent = getAgentById(a_AgentId);
  236. if(pAgent != NULL)
  237. return pAgent->lock();
  238. else
  239. return FALSE;
  240. }
  241. /*****************************************************************
  242. **【函数名称】 setAgentState4Calling
  243. **【函数功能】 为外呼设置座席状态
  244. **【参数】 a_AgentId: 座席工号
  245. **【返回值】
  246. ****************************************************************/
  247. BOOL CAgentOffice::setAgentState4Calling(UINT a_AgentId)
  248. {
  249. CAgent* pAgent = getAgentById(a_AgentId);
  250. ASSERT(pAgent != NULL);
  251. if (pAgent == NULL)
  252. return FALSE;
  253. // 确保一下座席状态空闲
  254. // ych 2018.7.11
  255. //if (!pAgent->setState(AGENT_STATE_FREE))
  256. if(!pAgent->setFreeForAgentCall())
  257. return FALSE;
  258. return pAgent->lockForCall();
  259. //return pAgent->lock();
  260. }
  261. /*****************************************************************
  262. **【函数名称】 onExtenStateUpdated
  263. **【函数功能】 分机状态变化事件响应
  264. **【参数】 a_pCmd: PDU命令内容
  265. **【返回值】
  266. ****************************************************************/
  267. void CAgentOffice::onExtenStateUpdated( UINT a_AgentID, CPduEntity* a_pCmd )
  268. {
  269. CAgent* pAgent = getAgentByExt(a_AgentID, a_pCmd->GetDataUInt(0));
  270. if(pAgent != NULL)
  271. pAgent->onAssoExtStatusUpdated((INNER_STATE)a_pCmd->GetDataUInt(1), a_pCmd);
  272. }
  273. /*****************************************************************
  274. **【函数名称】 setAgentState
  275. **【函数功能】 设置指定座席的逻辑状态
  276. **【参数】 a_AgentId: 座席工号
  277. a_AgentState: 座席状态
  278. **【返回值】
  279. ****************************************************************/
  280. BOOL CAgentOffice::setAgentState( UINT a_AgentID, AGENT_STATE a_AgentStatus )
  281. {
  282. CAgent* pAgent = getAgentById(a_AgentID);
  283. ASSERT(pAgent != NULL);
  284. if(pAgent != NULL)
  285. return pAgent->setState(a_AgentStatus);
  286. else
  287. return FALSE;
  288. }
  289. BOOL CAgentOffice::setAgentState(UINT a_AgentID, AGENT_STATE a_AgentStatus, bool forceLogout)
  290. {
  291. CAgent* pAgent = getAgentById(a_AgentID);
  292. ASSERT(pAgent != NULL);
  293. if (pAgent != NULL)
  294. return pAgent->setState(a_AgentStatus, forceLogout);
  295. else
  296. return FALSE;
  297. }
  298. /*****************************************************************
  299. **【函数名称】 isNoAgent
  300. **【函数功能】 是否无座席签入
  301. **【参数】
  302. **【返回值】
  303. ****************************************************************/
  304. BOOL CAgentOffice::isNoAgent( void )
  305. {
  306. return (m_AgentMap.GetCount() == 0) ? TRUE : FALSE;
  307. }
  308. /*****************************************************************
  309. **【函数名称】 getStateSpecAgentList
  310. **【函数功能】 获取指定状态的座席列表
  311. **【参数】 SpecState: 指定状态
  312. **【返回值】 座席工号字符串列表,以 | 进行分隔
  313. ****************************************************************/
  314. void CAgentOffice::getStateSpecAgentList( AGENT_LIST_SPEC_STATE SpecState, CString& AgentList )
  315. {
  316. AgentList = _T("");
  317. UINT Key;
  318. CAgent* pAgent;
  319. POSITION pos = m_AgentMap.GetStartPosition();
  320. while(pos != NULL)
  321. {
  322. bool IsInsert = false;
  323. m_AgentMap.GetNextAssoc(pos, Key, pAgent);
  324. ASSERT(pAgent != NULL);
  325. switch(SpecState)
  326. {
  327. case AGENT_LIST_FREE: // 空闲座席列表
  328. if(pAgent->state() == AGENT_STATE_FREE)
  329. IsInsert = true;
  330. break;
  331. case AGENT_LIST_BUSY: // 忙
  332. if(pAgent->state() != AGENT_STATE_FREE && pAgent->state() != AGENT_STATE_LOGOUT)
  333. IsInsert = true;
  334. break;
  335. case AGENT_LIST_TALKING: // 通话
  336. if(pAgent->state() == AGENT_STATE_BUSY)
  337. IsInsert = true;
  338. break;
  339. case AGENT_LIST_ALTERING: // 来电振铃
  340. if(CAcdCore::GetInstance().getExtenCtrl().getExtenState(pAgent->assoExten()) == INNER_STATE_ALERTING)
  341. IsInsert = true;
  342. break;
  343. } // end switch
  344. // 如果是指定状态的座席
  345. if(IsInsert)
  346. {
  347. CString block;
  348. block.Format(_T("%lu,%lu"), pAgent->id(), pAgent->assoExten());
  349. if(AgentList.GetLength() != 0)
  350. AgentList += _T("|");
  351. AgentList += block;
  352. } // end if
  353. } // end while
  354. }
  355. void CAgentOffice::GetCurAgentCount(int & AgentSpeakCount, int & AgentOnlineCount, int & AgentReposeCount)
  356. {
  357. UINT Key;
  358. CAgent* pAgent = NULL;
  359. POSITION pos = m_AgentMap.GetStartPosition();
  360. while (pos != NULL)
  361. {
  362. m_AgentMap.GetNextAssoc(pos, Key, pAgent);
  363. if (pAgent != NULL)
  364. {
  365. switch (pAgent->state())
  366. {
  367. case AGENT_STATE_REPOSE: // 小休
  368. AgentReposeCount++;
  369. break;
  370. case AGENT_STATE_BUSY: // 通话中
  371. AgentSpeakCount++;
  372. break;
  373. } // end switch
  374. }
  375. } // end while
  376. AgentOnlineCount = m_AgentMap.GetCount() - AgentReposeCount;
  377. }
  378. void CAgentOffice::GetCurAgentCount(int & AgentSpeakCount, int & AgentOnlineCount, int & AgentReposeCount, int & AgentFreeCount, int &AgentProcessingCount, int &AgentRingCount)
  379. {
  380. AgentSpeakCount = 0;
  381. AgentOnlineCount = 0;
  382. AgentReposeCount = 0;
  383. AgentFreeCount = 0;
  384. AgentProcessingCount = 0;
  385. AgentRingCount = 0;
  386. UINT Key;
  387. CAgent* pAgent = NULL;
  388. POSITION pos = m_AgentMap.GetStartPosition();
  389. while (pos != NULL)
  390. {
  391. m_AgentMap.GetNextAssoc(pos, Key, pAgent);
  392. if (pAgent != NULL)
  393. {
  394. switch (pAgent->state())
  395. {
  396. case AGENT_STATE_FREE: // 空闲
  397. AgentFreeCount++;
  398. break;
  399. case AGENT_STATE_REPOSE: // 小休
  400. AgentReposeCount++;
  401. break;
  402. case AGENT_STATE_BUSY: // 通话中
  403. AgentSpeakCount++;
  404. break;
  405. case AGENT_STATE_POST_PROCESSING: // 话后处理
  406. AgentProcessingCount++;
  407. break;
  408. case AGENT_STATE_REQUESTED: // 被请求/振铃
  409. AgentRingCount++;
  410. break;
  411. } // end switch
  412. }
  413. } // end while
  414. AgentOnlineCount = m_AgentMap.GetCount();
  415. }
  416. void CAgentOffice::getAllAgentStateList(CString & AgentList)
  417. {
  418. UINT Key;
  419. CAgent* pAgent = NULL;
  420. CString agent;
  421. POSITION pos = m_AgentMap.GetStartPosition();
  422. while (pos != NULL)
  423. {
  424. m_AgentMap.GetNextAssoc(pos, Key, pAgent);
  425. if (pAgent)
  426. {
  427. agent.Format("%d,%d,%d,%s|", pAgent->id(), pAgent->assoExten(), pAgent->state(), pAgent->group());
  428. AgentList += agent;
  429. }
  430. }
  431. }
  432. std::vector<int> CAgentOffice::getAgentIDAll()
  433. {
  434. UINT id = 0;
  435. CAgent* agent = NULL;
  436. std::vector<int> VecInt;
  437. POSITION pos = m_AgentMap.GetStartPosition();
  438. while (pos)
  439. {
  440. m_AgentMap.GetNextAssoc(pos, id, agent);
  441. int AgentID = agent->id();
  442. VecInt.push_back(AgentID);
  443. }
  444. return VecInt;
  445. }
  446. std::map<UINT, CAgentProperty>& CAgentOffice::GetAgentPropertyMap()
  447. {
  448. return m_AgentPropertyMap;
  449. }