linux版本中间件

OpRecord.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "OpRecord.h"
  2. #include "VirtualChan.h"
  3. #include "FsProxy.h"
  4. #include "Config.h"
  5. #include "OperationReactor.h"
  6. #include <boost/date_time/gregorian/gregorian.hpp>
  7. #include <boost/filesystem.hpp>
  8. #include <regex>
  9. #include "Session.h"
  10. COpRecord::COpRecord(COperationReactor* pParent, long Instance) : COperation(pParent, Instance)
  11. {
  12. }
  13. COpRecord::~COpRecord(void)
  14. {
  15. }
  16. /*****************************************************************
  17. **【函数名称】 _end
  18. **【函数功能】 操作完成
  19. **【参数】 IsSucceed 操作是否成功
  20. lpData 随路数据
  21. **【返回值】
  22. ****************************************************************/
  23. void COpRecord::_end(bool IsSucceed)
  24. {
  25. if (IsSucceed)
  26. {
  27. LOG_INFO("{OpRecord}: 通道[%lu]录音成功", m_pHostChan->no());
  28. }
  29. else
  30. {
  31. LOG_WARN("{OpRecord}: 通道[%lu]录音失败", m_pHostChan->no());
  32. }
  33. SqlWrite::GetInstance()->addSql(toSql(IsSucceed, "录音"));
  34. // 返回执行结果
  35. if (m_InstanceCancel != FS_LINK_INSTANCE_INVALID)
  36. m_pParent->onOpResult(m_InstanceCancel, !IsSucceed);
  37. m_pParent->onOpResult(m_Instance, this, IsSucceed,std::to_string(m_pHostChan->no()));
  38. }
  39. /*****************************************************************
  40. **【函数名称】 __makeDir
  41. **【函数功能】 创建录音目录
  42. **【参数】
  43. **【返回值】
  44. ****************************************************************/
  45. bool COpRecord::__makeDir(std::string& RcdFile)
  46. {
  47. using namespace boost::gregorian;
  48. date d(day_clock::local_day());
  49. std::string time = to_iso_extended_string(d);
  50. time = regex_replace(time,std::regex("-"),"");
  51. RcdFile.append(CConfig::GetInstance()->recordPath()).append("/");
  52. RcdFile.append(time).append("/");
  53. RcdFile.append(m_pHostChan->callerNum()).append(m_pHostChan->calleeNum());
  54. RcdFile.append(m_pHostChan->sessionId()).append(".wav");
  55. using namespace boost::filesystem;
  56. path p(RcdFile);
  57. if (exists(p))
  58. {
  59. LOG_WARN("录音文件已存在,sessionid[%s],path[%s]", m_pHostChan->sessionId().c_str(),RcdFile.c_str());
  60. return false;
  61. }
  62. return true;
  63. /*return create_directories(p);*/
  64. }
  65. /*****************************************************************
  66. **【函数名称】 attachHostChan
  67. **【函数功能】 关联操作与主控通道
  68. **【参数】
  69. **【返回值】
  70. *****************************************************************/
  71. bool COpRecord::attachHostChan(VirtualChan* pChan)
  72. {
  73. m_pHostChan = pChan;
  74. return true;
  75. }
  76. /*****************************************************************
  77. **【函数名称】 detachHostChan
  78. **【函数功能】 取消通道关联
  79. **【参数】
  80. **【返回值】
  81. *****************************************************************/
  82. void COpRecord::detachHostChan(void)
  83. {
  84. m_pHostChan = nullptr;
  85. }
  86. /*****************************************************************
  87. **【函数名称】 start
  88. **【函数功能】 执行操作
  89. **【参数】
  90. **【返回值】
  91. *****************************************************************/
  92. bool COpRecord::start(LineOpParam* pParam)
  93. {
  94. LOG_INFO("{OpRecord}: 通道[%lu]执行录音,State:%d", m_pHostChan->no(), m_pHostChan->state());
  95. // 校验状态
  96. //if(m_pHostChan->state() != CHAN_LOGIC_STATE_TALKING || m_pHostChan->state()!= CHAN_LOGIC_STATE_STANDBY)
  97. // return false;
  98. /*
  99. std::string rcdFile;
  100. if (!__makeDir(rcdFile))
  101. return false;
  102. LOG_INFO("{OpRecord}: 通道[%lu]执行录音, RecordFile = %s", m_pHostChan->no(), rcdFile.c_str());
  103. m_SessionId = m_pHostChan->sessionId();
  104. m_Caller = std::to_string(m_pHostChan->no());
  105. m_CallerAgent = CFsProxy::GetInstance().getAgentByExten(m_Caller);
  106. m_OpTime = Util::CurTime();
  107. auto pSession = CFsProxy::GetInstance().getSession(m_SessionId);
  108. if (pSession != nullptr)
  109. pSession->recordFile() = rcdFile;
  110. //pSession->recordFile() = rcdFile.substr(CConfig::GetInstance()->recordPath().length()+1, rcdFile.length()- CConfig::GetInstance()->recordPath().length()-1);
  111. */
  112. std::string rcdFile = pParam->szParam3;
  113. LOG_INFO("{OpRecord}: 通道[%lu]执行录音, RecordFile = %s", m_pHostChan->no(), rcdFile.c_str());
  114. m_SessionId = m_pHostChan->sessionId();
  115. m_Caller = std::to_string(m_pHostChan->no());
  116. m_CallerAgent = CFsProxy::GetInstance().getAgentByExten(m_Caller);
  117. m_OpTime = Util::CurTime();
  118. return CFsProxy::GetInstance().record(uniqueId(), m_pHostChan, rcdFile);
  119. }
  120. /*****************************************************************
  121. **【函数名称】 onBackgroudJobDone
  122. **【函数功能】 后台命令执行结束事件处理
  123. **【参数】
  124. **【返回值】
  125. *****************************************************************/
  126. void COpRecord::onBackgroudJobDone(PBG_JOB_NOTIFY pNotify)
  127. {
  128. std::cout << pNotify->JobBody << std::endl;
  129. if (pNotify->JobBody.find(ESL_JOB_DONE_RES_OK) == string::npos)
  130. _end(false);
  131. else
  132. _end(true);
  133. }