vehicle_upload.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "vehicle_upload.h"
  2. #include <QFile>
  3. #include <QString>
  4. #include <QStringList>
  5. extern std::string gstrserverip;
  6. extern std::string gstruploadPort;
  7. extern std::string gstruploadInterval;
  8. extern std::string gstrid;
  9. extern std::string gstrplateNumber;
  10. using org::jeecg::defsDetails::grpc::Empty; ///< other message
  11. using org::jeecg::defsDetails::grpc::GPSPoint;
  12. using org::jeecg::defsDetails::grpc::MapPoint;
  13. using org::jeecg::defsDetails::grpc::ShiftStatus; ///< other enum
  14. using org::jeecg::defsDetails::grpc::CtrlMode;
  15. DataExchangeClient::DataExchangeClient(std::shared_ptr<Channel> channel)
  16. {
  17. stub_ = DataExchange::NewStub(channel);
  18. }
  19. DataExchangeClient::~DataExchangeClient(void)
  20. {
  21. }
  22. std::string DataExchangeClient::uploadVehicleInfo(void)
  23. {
  24. // Data we are sending to the server.
  25. UplinkRequest request;
  26. request.set_id(id);
  27. request.set_timestamp(timeStamp);
  28. request.set_soc(SOC);
  29. request.set_statusfeedback(statusFeedback);
  30. request.set_mileage(mileage);
  31. request.set_speed(speed);
  32. request.set_shiftfeedback(shiftFeedback);
  33. request.set_steeringwheelanglefeedback(steeringWheelAngleFeedback);
  34. request.set_throttlefeedback(throttleFeedback);
  35. request.set_brakefeedback(brakeFeedback);
  36. request.set_gpsrtkstatus(GPSRTKStatus);
  37. request.mutable_positionfeedback()->CopyFrom(positionFeedback);
  38. request.set_pitch(pitch);
  39. request.set_roll(roll);
  40. request.set_heading(heading);
  41. request.set_cameraimagefront(cameraImageFront.data(),cameraImageFront.size());
  42. request.set_cameraimagerear(cameraImageRear.data(),cameraImageRear.size());
  43. request.set_cameraimageleft(cameraImageLeft.data(),cameraImageLeft.size());
  44. request.set_cameraimageright(cameraImageRight.data(),cameraImageRight.size());
  45. request.set_sensorstatusgpsimu(sensorStatusGPSIMU);
  46. request.set_sensorstatuslidar(sensorStatusLidar);
  47. request.set_sensorstatusradar(sensorStatusRadar);
  48. request.set_sensorstatuscamfront(sensorStatusCamFront);
  49. request.set_sensorstatuscamrear(sensorStatusCamRear);
  50. request.set_sensorstatuscamleft(sensorStatusCamLeft);
  51. request.set_sensorstatuscamright(sensorStatusCamRight);
  52. request.set_isarrived(isArrived);
  53. request.set_platenumber(plateNumber);
  54. request.set_modefeedback(modeFeedback);
  55. // Container for the data we expect from the server.
  56. ResponseMessage reply;
  57. // Context for the client. It could be used to convey extra information to
  58. // the server and/or tweak certain RPC behaviors.
  59. ClientContext context;
  60. gpr_timespec timespec;
  61. timespec.tv_sec = 2;
  62. timespec.tv_nsec = 0;
  63. timespec.clock_type = GPR_TIMESPAN;
  64. context.set_deadline(timespec);
  65. // The actual RPC.
  66. Status status = stub_ -> uploadVehicleInfo(&context, request, &reply);
  67. // Act upon its status.
  68. if (status.ok()) {
  69. destinationPosition.CopyFrom(reply.destinationposition());
  70. // std::cout<<"lat:"<<destinationPosition.latitude()<<"lon:"<<destinationPosition.longitude()<<"height:"<<destinationPosition.height()<<std::endl;
  71. return "uploadVehicleInfo RPC successed";
  72. } else {
  73. std::cout << status.error_code() << ": " << status.error_message()
  74. << std::endl;
  75. if(status.error_code() == 4)
  76. {
  77. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  78. }
  79. return "uploadVehicleInfo RPC failed";
  80. }
  81. }
  82. std::string DataExchangeClient::uploadPath(void)
  83. {
  84. // Data we are sending to the server.
  85. UploadPathRequest request;
  86. request.set_id(id);
  87. request.set_patrolpathid(patrolPathID);
  88. for(int i = 0;i < pathPoints.size();i++)
  89. {
  90. request.add_pathpoints();
  91. request.mutable_pathpoints(i)->operator =(pathPoints.at(i));
  92. }
  93. // Container for the data we expect from the server.
  94. Empty reply;
  95. // Context for the client. It could be used to convey extra information to
  96. // the server and/or tweak certain RPC behaviors.
  97. ClientContext context;
  98. gpr_timespec timespec;
  99. timespec.tv_sec = 5;
  100. timespec.tv_nsec = 0;
  101. timespec.clock_type = GPR_TIMESPAN;
  102. context.set_deadline(timespec);
  103. // The actual RPC.
  104. Status status = stub_ -> uploadPath(&context,request,&reply);
  105. // Act upon its status.
  106. if (status.ok()) {
  107. if(reply.id() == gstrid)
  108. {
  109. std::cout<<"Path uploaded by car id:"<<reply.id()<<std::endl;
  110. emit uploadPath_Finished(patrolPathID);
  111. }
  112. return "uploadPath RPC successed";
  113. } else {
  114. std::cout << status.error_code() << ": " << status.error_message()
  115. << std::endl;
  116. if(status.error_code() == 4)
  117. {
  118. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  119. }
  120. return "uploadPath RPC failed";
  121. }
  122. }
  123. void DataExchangeClient::updateData(void)
  124. {
  125. id = gstrid;
  126. timeStamp = QDateTime::currentMSecsSinceEpoch();
  127. SOC = 86.5;
  128. statusFeedback = VehicleStatus::STATUS_REMOTE;
  129. mileage = 123.45; // kilometer
  130. speed = 0.1; // m/s
  131. shiftFeedback = ShiftStatus::SHIFT_DRIVE;
  132. steeringWheelAngleFeedback = 1.23; //+/-540 degree
  133. throttleFeedback = 0.12;
  134. brakeFeedback = 0.34;
  135. GPSRTKStatus = 6; //GPS-RTK status 0-6 6 is best
  136. positionFeedback.set_latitude(39.0666552);
  137. positionFeedback.set_longitude(117.3540963);
  138. positionFeedback.set_height(0.84);
  139. pitch = 0.03;
  140. roll = 0.02;
  141. heading = 198.4;
  142. // QFile xFile;
  143. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  144. // if(xFile.open(QIODevice::ReadOnly))
  145. // {
  146. // cameraImageFront = xFile.readAll();
  147. // }
  148. // xFile.close();
  149. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  150. // if(xFile.open(QIODevice::ReadOnly))
  151. // {
  152. // cameraImageRear = xFile.readAll();
  153. // }
  154. // xFile.close();
  155. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  156. // if(xFile.open(QIODevice::ReadOnly))
  157. // {
  158. // cameraImageLeft = xFile.readAll();
  159. // }
  160. // xFile.close();
  161. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  162. // if(xFile.open(QIODevice::ReadOnly))
  163. // {
  164. // cameraImageRight = xFile.readAll();
  165. // }
  166. // xFile.close();
  167. sensorStatusGPSIMU = false; //0 GPS-IMU ok 1 GPS-IMU error
  168. sensorStatusLidar = false;
  169. sensorStatusRadar = false;
  170. sensorStatusCamFront = false;
  171. sensorStatusCamRear = false;
  172. sensorStatusCamLeft = false;
  173. sensorStatusCamRight = false;
  174. isArrived = 0; //0 no destination 1 not arrived 2 arrived
  175. plateNumber = gstrplateNumber;
  176. modeFeedback = CtrlMode::CMD_REMOTE; //mode Feedback
  177. }
  178. void DataExchangeClient::updatePath(std::string pathID, QVector<MapPoint> points)
  179. {
  180. id = gstrid;
  181. patrolPathID = pathID;
  182. pathPoints.clear();
  183. for(int i = 0;i < points.size();i++)
  184. {
  185. pathPoints.append(points.value(i));
  186. // std::cout<<pathPoints.at(i).index()<<std::endl;
  187. }
  188. }
  189. void DataExchangeClient::run()
  190. {
  191. QTime xTime;
  192. xTime.start();
  193. uint64_t lastTime = xTime.elapsed();
  194. uint64_t interval = std::atoi(gstruploadInterval.c_str());
  195. while (true)
  196. {
  197. if((xTime.elapsed() - lastTime)>=interval)
  198. {
  199. updateData();
  200. std::string reply = uploadVehicleInfo();
  201. std::cout<< reply <<std::endl;
  202. lastTime = xTime.elapsed();
  203. }
  204. }
  205. }
  206. void DataExchangeClient::patrolPOI_Recieved_Slot(std::string pathID)
  207. {
  208. std::cout<<"patrol path calculating"<<std::endl;
  209. QFile mapfile("/home/samuel/Documents/path1.txt");
  210. QVector<org::jeecg::defsDetails::grpc::MapPoint> somePoints;
  211. if(mapfile.open(QIODevice::ReadOnly | QIODevice::Text))
  212. {
  213. while(!mapfile.atEnd())
  214. {
  215. QByteArray line = mapfile.readLine();
  216. QString map_str(line);
  217. QStringList oneline = map_str.split(",");
  218. org::jeecg::defsDetails::grpc::MapPoint onePoint;
  219. onePoint.set_index(oneline.at(0).toInt());
  220. onePoint.mutable_mappoint()->set_longitude(oneline.at(1).toDouble());
  221. onePoint.mutable_mappoint()->set_latitude(oneline.at(2).toDouble());
  222. onePoint.mutable_mappoint()->set_height(oneline.at(3).toDouble());
  223. somePoints.append(onePoint);
  224. }
  225. }
  226. updatePath(pathID,somePoints);
  227. uploadPath();
  228. }