linux版本中间件

Agent.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "Agent.h"
  2. #include "SqlWrite.h"
  3. #include "JdbcHelper.h"
  4. Agent::Agent(std::string a_AgentID, std::string a_ExtID) :m_AgentID(a_AgentID), m_Exten(a_ExtID), m_IsRepose(false), m_State(AGENT_STATE_FREE)
  5. {
  6. m_TimeLogin = __getCurTime();
  7. m_LoginTimes = 0;
  8. m_FreeTimes = 0;
  9. m_ReposeTimes = 0;
  10. m_TalkTimes = 0;
  11. m_ReposeNum = 0;
  12. m_AnswerNum = 0;
  13. __login();
  14. }
  15. Agent::Agent(std::string a_AgentID, std::string a_ExtID, std::string a_Group) :m_AgentID(a_AgentID), m_Exten(a_ExtID), m_IsRepose(false), m_State(AGENT_STATE_FREE)
  16. {
  17. m_TimeLogin = __getCurTime();
  18. m_LoginTimes = 0;
  19. m_FreeTimes = 0;
  20. m_ReposeTimes = 0;
  21. m_TalkTimes = 0;
  22. m_ReposeNum = 0;
  23. m_AnswerNum = 0;
  24. m_Groups.clear();
  25. boost::split(m_Groups, a_Group, boost::is_any_of("|"));
  26. __login();
  27. }
  28. Agent::~Agent()
  29. {
  30. }
  31. void Agent::removeAgent(std::string HostAgent)
  32. {
  33. __logout();
  34. setState(AGENT_STATE_LOGOUT);
  35. m_TimeLogout = __getCurTime();
  36. m_LoginTimes = __subTime(m_TimeLogin, m_TimeLogout);
  37. Format fmt("insert into agent_detail(AgentId,ExtId,TimeLogin,TimeLogout,LoginTimes,LogoutHost) values(%s,%s,'%s','%s',%lld,'%s')");
  38. fmt %m_AgentID %m_Exten %__FormatTime(m_TimeLogin) % __FormatTime(m_TimeLogout) % m_LoginTimes %HostAgent;
  39. SqlWrite::GetInstance()->addSql(fmt.str());
  40. }
  41. bool Agent::setState(AGENT_STATE a_AgentState)
  42. {
  43. switch (a_AgentState)
  44. {
  45. case AGENT_STATE_FREE:
  46. m_IsRepose = false;
  47. break;
  48. case AGENT_STATE_BUSY:
  49. m_AnswerNum++;
  50. break;
  51. case AGENT_STATE_REPOSE:
  52. m_ReposeNum++;
  53. m_IsRepose = true;
  54. break;
  55. default:
  56. break;
  57. }
  58. if (a_AgentState == AGENT_STATE_FREE || a_AgentState == AGENT_STATE_REPOSE || a_AgentState == AGENT_STATE_LOGING || a_AgentState == AGENT_STATE_LOGOUT)
  59. {
  60. Format fmt("insert into agent_state(agent,exten,cur_time,state) values('%s','%s','%s',%d)");
  61. fmt %m_AgentID %m_Exten %__FormatTime(__getCurTime()) % a_AgentState;
  62. SqlWrite::GetInstance()->addSql(fmt.str());
  63. }
  64. m_State = a_AgentState;
  65. return false;
  66. }
  67. std::string Agent::state_s()
  68. {
  69. std::string state;
  70. switch ((int)m_State)
  71. {
  72. case AGENT_STATE_FREE: // 空闲
  73. state = "空闲";
  74. break;
  75. case AGENT_STATE_BUSY: // 通话中
  76. state = "通话中";
  77. break;
  78. case AGENT_STATE_REPOSE: // 小休
  79. state = "小休";
  80. break;
  81. case AGENT_STATE_REQUESTED: // 被请求
  82. state = "振铃中";
  83. break;
  84. }
  85. return state;
  86. }
  87. bool Agent::__login()
  88. {
  89. setState(AGENT_STATE_LOGING);
  90. setState(AGENT_STATE_FREE);
  91. /*Format fmt("update agent set exten = '%s' where agent = '%s'");
  92. fmt %m_Exten %m_AgentID;*/
  93. Format fmt("insert into agent(agent,exten) values('%s','%s')");
  94. fmt %m_AgentID %m_Exten;
  95. std::string sql;
  96. sql = fmt.str();
  97. int n = JdbcHelper::GetInstance()->jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
  98. }, [sql](sql::SQLException &e) {
  99. Format fmt("Sql执行失败,错误信息:[%s],Sql[%s]");
  100. fmt % e.what() % sql;
  101. LOG_ERROR(fmt.str().c_str());
  102. return false;
  103. });
  104. return n > 0 ? true : false;
  105. }
  106. bool Agent::__logout()
  107. {
  108. Format fmt("delete from agent where agent = '%s'");
  109. fmt %m_AgentID;
  110. /*Format fmt("update agent set exten = '%s' where agent = '%s'");
  111. fmt %"" %m_AgentID;*/
  112. std::string sql;
  113. sql = fmt.str();
  114. JdbcHelper::GetInstance()->jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
  115. }, [sql](sql::SQLException &e) {
  116. Format fmt("Sql执行失败,错误信息:[%s],Sql[%s]");
  117. fmt % e.what() % sql;
  118. LOG_ERROR(fmt.str().c_str());
  119. return false;
  120. });
  121. return true;
  122. }
  123. std::string Agent::__FormatTime(ptime time)
  124. {
  125. std::string now = to_iso_extended_string(time);
  126. std::regex reg("T");
  127. return std::regex_replace(now, reg, " ");
  128. }
  129. ptime Agent::__getCurTime()
  130. {
  131. return second_clock::local_time();
  132. }
  133. int64_t Agent::__subTime(ptime before, ptime after)
  134. {
  135. return (after - before).total_seconds();
  136. }
  137. //void Agent::onTimer()
  138. //{
  139. // switch (m_State)
  140. // {
  141. // case AGENT_STATE_FREE:
  142. // m_FreeTimes++;
  143. // break;
  144. // case AGENT_STATE_REPOSE:
  145. // m_ReposeTimes++;
  146. // break;
  147. // case AGENT_STATE_BUSY:
  148. // m_TalkTimes++;
  149. // break;
  150. // default:
  151. // break;
  152. // }
  153. //
  154. //}