gsd_HttpPlugin.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. *
  3. * gsd_HttpPlugin.cc
  4. *
  5. */
  6. #include "gsd_HttpPlugin.h"
  7. using namespace drogon;
  8. using namespace gsd;
  9. void HttpPlugin::initAndStart(const Json::Value &config)
  10. {
  11. /// Initialize and start the plugin
  12. if(this->Init() != OK){
  13. ErrorL << "HttpPlugin 初始化失败" << endl;
  14. }
  15. }
  16. void HttpPlugin::shutdown()
  17. {
  18. /// Shutdown the plugin
  19. }
  20. /**
  21. * @description: 初始化
  22. * @return {*}
  23. */
  24. int8_t HttpPlugin::Init(){
  25. if(!config::getPtr()->debug) {
  26. if(!config::getPtr()->LowVersion){
  27. if(this->m_httpClient == nullptr) this->m_httpClient = HttpClient::CreateNew();
  28. // HTTP NETTY登录
  29. if(this->m_httpClient->Init( config::getPtr()->usr, config::getPtr()->NettyIP, 8860) == OK){
  30. InfoL << "The Netty login succeeds. Procedure" << endl;
  31. }else{
  32. WarnL << "Netty login failed. Procedure" << endl;
  33. }
  34. }
  35. }
  36. return OK;
  37. }
  38. /**
  39. * @description: 启动任务
  40. * @return {*}
  41. */
  42. void HttpPlugin::StartTask(){
  43. this->timer0 = std::make_shared<Timer>(2.0f, [&](){
  44. if(!config::getPtr()->debug) {
  45. if(!config::getPtr()->LowVersion){
  46. if(!this->m_httpClient->alive()){
  47. this->m_httpClient->LoginNetty();
  48. }else{
  49. this->getHttpInfo();
  50. }
  51. }
  52. }
  53. return true;
  54. }, nullptr);
  55. }
  56. /**
  57. * @description: 获取数据
  58. * @param {*}
  59. * @return {*}
  60. */
  61. void HttpPlugin::getHttpInfo()
  62. {
  63. try
  64. {
  65. NettyHttpResultMsg<NettyExpelDevInfo,NettyHttpNull> nettyHttpResultMsg;
  66. NettyHttpResultMsg<NettyHttpDicCode,NettyHttpNull> nettyHttpResultMsg1;
  67. ExpelDevice expelDevice;
  68. if(this->m_httpClient->getHttpDicCode("peyServerIp", nettyHttpResultMsg1) == OK){
  69. if(!nettyHttpResultMsg1.datas.empty()){
  70. auto iter = nettyHttpResultMsg1.datas.begin();
  71. expelDevice.ServerIp = iter->attr1;
  72. nettyHttpResultMsg1.datas.clear();
  73. }
  74. }
  75. if(this->m_httpClient->getHttpDicCode("peyServerCname", nettyHttpResultMsg1) == OK){
  76. if(!nettyHttpResultMsg1.datas.empty()){
  77. auto iter = nettyHttpResultMsg1.datas.begin();
  78. expelDevice.ServerCname = iter->attr1;
  79. nettyHttpResultMsg1.datas.clear();
  80. }
  81. }
  82. if(this->m_httpClient->getHttpDicCode("peyServerCpwd", nettyHttpResultMsg1) == OK){
  83. if(!nettyHttpResultMsg1.datas.empty()){
  84. auto iter = nettyHttpResultMsg1.datas.begin();
  85. expelDevice.ServerCpwd = iter->attr1;
  86. nettyHttpResultMsg1.datas.clear();
  87. }
  88. }
  89. config::Ptr aconfig = config::getPtr();
  90. if(this->m_httpClient->getExpelInfo(aconfig->SimCode, nettyHttpResultMsg) == OK){
  91. char ctime[80];
  92. HttpClient::getDataTime(ctime);
  93. std::string updateTime = ctime;
  94. int i = 1;
  95. vector<vector<std::string>> ret;
  96. // 同步数据
  97. for(auto iter = nettyHttpResultMsg.datas.begin(); iter != nettyHttpResultMsg.datas.end(); iter++){
  98. expelDevice.DeviceId = iter->birdDeviceCode;
  99. switch ((int)iter->deviceControlType)
  100. {
  101. case 2:
  102. // 上海民航1
  103. expelDevice.deviceType = DeviceGasV1;
  104. expelDevice.deviceToken = serial;
  105. break;
  106. case 3:
  107. // 上海民航2
  108. expelDevice.deviceType = DeviceUlt;
  109. expelDevice.deviceToken = serial;
  110. break;
  111. case 4:
  112. // 兰州
  113. expelDevice.deviceType = DeviceGasV2;
  114. expelDevice.deviceToken = serial;
  115. break;
  116. case 5:
  117. // 派尔云
  118. expelDevice.deviceType = DevicePlayer;
  119. expelDevice.deviceToken = http;
  120. break;
  121. default:
  122. // 协议不存在
  123. return;
  124. break;
  125. }
  126. expelDevice.UpdateTime = updateTime;
  127. expelDevice.Id = i;
  128. std::string sql = expelDevice.ObjectToSql();
  129. SqlWriter writer(sql.c_str());
  130. writer << ret;
  131. i++;
  132. }
  133. SqlWriter Update("UPDATE gsdDB.`DeviceList` SET ISDELETE = 1 WHERE UpdateTime != '?'");
  134. Update << updateTime << ret;
  135. }
  136. }
  137. catch(const std::exception& e)
  138. {
  139. ErrorL << e.what() << '\n';
  140. }
  141. }