vehicle_patrol.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "vehicle_patrol.h"
  2. #include <QFile>
  3. #include <math.h>
  4. #include <thread>
  5. #include "modulecomm.h"
  6. #include "gpsimu.pb.h"
  7. #include "licenseplate.pb.h"
  8. #include "startturnstile.pb.h"
  9. #include "turnstile.pb.h"
  10. extern std::string gstrserverip;
  11. extern std::string gstrpatrolPort;
  12. extern std::string gstrpatrolInterval;
  13. extern std::string gstrid;
  14. extern std::string gstrplateNumber;
  15. extern char stryamlpath[256];
  16. using org::jeecg::defsPatrol::grpc::Empty;
  17. using org::jeecg::defsPatrol::grpc::GPSPoint;
  18. VehiclePatrolExceptionClient::VehiclePatrolExceptionClient(std::shared_ptr<Channel> channel)
  19. {
  20. stub_ = VehiclePatrolException::NewStub(channel);
  21. dec_yaml(stryamlpath);
  22. ModuleFun funupdate = std::bind(&VehiclePatrolExceptionClient::ListenGPSIMUMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  23. shmGPSIMU.mpa = iv::modulecomm::RegisterRecvPlus(shmGPSIMU.mstrmsgname,funupdate);
  24. funupdate = std::bind(&VehiclePatrolExceptionClient::ListenTurnstileMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  25. shmTurnstile.mpa = iv::modulecomm::RegisterRecvPlus(shmTurnstile.mstrmsgname,funupdate);
  26. shmStartTurnstile.mpa = iv::modulecomm::RegisterSend(shmStartTurnstile.mstrmsgname,shmStartTurnstile.mnBufferSize,shmStartTurnstile.mnBufferCount);
  27. }
  28. VehiclePatrolExceptionClient::~VehiclePatrolExceptionClient(void)
  29. {
  30. if(shmGPSIMU.mpa != nullptr)iv::modulecomm::Unregister(shmGPSIMU.mpa);
  31. requestInterruption();
  32. while(this->isFinished() == false);
  33. }
  34. void VehiclePatrolExceptionClient::dec_yaml(const char *stryamlpath)
  35. {
  36. YAML::Node config;
  37. try
  38. {
  39. config = YAML::LoadFile(stryamlpath);
  40. }
  41. catch(YAML::BadFile &e)
  42. {
  43. std::cout<<e.what()<<std::endl;
  44. std::cout<<"yaml file load fail."<<std::endl;
  45. return;
  46. }
  47. catch(YAML::ParserException &e)
  48. {
  49. std::cout<<e.what()<<std::endl;
  50. std::cout<<"yaml file is malformed."<<std::endl;
  51. return;
  52. }
  53. std::string strmsgname;
  54. if(config["GPS_IMU"])
  55. {
  56. if(config["GPS_IMU"]["msgname"]&&config["GPS_IMU"]["buffersize"]&&config["GPS_IMU"]["buffercount"])
  57. {
  58. strmsgname = config["GPS_IMU"]["msgname"].as<std::string>();
  59. strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
  60. shmGPSIMU.mnBufferSize = config["GPS_IMU"]["buffersize"].as<int>();
  61. shmGPSIMU.mnBufferCount = config["GPS_IMU"]["buffercount"].as<int>();
  62. std::cout << "GPS_IMU:" << shmGPSIMU.mstrmsgname << "," << shmGPSIMU.mnBufferSize << "," << shmGPSIMU.mnBufferCount << std::endl;
  63. }
  64. }
  65. else
  66. {
  67. strmsgname = "hcp2_gpsimu";
  68. strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
  69. shmGPSIMU.mnBufferSize = 10000;
  70. shmGPSIMU.mnBufferCount = 1;
  71. }
  72. if(config["pic_front"])
  73. {
  74. if(config["pic_front"]["msgname"]&&config["pic_front"]["buffersize"]&&config["pic_front"]["buffercount"])
  75. {
  76. strmsgname = config["pic_front"]["msgname"].as<std::string>();
  77. strncpy(shmPicFront.mstrmsgname,strmsgname.data(),255);
  78. shmPicFront.mnBufferSize = config["pic_front"]["buffersize"].as<int>();
  79. shmPicFront.mnBufferCount = config["pic_front"]["buffercount"].as<int>();
  80. std::cout << "pic_front:" << shmPicFront.mstrmsgname << "," << shmPicFront.mnBufferSize << "," << shmPicFront.mnBufferCount << std::endl;
  81. }
  82. }
  83. else
  84. {
  85. strmsgname = "picfront";
  86. strncpy(shmPicFront.mstrmsgname,strmsgname.data(),255);
  87. shmPicFront.mnBufferSize = 10000000;
  88. shmPicFront.mnBufferCount = 1;
  89. }
  90. if(config["pic_rear"])
  91. {
  92. if(config["pic_rear"]["msgname"]&&config["pic_rear"]["buffersize"]&&config["pic_rear"]["buffercount"])
  93. {
  94. strmsgname = config["pic_rear"]["msgname"].as<std::string>();
  95. strncpy(shmPicRear.mstrmsgname,strmsgname.data(),255);
  96. shmPicRear.mnBufferSize = config["pic_rear"]["buffersize"].as<int>();
  97. shmPicRear.mnBufferCount = config["pic_rear"]["buffercount"].as<int>();
  98. std::cout << "pic_rear:" << shmPicRear.mstrmsgname << "," << shmPicRear.mnBufferSize << "," << shmPicRear.mnBufferCount << std::endl;
  99. }
  100. }
  101. else
  102. {
  103. strmsgname = "picrear";
  104. strncpy(shmPicRear.mstrmsgname,strmsgname.data(),255);
  105. shmPicRear.mnBufferSize = 10000000;
  106. shmPicRear.mnBufferCount = 1;
  107. }
  108. if(config["pic_left"])
  109. {
  110. if(config["pic_left"]["msgname"]&&config["pic_left"]["buffersize"]&&config["pic_left"]["buffercount"])
  111. {
  112. strmsgname = config["pic_left"]["msgname"].as<std::string>();
  113. strncpy(shmPicLeft.mstrmsgname,strmsgname.data(),255);
  114. shmPicLeft.mnBufferSize = config["pic_left"]["buffersize"].as<int>();
  115. shmPicLeft.mnBufferCount = config["pic_left"]["buffercount"].as<int>();
  116. std::cout << "pic_left:" << shmPicLeft.mstrmsgname << "," << shmPicLeft.mnBufferSize << "," << shmPicLeft.mnBufferCount << std::endl;
  117. }
  118. }
  119. else
  120. {
  121. strmsgname = "picleft";
  122. strncpy(shmPicLeft.mstrmsgname,strmsgname.data(),255);
  123. shmPicLeft.mnBufferSize = 10000000;
  124. shmPicLeft.mnBufferCount = 1;
  125. }
  126. if(config["pic_right"])
  127. {
  128. if(config["pic_right"]["msgname"]&&config["pic_right"]["buffersize"]&&config["pic_right"]["buffercount"])
  129. {
  130. strmsgname = config["pic_right"]["msgname"].as<std::string>();
  131. strncpy(shmPicRight.mstrmsgname,strmsgname.data(),255);
  132. shmPicRight.mnBufferSize = config["pic_right"]["buffersize"].as<int>();
  133. shmPicRight.mnBufferCount = config["pic_right"]["buffercount"].as<int>();
  134. std::cout << "pic_right:" << shmPicRight.mstrmsgname << "," << shmPicRight.mnBufferSize << "," << shmPicRight.mnBufferCount << std::endl;
  135. }
  136. }
  137. else
  138. {
  139. strmsgname = "picright";
  140. strncpy(shmPicRight.mstrmsgname,strmsgname.data(),255);
  141. shmPicRight.mnBufferSize = 10000000;
  142. shmPicRight.mnBufferCount = 1;
  143. }
  144. if(config["start_turnstile"])
  145. {
  146. if(config["start_turnstile"]["msgname"]&&config["start_turnstile"]["buffersize"]&&config["start_turnstile"]["buffercount"])
  147. {
  148. strmsgname = config["start_turnstile"]["msgname"].as<std::string>();
  149. strncpy(shmStartTurnstile.mstrmsgname,strmsgname.data(),255);
  150. shmStartTurnstile.mnBufferSize = config["start_turnstile"]["buffersize"].as<int>();
  151. shmStartTurnstile.mnBufferCount = config["start_turnstile"]["buffercount"].as<int>();
  152. std::cout << "start_turnstile:" << shmStartTurnstile.mstrmsgname << "," << shmStartTurnstile.mnBufferSize << "," << shmStartTurnstile.mnBufferCount << std::endl;
  153. }
  154. }
  155. else
  156. {
  157. strmsgname = "startturnstile";
  158. strncpy(shmStartTurnstile.mstrmsgname,strmsgname.data(),255);
  159. shmStartTurnstile.mnBufferSize = 10000;
  160. shmStartTurnstile.mnBufferCount = 1;
  161. }
  162. if(config["turnstile"])
  163. {
  164. if(config["turnstile"]["msgname"]&&config["turnstile"]["buffersize"]&&config["turnstile"]["buffercount"])
  165. {
  166. strmsgname = config["turnstile"]["msgname"].as<std::string>();
  167. strncpy(shmTurnstile.mstrmsgname,strmsgname.data(),255);
  168. shmTurnstile.mnBufferSize = config["turnstile"]["buffersize"].as<int>();
  169. shmTurnstile.mnBufferCount = config["turnstile"]["buffercount"].as<int>();
  170. std::cout << "turnstile:" << shmTurnstile.mstrmsgname << "," << shmTurnstile.mnBufferSize << "," << shmTurnstile.mnBufferCount << std::endl;
  171. }
  172. }
  173. else
  174. {
  175. strmsgname = "turnstile";
  176. strncpy(shmTurnstile.mstrmsgname,strmsgname.data(),255);
  177. shmTurnstile.mnBufferSize = 10000000;
  178. shmTurnstile.mnBufferCount = 1;
  179. }
  180. return;
  181. }
  182. void VehiclePatrolExceptionClient::ListenGPSIMUMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  183. {
  184. iv::gps::gpsimu xdata;
  185. if(!xdata.ParseFromArray(strdata,nSize))
  186. {
  187. std::cout<<" ListenGPSIMUMsg parese error."<<std::endl;
  188. return;
  189. }
  190. double speed = sqrt(xdata.ve()*xdata.ve() + xdata.vn()*xdata.vn() + xdata.vd()*xdata.vd());
  191. mutex_GPSIMU.lock();
  192. currentPosition.set_latitude(xdata.lat());
  193. currentPosition.set_longitude(xdata.lon());
  194. currentPosition.set_height(xdata.height());
  195. currentSpeed = speed;
  196. mutex_GPSIMU.unlock();
  197. }
  198. void VehiclePatrolExceptionClient::ListenTurnstileMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  199. {
  200. iv::vision::turnstile xdata;
  201. if(!xdata.ParseFromArray(strdata,nSize))
  202. {
  203. std::cout<<" ListenTurnstileMsg parese error."<<std::endl;
  204. return;
  205. }
  206. if(statusTSGM == 1)
  207. {
  208. std::cout<<"Get Turnstile result"<<std::endl;
  209. //set TSGM resul
  210. isTSGM = true;
  211. if(xdata.state() == false)
  212. gateStatus = 1; //0 no gate 1 gate close 2 gate open
  213. else
  214. gateStatus = 2;
  215. std::cout<<"gateStatus : "<<(int)gateStatus<<std::endl;
  216. gateImage.clear();
  217. gateImage.append(xdata.pic().picdata().data(),xdata.pic().picdata().size());
  218. gateTime = QDateTime::currentMSecsSinceEpoch(); //time when get gateImage
  219. mutex_GPSIMU.lock();
  220. gatePosition.CopyFrom(currentPosition); //positon when get gateImage
  221. mutex_GPSIMU.unlock();
  222. timerTSGM.restart();
  223. statusTSGM = 2;
  224. }
  225. }
  226. std::string VehiclePatrolExceptionClient::uploadVehiclePatrolInfo(void)
  227. {
  228. // Data we are sending to the server.
  229. PatrolRequest request;
  230. request.set_id(id);
  231. request.set_istvr(isTVR);
  232. request.set_violationstatus(violationStatus);
  233. request.set_vehiclelicensenumber(vehicleLicenseNumber);
  234. request.set_violationimage(violationImage.data(),violationImage.size());
  235. request.set_violationtime(violationTime);
  236. request.mutable_violationposition()->CopyFrom(violationPosition);
  237. request.set_isfsm(isFSM);
  238. request.set_firestatus(fireStatus);
  239. request.set_fireimage(fireImage.data(),fireImage.size());
  240. request.set_firetime(fireTime);
  241. request.mutable_fireposition()->CopyFrom(firePosition);
  242. request.set_istsgm(isTSGM);
  243. request.set_gatestatus(gateStatus);
  244. request.set_gateimage(gateImage.data(),gateImage.size());
  245. request.set_gatetime(gateTime);
  246. request.mutable_gateposition()->CopyFrom(gatePosition);
  247. request.set_platenumber(plateNumber);
  248. // Container for the data we expect from the server.
  249. Empty reply;
  250. // Context for the client. It could be used to convey extra information to
  251. // the server and/or tweak certain RPC behaviors.
  252. ClientContext context;
  253. gpr_timespec timespec;
  254. timespec.tv_sec = 5;
  255. timespec.tv_nsec = 0;
  256. timespec.clock_type = GPR_TIMESPAN;
  257. context.set_deadline(timespec);
  258. // The actual RPC.
  259. Status status = stub_ -> uploadVehiclePatrolInfo(&context,request,&reply);
  260. // Act upon its status.
  261. if (status.ok()) {
  262. return "uploadVehiclePatrolInfo RPC successed";
  263. } else {
  264. std::cout << status.error_code() << ": " << status.error_message()
  265. << std::endl;
  266. if(status.error_code() == 4)
  267. {
  268. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  269. }
  270. return "uploadVehiclePatrolInfo RPC failed";
  271. }
  272. }
  273. void VehiclePatrolExceptionClient::updatePatrolData(void)
  274. {
  275. id = gstrid;
  276. // isTVR = true;
  277. // violationStatus = 2;
  278. // vehicleLicenseNumber = "津B654321";
  279. // QFile xFile;
  280. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  281. // if(xFile.open(QIODevice::ReadOnly))
  282. // {
  283. // violationImage = xFile.readAll();
  284. // }
  285. // xFile.close();
  286. // violationTime = QDateTime::currentMSecsSinceEpoch();
  287. // violationPosition.set_height(0.1);
  288. // violationPosition.set_latitude(39.0666552);
  289. // violationPosition.set_longitude(117.3542963);
  290. // isFSM = true;
  291. // fireStatus = 1;
  292. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  293. // if(xFile.open(QIODevice::ReadOnly))
  294. // {
  295. // fireImage = xFile.readAll();
  296. // }
  297. // xFile.close();
  298. // fireTime = QDateTime::currentMSecsSinceEpoch();
  299. // firePosition.set_height(0.1);
  300. // firePosition.set_latitude(39.0667552);
  301. // firePosition.set_longitude(117.3542963);
  302. // isTSGM = true;
  303. // gateStatus = 2;
  304. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  305. // if(xFile.open(QIODevice::ReadOnly))
  306. // {
  307. // gateImage = xFile.readAll();
  308. // }
  309. // xFile.close();
  310. // gateTime = QDateTime::currentMSecsSinceEpoch();
  311. // gatePosition.set_height(0.1);
  312. // gatePosition.set_latitude(39.0665552);
  313. // gatePosition.set_longitude(117.3542963);
  314. plateNumber = gstrplateNumber;
  315. }
  316. void VehiclePatrolExceptionClient::run()
  317. {
  318. QTime xTime;
  319. xTime.start();
  320. int lastTime = xTime.elapsed();
  321. uint64_t interval = std::atoi(gstrpatrolInterval.c_str());
  322. org::jeecg::defsPatrol::grpc::GPSPoint tempPosition;
  323. //set isNeedTSGM
  324. isNeedTSGM = true;
  325. //set gateDestination
  326. gateDestination.set_latitude(39.0665855);
  327. gateDestination.set_longitude(117.3554362);
  328. timerTSGM.start();
  329. while (!QThread::isInterruptionRequested())
  330. {
  331. if(abs(xTime.elapsed() - lastTime)>=interval)
  332. {
  333. // do something
  334. mutex_GPSIMU.lock();
  335. tempPosition.CopyFrom(currentPosition);
  336. double tempSpeed = currentSpeed;
  337. mutex_GPSIMU.unlock();
  338. if(fabs(tempPosition.latitude()-gateDestination.latitude()) < 0.0001 && fabs(tempPosition.longitude()-gateDestination.longitude()) < 0.0001)
  339. {
  340. if(tempSpeed < 0.1 && statusTSGM == 0 && isNeedTSGM == true)
  341. {
  342. std::cout<<"Send startTurnstile"<<std::endl;
  343. iv::vision::startturnstile xmsg;
  344. xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
  345. xmsg.set_cameraname(shmPicRight.mstrmsgname);
  346. xmsg.set_start(true);
  347. int ndatasize = xmsg.ByteSize();
  348. char * str = new char[ndatasize];
  349. std::shared_ptr<char> pstr;pstr.reset(str);
  350. if(!xmsg.SerializeToArray(str,ndatasize))
  351. {
  352. std::cout<<"StartTurnstile serialize error."<<std::endl;
  353. return;
  354. }
  355. iv::modulecomm::ModuleSendMsg(shmStartTurnstile.mpa,str,ndatasize);
  356. statusTSGM = 1;
  357. timerTSGM.restart();
  358. }
  359. }
  360. if(statusTSGM == 1 && timerTSGM.elapsed() > 80000)
  361. {
  362. //timeout
  363. std::cout<<"Turnstile timeout"<<std::endl;
  364. iv::vision::startturnstile xmsg;
  365. xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
  366. xmsg.set_cameraname(shmPicLeft.mstrmsgname);
  367. xmsg.set_start(false);
  368. int ndatasize = xmsg.ByteSize();
  369. char * str = new char[ndatasize];
  370. std::shared_ptr<char> pstr;pstr.reset(str);
  371. if(!xmsg.SerializeToArray(str,ndatasize))
  372. {
  373. std::cout<<"StartTurnstile serialize error."<<std::endl;
  374. return;
  375. }
  376. iv::modulecomm::ModuleSendMsg(shmStartTurnstile.mpa,str,ndatasize);
  377. isTSGM = false;
  378. gateStatus = 0;
  379. gateImage.clear();
  380. gateTime = QDateTime::currentMSecsSinceEpoch();
  381. mutex_GPSIMU.lock();
  382. gatePosition.CopyFrom(currentPosition);
  383. mutex_GPSIMU.unlock();
  384. timerTSGM.restart();
  385. statusTSGM = 3;
  386. }
  387. if(statusTSGM == 2 || statusTSGM == 3)
  388. {
  389. if(fabs(tempPosition.latitude()-gateDestination.latitude()) > 0.0001 || fabs(tempPosition.longitude()-gateDestination.longitude()) > 0.0001)
  390. {
  391. std::cout<<"Turnstile detect finish"<<std::endl;
  392. iv::vision::startturnstile xmsg;
  393. xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
  394. xmsg.set_cameraname(shmPicLeft.mstrmsgname);
  395. xmsg.set_start(false);
  396. int ndatasize = xmsg.ByteSize();
  397. char * str = new char[ndatasize];
  398. std::shared_ptr<char> pstr;pstr.reset(str);
  399. if(!xmsg.SerializeToArray(str,ndatasize))
  400. {
  401. std::cout<<"StartTurnstile serialize error."<<std::endl;
  402. return;
  403. }
  404. iv::modulecomm::ModuleSendMsg(shmStartTurnstile.mpa,str,ndatasize);
  405. timerTSGM.restart();
  406. statusTSGM = 0;
  407. }
  408. }
  409. // updatePatrolData();
  410. // std::string reply = uploadVehiclePatrolInfo();
  411. // std::cout<< reply <<std::endl;
  412. lastTime = xTime.elapsed();
  413. }
  414. else
  415. {
  416. std::this_thread::sleep_for(std::chrono::microseconds(1000));//sleep 1ms
  417. }
  418. }
  419. }