123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /*
- * @Description:
- * @Version: 1.0
- * @Autor: lishengyin
- * @Date: 2021-11-04 15:59:10
- * @LastEditors: Please set LastEditors
- * @LastEditTime: 2021-11-18 22:26:17
- */
- #pragma once
- #include <iostream>
- #include <vector>
- #include "Util/logger.h"
- #include "Util/NoticeCenter.h"
- #include "Poller/EventPoller.h"
- #include "Player/PlayerProxy.h"
- #include "Rtmp/RtmpPusher.h"
- #include "Common/config.h"
- #include "Pusher/MediaPusher.h"
- #include "Extension/Frame.h"
- #include "Util/SqlPool.h"
- #include "Network/TcpClient.h"
- #include "Poller/Timer.h"
- #include "NettyHttpTrainInfo.h"
- #include "NettyHttpCarDevData.h"
- using namespace std;
- class NettyHttpCarInfo
- {
- public:
- std::string id;
- std::string pid;
- std::string label;
- NettyHttpTrainInfo attr;
- std::string attr1;
- std::string child;
- vector<NettyHttpCarDevData> children;
- public:
- NettyHttpCarInfo() {}
- ~NettyHttpCarInfo() {}
- /**
- * @description: json转换为对象
- * @param {Value&} object
- * @return {*}
- */
- bool jsonToObject(const rapidjson::Value& object){
- const auto end = object.MemberEnd();
- if(end == object.FindMember("id") || !object["id"].IsString()){
- return false;
- }else{
- id = object["id"].GetString();
- }
- // if(end == object.FindMember("pid") || !object["pid"].IsString()){
- // return false;
- // }else{
- // pid = object["pid"].GetString();
- // }
- if(end == object.FindMember("label") || !object["label"].IsString()){
- return false;
- }else{
- label = object["label"].GetString();
- }
- if(end == object.FindMember("attr") || !object["attr"].IsObject()){
- return false;
- }else{
- const rapidjson::Value& obj = object["attr"];
- if(!attr.jsonToObject(obj)){
- return false;
- }
- }
- if(end == object.FindMember("children")|| !(object["children"].IsArray())){
- return false;
- }else{
- const rapidjson::Value& objs = object["children"];
- for (size_t i = 0; i < objs.Size(); i++) {
- const rapidjson::Value& obj = objs[i];
- NettyHttpCarDevData data;
- if(!(data.jsonToObject(obj))){
- return false;
- }
- children.push_back(data);
- }
- }
- return true;
- }
- /**
- * @description: 数据同步
- * @param {*}
- * @return {*}
- */
- bool NettyClientDataSync()
- {
- vector<vector<string>> sqlRet;
- // 更新TrainInfo表
- SqlWriter sqlSelectTrain("SELECT * FROM MIVA_DB.TrainInfo WHERE carId='?'");
- sqlSelectTrain << attr.carId << sqlRet;
- if(sqlRet.size() != 0){
- SqlWriter sqlUpdate("UPDATE MIVA_DB.TrainInfo SET carCode='?',carName='?',enableFlag='?' WHERE carId='?'");
- sqlUpdate << attr.carCode << attr.carName << attr.enableFlag << attr.carId << sqlRet;
- }else{
- SqlWriter sqlInstert("INSERT INTO MIVA_DB.TrainInfo(carId,carCode, carName, enableFlag) VALUES('?', '?', '?', '?')");
- sqlInstert << attr.carId << attr.carCode << attr.carName << attr.enableFlag << sqlRet;
- }
- // 更新CarData表
- vector<int> CarIds;
- CarIds.clear();
- vector<NettyHttpCarDevData>::iterator iter;
- for(iter=children.begin();iter!=children.end();iter++){
- std::string sql = "SELECT * FROM MIVA_DB.CarData WHERE CarId='?'";
- SqlWriter sqlSelect(sql.c_str());
- sqlSelect << iter->attr.carId << sqlRet;
- if(sqlRet.size() != 0){
- SqlWriter sqlUpdate("UPDATE MIVA_DB.CarData SET label='?',PId='?',enableFlag='?',addTime='?',updateTime='?' WHERE CarId='?'");
- sqlUpdate << iter->label << iter->pid << iter->attr.enableFlag << iter->attr.addTime << iter->attr.updateTime << iter->attr.carId << sqlRet;
- }else{
- SqlWriter sqlInstert("INSERT INTO MIVA_DB.CarData(NoLoad,DynamicLoad,Grade,Num,label,PId,CarId,enableFlag,addTime,updateTime)\
- VALUES('?','?','?','?','?','?','?','?','?','?')");
- sqlInstert << 0 << 0 << 0 << 0 << iter->label << iter->pid << iter->attr.carId << iter->attr.enableFlag << iter->attr.addTime << iter->attr.updateTime << sqlRet;
- }
- CarIds.push_back(iter->attr.carId);
- }
- if(CarIds.empty()){
- SqlWriter sqlDel("DELETE FROM MIVA_DB.CarData");
- sqlDel << sqlRet;
- }else{
- std::string sql = "DELETE FROM MIVA_DB.CarData WHERE carId NOT IN (";
- vector<int>::iterator iter1;
- int i = 0;
- for(iter1=CarIds.begin();iter1!=CarIds.end();iter1++){
- if(i == CarIds.size() - 1) sql += to_string(*iter1);
- else sql += to_string(*iter1) + ",";
- i++;
- }
- sql += ")";
- SqlWriter sqlDel(sql.c_str());
- sqlDel << sqlRet;
- }
-
- CarIds.clear();
- // 更新DataSources表
- for(iter=children.begin();iter!=children.end();iter++)
- {
- vector<CarBasicData>::iterator iter1;
- for(iter1=iter->children.begin(); iter1!=iter->children.end();iter1++){
- std::string sql = "SELECT * FROM MIVA_DB.DataSources WHERE dv_Id='?'";
- SqlWriter sqlSelect(sql.c_str());
- sqlSelect << iter1->attr.dvId << sqlRet;
- if(sqlRet.size() != 0){
- SqlWriter sqlUpdate("UPDATE MIVA_DB.DataSources SET CarId='?',Uri='?',PId='?',label='?',Del='?',addTime='?',updateTime='?',IP='?' WHERE dv_Id='?'");
- 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;
- }else{
- SqlWriter sqlInstert("INSERT INTO MIVA_DB.DataSources(CarId,Uri,Play,PId,label,Num,Del,addTime,updateTime,IP,dv_Id)\
- VALUES('?','?','?','?','?','?','?','?','?','?')");
- 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;
- }
- CarIds.push_back(iter1->attr.dvId);
- }
- }
-
- if(CarIds.empty()){
- SqlWriter sqlUpdate("UPDATE MIVA_DB.DataSources SET Del=1");
- sqlUpdate << sqlRet;
- }else{
- std::string sql = "UPDATE MIVA_DB.DataSources SET Del=1 WHERE dv_Id NOT IN (";
- vector<int>::iterator iter1;
- int i = 0;
- for(iter1=CarIds.begin();iter1!=CarIds.end();iter1++){
- if(i == CarIds.size() - 1) sql += to_string(*iter1);
- else sql += to_string(*iter1) + ",";
- i++;
- }
- sql += ")";
- SqlWriter sqlUpdate(sql.c_str());
- sqlUpdate << sqlRet;
- }
- return true;
- }
-
- };
|