vehicle_patrol.cpp 17 KB

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