NettyHttpCarInfo.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-04 15:59:10
  6. * @LastEditors: Please set LastEditors
  7. * @LastEditTime: 2021-11-18 22:26:17
  8. */
  9. #pragma once
  10. #include <iostream>
  11. #include <vector>
  12. #include "Util/logger.h"
  13. #include "Util/NoticeCenter.h"
  14. #include "Poller/EventPoller.h"
  15. #include "Player/PlayerProxy.h"
  16. #include "Rtmp/RtmpPusher.h"
  17. #include "Common/config.h"
  18. #include "Pusher/MediaPusher.h"
  19. #include "Extension/Frame.h"
  20. #include "Util/SqlPool.h"
  21. #include "Network/TcpClient.h"
  22. #include "Poller/Timer.h"
  23. #include "NettyHttpTrainInfo.h"
  24. #include "NettyHttpCarDevData.h"
  25. using namespace std;
  26. class NettyHttpCarInfo
  27. {
  28. public:
  29. std::string id;
  30. std::string pid;
  31. std::string label;
  32. NettyHttpTrainInfo attr;
  33. std::string attr1;
  34. std::string child;
  35. vector<NettyHttpCarDevData> children;
  36. public:
  37. NettyHttpCarInfo() {}
  38. ~NettyHttpCarInfo() {}
  39. /**
  40. * @description: json转换为对象
  41. * @param {Value&} object
  42. * @return {*}
  43. */
  44. bool jsonToObject(const rapidjson::Value& object){
  45. const auto end = object.MemberEnd();
  46. if(end == object.FindMember("id") || !object["id"].IsString()){
  47. return false;
  48. }else{
  49. id = object["id"].GetString();
  50. }
  51. // if(end == object.FindMember("pid") || !object["pid"].IsString()){
  52. // return false;
  53. // }else{
  54. // pid = object["pid"].GetString();
  55. // }
  56. if(end == object.FindMember("label") || !object["label"].IsString()){
  57. return false;
  58. }else{
  59. label = object["label"].GetString();
  60. }
  61. if(end == object.FindMember("attr") || !object["attr"].IsObject()){
  62. return false;
  63. }else{
  64. const rapidjson::Value& obj = object["attr"];
  65. if(!attr.jsonToObject(obj)){
  66. return false;
  67. }
  68. }
  69. if(end == object.FindMember("children")|| !(object["children"].IsArray())){
  70. return false;
  71. }else{
  72. const rapidjson::Value& objs = object["children"];
  73. for (size_t i = 0; i < objs.Size(); i++) {
  74. const rapidjson::Value& obj = objs[i];
  75. NettyHttpCarDevData data;
  76. if(!(data.jsonToObject(obj))){
  77. return false;
  78. }
  79. children.push_back(data);
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * @description: 数据同步
  86. * @param {*}
  87. * @return {*}
  88. */
  89. bool NettyClientDataSync()
  90. {
  91. vector<vector<string>> sqlRet;
  92. // 更新TrainInfo表
  93. SqlWriter sqlSelectTrain("SELECT * FROM MIVA_DB.TrainInfo WHERE carId='?'");
  94. sqlSelectTrain << attr.carId << sqlRet;
  95. if(sqlRet.size() != 0){
  96. SqlWriter sqlUpdate("UPDATE MIVA_DB.TrainInfo SET carCode='?',carName='?',enableFlag='?' WHERE carId='?'");
  97. sqlUpdate << attr.carCode << attr.carName << attr.enableFlag << attr.carId << sqlRet;
  98. }else{
  99. SqlWriter sqlInstert("INSERT INTO MIVA_DB.TrainInfo(carId,carCode, carName, enableFlag) VALUES('?', '?', '?', '?')");
  100. sqlInstert << attr.carId << attr.carCode << attr.carName << attr.enableFlag << sqlRet;
  101. }
  102. // 更新CarData表
  103. vector<int> CarIds;
  104. CarIds.clear();
  105. vector<NettyHttpCarDevData>::iterator iter;
  106. for(iter=children.begin();iter!=children.end();iter++){
  107. std::string sql = "SELECT * FROM MIVA_DB.CarData WHERE CarId='?'";
  108. SqlWriter sqlSelect(sql.c_str());
  109. sqlSelect << iter->attr.carId << sqlRet;
  110. if(sqlRet.size() != 0){
  111. SqlWriter sqlUpdate("UPDATE MIVA_DB.CarData SET label='?',PId='?',enableFlag='?',addTime='?',updateTime='?' WHERE CarId='?'");
  112. sqlUpdate << iter->label << iter->pid << iter->attr.enableFlag << iter->attr.addTime << iter->attr.updateTime << iter->attr.carId << sqlRet;
  113. }else{
  114. SqlWriter sqlInstert("INSERT INTO MIVA_DB.CarData(NoLoad,DynamicLoad,Grade,Num,label,PId,CarId,enableFlag,addTime,updateTime)\
  115. VALUES('?','?','?','?','?','?','?','?','?','?')");
  116. sqlInstert << 0 << 0 << 0 << 0 << iter->label << iter->pid << iter->attr.carId << iter->attr.enableFlag << iter->attr.addTime << iter->attr.updateTime << sqlRet;
  117. }
  118. CarIds.push_back(iter->attr.carId);
  119. }
  120. if(CarIds.empty()){
  121. SqlWriter sqlDel("DELETE FROM MIVA_DB.CarData");
  122. sqlDel << sqlRet;
  123. }else{
  124. std::string sql = "DELETE FROM MIVA_DB.CarData WHERE carId NOT IN (";
  125. vector<int>::iterator iter1;
  126. int i = 0;
  127. for(iter1=CarIds.begin();iter1!=CarIds.end();iter1++){
  128. if(i == CarIds.size() - 1) sql += to_string(*iter1);
  129. else sql += to_string(*iter1) + ",";
  130. i++;
  131. }
  132. sql += ")";
  133. SqlWriter sqlDel(sql.c_str());
  134. sqlDel << sqlRet;
  135. }
  136. CarIds.clear();
  137. // 更新DataSources表
  138. for(iter=children.begin();iter!=children.end();iter++)
  139. {
  140. vector<CarBasicData>::iterator iter1;
  141. for(iter1=iter->children.begin(); iter1!=iter->children.end();iter1++){
  142. std::string sql = "SELECT * FROM MIVA_DB.DataSources WHERE dv_Id='?'";
  143. SqlWriter sqlSelect(sql.c_str());
  144. sqlSelect << iter1->attr.dvId << sqlRet;
  145. if(sqlRet.size() != 0){
  146. SqlWriter sqlUpdate("UPDATE MIVA_DB.DataSources SET CarId='?',Uri='?',PId='?',label='?',Del='?',addTime='?',updateTime='?',IP='?' WHERE dv_Id='?'");
  147. sqlUpdate << iter->attr.carId << iter1->attr.stream << iter1->pid << iter1->label << 0 << iter1->attr.addTime << iter1->attr.updateTime << iter1->attr.localIp << iter1->attr.dvId << sqlRet;
  148. }else{
  149. SqlWriter sqlInstert("INSERT INTO MIVA_DB.DataSources(CarId,Uri,Play,PId,label,Num,Del,addTime,updateTime,IP,dv_Id)\
  150. VALUES('?','?','?','?','?','?','?','?','?','?')");
  151. sqlInstert << iter->attr.carId<< iter1->attr.stream << 0 << iter1->pid << iter1->label << 0 << 0 << iter1->attr.addTime << iter1->attr.updateTime << iter1->attr.localIp << iter1->attr.dvId << sqlRet;
  152. }
  153. CarIds.push_back(iter1->attr.dvId);
  154. }
  155. }
  156. if(CarIds.empty()){
  157. SqlWriter sqlUpdate("UPDATE MIVA_DB.DataSources SET Del=1");
  158. sqlUpdate << sqlRet;
  159. }else{
  160. std::string sql = "UPDATE MIVA_DB.DataSources SET Del=1 WHERE dv_Id NOT IN (";
  161. vector<int>::iterator iter1;
  162. int i = 0;
  163. for(iter1=CarIds.begin();iter1!=CarIds.end();iter1++){
  164. if(i == CarIds.size() - 1) sql += to_string(*iter1);
  165. else sql += to_string(*iter1) + ",";
  166. i++;
  167. }
  168. sql += ")";
  169. SqlWriter sqlUpdate(sql.c_str());
  170. sqlUpdate << sqlRet;
  171. }
  172. return true;
  173. }
  174. };