vehicle_patrol.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "vehicle_patrol.h"
  2. #include <QFile>
  3. #include <math.h>
  4. #include "modulecomm.h"
  5. #include "gpsimu.pb.h"
  6. extern std::string gstrserverip;
  7. extern std::string gstrpatrolPort;
  8. extern std::string gstrpatrolInterval;
  9. extern std::string gstrid;
  10. extern std::string gstrplateNumber;
  11. extern char stryamlpath[256];
  12. using org::jeecg::defsPatrol::grpc::Empty;
  13. using org::jeecg::defsPatrol::grpc::GPSPoint;
  14. VehiclePatrolExceptionClient::VehiclePatrolExceptionClient(std::shared_ptr<Channel> channel)
  15. {
  16. stub_ = VehiclePatrolException::NewStub(channel);
  17. dec_yaml(stryamlpath);
  18. ModuleFun funupdate = std::bind(&VehiclePatrolExceptionClient::ListenGPSIMUMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  19. shmGPSIMU.mpa = iv::modulecomm::RegisterRecvPlus(shmGPSIMU.mstrmsgname,funupdate);
  20. }
  21. VehiclePatrolExceptionClient::~VehiclePatrolExceptionClient(void)
  22. {
  23. }
  24. void VehiclePatrolExceptionClient::dec_yaml(const char *stryamlpath)
  25. {
  26. YAML::Node config;
  27. try
  28. {
  29. config = YAML::LoadFile(stryamlpath);
  30. }
  31. catch(YAML::BadFile &e)
  32. {
  33. std::cout<<e.what()<<std::endl;
  34. std::cout<<"yaml file load fail."<<std::endl;
  35. return;
  36. }
  37. catch(YAML::ParserException &e)
  38. {
  39. std::cout<<e.what()<<std::endl;
  40. std::cout<<"yaml file is malformed."<<std::endl;
  41. return;
  42. }
  43. std::string strmsgname;
  44. if(config["GPS_IMU"])
  45. {
  46. if(config["GPS_IMU"]["msgname"]&&config["GPS_IMU"]["buffersize"]&&config["GPS_IMU"]["buffercount"])
  47. {
  48. strmsgname = config["GPS_IMU"]["msgname"].as<std::string>();
  49. strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
  50. shmGPSIMU.mnBufferSize = config["GPS_IMU"]["buffersize"].as<int>();
  51. shmGPSIMU.mnBufferCount = config["GPS_IMU"]["buffercount"].as<int>();
  52. std::cout << "GPS_IMU:" << shmGPSIMU.mstrmsgname << "," << shmGPSIMU.mnBufferSize << "," << shmGPSIMU.mnBufferCount << std::endl;
  53. }
  54. }
  55. else
  56. {
  57. strmsgname = "hcp2_gpsimu";
  58. strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
  59. shmGPSIMU.mnBufferSize = 10000;
  60. shmGPSIMU.mnBufferCount = 1;
  61. }
  62. return;
  63. }
  64. void VehiclePatrolExceptionClient::ListenGPSIMUMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  65. {
  66. iv::gps::gpsimu xdata;
  67. if(!xdata.ParseFromArray(strdata,nSize))
  68. {
  69. std::cout<<" ListenGPSIMUMsg parese error."<<std::endl;
  70. return;
  71. }
  72. gMutex_GPSIMU.lock();
  73. currentPosition.set_latitude(xdata.lat());
  74. currentPosition.set_longitude(xdata.lon());
  75. currentPosition.set_height(xdata.height());
  76. gMutex_GPSIMU.unlock();
  77. }
  78. std::string VehiclePatrolExceptionClient::uploadVehiclePatrolInfo(void)
  79. {
  80. // Data we are sending to the server.
  81. PatrolRequest request;
  82. request.set_id(id);
  83. request.set_istvr(isTVR);
  84. request.set_violationstatus(violationStatus);
  85. request.set_vehiclelicensenumber(vehicleLicenseNumber);
  86. request.set_violationimage(violationImage.data(),violationImage.size());
  87. request.set_violationtime(violationTime);
  88. request.mutable_violationposition()->CopyFrom(violationPosition);
  89. request.set_isfsm(isFSM);
  90. request.set_firestatus(fireStatus);
  91. request.set_fireimage(fireImage.data(),fireImage.size());
  92. request.set_firetime(fireTime);
  93. request.mutable_fireposition()->CopyFrom(firePosition);
  94. request.set_istsgm(isTSGM);
  95. request.set_gatestatus(gateStatus);
  96. request.set_gateimage(gateImage.data(),gateImage.size());
  97. request.set_gatetime(gateTime);
  98. request.mutable_gateposition()->CopyFrom(gatePosition);
  99. request.set_platenumber(plateNumber);
  100. // Container for the data we expect from the server.
  101. Empty reply;
  102. // Context for the client. It could be used to convey extra information to
  103. // the server and/or tweak certain RPC behaviors.
  104. ClientContext context;
  105. gpr_timespec timespec;
  106. timespec.tv_sec = 5;
  107. timespec.tv_nsec = 0;
  108. timespec.clock_type = GPR_TIMESPAN;
  109. context.set_deadline(timespec);
  110. // The actual RPC.
  111. Status status = stub_ -> uploadVehiclePatrolInfo(&context,request,&reply);
  112. // Act upon its status.
  113. if (status.ok()) {
  114. return "uploadVehiclePatrolInfo RPC successed";
  115. } else {
  116. std::cout << status.error_code() << ": " << status.error_message()
  117. << std::endl;
  118. if(status.error_code() == 4)
  119. {
  120. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  121. }
  122. return "uploadVehiclePatrolInfo RPC failed";
  123. }
  124. }
  125. void VehiclePatrolExceptionClient::updatePatrolData(void)
  126. {
  127. id = gstrid;
  128. // isTVR = true;
  129. // violationStatus = 2;
  130. // vehicleLicenseNumber = "津B654321";
  131. // QFile xFile;
  132. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  133. // if(xFile.open(QIODevice::ReadOnly))
  134. // {
  135. // violationImage = xFile.readAll();
  136. // }
  137. // xFile.close();
  138. // violationTime = QDateTime::currentMSecsSinceEpoch();
  139. // violationPosition.set_height(0.1);
  140. // violationPosition.set_latitude(39.0666552);
  141. // violationPosition.set_longitude(117.3542963);
  142. // isFSM = true;
  143. // fireStatus = 1;
  144. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  145. // if(xFile.open(QIODevice::ReadOnly))
  146. // {
  147. // fireImage = xFile.readAll();
  148. // }
  149. // xFile.close();
  150. // fireTime = QDateTime::currentMSecsSinceEpoch();
  151. // firePosition.set_height(0.1);
  152. // firePosition.set_latitude(39.0667552);
  153. // firePosition.set_longitude(117.3542963);
  154. // isTSGM = true;
  155. // gateStatus = 2;
  156. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  157. // if(xFile.open(QIODevice::ReadOnly))
  158. // {
  159. // gateImage = xFile.readAll();
  160. // }
  161. // xFile.close();
  162. // gateTime = QDateTime::currentMSecsSinceEpoch();
  163. // gatePosition.set_height(0.1);
  164. // gatePosition.set_latitude(39.0665552);
  165. // gatePosition.set_longitude(117.3542963);
  166. plateNumber = gstrplateNumber;
  167. }
  168. void VehiclePatrolExceptionClient::run()
  169. {
  170. QTime xTime;
  171. xTime.start();
  172. int lastTime = xTime.elapsed();
  173. uint64_t interval = std::atoi(gstrpatrolInterval.c_str());
  174. while (true)
  175. {
  176. if(abs(xTime.elapsed() - lastTime)>=interval)
  177. {
  178. updatePatrolData();
  179. std::string reply = uploadVehiclePatrolInfo();
  180. std::cout<< reply <<std::endl;
  181. lastTime = xTime.elapsed();
  182. }
  183. }
  184. }