mainwindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. /*
  4. #include <yaml-cpp/yaml.h>
  5. #include <QDateTime>
  6. #include <iostream>
  7. #include <vector>
  8. #include <memory>
  9. #include <QMutex>
  10. #include <thread>
  11. #include "modulecomm.h"
  12. #include "cloud.pb.h"
  13. #include <iostream>
  14. #include <memory>
  15. #include <string>
  16. #include <grpcpp/grpcpp.h>
  17. #include "uploadmsg.grpc.pb.h"
  18. using grpc::Channel;
  19. using grpc::ClientContext;
  20. using grpc::Status;
  21. std::string gstrserverip = "140.143.237.38";
  22. std::string gstrserverport = "9000";
  23. std::string gstruploadinterval = "100";
  24. void * gpa;
  25. QMutex gMutexMsg;
  26. std::thread * guploadthread;
  27. namespace iv {
  28. struct msgunit
  29. {
  30. char mstrmsgname[256];
  31. int mnBufferSize = 10000;
  32. int mnBufferCount = 1;
  33. void * mpa;
  34. std::shared_ptr<char> mpstrmsgdata;
  35. int mndatasize = 0;
  36. bool mbRefresh = false;
  37. bool mbImportant = false;
  38. int mnkeeptime = 100;
  39. };
  40. }
  41. std::vector<iv::msgunit> mvectormsgunit;
  42. std::vector<iv::msgunit> mvectorctrlmsgunit;
  43. std::string gstrVIN = "AAAAAAAAAAAAAAAAA";
  44. std::string gstrqueryMD5 = "5d41402abc4b2a76b9719d911017c592";
  45. std::string gstrctrlMD5 = "5d41402abc4b2a76b9719d911017c592";
  46. int gindex = 0;
  47. void ListenData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  48. {
  49. int nsize = mvectorctrlmsgunit.size();
  50. int i;
  51. for(i=0;i<nsize;i++)
  52. {
  53. if(strncmp(strmemname,mvectorctrlmsgunit[i].mstrmsgname,255) == 0)
  54. {
  55. gMutexMsg.lock();
  56. char * strtem = new char[nSize];
  57. memcpy(strtem,strdata,nSize);
  58. mvectorctrlmsgunit[i].mpstrmsgdata.reset(strtem);
  59. mvectorctrlmsgunit[i].mndatasize = nSize;
  60. mvectorctrlmsgunit[i].mbRefresh = true;
  61. gMutexMsg.unlock();
  62. break;
  63. }
  64. }
  65. }
  66. void sharequerymsg(iv::cloud::cloudmsg * pxmsg)
  67. {
  68. int i;
  69. int nsize = pxmsg->xclouddata_size();
  70. for(i=0;i<nsize;i++)
  71. {
  72. int j;
  73. int nquerysize = mvectormsgunit.size();
  74. for(j=0;j<nquerysize;j++)
  75. {
  76. if(strncmp(pxmsg->xclouddata(i).msgname().data(), mvectormsgunit[j].mstrmsgname,255) == 0)
  77. {
  78. qDebug("size is %d ",pxmsg->xclouddata(i).data().size());
  79. iv::modulecomm::ModuleSendMsg(mvectormsgunit[j].mpa,pxmsg->xclouddata(i).data().data(),pxmsg->xclouddata(i).data().size());
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. void threadquery()
  86. {
  87. int nsize = mvectormsgunit.size();
  88. int nctrlsize = mvectorctrlmsgunit.size();
  89. int i;
  90. qint64 nlasttime = 0;
  91. int ninterval = atoi(gstruploadinterval.data());
  92. if(ninterval<=0)ninterval = 100;
  93. QTime xTime;
  94. xTime.start();
  95. int nlastsend = xTime.elapsed();
  96. std::string target_str = gstrserverip+":";
  97. target_str = target_str + gstrserverport ;//std::to_string()
  98. auto cargs = grpc::ChannelArguments();
  99. cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
  100. cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
  101. std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
  102. target_str, grpc::InsecureChannelCredentials(),cargs);
  103. std::unique_ptr<iv::Upload::Stub> stub_ = iv::Upload::NewStub(channel);
  104. iv::queryreq request;
  105. iv::ctrlreq ctrreq;
  106. iv::ctrlReply ctrreply;
  107. int nid = 0;
  108. int nctrlid = 0;
  109. // Container for the data we expect from the server.
  110. iv::queryReply reply;
  111. while(true)
  112. {
  113. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  114. if(abs(xTime.elapsed()-nlastsend)<ninterval)
  115. {
  116. continue;
  117. }
  118. bool bImportant = false;
  119. int nkeeptime = 0;
  120. iv::cloud::cloudmsg xmsg;
  121. xmsg.set_xtime(QDateTime::currentMSecsSinceEpoch());
  122. nlastsend = xTime.elapsed();
  123. gMutexMsg.lock();
  124. // std::vector<iv::msgunit> xv = mvectorctrlmsgunit;
  125. for(i=0;i<nctrlsize;i++)
  126. {
  127. if(mvectorctrlmsgunit[i].mbRefresh)
  128. {
  129. mvectorctrlmsgunit[i].mbRefresh = false;
  130. if(mvectorctrlmsgunit[i].mbImportant)
  131. {
  132. bImportant = true;
  133. }
  134. if(mvectorctrlmsgunit[i].mnkeeptime > nkeeptime)
  135. {
  136. nkeeptime = mvectorctrlmsgunit[i].mnkeeptime;
  137. }
  138. iv::cloud::cloudunit xcloudunit;
  139. xcloudunit.set_msgname(mvectorctrlmsgunit[i].mstrmsgname);
  140. xcloudunit.set_data(mvectorctrlmsgunit[i].mpstrmsgdata.get(),mvectorctrlmsgunit[i].mndatasize);
  141. iv::cloud::cloudunit * pcu = xmsg.add_xclouddata();
  142. pcu->CopyFrom(xcloudunit);
  143. }
  144. }
  145. gMutexMsg.unlock();
  146. {
  147. ClientContext context ;
  148. // qint64 time1 = QDateTime::currentMSecsSinceEpoch();
  149. request.set_strquerymd5(gstrqueryMD5);
  150. request.set_strvin(gstrVIN);
  151. request.set_nlasttime(nlasttime);
  152. QDateTime xTime;
  153. xTime.fromMSecsSinceEpoch(1607905685318); //1607914763641
  154. // qDebug("time:%s",xTime.toString("yyyy-MM-dd:hh:mm:ss:zzz").toLatin1().data());
  155. qDebug("nlasttime is %ld",nlasttime);//1607905685318
  156. nid++;
  157. // The actual RPC.
  158. Status status = stub_->query(&context, request, &reply);
  159. if (status.ok()) {
  160. std::cout<<nid<<" query successfully, res is "<<reply.nres()<<std::endl;
  161. if(reply.nres() == 1)
  162. {
  163. if(nlasttime != xmsg.xtime())
  164. {
  165. iv::cloud::cloudmsg xmsg;
  166. if(xmsg.ParseFromArray(reply.data().data(),reply.data().size()))
  167. {
  168. sharequerymsg(&xmsg);
  169. }
  170. }
  171. nlasttime = reply.ntime();
  172. }
  173. } else {
  174. std::cout << status.error_code() << ": " << status.error_message()
  175. << std::endl;
  176. std::cout<<"RPC failed"<<std::endl;
  177. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  178. }
  179. }
  180. if(xmsg.xclouddata_size()>0)
  181. {
  182. int nbytesize = xmsg.ByteSize();
  183. std::vector<char> pvectordata;
  184. pvectordata.resize(nbytesize);
  185. if(xmsg.SerializeToArray(pvectordata.data(),nbytesize))
  186. {
  187. ClientContext context ;
  188. qint64 time1 = QDateTime::currentMSecsSinceEpoch();
  189. ctrreq.set_id(nctrlid);nctrlid++;
  190. ctrreq.set_strctrlmd5(gstrctrlMD5);
  191. ctrreq.set_strvin(gstrVIN);
  192. ctrreq.set_ntime(time1);
  193. ctrreq.set_data(pvectordata.data(),pvectordata.size());
  194. ctrreq.set_bimportant(bImportant);
  195. ctrreq.set_kepptime(nkeeptime);
  196. Status status = stub_->ctrl(&context, ctrreq, &ctrreply);
  197. if (status.ok()) {
  198. std::cout<<"send id "<<ctrreply.nsendid()<<std::endl;
  199. } else {
  200. std::cout << status.error_code() << ": " << status.error_message()
  201. << std::endl;
  202. std::cout<<"RPC failed"<<std::endl;
  203. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  204. }
  205. }
  206. pvectordata.clear();
  207. }
  208. }
  209. }
  210. void dec_yaml(const char * stryamlpath)
  211. {
  212. YAML::Node config;
  213. try
  214. {
  215. config = YAML::LoadFile(stryamlpath);
  216. }
  217. catch(YAML::BadFile e)
  218. {
  219. qDebug("load error.");
  220. return;
  221. }
  222. std::vector<std::string> vecmodulename;
  223. if(config["server"])
  224. {
  225. gstrserverip = config["server"].as<std::string>();
  226. }
  227. if(config["port"])
  228. {
  229. gstrserverport = config["port"].as<std::string>();
  230. }
  231. if(config["uploadinterval"])
  232. {
  233. gstruploadinterval = config["uploadinterval"].as<std::string>();
  234. }
  235. if(config["VIN"])
  236. {
  237. gstrVIN = config["VIN"].as<std::string>();
  238. }
  239. if(config["VIN"])
  240. {
  241. gstrVIN = config["VIN"].as<std::string>();
  242. }
  243. if(config["queryMD5"])
  244. {
  245. gstrqueryMD5 = config["queryMD5"].as<std::string>();
  246. }
  247. if(config["ctrlMD5"])
  248. {
  249. gstrctrlMD5 = config["ctrlMD5"].as<std::string>();
  250. }
  251. std::string strmsgname;
  252. if(config["querymessage"])
  253. {
  254. for(YAML::const_iterator it= config["querymessage"].begin(); it != config["querymessage"].end();++it)
  255. {
  256. std::string strtitle = it->first.as<std::string>();
  257. std::cout<<strtitle<<std::endl;
  258. if(config["querymessage"][strtitle]["msgname"]&&config["querymessage"][strtitle]["buffersize"]&&config["querymessage"][strtitle]["buffercount"])
  259. {
  260. iv::msgunit xmu;
  261. strmsgname = config["querymessage"][strtitle]["msgname"].as<std::string>();
  262. strncpy(xmu.mstrmsgname,strmsgname.data(),255);
  263. xmu.mnBufferSize = config["querymessage"][strtitle]["buffersize"].as<int>();
  264. xmu.mnBufferCount = config["querymessage"][strtitle]["buffercount"].as<int>();
  265. mvectormsgunit.push_back(xmu);
  266. }
  267. }
  268. }
  269. else
  270. {
  271. }
  272. if(config["ctrlmessage"])
  273. {
  274. std::string strnodename = "ctrlmessage";
  275. for(YAML::const_iterator it= config[strnodename].begin(); it != config[strnodename].end();++it)
  276. {
  277. std::string strtitle = it->first.as<std::string>();
  278. std::cout<<strtitle<<std::endl;
  279. if(config[strnodename][strtitle]["msgname"]&&config[strnodename][strtitle]["buffersize"]&&config[strnodename][strtitle]["buffercount"])
  280. {
  281. iv::msgunit xmu;
  282. strmsgname = config[strnodename][strtitle]["msgname"].as<std::string>();
  283. strncpy(xmu.mstrmsgname,strmsgname.data(),255);
  284. xmu.mnBufferSize = config[strnodename][strtitle]["buffersize"].as<int>();
  285. xmu.mnBufferCount = config[strnodename][strtitle]["buffercount"].as<int>();
  286. if(config[strnodename][strtitle]["bimportant"])
  287. {
  288. std::string strimportant = config[strnodename][strtitle]["bimportant"].as<std::string>();
  289. if(strimportant == "true")
  290. {
  291. xmu.mbImportant = true;
  292. }
  293. }
  294. if(config[strnodename][strtitle]["keeptime"])
  295. {
  296. std::string strkeep = config[strnodename][strtitle]["keeptime"].as<std::string>();
  297. xmu.mnkeeptime = atoi(strkeep.data());
  298. }
  299. mvectorctrlmsgunit.push_back(xmu);
  300. }
  301. }
  302. }
  303. else
  304. {
  305. }
  306. return;
  307. }
  308. */
  309. MainWindow::MainWindow(QWidget *parent)
  310. : QMainWindow(parent)
  311. , ui(new Ui::MainWindow)
  312. {
  313. ui->setupUi(this);
  314. std::string stryamlpath = "./driver_cloud_grpc_pc.yaml";
  315. // dec_yaml(stryamlpath.data());
  316. // int i;
  317. // for(i=0;i<mvectormsgunit.size();i++)
  318. // {
  319. // mvectormsgunit[i].mpa = iv::modulecomm::RegisterSend(mvectormsgunit[i].mstrmsgname,mvectormsgunit[i].mnBufferSize,mvectormsgunit[i].mnBufferCount);
  320. // }
  321. // for(i=0;i<mvectorctrlmsgunit.size();i++)
  322. // {
  323. // mvectorctrlmsgunit[i].mpa = iv::modulecomm::RegisterRecv(mvectorctrlmsgunit[i].mstrmsgname,ListenData);
  324. // }
  325. // guploadthread = new std::thread(threadquery);
  326. mgrpcpc = new grpcpc(stryamlpath);
  327. mgrpcpc->start();
  328. }
  329. MainWindow::~MainWindow()
  330. {
  331. mgrpcpc->requestInterruption();
  332. while(mgrpcpc->isFinished());
  333. delete ui;
  334. }