123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- #include "vehicle_patrol.h"
- #include <QFile>
- #include <math.h>
- #include "modulecomm.h"
- #include "gpsimu.pb.h"
- #include "licenseplate.pb.h"
- #include "startturnstile.pb.h"
- #include "turnstile.pb.h"
- extern std::string gstrserverip;
- extern std::string gstrpatrolPort;
- extern std::string gstrpatrolInterval;
- extern std::string gstrid;
- extern std::string gstrplateNumber;
- extern char stryamlpath[256];
- using org::jeecg::defsPatrol::grpc::Empty;
- using org::jeecg::defsPatrol::grpc::GPSPoint;
- VehiclePatrolExceptionClient::VehiclePatrolExceptionClient(std::shared_ptr<Channel> channel)
- {
- stub_ = VehiclePatrolException::NewStub(channel);
- dec_yaml(stryamlpath);
- ModuleFun funupdate = std::bind(&VehiclePatrolExceptionClient::ListenGPSIMUMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
- shmGPSIMU.mpa = iv::modulecomm::RegisterRecvPlus(shmGPSIMU.mstrmsgname,funupdate);
- funupdate = std::bind(&VehiclePatrolExceptionClient::ListenTurnstileMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
- shmTurnstile.mpa = iv::modulecomm::RegisterRecvPlus(shmTurnstile.mstrmsgname,funupdate);
- shmStartTurnstile.mpa = iv::modulecomm::RegisterSend(shmStartTurnstile.mstrmsgname,shmStartTurnstile.mnBufferSize,shmStartTurnstile.mnBufferCount);
- }
- VehiclePatrolExceptionClient::~VehiclePatrolExceptionClient(void)
- {
- if(shmGPSIMU.mpa != nullptr)iv::modulecomm::Unregister(shmGPSIMU.mpa);
- requestInterruption();
- while(this->isFinished() == false);
- }
- void VehiclePatrolExceptionClient::dec_yaml(const char *stryamlpath)
- {
- YAML::Node config;
- try
- {
- config = YAML::LoadFile(stryamlpath);
- }
- catch(YAML::BadFile &e)
- {
- std::cout<<e.what()<<std::endl;
- std::cout<<"yaml file load fail."<<std::endl;
- return;
- }
- catch(YAML::ParserException &e)
- {
- std::cout<<e.what()<<std::endl;
- std::cout<<"yaml file is malformed."<<std::endl;
- return;
- }
- std::string strmsgname;
- if(config["GPS_IMU"])
- {
- if(config["GPS_IMU"]["msgname"]&&config["GPS_IMU"]["buffersize"]&&config["GPS_IMU"]["buffercount"])
- {
- strmsgname = config["GPS_IMU"]["msgname"].as<std::string>();
- strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
- shmGPSIMU.mnBufferSize = config["GPS_IMU"]["buffersize"].as<int>();
- shmGPSIMU.mnBufferCount = config["GPS_IMU"]["buffercount"].as<int>();
- std::cout << "GPS_IMU:" << shmGPSIMU.mstrmsgname << "," << shmGPSIMU.mnBufferSize << "," << shmGPSIMU.mnBufferCount << std::endl;
- }
- }
- else
- {
- strmsgname = "hcp2_gpsimu";
- strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
- shmGPSIMU.mnBufferSize = 10000;
- shmGPSIMU.mnBufferCount = 1;
- }
- if(config["pic_front"])
- {
- if(config["pic_front"]["msgname"]&&config["pic_front"]["buffersize"]&&config["pic_front"]["buffercount"])
- {
- strmsgname = config["pic_front"]["msgname"].as<std::string>();
- strncpy(shmPicFront.mstrmsgname,strmsgname.data(),255);
- shmPicFront.mnBufferSize = config["pic_front"]["buffersize"].as<int>();
- shmPicFront.mnBufferCount = config["pic_front"]["buffercount"].as<int>();
- std::cout << "pic_front:" << shmPicFront.mstrmsgname << "," << shmPicFront.mnBufferSize << "," << shmPicFront.mnBufferCount << std::endl;
- }
- }
- else
- {
- strmsgname = "picfront";
- strncpy(shmPicFront.mstrmsgname,strmsgname.data(),255);
- shmPicFront.mnBufferSize = 10000000;
- shmPicFront.mnBufferCount = 1;
- }
- if(config["pic_rear"])
- {
- if(config["pic_rear"]["msgname"]&&config["pic_rear"]["buffersize"]&&config["pic_rear"]["buffercount"])
- {
- strmsgname = config["pic_rear"]["msgname"].as<std::string>();
- strncpy(shmPicRear.mstrmsgname,strmsgname.data(),255);
- shmPicRear.mnBufferSize = config["pic_rear"]["buffersize"].as<int>();
- shmPicRear.mnBufferCount = config["pic_rear"]["buffercount"].as<int>();
- std::cout << "pic_rear:" << shmPicRear.mstrmsgname << "," << shmPicRear.mnBufferSize << "," << shmPicRear.mnBufferCount << std::endl;
- }
- }
- else
- {
- strmsgname = "picrear";
- strncpy(shmPicRear.mstrmsgname,strmsgname.data(),255);
- shmPicRear.mnBufferSize = 10000000;
- shmPicRear.mnBufferCount = 1;
- }
- if(config["pic_left"])
- {
- if(config["pic_left"]["msgname"]&&config["pic_left"]["buffersize"]&&config["pic_left"]["buffercount"])
- {
- strmsgname = config["pic_left"]["msgname"].as<std::string>();
- strncpy(shmPicLeft.mstrmsgname,strmsgname.data(),255);
- shmPicLeft.mnBufferSize = config["pic_left"]["buffersize"].as<int>();
- shmPicLeft.mnBufferCount = config["pic_left"]["buffercount"].as<int>();
- std::cout << "pic_left:" << shmPicLeft.mstrmsgname << "," << shmPicLeft.mnBufferSize << "," << shmPicLeft.mnBufferCount << std::endl;
- }
- }
- else
- {
- strmsgname = "picleft";
- strncpy(shmPicLeft.mstrmsgname,strmsgname.data(),255);
- shmPicLeft.mnBufferSize = 10000000;
- shmPicLeft.mnBufferCount = 1;
- }
- if(config["pic_right"])
- {
- if(config["pic_right"]["msgname"]&&config["pic_right"]["buffersize"]&&config["pic_right"]["buffercount"])
- {
- strmsgname = config["pic_right"]["msgname"].as<std::string>();
- strncpy(shmPicRight.mstrmsgname,strmsgname.data(),255);
- shmPicRight.mnBufferSize = config["pic_right"]["buffersize"].as<int>();
- shmPicRight.mnBufferCount = config["pic_right"]["buffercount"].as<int>();
- std::cout << "pic_right:" << shmPicRight.mstrmsgname << "," << shmPicRight.mnBufferSize << "," << shmPicRight.mnBufferCount << std::endl;
- }
- }
- else
- {
- strmsgname = "picright";
- strncpy(shmPicRight.mstrmsgname,strmsgname.data(),255);
- shmPicRight.mnBufferSize = 10000000;
- shmPicRight.mnBufferCount = 1;
- }
- if(config["start_turnstile"])
- {
- if(config["start_turnstile"]["msgname"]&&config["start_turnstile"]["buffersize"]&&config["start_turnstile"]["buffercount"])
- {
- strmsgname = config["start_turnstile"]["msgname"].as<std::string>();
- strncpy(shmStartTurnstile.mstrmsgname,strmsgname.data(),255);
- shmStartTurnstile.mnBufferSize = config["start_turnstile"]["buffersize"].as<int>();
- shmStartTurnstile.mnBufferCount = config["start_turnstile"]["buffercount"].as<int>();
- std::cout << "start_turnstile:" << shmStartTurnstile.mstrmsgname << "," << shmStartTurnstile.mnBufferSize << "," << shmStartTurnstile.mnBufferCount << std::endl;
- }
- }
- else
- {
- strmsgname = "startturnstile";
- strncpy(shmStartTurnstile.mstrmsgname,strmsgname.data(),255);
- shmStartTurnstile.mnBufferSize = 10000;
- shmStartTurnstile.mnBufferCount = 1;
- }
- if(config["turnstile"])
- {
- if(config["turnstile"]["msgname"]&&config["turnstile"]["buffersize"]&&config["turnstile"]["buffercount"])
- {
- strmsgname = config["turnstile"]["msgname"].as<std::string>();
- strncpy(shmTurnstile.mstrmsgname,strmsgname.data(),255);
- shmTurnstile.mnBufferSize = config["turnstile"]["buffersize"].as<int>();
- shmTurnstile.mnBufferCount = config["turnstile"]["buffercount"].as<int>();
- std::cout << "turnstile:" << shmTurnstile.mstrmsgname << "," << shmTurnstile.mnBufferSize << "," << shmTurnstile.mnBufferCount << std::endl;
- }
- }
- else
- {
- strmsgname = "turnstile";
- strncpy(shmTurnstile.mstrmsgname,strmsgname.data(),255);
- shmTurnstile.mnBufferSize = 10000000;
- shmTurnstile.mnBufferCount = 1;
- }
- return;
- }
- void VehiclePatrolExceptionClient::ListenGPSIMUMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
- {
- iv::gps::gpsimu xdata;
- if(!xdata.ParseFromArray(strdata,nSize))
- {
- std::cout<<" ListenGPSIMUMsg parese error."<<std::endl;
- return;
- }
- double speed = sqrt(xdata.ve()*xdata.ve() + xdata.vn()*xdata.vn() + xdata.vd()*xdata.vd());
- mutex_GPSIMU.lock();
- currentPosition.set_latitude(xdata.lat());
- currentPosition.set_longitude(xdata.lon());
- currentPosition.set_height(xdata.height());
- currentSpeed = speed;
- mutex_GPSIMU.unlock();
- }
- void VehiclePatrolExceptionClient::ListenTurnstileMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
- {
- iv::vision::turnstile xdata;
- if(!xdata.ParseFromArray(strdata,nSize))
- {
- std::cout<<" ListenTurnstileMsg parese error."<<std::endl;
- return;
- }
- if(statusTSGM == 1)
- {
- std::cout<<"Get Turnstile result"<<std::endl;
- //set TSGM resul
- isTSGM = true;
- if(xdata.state() == false)
- gateStatus = 1; //0 no gate 1 gate close 2 gate open
- else
- gateStatus = 2;
- std::cout<<"gateStatus : "<<(int)gateStatus<<std::endl;
- gateImage.clear();
- gateImage.append(xdata.pic().picdata().data(),xdata.pic().picdata().size());
- gateTime = QDateTime::currentMSecsSinceEpoch(); //time when get gateImage
- mutex_GPSIMU.lock();
- gatePosition.CopyFrom(currentPosition); //positon when get gateImage
- mutex_GPSIMU.unlock();
- timerTSGM.restart();
- statusTSGM = 2;
- }
- }
- std::string VehiclePatrolExceptionClient::uploadVehiclePatrolInfo(void)
- {
- // Data we are sending to the server.
- PatrolRequest request;
- request.set_id(id);
- request.set_istvr(isTVR);
- request.set_violationstatus(violationStatus);
- request.set_vehiclelicensenumber(vehicleLicenseNumber);
- request.set_violationimage(violationImage.data(),violationImage.size());
- request.set_violationtime(violationTime);
- request.mutable_violationposition()->CopyFrom(violationPosition);
- request.set_isfsm(isFSM);
- request.set_firestatus(fireStatus);
- request.set_fireimage(fireImage.data(),fireImage.size());
- request.set_firetime(fireTime);
- request.mutable_fireposition()->CopyFrom(firePosition);
- request.set_istsgm(isTSGM);
- request.set_gatestatus(gateStatus);
- request.set_gateimage(gateImage.data(),gateImage.size());
- request.set_gatetime(gateTime);
- request.mutable_gateposition()->CopyFrom(gatePosition);
- request.set_platenumber(plateNumber);
- // Container for the data we expect from the server.
- Empty reply;
- // Context for the client. It could be used to convey extra information to
- // the server and/or tweak certain RPC behaviors.
- ClientContext context;
- gpr_timespec timespec;
- timespec.tv_sec = 5;
- timespec.tv_nsec = 0;
- timespec.clock_type = GPR_TIMESPAN;
- context.set_deadline(timespec);
- // The actual RPC.
- Status status = stub_ -> uploadVehiclePatrolInfo(&context,request,&reply);
- // Act upon its status.
- if (status.ok()) {
- return "uploadVehiclePatrolInfo RPC successed";
- } else {
- std::cout << status.error_code() << ": " << status.error_message()
- << std::endl;
- if(status.error_code() == 4)
- {
- std::cout << "vehicleControl RPC connect timeout" << std::endl;
- }
- return "uploadVehiclePatrolInfo RPC failed";
- }
- }
- void VehiclePatrolExceptionClient::updatePatrolData(void)
- {
- id = gstrid;
- // isTVR = true;
- // violationStatus = 2;
- // vehicleLicenseNumber = "津B654321";
- // QFile xFile;
- // xFile.setFileName("/home/samuel/Pictures/123.jpg");
- // if(xFile.open(QIODevice::ReadOnly))
- // {
- // violationImage = xFile.readAll();
- // }
- // xFile.close();
- // violationTime = QDateTime::currentMSecsSinceEpoch();
- // violationPosition.set_height(0.1);
- // violationPosition.set_latitude(39.0666552);
- // violationPosition.set_longitude(117.3542963);
- // isFSM = true;
- // fireStatus = 1;
- // xFile.setFileName("/home/samuel/Pictures/123.jpg");
- // if(xFile.open(QIODevice::ReadOnly))
- // {
- // fireImage = xFile.readAll();
- // }
- // xFile.close();
- // fireTime = QDateTime::currentMSecsSinceEpoch();
- // firePosition.set_height(0.1);
- // firePosition.set_latitude(39.0667552);
- // firePosition.set_longitude(117.3542963);
- // isTSGM = true;
- // gateStatus = 2;
- // xFile.setFileName("/home/samuel/Pictures/123.jpg");
- // if(xFile.open(QIODevice::ReadOnly))
- // {
- // gateImage = xFile.readAll();
- // }
- // xFile.close();
- // gateTime = QDateTime::currentMSecsSinceEpoch();
- // gatePosition.set_height(0.1);
- // gatePosition.set_latitude(39.0665552);
- // gatePosition.set_longitude(117.3542963);
- plateNumber = gstrplateNumber;
- }
- void VehiclePatrolExceptionClient::run()
- {
- QTime xTime;
- xTime.start();
- int lastTime = xTime.elapsed();
- uint64_t interval = std::atoi(gstrpatrolInterval.c_str());
- org::jeecg::defsPatrol::grpc::GPSPoint tempPosition;
- //set isNeedTSGM
- isNeedTSGM = true;
- //set gateDestination
- gateDestination.set_latitude(39.0665855);
- gateDestination.set_longitude(117.3554362);
- timerTSGM.start();
- while (!QThread::isInterruptionRequested())
- {
- if(abs(xTime.elapsed() - lastTime)>=interval)
- {
- // do something
- mutex_GPSIMU.lock();
- tempPosition.CopyFrom(currentPosition);
- double tempSpeed = currentSpeed;
- mutex_GPSIMU.unlock();
- if(fabs(tempPosition.latitude()-gateDestination.latitude()) < 0.0001 && fabs(tempPosition.longitude()-gateDestination.longitude()) < 0.0001)
- {
- if(tempSpeed < 0.1 && statusTSGM == 0 && isNeedTSGM == true)
- {
- std::cout<<"Send startTurnstile"<<std::endl;
- iv::vision::startturnstile xmsg;
- xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
- xmsg.set_cameraname(shmPicRight.mstrmsgname);
- xmsg.set_start(true);
- int ndatasize = xmsg.ByteSize();
- char * str = new char[ndatasize];
- std::shared_ptr<char> pstr;pstr.reset(str);
- if(!xmsg.SerializeToArray(str,ndatasize))
- {
- std::cout<<"StartTurnstile serialize error."<<std::endl;
- return;
- }
- iv::modulecomm::ModuleSendMsg(shmStartTurnstile.mpa,str,ndatasize);
- statusTSGM = 1;
- timerTSGM.restart();
- }
- }
- if(statusTSGM == 1 && timerTSGM.elapsed() > 80000)
- {
- //timeout
- std::cout<<"Turnstile timeout"<<std::endl;
- iv::vision::startturnstile xmsg;
- xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
- xmsg.set_cameraname(shmPicLeft.mstrmsgname);
- xmsg.set_start(false);
- int ndatasize = xmsg.ByteSize();
- char * str = new char[ndatasize];
- std::shared_ptr<char> pstr;pstr.reset(str);
- if(!xmsg.SerializeToArray(str,ndatasize))
- {
- std::cout<<"StartTurnstile serialize error."<<std::endl;
- return;
- }
- iv::modulecomm::ModuleSendMsg(shmStartTurnstile.mpa,str,ndatasize);
- isTSGM = false;
- gateStatus = 0;
- gateImage.clear();
- gateTime = QDateTime::currentMSecsSinceEpoch();
- mutex_GPSIMU.lock();
- gatePosition.CopyFrom(currentPosition);
- mutex_GPSIMU.unlock();
- timerTSGM.restart();
- statusTSGM = 3;
- }
- if(statusTSGM == 2 || statusTSGM == 3)
- {
- if(fabs(tempPosition.latitude()-gateDestination.latitude()) > 0.0001 || fabs(tempPosition.longitude()-gateDestination.longitude()) > 0.0001)
- {
- std::cout<<"Turnstile detect finish"<<std::endl;
- iv::vision::startturnstile xmsg;
- xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
- xmsg.set_cameraname(shmPicLeft.mstrmsgname);
- xmsg.set_start(false);
- int ndatasize = xmsg.ByteSize();
- char * str = new char[ndatasize];
- std::shared_ptr<char> pstr;pstr.reset(str);
- if(!xmsg.SerializeToArray(str,ndatasize))
- {
- std::cout<<"StartTurnstile serialize error."<<std::endl;
- return;
- }
- iv::modulecomm::ModuleSendMsg(shmStartTurnstile.mpa,str,ndatasize);
- timerTSGM.restart();
- statusTSGM = 0;
- }
- }
- // updatePatrolData();
- // std::string reply = uploadVehiclePatrolInfo();
- // std::cout<< reply <<std::endl;
- lastTime = xTime.elapsed();
- }
- else
- {
- std::this_thread::sleep_for(std::chrono::microseconds(1000));//sleep 1ms
- }
- }
- }
|