linux版本中间件

Agent.cpp 4.0KB

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