#include #include #include #include #include #include #include #include #include "modulecomm.h" #include "rpc_client.hpp" #include "cloud.pb.h" std::string gstrserverip = "123.57.212.138"; std::string gstrserverport = "9000"; std::string gstruploadinterval = "1000"; void * gpa; QMutex gMutexMsg; std::thread * guploadthread; namespace iv { struct msgunit { char mstrmsgname[256]; int mnBufferSize = 10000; int mnBufferCount = 1; void * mpa; std::shared_ptr mpstrmsgdata; int mndatasize = 0; bool mbRefresh = false; }; } std::vector mvectormsgunit; std::vector mvectorctrlmsgunit; std::string gstrVIN = "AAAAAAAAAAAAAAAAA"; std::string gstrqueryMD5 = "5d41402abc4b2a76b9719d911017c592"; std::string gstrctrlMD5 = "5d41402abc4b2a76b9719d911017c592"; struct uploadresponsemsg { int nres; //0 no message 1 have message int id; qint64 ntime; std::string strVIN; std::string strctrlMD5; std::vector xdata; MSGPACK_DEFINE(nres,id, ntime,strVIN, strctrlMD5,xdata); }; int gindex = 0; void ListenData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) { int nsize = mvectormsgunit.size(); int i; for(i=0;ixclouddata_size(); for(i=0;ixclouddata(i).msgname().data(), mvectorctrlmsgunit[j].mstrmsgname,255) == 0) { // qDebug("size is %d ",pxmsg->xclouddata(i).data().size()); iv::modulecomm::ModuleSendMsg(mvectorctrlmsgunit[j].mpa,pxmsg->xclouddata(i).data().data(),pxmsg->xclouddata(i).data().size()); break; } } } } void threadupload() { int nsize = mvectormsgunit.size(); int i; int ninterval = atoi(gstruploadinterval.data()); if(ninterval<=0)ninterval = 100; QTime xTime; xTime.start(); int nlastsend = xTime.elapsed(); bool r; rest_rpc::rpc_client client; client.enable_auto_reconnect(); //automatic reconnect client.enable_auto_heartbeat(); //automatic heartbeat // bool r = client.connect("140.143.237.38", 9000); r = client.connect(gstrserverip, atoi(gstrserverport.data())); int count = 0; CT: while (true) { if (client.has_connected()) { std::cout << "connected ok\n"; break; } else { std::cout << "connected failed: "<< count++<<"\n"; } std::this_thread::sleep_for(std::chrono::seconds(1)); } while(true) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); if((xTime.elapsed()-nlastsend)CopyFrom(xcloudunit); } } gMutexMsg.unlock(); int nbytesize = xmsg.ByteSize(); std::vector pvectordata; pvectordata.resize(nbytesize); if(xmsg.SerializeToArray(pvectordata.data(),nbytesize)) { uploadresponsemsg result = client.call<60000,uploadresponsemsg>("get_clientresp",gindex,QDateTime::currentMSecsSinceEpoch(), gstrVIN,gstrqueryMD5,gstrctrlMD5,pvectordata); // qDebug("res is %d ",result.nres); if(result.nres == 1) { iv::cloud::cloudmsg xmsg; if(xmsg.ParseFromArray(result.xdata.data(),result.xdata.size())) { sharectrlmsg(&xmsg); } } gindex++; } nlastsend = xTime.elapsed(); pvectordata.clear(); } catch (const std::exception & e) { std::cout << e.what() << std::endl; // client.close(); goto CT; } } } void dec_yaml(const char * stryamlpath) { YAML::Node config; try { config = YAML::LoadFile(stryamlpath); } catch(YAML::BadFile e) { qDebug("load error."); return; } std::vector vecmodulename; if(config["server"]) { gstrserverip = config["server"].as(); } if(config["port"]) { gstrserverport = config["port"].as(); } if(config["uploadinterval"]) { gstruploadinterval = config["uploadinterval"].as(); } std::string strmsgname; if(config["uploadmessage"]) { for(YAML::const_iterator it= config["uploadmessage"].begin(); it != config["uploadmessage"].end();++it) { std::string strtitle = it->first.as(); std::cout<(); strncpy(xmu.mstrmsgname,strmsgname.data(),255); xmu.mnBufferSize = config["uploadmessage"][strtitle]["buffersize"].as(); xmu.mnBufferCount = config["uploadmessage"][strtitle]["buffercount"].as(); mvectormsgunit.push_back(xmu); } } } else { } if(config["ctrlmessage"]) { std::string strnodename = "ctrlmessage"; for(YAML::const_iterator it= config[strnodename].begin(); it != config[strnodename].end();++it) { std::string strtitle = it->first.as(); std::cout<(); strncpy(xmu.mstrmsgname,strmsgname.data(),255); xmu.mnBufferSize = config[strnodename][strtitle]["buffersize"].as(); xmu.mnBufferCount = config[strnodename][strtitle]["buffercount"].as(); mvectorctrlmsgunit.push_back(xmu); } } } else { } return; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); char stryamlpath[256]; if(argc<2) { snprintf(stryamlpath,255,"driver_cloud_client.yaml"); // strncpy(stryamlpath,abs_ymlpath,255); } else { strncpy(stryamlpath,argv[1],255); } dec_yaml(stryamlpath); int i; for(i=0;i