Browse Source

fix(grpc_client_BS):use signal-slot to process patrol path upload

孙嘉城 3 years ago
parent
commit
820b416ae6

+ 52 - 49
src/driver/driver_cloud_grpc_client_BS/main.cpp

@@ -23,53 +23,53 @@ std::string gstruploadMapInterval = "500";
 std::string gstrid = "1234567890123456789H";
 std::string gstrplateNumber = "津A123456";
 
-class mainloop : public QThread
-{
-//    Q_OBJECT
-public:
-    mainloop(VehicleControlClient *pCC,DataExchangeClient *pUC);
-    void run();
-private:
-    VehicleControlClient *pControlClient;
-    DataExchangeClient *pUploadClient;
-};
-
-mainloop::mainloop(VehicleControlClient *pCC,DataExchangeClient *pUC)
-{
-    pControlClient = pCC;
-    pUploadClient = pUC;
-}
-
-void mainloop::run()
-{
-    while (1) {
-//        msleep(100);
-        if(pControlClient->get_isNeedMap() == true)
-        {
-            std::cout<<"patrol path calculating"<<std::endl;
-            QFile mapfile("/home/samuel/Documents/path1.txt");
-            QVector<org::jeecg::defsDetails::grpc::MapPoint> somePoints;
-            if(mapfile.open(QIODevice::ReadOnly | QIODevice::Text))
-            {
-                while(!mapfile.atEnd())
-                {
-                    QByteArray line = mapfile.readLine();
-                    QString map_str(line);
-                    QStringList oneline = map_str.split(",");
-                    org::jeecg::defsDetails::grpc::MapPoint onePoint;
-                    onePoint.set_index(oneline.at(0).toInt());
-                    onePoint.mutable_mappoint()->set_longitude(oneline.at(1).toDouble());
-                    onePoint.mutable_mappoint()->set_latitude(oneline.at(2).toDouble());
-                    onePoint.mutable_mappoint()->set_height(oneline.at(3).toDouble());
-                    somePoints.append(onePoint);
-                }
-            }
-            pUploadClient->updatePath(pControlClient->get_patrolPathID(),somePoints);
-            pUploadClient->uploadPath();
-            pControlClient->set_isNeedMap(false);
-        }
-    }
-}
+//class mainloop : public QThread
+//{
+////    Q_OBJECT
+//public:
+//    mainloop(VehicleControlClient *pCC,DataExchangeClient *pUC);
+//    void run();
+//private:
+//    VehicleControlClient *pControlClient;
+//    DataExchangeClient *pUploadClient;
+//};
+
+//mainloop::mainloop(VehicleControlClient *pCC,DataExchangeClient *pUC)
+//{
+//    pControlClient = pCC;
+//    pUploadClient = pUC;
+//}
+
+//void mainloop::run()
+//{
+//    while (1) {
+////        msleep(100);
+//        if(pControlClient->get_isNeedMap() == true)
+//        {
+//            std::cout<<"patrol path calculating"<<std::endl;
+//            QFile mapfile("/home/samuel/Documents/path1.txt");
+//            QVector<org::jeecg::defsDetails::grpc::MapPoint> somePoints;
+//            if(mapfile.open(QIODevice::ReadOnly | QIODevice::Text))
+//            {
+//                while(!mapfile.atEnd())
+//                {
+//                    QByteArray line = mapfile.readLine();
+//                    QString map_str(line);
+//                    QStringList oneline = map_str.split(",");
+//                    org::jeecg::defsDetails::grpc::MapPoint onePoint;
+//                    onePoint.set_index(oneline.at(0).toInt());
+//                    onePoint.mutable_mappoint()->set_longitude(oneline.at(1).toDouble());
+//                    onePoint.mutable_mappoint()->set_latitude(oneline.at(2).toDouble());
+//                    onePoint.mutable_mappoint()->set_height(oneline.at(3).toDouble());
+//                    somePoints.append(onePoint);
+//                }
+//            }
+//            pUploadClient->updatePath(pControlClient->get_patrolPathID(),somePoints);
+//            pUploadClient->uploadPath();
+//            pControlClient->set_isNeedMap(false);
+//        }
+//    }
+//}
 
 void dec_yaml(const char * stryamlpath)
 {
@@ -161,8 +161,11 @@ int main(int argc, char *argv[])
 
     VehiclePatrolExceptionClient vehiclepatrol(grpc::CreateChannel(patrol_str, grpc::InsecureChannelCredentials()));
 
-    mainloop loop(vehiclecontrol,vehicleupload);
-    loop.start();
+    QObject::connect(vehiclecontrol,&VehicleControlClient::patrolPOI_Recieved,vehicleupload,&DataExchangeClient::patrolPOI_Recieved_Slot);
+    QObject::connect(vehicleupload,&DataExchangeClient::uploadPath_Finished,vehiclecontrol,&VehicleControlClient::uploadPath_Finished_Slot);
+
+//    mainloop loop(vehiclecontrol,vehicleupload);
+//    loop.start();
 
     return a.exec();
 }

