gsd_TcpPlugin.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. *
  3. * gsd_TcpPlugin.cc
  4. *
  5. */
  6. #include "gsd_TcpPlugin.h"
  7. #include "config.hpp"
  8. using namespace drogon;
  9. using namespace gsd;
  10. using namespace utility;
  11. void TcpPlugin::initAndStart(const Json::Value &config)
  12. {
  13. this->tcpClient = std::shared_ptr<TCPClient>(new TCPClient);
  14. NoticeCenter::Instance().addListener(0, NOTICE_NETTY,
  15. [&](const Buffer::Ptr &pBuf){
  16. this->ListenNettyData(pBuf);
  17. });
  18. this->pool = std::make_shared<ThreadPool>(1,ThreadPool::PRIORITY_HIGHEST, false);
  19. }
  20. void TcpPlugin::shutdown()
  21. {
  22. /// Shutdown the plugin
  23. }
  24. /**
  25. * @description: 启动任务
  26. * @return {*}
  27. */
  28. void TcpPlugin::StartTask(){
  29. // 连接Netty
  30. this->tcpClient->startConnect(config::getPtr()->NettyIP, config::getPtr()->NettyPort);
  31. // 心跳定时器
  32. this->HeartbeatTimer = std::make_shared<Timer>(10.0f,[this](){
  33. if(this->tcpClient == nullptr){
  34. this->tcpClient = std::shared_ptr<TCPClient>(new TCPClient);
  35. return true;
  36. }
  37. this->SendHeartbeatData();
  38. return true;
  39. }, nullptr);
  40. }
  41. /**
  42. * @description: 发送心跳数据
  43. * @return {*}
  44. */
  45. void TcpPlugin::SendHeartbeatData(){
  46. if(this->tcpClient->alive()){
  47. if(!this->TcpNettyLogin){
  48. this->TCPLoginNetty();
  49. }
  50. }
  51. else{
  52. this->tcpClient->startConnect(config::getPtr()->NettyIP, config::getPtr()->NettyPort);
  53. }
  54. }
  55. /**
  56. * @description: 登录Netty
  57. * @return {*}
  58. */
  59. void TcpPlugin::TCPLoginNetty(){
  60. config::Ptr m_config = config::getPtr();
  61. // 登录
  62. SendLogin sendLogin;
  63. sendLogin.setAccount(m_config->usr);
  64. sendLogin.setPwd(md5(m_config->usr + m_config->pwd));
  65. DebugL << "VPN_IP:" << m_config->vpnIP << endl;
  66. sendLogin.setCameraIp(m_config->vpnIP);
  67. sendLogin.setSimCode(m_config->SimCode);
  68. std::string str;
  69. DeviceInfo deviceInfo;
  70. deviceInfo.localIp = m_config->vpnIP;
  71. deviceInfo.stream = "rtsp://"+ deviceInfo.localIp + ":8554/live";
  72. deviceInfo.objectToJson(str);
  73. sendLogin.setAttr1(str);
  74. NettyClientResultMsg<SendLogin> nettyClientResultMsg;
  75. std::string RequestId = uuid::generate();
  76. nettyClientResultMsg.setRequestId(RequestId);
  77. nettyClientResultMsg.setDataType(NettyClientCommandEnum().login);
  78. nettyClientResultMsg.setData(sendLogin);
  79. std::string json;
  80. nettyClientResultMsg.objectToJson(json);
  81. json += "\r\n";
  82. if(this->tcpClient->alive()){
  83. this->tcpClient->sendRequest(RequestId, NettyClientCommandEnum().login ,json, [&](int status,std::string buf){
  84. if(!config::getPtr()->debug){
  85. if(status == 200) TcpNettyLogin = true;
  86. else TcpNettyLogin = false;
  87. }else{
  88. TcpNettyLogin = true;
  89. }
  90. });
  91. }
  92. }
  93. /**
  94. * @description: 获取登录Netty结果
  95. * @return {*}
  96. */
  97. bool TcpPlugin::getLoginNetty(){
  98. return this->TcpNettyLogin;
  99. }
  100. /**
  101. * @description: 监听netty数据
  102. * @param {Ptr} &buf
  103. * @return {*}
  104. */
  105. void TcpPlugin::ListenNettyData(const Buffer::Ptr &buf)
  106. {
  107. std::string json = buf->toString();
  108. if(config::getPtr()->debug) DebugL << json << endl;
  109. NettyServerResultMsg<RecDeviceCommand> nettyServerResultMsg;
  110. NettyServerResultMsg<RecLoginMsg> NettyServerResultMsgLogin;
  111. if(nettyServerResultMsg.jsonToObject(json)){
  112. if(config::getPtr()->debug) InfoL << "接收到控制指令" << endl;
  113. std::string deviceId = std::to_string(nettyServerResultMsg.getData().getDeviceId());
  114. ExpelDevice expelDevice;
  115. if(!config::getPtr()->LowVersion){
  116. vector<vector<std::string>> results;
  117. SqlWriter selete("SELECT DeviceToken,DeviceType,ServerIp,ServerCname,ServerCpwd FROM gsdDB.`DeviceList` WHERE DeviceId = '?' and ISDELETE=0");
  118. selete << deviceId << results;
  119. if(results.empty()){
  120. WarnL << "No device is queried" << endl;
  121. return;
  122. }
  123. expelDevice.DeviceId = deviceId;
  124. expelDevice.deviceToken = (enum DeviceToken)std::atoi(results[0][0].c_str());
  125. expelDevice.deviceType = (enum DeviceType)std::atoi(results[0][1].c_str());
  126. expelDevice.ServerIp = results[0][2];
  127. expelDevice.ServerCname = results[0][3];
  128. expelDevice.ServerCpwd = results[0][4];
  129. }else{
  130. expelDevice.DeviceId = deviceId;
  131. expelDevice.deviceType = (enum DeviceType)config::getPtr()->deviceType;
  132. expelDevice.deviceToken = serial;
  133. }
  134. this->pool->async([&,nettyServerResultMsg, expelDevice](){
  135. NettyServerResultMsg<RecDeviceCommand> ResultMsg = nettyServerResultMsg;
  136. Expel::getPtr() -> sendDeviceMsgCallback(ResultMsg, expelDevice);
  137. });
  138. this->pool->start();
  139. }else if(NettyServerResultMsgLogin.jsonToObject(json)){
  140. RecLoginMsg Loginmsg = NettyServerResultMsgLogin.getData();
  141. if(NettyServerResultMsgLogin.getDataType() == "client_conn_success"){
  142. InfoL << "连接Netty成功,发送登录信息" << endl;
  143. this->TCPLoginNetty();
  144. }
  145. }
  146. }
  147. /**
  148. * @description: 获取与Netty的通讯是否存活
  149. * @return {*}
  150. */
  151. bool TcpPlugin::getAlive(){
  152. if(!config::getPtr()->debug) {
  153. if(!this->tcpClient->alive() || !this->TcpNettyLogin) return false;
  154. else return true;
  155. }else{
  156. return this->tcpClient->alive();
  157. }
  158. }
  159. /**
  160. * @description: 发送请求
  161. * @param {string} RequestId UUID
  162. * @param {string} CommandEnum 命令枚举
  163. * @param {string&} data 数据
  164. * @param {function<void(std::string)>} t 回调函数(请求结果, 返回的数据)
  165. * @return {*}
  166. */
  167. void TcpPlugin::sendRequest(std::string RequestId,std::string CommandEnum, std::string& data,function<void(int, std::string)> t){
  168. this->tcpClient->sendRequest(RequestId, CommandEnum, data, t);
  169. }