linux版本中间件

Session.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include "Session.h"
  2. #include "FsProxy.h"
  3. #include "ChanTrunk.h"
  4. #include "ChanExten.h"
  5. #include "JsonStringMaker.h"
  6. #include "Util.h"
  7. #include "SqlWrite.h"
  8. #include "Config.h"
  9. Session::Session(CFsProxy* pParent, string Id) : m_pParent(pParent), m_Id(Id), m_IsSaveDb(true), m_AtionId(0)
  10. {
  11. }
  12. Session::~Session()
  13. {
  14. }
  15. void Session::prepare(PCHAN_EVENT_NOTIFY pNotify)
  16. {
  17. if (m_Id != pNotify->ChanId) // 触发会话的通道不是会话主通道,则查找并添加主通道
  18. {
  19. VirtualChan* pChan = m_pParent->getBusyChan(m_Id);
  20. if (pChan != nullptr)
  21. __addChan(pChan);
  22. }
  23. }
  24. void Session::onChanEvent(PCHAN_EVENT_NOTIFY pNotify)
  25. {
  26. if (pNotify == nullptr) return;
  27. VirtualChan* pChan = __getChan(pNotify->ChanId);
  28. if (pChan == nullptr)
  29. {
  30. pChan = __findChan(pNotify);
  31. if (pChan != nullptr)
  32. __addChan(pChan);
  33. else
  34. {
  35. Format fmt("{Fs.Session}: 处理通道事件时查找对应通道失败, Caller = %s, Callee = %s");
  36. fmt % pNotify->Caller % pNotify->Callee;
  37. LOG_WARN_S(fmt.str());
  38. return;
  39. }
  40. }
  41. __notifyDbSaveChanEvent(pChan, pNotify); // 通话记录保存
  42. pChan->onChanEvent(pNotify);
  43. __notifyIncomingChanEvent(pChan, pNotify); // 来电弹屏
  44. __onRecord(pChan, pNotify); // 执行录音
  45. // 过滤无效状态
  46. if (pChan->state() == CHAN_LOGIC_STATE_FREE && pNotify->EventId == CHANNEL_EVENT_CREATE)
  47. return;
  48. __notifySessionChanEvent(pChan, pNotify);
  49. try
  50. {
  51. if (pChan->isFree())
  52. __onChanFree(pChan, pNotify);
  53. }
  54. catch (const std::exception& e)
  55. {
  56. LOG_WARN(("{Fs.Session.onChanEvent}: 通道空闲处理函数执行发生异常[%s], Caller = %s, Callee = %s"),e.what(), pNotify->Caller.c_str(), pNotify->Callee.c_str());
  57. }
  58. __onPlayAgentNo(pChan, pNotify); // 判断是否报工号
  59. }
  60. void Session::onChanDtmf(PDTMF_NOTIFY pNotify)
  61. {
  62. }
  63. void Session::onChanHold(PHOLD_NOTIFY pNotify)
  64. {
  65. VirtualChan* pChan = __getChan(pNotify->ChanId);
  66. if (pChan != nullptr)
  67. {
  68. pChan->onChanHold(pNotify->EvtType);
  69. }
  70. }
  71. VirtualChan * Session::getAssoChan(VirtualChan * pChan)
  72. {
  73. auto it = m_ListChan.begin();
  74. while (it != m_ListChan.end())
  75. {
  76. if (*it != pChan)
  77. return *it;
  78. ++it;
  79. }
  80. return nullptr;
  81. }
  82. VirtualChan * Session::__getFirstChan()
  83. {
  84. if(m_ListChan.empty())
  85. return nullptr;
  86. return m_ListChan.front();
  87. }
  88. VirtualChan * Session::__getChan(string ChanId)
  89. {
  90. auto it = m_ListChan.begin();
  91. while (it != m_ListChan.end())
  92. {
  93. if ((*it)->chanId() == ChanId)
  94. return *it;
  95. ++it;
  96. }
  97. return nullptr;
  98. }
  99. VirtualChan * Session::__findChan(PCHAN_EVENT_NOTIFY pNotify)
  100. {
  101. VirtualChan* pChan = nullptr;
  102. if (pNotify->EventId == CHANNEL_EVENT_CREATE)
  103. {
  104. stringstream str;
  105. uint32_t ExtenNo = 0;
  106. if (pNotify->Direction == CALL_DIRECTION_INBOUND)
  107. str << pNotify->Caller; //ExtenNo = stoul(pNotify->Caller); // 呼入FS
  108. else
  109. str << pNotify->Callee; //ExtenNo = stoul(pNotify->Callee); // FS呼出
  110. str >> ExtenNo;
  111. pChan = m_pParent->getExten(ExtenNo);
  112. if (pChan == nullptr) // 分机通道中无,则说明是中继通道
  113. {
  114. pChan = m_pParent->getFreeTrunk();
  115. if (pChan == nullptr)
  116. {
  117. Format fmt("{Fs.Session}: 处理通道事件时获取空闲中继通道失败, Caller = %s, Callee = %s");
  118. fmt % pNotify->Caller % pNotify->Callee;
  119. LOG_WARN_S(fmt.str());
  120. m_pParent->onChanPoor(this, pNotify);
  121. }
  122. }
  123. }
  124. else
  125. {
  126. pChan = m_pParent->getBusyChan(pNotify->ChanId);
  127. }
  128. return pChan;
  129. }
  130. void Session::__addChan(VirtualChan * pChan)
  131. {
  132. LOG_DEBUG("通道数量:%d %s,%d",m_ListChan.size(),__FUNCTION__,__LINE__);
  133. m_ListChan.emplace_back(pChan);
  134. LOG_DEBUG("通道数量:%d %s,%d", m_ListChan.size(), __FUNCTION__, __LINE__);
  135. }
  136. /*****************************************************************
  137. **【函数名称】 __delChan
  138. **【函数功能】 删除指定通道
  139. **【参数】
  140. **【返回值】
  141. *****************************************************************/
  142. void Session::__delChan(VirtualChan * pChan)
  143. {
  144. m_ListChan.remove(pChan);
  145. }
  146. /*****************************************************************
  147. **【函数名称】 __clearChan
  148. **【函数功能】 清空通道
  149. **【参数】
  150. **【返回值】
  151. *****************************************************************/
  152. void Session::__clearChan(void)
  153. {
  154. m_ListChan.clear();
  155. }
  156. void Session::__notifySessionChanEvent(VirtualChan * pChanHost, PCHAN_EVENT_NOTIFY pNotify)
  157. {
  158. auto it = m_ListChan.begin();
  159. while (it != m_ListChan.end())
  160. {
  161. if ((*it) != pChanHost)
  162. {
  163. (*it)->onSessionChanEvent(pChanHost, pNotify);
  164. }
  165. ++it;
  166. }
  167. }
  168. /*
  169. ** 分机来电事件
  170. */
  171. void Session::__notifyIncomingChanEvent(VirtualChan * pChanHost, PCHAN_EVENT_NOTIFY pNotify)
  172. {
  173. // 会话有两条通道,且当前通道为分机,当前时间为创建对话,发送来电事件给当前分机
  174. //if (m_ListChan.size()==2 && pChanHost->type() == DEV_RES_TYPE_EXT && pNotify->EventId == CHANNEL_EVENT_CREATE)
  175. //{
  176. // std::string CallID = pNotify->CcId.empty()==true ? pNotify->CallId : pNotify->CcId;
  177. // std::string ExtenNo = std::to_string(pChanHost->no());
  178. // std::string Number = pNotify->Caller; // 主叫号码
  179. // std::string callee = pNotify->Callee; // 被叫号码
  180. // std::string TrunkNumber;
  181. // auto pFirstChan = __getFirstChan();
  182. // if (pFirstChan != nullptr && pFirstChan->type() == DEV_RES_TYPE_TRUNK)
  183. // TrunkNumber = pFirstChan->calleeNum();
  184. // std::string data = m_pParent->creatJsonIncoming(CallID, Number, callee, TrunkNumber);
  185. // m_pParent->send2Agent(ExtenNo,data); // 发送来电事件到坐席
  186. //}
  187. if (m_ListChan.size() == 2 && pNotify->EventId == CHANNEL_EVENT_CREATE)
  188. {
  189. std::string CallID = pNotify->CcId.empty() == true ? pNotify->CallId : pNotify->CcId;
  190. std::string ExtenNo = std::to_string(pChanHost->no());
  191. std::string Number = pNotify->Caller; // 主叫号码
  192. std::string callee = pNotify->Callee; // 被叫号码
  193. std::string TrunkNumber; // 中继号码
  194. int inComingType = 0;
  195. auto pFirstChan = __getFirstChan();
  196. if (pFirstChan != nullptr && pFirstChan->type() == DEV_RES_TYPE_TRUNK)
  197. {
  198. TrunkNumber = pFirstChan->calleeNum();
  199. inComingType = 2; // 来电
  200. }
  201. else if(pFirstChan != nullptr && pFirstChan->type() == DEV_RES_TYPE_EXT)
  202. {
  203. inComingType = 1; // 分机外呼
  204. }
  205. std::string data = m_pParent->creatJsonIncoming(CallID, Number, callee, TrunkNumber, inComingType);
  206. m_pParent->send2Agent(ExtenNo, data); // 发送来电事件到坐席
  207. }
  208. }
  209. void Session::__notifyDbSaveChanEvent(VirtualChan * pChanHost, PCHAN_EVENT_NOTIFY pNotify)
  210. {
  211. if (pNotify == nullptr || pChanHost == nullptr) return;
  212. // 自动外呼不用该方法保存数据库
  213. if (pNotify->CallType != 0)
  214. {
  215. __saveAutoCallDB(pChanHost,pNotify);
  216. return;
  217. }
  218. if (pChanHost->isInMeeting()&& pNotify->EventId == CHANNEL_EVENT_HANGUP_COMPLETE) // 会议,
  219. {
  220. Format fmt("update conference set end_time = '%s' where uuid = '%s'");
  221. fmt %Util::CurTime() % pChanHost->sessionId();
  222. SqlWrite::GetInstance()->addSql(fmt.str());
  223. }
  224. if (!pChanHost->opNumber().empty()) // 强插等操作会有操作码
  225. {
  226. return;
  227. }
  228. if (pNotify->EventId == CHANNEL_EVENT_CREATE && pChanHost->type() == DEV_RES_TYPE_EXT && pChanHost == __getFirstChan()) // 分机为主叫时数据库新添加一条记录
  229. {
  230. std::string curTime = Util::CurTime();
  231. std::string callid = pNotify->CcId.empty() == true ? pNotify->CallId : pNotify->CcId;
  232. std::string agent = m_pParent->getAgentByExten(pNotify->Caller);
  233. Format fmt("insert into cdr(uuid,caller_agent,caller,call_type,create_time,action_id) values('%s','%s','%s',%d,'%s',%d)"); // 分机呼叫 call_type=1
  234. fmt %callid %agent % pNotify->Caller % 1 % curTime % 1;
  235. SqlWrite::GetInstance()->addSql(fmt.str());
  236. }
  237. else if (pNotify->EventId == CHANNEL_EVENT_CREATE && pChanHost != __getFirstChan()) // b腿创建
  238. {
  239. m_AtionId++;
  240. m_RingTime.clear();
  241. m_AnswerTime.clear();
  242. m_IsSaveDb = true;
  243. if (m_AtionId >1 )
  244. {
  245. auto pChan = __getFirstChan();
  246. if (pChan != nullptr)
  247. {
  248. int call_type = 0;
  249. if (pChan->type() == DEV_RES_TYPE_EXT)
  250. {
  251. call_type = 1;
  252. }
  253. std::string curTime = Util::CurTime();
  254. std::string callid = pNotify->CcId.empty() == true ? pNotify->CallId : pNotify->CcId;
  255. std::string agent = m_pParent->getAgentByExten(pNotify->Caller);
  256. Format fmt("insert into cdr(uuid,caller_agent,caller,call_type,create_time,action_id) values('%s','%s','%s',%d,'%s',%d)"); // 分机呼叫 call_type=1
  257. fmt %callid %agent % pNotify->Caller % call_type % curTime % m_AtionId;
  258. SqlWrite::GetInstance()->addSql(fmt.str());
  259. }
  260. }
  261. }
  262. else if ((pNotify->EventId == CHANNEL_EVENT_PROGRESS || pNotify->EventId == CHANNEL_EVENT_PROGRESS_MEDIA) && pChanHost != __getFirstChan()) // 呼出振铃
  263. {
  264. if (m_RingTime.empty())
  265. m_RingTime = Util::CurTime();
  266. }
  267. else if (pNotify->EventId == CHANNEL_EVENT_ANSWER && pChanHost != __getFirstChan())
  268. {
  269. m_AnswerTime = Util::CurTime();
  270. }
  271. if (pNotify->EventId == CHANNEL_EVENT_HANGUP_COMPLETE && m_IsSaveDb ) // 收到挂机事件,且第一次更新数据库(update)
  272. {
  273. std::string curTime = Util::CurTime();
  274. std::string agent;
  275. std::string callee; // 被叫
  276. if (pChanHost->type() == DEV_RES_TYPE_TRUNK && pChanHost == __getFirstChan()) // 中继呼入且先挂机
  277. {
  278. auto pAssoChan = getAssoChan(pChanHost); // 根据对端线路获取被叫
  279. if (pAssoChan != nullptr)
  280. {
  281. callee = pAssoChan->calleeNum();
  282. }
  283. }
  284. else
  285. {
  286. if (pChanHost->isInMeeting()) // 会议时
  287. callee = pNotify->Caller;
  288. else
  289. callee = pNotify->Callee;
  290. }
  291. agent = m_pParent->getAgentByExten(callee);
  292. //std::string callid = pNotify->CcId.empty() == true ? pNotify->CallId : pNotify->CcId; // 获取callid
  293. auto fun = [](std::string time)->std::string { if (time.empty()) return "null"; else return "'" + time + "'"; }; // 匿名函数用于转化时间
  294. m_RingTime = fun(m_RingTime);
  295. m_AnswerTime = fun(m_AnswerTime);
  296. if (m_AtionId == 0)
  297. m_AtionId = 1;
  298. Format fmt("update cdr set callee_agent = '%s',callee = '%s', ring_time = %s,answer_time = %s,end_time = '%s',record_path = '%s', hangup_cause = '%s' where uuid = '%s' and action_id = %d");
  299. fmt %agent %callee % m_RingTime %m_AnswerTime %curTime %m_RecordFile %pNotify->HangupCause %pChanHost->sessionId() % m_AtionId;
  300. SqlWrite::GetInstance()->addSql(fmt.str());
  301. // 清空
  302. m_RingTime.clear();
  303. m_AnswerTime.clear();
  304. m_IsSaveDb = false;
  305. }
  306. }
  307. void Session::__saveAutoCallDB(VirtualChan * pChanHost, PCHAN_EVENT_NOTIFY pNotify)
  308. {
  309. if (pNotify->EventId == CHANNEL_EVENT_CREATE && pChanHost == __getFirstChan()) // 分机为主叫时数据库新添加一条记录
  310. {
  311. std::string curTime = Util::CurTime();
  312. std::string callid = pNotify->CcId.empty() == true ? pNotify->CallId : pNotify->CcId;
  313. std::string sql;
  314. if (pNotify->CallType == VOICE_CALL)
  315. {
  316. Format fmt("insert into auto_cdr(uuid,caller,create_time,task_id) values('%s','%s','%s',%ld)"); // 分机呼叫 call_type=1
  317. fmt %callid % pNotify->Caller % curTime %pNotify->TaskId;
  318. sql = fmt.str();
  319. }
  320. else
  321. {
  322. Format fmt("insert into auto_cdr(uuid,caller,create_time,task_id) values('%s','%s','%s',%ld)"); // 分机呼叫 call_type=1
  323. fmt %callid % pNotify->Callee % curTime %pNotify->TaskId;
  324. sql = fmt.str();
  325. }
  326. SqlWrite::GetInstance()->addSql(sql);
  327. }
  328. else if ((pNotify->EventId == CHANNEL_EVENT_PROGRESS || pNotify->EventId == CHANNEL_EVENT_PROGRESS_MEDIA) && (pChanHost != __getFirstChan() || pNotify->CallType == VOICE_CALL)) // 呼出振铃
  329. {
  330. if (m_RingTime.empty())
  331. m_RingTime = Util::CurTime();
  332. }
  333. else if (pNotify->EventId == CHANNEL_EVENT_ANSWER && (pChanHost != __getFirstChan() || pNotify->CallType == VOICE_CALL))
  334. {
  335. m_AnswerTime = Util::CurTime();
  336. }
  337. else if (pNotify->EventId == CHANNEL_EVENT_HANGUP_COMPLETE && m_IsSaveDb) // 收到挂机事件,且第一次更新数据库(update)
  338. {
  339. std::string curTime = Util::CurTime();
  340. auto fun = [](std::string time)->std::string { if (time.empty()) return "null"; else return "'" + time + "'"; }; // 匿名函数用于转化时间
  341. m_RingTime = fun(m_RingTime);
  342. m_AnswerTime = fun(m_AnswerTime);
  343. Format fmt("update auto_cdr set callee = '%s', ring_time = %s,answer_time = %s,end_time = '%s',record_path = '%s', hangup_cause = '%s' where uuid = '%s'");
  344. if (pNotify->CallType == VOICE_CALL) // 语音外呼被叫 接通挂机时取 caller
  345. fmt %pNotify->Caller % m_RingTime %m_AnswerTime %curTime %m_RecordFile %pNotify->HangupCause %pChanHost->sessionId();
  346. else
  347. fmt %pNotify->Callee % m_RingTime %m_AnswerTime %curTime %m_RecordFile %pNotify->HangupCause %pChanHost->sessionId();
  348. SqlWrite::GetInstance()->addSql(fmt.str());
  349. // 清空
  350. m_RingTime.clear();
  351. m_AnswerTime.clear();
  352. m_IsSaveDb = false;
  353. }
  354. }
  355. void Session::__onChanFree(VirtualChan * pChan, PCHAN_EVENT_NOTIFY pNotify)
  356. {
  357. if (m_Id == pNotify->ChanId) // 若会话主通道空闲则清空会话
  358. __clearChan();
  359. else
  360. __delChan(pChan);
  361. if (pChan->isVoid()) // 若通道已被丢弃,则删除
  362. m_pParent->delChan(pChan);
  363. }
  364. void Session::__onPlayAgentNo(VirtualChan * pChan, PCHAN_EVENT_NOTIFY pNotify)
  365. {
  366. LOG_DEBUG("调用播报工号 %d %d %d %d", pNotify->EventId, pChan->isInMeeting(), m_ListChan.size(), pChan->type());
  367. // 事件为接听,通道为分机,线路数为2, 对端为外线,进行播报工号
  368. if (pNotify->EventId != CHANNEL_EVENT_ANSWER || pChan->isInMeeting() || m_ListChan.size() != 2)
  369. {
  370. LOG_DEBUG("调用播报工号 %d %d %d ", pNotify->EventId, pChan->isInMeeting(), m_ListChan.size());
  371. return;
  372. }
  373. VirtualChan* pFirstChan = __getFirstChan();
  374. if (pFirstChan != nullptr && pFirstChan->type() == DEV_RES_TYPE_TRUNK && pChan->type() == DEV_RES_TYPE_EXT)
  375. {
  376. m_pParent->playAgentNo(pChan);
  377. }
  378. }
  379. void Session::__onRecord(VirtualChan * pChan, PCHAN_EVENT_NOTIFY pNotify)
  380. {
  381. if (pChan == __getFirstChan() && pNotify->EventId == CHANNEL_EVENT_ANSWER)
  382. {
  383. std::string Number;
  384. Number = pNotify->Caller;
  385. // 判断是否需要录音
  386. std::string sql = boost::str(Format("select id from record where caller = '%s'") % Number);
  387. JdbcHelper::GetInstance()->jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) {
  388. }, [=](sql::ResultSet* result) {
  389. if (!result->next()) // 数据库查不到记录,进行录音
  390. {
  391. LOG_DEBUG("操作标识:%ld", pNotify->ChanOpInstance);
  392. //if (pNotify->ChanOpInstance == 0) // 会议不进行再次录音
  393. if(CConfig::GetInstance()->isAutoRecord())
  394. COperationReactor::GetInstance()->procOperation(-1, LINE_OP_RECORD, pChan->no(), nullptr); // 执行录音
  395. }
  396. }, NULL);
  397. }
  398. }