+ 37 - 0
src/driver/driver_cloud_grpc_client_BS/vehicle_control.cpp

@@ -45,6 +45,11 @@ std::string VehicleControlClient::vehicleControl(void)
     // 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 = 1;
+    timespec.tv_nsec = 0;
+    timespec.clock_type = GPR_TIMESPAN;
+    context.set_deadline(timespec);
 
     // The actual RPC.
     Status status = stub_ -> vehicleControl(&context,request,&reply);
@@ -71,6 +76,10 @@ std::string VehicleControlClient::vehicleControl(void)
     } 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 "vehicleControl RPC failed";
     }
 }
@@ -87,6 +96,11 @@ std::string VehicleControlClient::uploadMap(void)
     // 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 = 2;
+    timespec.tv_nsec = 0;
+    timespec.clock_type = GPR_TIMESPAN;
+    context.set_deadline(timespec);
 
     // The actual RPC.
     Status status = stub_ -> UploadMap(&context,request,&reply);
@@ -114,6 +128,10 @@ std::string VehicleControlClient::uploadMap(void)
     } 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 "UploadMap RPC failed";
     }
 }
@@ -130,6 +148,11 @@ std::string VehicleControlClient::changeCtrlMode(void)
     // Context for the client. It could be used to convey extra information to
     // the server and/or tweastd::cout<<"shift:"<<shiftCMD<<std::endl;k certain RPC behaviors.
     ClientContext context;
+    gpr_timespec timespec;
+    timespec.tv_sec = 1;
+    timespec.tv_nsec = 0;
+    timespec.clock_type = GPR_TIMESPAN;
+    context.set_deadline(timespec);
 
     // The actual RPC.
     Status status = stub_ -> changeCtrlMode(&context,request,&reply);
@@ -144,6 +167,10 @@ std::string VehicleControlClient::changeCtrlMode(void)
     } 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 "changeCtrlMode RPC failed";
     }
 }
@@ -160,6 +187,7 @@ void VehicleControlClient::updateMapPOIData(void)
 {
     std::cout<<"isNeedMap:"<<isNeedMap<<std::endl;
     std::cout<<"patrolPathID:"<<patrolPathID<<std::endl;
+    emit patrolPOI_Recieved(patrolPathID);
 }
 
 void VehicleControlClient::updateCtrolMode(void)
@@ -208,3 +236,12 @@ void VehicleControlClient::set_isNeedMap(bool needMapStatus)
 {
     isNeedMap = needMapStatus;
 }
+
+void VehicleControlClient::uploadPath_Finished_Slot(std::string pathID)
+{
+    std::cout<<"Path ID:"<<pathID<<" upload finished"<<std::endl;
+    if(isNeedMap == true)
+    {
+        isNeedMap = false;
+    }
+}

+ 6 - 0
src/driver/driver_cloud_grpc_client_BS/vehicle_control.h

@@ -59,6 +59,12 @@ private:
     QTimer *controlTimer;
     QTimer *uploadMapTimer;
 
+signals:
+    void patrolPOI_Recieved(std::string pathID);
+
+public slots:
+    void uploadPath_Finished_Slot(std::string pathID);
+
 private slots:
     void controlTimeout(void);
     void uploadMapTimeout(void);

+ 9 - 0
src/driver/driver_cloud_grpc_client_BS/vehicle_patrol.cpp

@@ -57,6 +57,11 @@ std::string VehiclePatrolExceptionClient::uploadVehiclePatrolInfo(void)
     // 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 = 1;
+    timespec.tv_nsec = 0;
+    timespec.clock_type = GPR_TIMESPAN;
+    context.set_deadline(timespec);
 
     // The actual RPC.
     Status status = stub_ -> uploadVehiclePatrolInfo(&context,request,&reply);
@@ -68,6 +73,10 @@ std::string VehiclePatrolExceptionClient::uploadVehiclePatrolInfo(void)
     } 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";
     }
 }

+ 46 - 0
src/driver/driver_cloud_grpc_client_BS/vehicle_upload.cpp

@@ -1,6 +1,9 @@
 #include "vehicle_upload.h"
 #include <QDateTime>
 #include <QFile>
+#include <QString>
+#include <QStringList>
+#include <QThread>
 
 extern std::string gstrserverip;
 extern std::string gstruploadPort;
@@ -71,6 +74,11 @@ std::string DataExchangeClient::uploadVehicleInfo(void)
     // 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 = 2;
+    timespec.tv_nsec = 0;
+    timespec.clock_type = GPR_TIMESPAN;
+    context.set_deadline(timespec);
 
     // The actual RPC.
     Status status = stub_ -> uploadVehicleInfo(&context, request, &reply);
@@ -83,6 +91,10 @@ std::string DataExchangeClient::uploadVehicleInfo(void)
     } 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 "uploadVehicleInfo RPC failed";
     }
 }
@@ -105,6 +117,11 @@ std::string DataExchangeClient::uploadPath(void)
     // 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_ -> uploadPath(&context,request,&reply);
@@ -114,11 +131,16 @@ std::string DataExchangeClient::uploadPath(void)
         if(reply.id() == gstrid)
         {
             std::cout<<"Path uploaded by car id:"<<reply.id()<<std::endl;
+            emit uploadPath_Finished(patrolPathID);
         }
         return "uploadPath 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 "uploadPath RPC failed";
     }
 }
@@ -205,3 +227,27 @@ void DataExchangeClient::uploadTimeout(void)
     std::string reply = uploadVehicleInfo();
 //    std::cout<< reply <<std::endl;
 }
+
+void DataExchangeClient::patrolPOI_Recieved_Slot(std::string pathID)
+{
+    std::cout<<"patrol path calculating"<<std::endl;
+    QFile mapfile("/home/samuel/Documents/path1.txt");
+    QVector<org::jeecg::defsDetails::grpc::MapPoint> somePoints;
+    if(mapfile.open(QIODevice::ReadOnly | QIODevice::Text))
+    {
+        while(!mapfile.atEnd())
+        {
+            QByteArray line = mapfile.readLine();
+            QString map_str(line);
+            QStringList oneline = map_str.split(",");
+            org::jeecg::defsDetails::grpc::MapPoint onePoint;
+            onePoint.set_index(oneline.at(0).toInt());
+            onePoint.mutable_mappoint()->set_longitude(oneline.at(1).toDouble());
+            onePoint.mutable_mappoint()->set_latitude(oneline.at(2).toDouble());
+            onePoint.mutable_mappoint()->set_height(oneline.at(3).toDouble());
+            somePoints.append(onePoint);
+        }
+    }
+    updatePath(pathID,somePoints);
+    uploadPath();
+}

+ 6 - 0
src/driver/driver_cloud_grpc_client_BS/vehicle_upload.h

@@ -84,6 +84,12 @@ private:
 
     QTimer *uploadTimer;
 
+signals:
+    void uploadPath_Finished(std::string pathID);
+
+public slots:
+    void patrolPOI_Recieved_Slot(std::string pathID);
+
 private slots:
     void uploadTimeout(void);
 };