Browse Source

完成terminal测试cpp

zx 4 years ago
parent
commit
b6e8c9fb62

+ 6 - 4
CMakeLists.txt

@@ -44,7 +44,7 @@ aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/communication COMMUNICATION_SRC )
 aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/system SYSTEM_SRC )
 aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/parkspace PARKSPACE_SRC )
 
-add_executable(terminal
+add_executable(process
         main.cpp
         ./error_code/error_code.cpp
         ${locate_src}
@@ -63,7 +63,7 @@ add_executable(terminal
         ${PARKSPACE_SRC}
         )
 
-target_link_libraries(terminal
+target_link_libraries(process
         nnxx
         nanomsg
         ${PROTOBUF_LIBRARIES}
@@ -91,8 +91,10 @@ target_link_libraries(locate_client
         )
 
 
-add_executable(terminal_client
+add_executable(terminal
         ./test/terminal_client.cpp
+        ./test/store_terminal.cpp
+        ./test/pickup_terminal.cpp
         ./test/Terminal_communication.cpp
         ./lidar_locate/Locate_communicator.cpp
         ./error_code/error_code.cpp
@@ -100,7 +102,7 @@ add_executable(terminal_client
         ${TOOL_SRC}
         ${COMMUNICATION_SRC}
         )
-target_link_libraries(terminal_client
+target_link_libraries(terminal
         nnxx
         nanomsg
         ${PROTOBUF_LIBRARIES}

+ 4 - 4
lidar_locate/Locate_communicator.cpp

@@ -163,7 +163,7 @@ Error_manager Locate_communicator::execute_msg(Communication_message* p_msg)
             response.ParseFromString(p_msg->get_message_buf());
             message::Measure_request_msg request=create_request_by_response(response);
             ///查询请求表是否存在,并且更新
-            if(m_response_table.find_update(request,response))
+            if(m_response_table.find_update(request,response)==false)
             {
                 return Error_manager(LOCATER_MSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"measure response without request");
             }
@@ -193,11 +193,11 @@ message::Measure_request_msg Locate_communicator::create_request_by_response(mes
     message::Measure_request_msg request;
     message::Base_info baseInfo;
     baseInfo.set_msg_type(message::eLocate_request_msg);
-    baseInfo.set_sender(response.base_info().sender());
-    baseInfo.set_receiver(response.base_info().receiver());
+    baseInfo.set_sender(response.base_info().receiver());
+    baseInfo.set_receiver(response.base_info().sender());
     request.mutable_base_info()->CopyFrom(baseInfo);
     request.set_command_id(response.command_id());
-    request.set_terminal_id(response.command_id());
+    request.set_terminal_id(response.terminal_id());
     return request;
 }
 

File diff suppressed because it is too large
+ 145 - 3017
message/process_message.pb.cc


File diff suppressed because it is too large
+ 42 - 1757
message/process_message.pb.h


+ 5 - 61
message/process_message.proto

@@ -3,72 +3,16 @@ package message;
 import "message_base.proto";
 
 
-enum Step_statu
+enum Entrance_statu
 {
     eWorking=0;             //正在执行
-    eComplete=1;            //完成
+    eReady=1;               //完成/空闲
 }
 
-//停车指令预处理状态
-message Preprocess_store_statu
-{
-    optional Step_statu         statu=1;         //当前步骤状态
-}
-
-//停车指令测量状态
-message Measure_store_statu
-{
-    optional Step_statu         statu=1;         //当前步骤状态
-}
-
-//停车指令调度状态
-message Storing_statu
-{
-    optional Step_statu         statu=1;         //当前步骤状态
-}
-//停车指令收尾状态
-message Finish_store_statu
-{
-    optional Step_statu         statu=1;         //当前步骤状态
-}
-
-//取车指令预处理状态
-message Preprocess_pickup_statu
-{
-    optional Step_statu     statu=1;
-}
-
-//取车指令调度状态
-message Picking_statu
-{
-    optional Step_statu     statu=1;
-}
-//取车指令收尾状态
-message Finish_pickup_statu
-{
-    optional Step_statu         statu=1;         //当前步骤状态
-}
-
-/////停车流程状态
-message Process_store_statu
-{
-    optional Preprocess_store_statu             preprocess_statu=1;
-    optional Measure_store_statu                measure_statu=2;
-    optional Storing_statu                      storing_statu=3;
-    optional Finish_store_statu                 finish_statu=4;
-}
-
-/////取车流程状态
-message Process_pickup_statu
-{
-    optional Preprocess_pickup_statu             preprocess_statu=1;
-    optional Picking_statu                      picking_statu=2;
-    optional Finish_store_statu                 finish_statu=3;
-}
 
 message Entrance_statu_msg
 {
-    required Base_info                      base_info=1;
-    map<int32,Process_store_statu>         store_msg_map=2;
-    map<int32,Process_pickup_statu>        pickup_msg_map=3;
+    required Base_info                          base_info=1;                 //消息类型
+    map<int32,Entrance_statu>                   store_msg_map=2;
+    map<int32,Entrance_statu>                   pickup_msg_map=3;
 }

+ 7 - 7
parkspace/Parkspace_communicator.cpp

@@ -329,7 +329,7 @@ Error_manager Parkspace_communicator::execute_msg(Communication_message* p_msg)
             response.ParseFromString(p_msg->get_message_buf());
             message::Parkspace_allocation_request_msg request=create_request_by_response(response);
             ///查询请求表是否存在,并且更新
-            if(m_alloc_table.find_update(request,response))
+            if(m_alloc_table.find_update(request,response)==false)
             {
                 return Error_manager(PARKSPACE_ALLOCMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace alloc response without request");
             }
@@ -345,7 +345,7 @@ Error_manager Parkspace_communicator::execute_msg(Communication_message* p_msg)
             }
             message::Parkspace_search_request_msg request=create_request_by_response(response);
             ///查询请求表是否存在,并且更新
-            if(m_search_table.find_update(request,response))
+            if(m_search_table.find_update(request,response)==false)
             {
                 return Error_manager(PARKSPACE_SEARCHMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace search response without request");
             }
@@ -357,7 +357,7 @@ Error_manager Parkspace_communicator::execute_msg(Communication_message* p_msg)
             response.ParseFromString(p_msg->get_message_buf());
             message::Parkspace_release_request_msg request=create_request_by_response(response);
             ///查询请求表是否存在,并且更新
-            if(m_release_table.find_update(request,response))
+            if(m_release_table.find_update(request,response)==false)
             {
                 return Error_manager(PARKSPACE_RELEASEMSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"parkspace release response without request");
             }
@@ -418,8 +418,8 @@ Parkspace_communicator::create_request_by_response(message::Parkspace_allocation
     message::Parkspace_allocation_request_msg request;
     message::Base_info baseInfo;
     baseInfo.set_msg_type(message::eParkspace_allocation_request_msg);
-    baseInfo.set_sender(response.base_info().sender());
-    baseInfo.set_receiver(response.base_info().receiver());
+    baseInfo.set_sender(response.base_info().receiver());
+    baseInfo.set_receiver(response.base_info().sender());
     request.mutable_base_info()->CopyFrom(baseInfo);
     request.set_command_id(response.command_id());
     request.set_terminal_id(response.command_id());
@@ -432,8 +432,8 @@ Parkspace_communicator::create_request_by_response(message::Parkspace_search_res
     message::Parkspace_search_request_msg request;
     message::Base_info baseInfo;
     baseInfo.set_msg_type(message::eParkspace_search_request_msg);
-    baseInfo.set_sender(response.base_info().sender());
-    baseInfo.set_receiver(response.base_info().receiver());
+    baseInfo.set_sender(response.base_info().receiver());
+    baseInfo.set_receiver(response.base_info().sender());
     request.mutable_base_info()->CopyFrom(baseInfo);
     request.set_command_id(response.command_id());
 

+ 0 - 2
parkspace/Parkspace_communicator.h

@@ -40,10 +40,8 @@ public:
      */
     Error_manager release_request(message::Parkspace_release_request_msg& request,message::Parkspace_release_response_msg& response);
 
-
     Error_manager check_statu();
 
-
 protected:
     Parkspace_communicator();
     virtual Error_manager encapsulate_msg(Communication_message* message);

+ 13 - 11
system/PickupProcessTask.cpp

@@ -45,14 +45,14 @@ Error_manager PickupProcessTask::search_space()
 void PickupProcessTask::Main()
 {
     Error_manager code;
-    message::Process_pickup_statu pickup_statu;
+
+    Command_manager::get_instance_pointer()->updata_pickup_entrance_statu(m_command_id,message::eWorking);
     //进入取车流程
     switch (0)
     {
         //第一步,执行取车动作
         case 0:
         {
-            message::Picking_statu picking_statu;
             code=pickup_step();
             if(code!=SUCCESS)
             {
@@ -65,6 +65,7 @@ void PickupProcessTask::Main()
             code=release_space_step();
             if(code!=SUCCESS)
             {
+                LOG(ERROR)<<"取车释放车位失败:"<<code.to_string();
                 break;
             }
         }
@@ -78,10 +79,16 @@ void PickupProcessTask::Main()
             }
         }
     }
-    message::Finish_pickup_statu finish_statu;
-    finish_statu.set_statu(message::eComplete);
-    pickup_statu.mutable_finish_statu()->CopyFrom(finish_statu);
-    Command_manager::get_instance_pointer()->updata_entrance_statu(m_command_id,pickup_statu);
+    //暂停 5-15s 代替
+    int delay=rand()%7000+4000;
+    usleep(1000*delay);
+    LOG(WARNING)<<"取车成功,取车终端:"<<m_terminor_id<<"指令id:"<<m_command_id
+                <<", 车位id:"<<m_parcspace_search_response_msg.car_position().parkspace_id()
+                <<", 车位楼层:"<<m_parcspace_search_response_msg.car_position().floor()
+                <<", 车位序号:"<<m_parcspace_search_response_msg.car_position().index()
+                <<", 车牌号:"<<m_car_info.license()
+                <<", 库内车牌号:"<<m_parcspace_search_response_msg.car_position().car_info().license();
+    Command_manager::get_instance_pointer()->updata_pickup_entrance_statu(m_terminor_id,message::eReady);
     ////资源回收
 
 }
@@ -135,11 +142,6 @@ Error_manager PickupProcessTask::release_space_step()
         return code;
 
     if(release_response.error_manager().error_code()==0) {
-        LOG(INFO)<<"取车流程释放车位成功,停车终端:"<<m_terminor_id
-                 <<", 指令id:"<<m_command_id
-                 <<", 车位楼层:"<<m_parcspace_search_response_msg.car_position().floor()
-                 <<", 车位序号:"<<m_parcspace_search_response_msg.car_position().index()
-                 <<", 车牌号:"<<m_parcspace_search_response_msg.car_position().car_info().license();
         return SUCCESS;
     }
     else

+ 22 - 18
system/StoreProcessTask.cpp

@@ -46,12 +46,12 @@ Error_manager StoreProcessTask::locate_step() {
         return code;
 
     if(m_measure_response_msg.error_manager().error_code()==0) {
-        LOG(INFO)<<"测量成功,停车终端:"<<m_terminor_id
+        /*LOG(INFO)<<"测量成功,停车终端:"<<m_terminor_id
                  <<", 指令id:"<<m_command_id
                  <<", 车辆宽:"<<m_measure_response_msg.locate_information().locate_width()
                  <<", 车辆高:"<<m_measure_response_msg.locate_information().locate_height()
                  <<", 车辆角度:"<<m_measure_response_msg.locate_information().locate_angle()
-                 <<", 车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();
+                 <<", 车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();*/
         return SUCCESS;
     }
     else
@@ -91,11 +91,11 @@ Error_manager StoreProcessTask::alloc_space()
 
     if(m_parcspace_alloc_response_msg.error_manager().error_code()==0)
     {
-        LOG(INFO)<<"分配车位成功,停车终端:"<<m_terminor_id
+        /*LOG(INFO)<<"分配车位成功,停车终端:"<<m_terminor_id
             <<", 指令id:"<<m_command_id
             <<", 车位楼层:"<<m_parcspace_alloc_response_msg.allocated_space_info().floor()
             <<", 车位序号:"<<m_parcspace_alloc_response_msg.allocated_space_info().index()
-            <<", 车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();
+            <<", 车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();*/
         return SUCCESS;
     }
     else
@@ -135,7 +135,7 @@ Error_manager StoreProcessTask::release_space_step()
         return code;
 
     if(release_response.error_manager().error_code()==0) {
-        LOG(INFO)<<"停车流程异常,释放车位成功,停车终端:"<<m_terminor_id
+        LOG(ERROR)<<"停车流程异常,释放车位成功,停车终端:"<<m_terminor_id
                  <<", 指令id:"<<m_command_id
                  <<", 车位楼层:"<<m_parcspace_alloc_response_msg.allocated_space_info().floor()
                  <<", 车位序号:"<<m_parcspace_alloc_response_msg.allocated_space_info().index()
@@ -152,7 +152,7 @@ void StoreProcessTask::Main()
      * 外部已经分配好车位,进入到此流程说明车位已经分配好, 存放在 m_parcspace_alloc_response_msg
      */
     Error_manager code;
-    message::Process_store_statu store_statu;
+    Command_manager::get_instance_pointer()->updata_store_entrance_statu(m_terminor_id,message::eWorking);
     //开始执行停车指令
     //第一步测量
     switch (0)
@@ -163,33 +163,37 @@ void StoreProcessTask::Main()
             code=locate_step();
             if(code!=SUCCESS)
             {
-                LOG(INFO)<<"释放车位成功,停车终端:"<<m_terminor_id
-                         <<", 指令id:"<<m_command_id
-                         <<", 车位楼层:"<<m_parcspace_alloc_response_msg.allocated_space_info().floor()
-                         <<", 车位序号:"<<m_parcspace_alloc_response_msg.allocated_space_info().index()
-                         <<", 车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();
+                LOG(ERROR)<<"测量失败:"<<code.to_string();
                 break;
             }
         }
         //第二步,调度
         case 1:
         {
-            //暂停 0.5-3.5s 代替
-            int delay=rand()%5000+10000;
+            //暂停 5-15s 代替
+            int delay=rand()%5000+5000;
             usleep(1000*delay);
             //调度失败,break,完成return
+        }
+        //第三步,占据车位
+        case 2:
+        {
+            ////
+            Command_manager::get_instance_pointer()->updata_store_entrance_statu(m_terminor_id,message::eReady);
+            LOG(INFO)<<"停车成功,停车终端:"<<m_terminor_id<<"指令id:"<<m_command_id
+                     <<", 车位id:"<<m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id()
+                     <<", 车位楼层:"<<m_parcspace_alloc_response_msg.allocated_space_info().floor()
+                     <<", 车位序号:"<<m_parcspace_alloc_response_msg.allocated_space_info().index()
+                     <<", 车牌号:"<<m_car_info.license()
+                     <<", 库内车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();
             return ;
         }
     }
+
     /*
      * switch 语句break,说明停车故障,需清除车位
      */
 
-    message::Finish_store_statu finish_statu;
-    finish_statu.set_statu(message::eComplete);
-    store_statu.mutable_finish_statu()->CopyFrom(finish_statu);
-    Command_manager::get_instance_pointer()->updata_entrance_statu(m_command_id,store_statu);
-
     //失败,清理车位
     code=release_space_step();
     if(code!=SUCCESS)

+ 16 - 20
system/command_manager.cpp

@@ -89,20 +89,15 @@ Error_manager Command_manager::execute_store_command(message::Store_command_requ
                         error_msg.set_error_code(0);
 
                         StoreProcessTask* task=new StoreProcessTask();
-                        int command_id=rand()%900000+100000;
+                        int command_id=request.terminal_id()+10000;
                         //初始化流程
                         task->init_task(command_id,request.terminal_id(),locate_info,request.car_info());
                         //获取车位
                         Error_manager code=task->alloc_space();
                         if(code==SUCCESS)
                         {
-                            //清理当前状态
-                            /////
-                            std::lock_guard<std::mutex> lk(m_entrance_msg_lock);
-                            auto it=m_entrance_status.mutable_store_msg_map()->find(command_id);
-                            if(it!=m_entrance_status.mutable_store_msg_map()->end()) {
-                                it->second=message::Process_store_statu();
-                            }
+                            //更新状态
+                            Command_manager::get_instance_pointer()->updata_store_entrance_statu(request.terminal_id(),message::eWorking);
                             m_thread_queue_process->AddTask(task);
                             response.mutable_code()->CopyFrom(error_msg);
                             return SUCCESS;
@@ -148,10 +143,11 @@ Error_manager Command_manager::execute_pickup_command(message::Pickup_command_re
         baseInfo.set_sender(message::eMain);
         baseInfo.set_receiver(message::eTerminor);
         response.mutable_base_info()->CopyFrom(baseInfo);
+        response.set_terminal_id(request.terminal_id());
 
         Error_manager code;
         PickupProcessTask *task = new PickupProcessTask();
-        int command_id = rand() % 900000 + 100000;
+        int command_id = request.terminal_id()+20000;
         //初始化流程
         task->init_task(command_id, request.terminal_id(), request.car_info());
         /////查询车位
@@ -160,12 +156,8 @@ Error_manager Command_manager::execute_pickup_command(message::Pickup_command_re
         error_msg.set_error_code(0);
         if(code==SUCCESS)
         {
-            /////
-            std::lock_guard<std::mutex> lk(m_entrance_msg_lock);
-            auto it=m_entrance_status.mutable_pickup_msg_map()->find(command_id);
-            if(it!=m_entrance_status.mutable_pickup_msg_map()->end()) {
-                it->second=message::Process_pickup_statu();
-            }
+            //更新状态
+            Command_manager::get_instance_pointer()->updata_pickup_entrance_statu(request.terminal_id(),message::eWorking);
             m_thread_queue_process->AddTask(task);
             response.mutable_code()->CopyFrom(error_msg);
             return SUCCESS;
@@ -186,7 +178,7 @@ Error_manager Command_manager::execute_pickup_command(message::Pickup_command_re
 /*
      * 更新状态
      */
-void Command_manager::updata_entrance_statu(int command_id,message::Process_store_statu& msg)
+void Command_manager::updata_store_entrance_statu(int command_id,message::Entrance_statu  msg)
 {
     if(command_id<0 || command_id>100)
         return;
@@ -195,14 +187,16 @@ void Command_manager::updata_entrance_statu(int command_id,message::Process_stor
     auto it=m_entrance_status.mutable_store_msg_map()->find(command_id);
     if(it==m_entrance_status.mutable_store_msg_map()->end())
     {
-        m_entrance_status.mutable_store_msg_map()->insert(google::protobuf::MapPair<int,message::Process_store_statu>(command_id,msg));
+        m_entrance_status.mutable_store_msg_map()->insert(google::protobuf::MapPair<int,message::Entrance_statu >(command_id,msg));
     }
     else
     {
         it->second=msg;
     }
+    //立刻发送
+    System_communicator::get_instance_pointer()->send_entrance_statu(m_entrance_status);
 }
-void Command_manager::updata_entrance_statu(int command_id,message::Process_pickup_statu& msg)
+void Command_manager::updata_pickup_entrance_statu(int command_id,message::Entrance_statu statu)
 {
     if(command_id<0 || command_id>100)
         return;
@@ -211,12 +205,13 @@ void Command_manager::updata_entrance_statu(int command_id,message::Process_pick
     auto it=m_entrance_status.mutable_pickup_msg_map()->find(command_id);
     if(it==m_entrance_status.mutable_pickup_msg_map()->end())
     {
-        m_entrance_status.mutable_pickup_msg_map()->insert(google::protobuf::MapPair<int,message::Process_pickup_statu>(command_id,msg));
+        m_entrance_status.mutable_pickup_msg_map()->insert(google::protobuf::MapPair<int,message::Entrance_statu>(command_id,statu));
     }
     else
     {
-        it->second=msg;
+        it->second=statu;
     }
+    System_communicator::get_instance_pointer()->send_entrance_statu(m_entrance_status);
 }
 
 /*
@@ -245,6 +240,7 @@ void Command_manager::publish_status()
                 baseInfo.set_msg_type(message::eEntrance_statu_msg);
                 baseInfo.set_sender(message::eMain);
                 baseInfo.set_receiver(message::eEmpty); //所有对象都可接收
+                m_entrance_status.mutable_base_info()->CopyFrom(baseInfo);
             }
             System_communicator::get_instance_pointer()->send_entrance_statu(m_entrance_status);
         }

+ 2 - 2
system/command_manager.h

@@ -40,8 +40,8 @@ public:
     /*
      * 更新状态
      */
-    void updata_entrance_statu(int command_id,message::Process_store_statu& msg);
-    void updata_entrance_statu(int command_id,message::Process_pickup_statu& msg);
+    void updata_store_entrance_statu(int command_id,message::Entrance_statu statu);
+    void updata_pickup_entrance_statu(int command_id,message::Entrance_statu statu);
 private:
     Command_manager();
 

+ 90 - 12
test/Terminal_communication.cpp

@@ -4,6 +4,7 @@
 
 #include "Terminal_communication.h"
 #include "process_message.pb.h"
+#include <condition_variable>
 
 Terminal_communication::Terminal_communication(){}
 Terminal_communication::~Terminal_communication(){}
@@ -62,6 +63,40 @@ Error_manager Terminal_communication::execute_msg(Communication_message* p_msg)
             }
         }
     }
+
+    if(p_msg->get_message_type()==Communication_message::ePickup_command_response_msg)
+    {
+        message::Pickup_command_response_msg response;
+        if(false==response.ParseFromString(p_msg->get_message_buf()))
+        {
+            return Error_manager(ERROR,CRITICAL_ERROR,"停车指令反馈信息解析错误");
+        }
+        message::Base_info base_info=response.base_info();
+        if(base_info.sender()==message::eMain && base_info.receiver()==message::eTerminor)
+        {
+            message::Error_manager error_code=response.code();
+            if(error_code.error_code()==0)
+            {
+                m_pickup_response_msg=response;
+                return SUCCESS;
+            }
+        }
+    }
+
+    if(p_msg->get_message_type()==Communication_message::eEntrance_statu_msg)
+    {
+        message::Entrance_statu_msg msg;
+        if(msg.ParseFromString(p_msg->get_message_buf())==false)
+        {
+            std::cout<<" ERROR 解析Entrance_statu_msg 失败"<<std::endl;
+        }
+        message::Base_info base_info=msg.base_info();
+        if(base_info.sender()==message::eMain)
+        {
+            m_entrance_status=msg;
+        }
+    }
+
     return SUCCESS;
 
     return Error_manager(FAILED,MINOR_ERROR,"terminal communication 未知消息");
@@ -97,15 +132,24 @@ Error_manager Terminal_communication::set_terminal_id(int id)
     return SUCCESS;
 }
 
+
+Error_manager Terminal_communication::send_pickup_task(message::Parkspace_search_request_msg& msg)
+{
+    if(msg.has_base_info()==false)
+        return FAILED;
+    if(msg.base_info().sender()!=message::eTerminor ||msg.base_info().msg_type()!=message::eParkspace_search_request_msg)
+        return FAILED;
+    Communication_message message;
+    message.reset(msg.base_info(),msg.SerializeAsString());
+    return Communication_socket_base::encapsulate_msg(&message);
+}
+
 /*
     * 发送停车指令请求
     */
 Error_manager Terminal_communication::store_request(message::Store_command_request_msg& request,message::Store_command_response_msg& response)
 {
-    if(m_statu!=eTerminal_ready)
-    {
-        return Error_manager(FAILED,MINOR_ERROR,"停车指令发送失败,终端不空闲");
-    }
+
     if(request.has_locate_information()==false ||
         request.has_car_info()==false)
     {
@@ -120,7 +164,7 @@ Error_manager Terminal_communication::store_request(message::Store_command_reque
     code=encapsulate_msg(&message);
     if(code!=SUCCESS)
         return code;
-    m_statu=eTerminal_storing;
+
     //循环查询请求是否被处理
     auto start_time=std::chrono::system_clock::now();
     double time=0;
@@ -142,6 +186,7 @@ Error_manager Terminal_communication::store_request(message::Store_command_reque
                                      "parkspace store response msg info error");
             }
             response=m_store_response_msg;
+            m_store_response_msg=message::Store_command_response_msg();
             return SUCCESS;
 
         }
@@ -163,12 +208,6 @@ Error_manager Terminal_communication::store_request(message::Store_command_reque
  */
 Error_manager Terminal_communication::pickup_request(message::Pickup_command_request_msg& request,message::Pickup_command_response_msg& response)
 {
-    if(m_statu!=eTerminal_ready)
-    {
-        return Error_manager(FAILED,MINOR_ERROR,"取车指令发送失败,终端不空闲");
-    }
-
-
     if(request.has_car_info()==false)
     {
         return Error_manager(ERROR,CRITICAL_ERROR,"取车指令请求消息缺少车辆信息");
@@ -192,7 +231,8 @@ Error_manager Terminal_communication::pickup_request(message::Pickup_command_req
         //查询到记录
         ///查询是否存在,并且删除该记录,
         //判断是否接收到回应,若回应信息被赋值则证明有回应
-        if (m_pickup_response_msg.has_base_info() && m_pickup_response_msg.has_code()) {
+        if (m_pickup_response_msg.has_base_info() && m_pickup_response_msg.has_code())
+        {
             message::Base_info response_base = m_pickup_response_msg.base_info();
             //检查类型是否匹配
             if (response_base.msg_type() != message::ePickup_command_response_msg) {
@@ -206,6 +246,7 @@ Error_manager Terminal_communication::pickup_request(message::Pickup_command_req
                                      "parkspace release response msg info error");
             }
             response=m_pickup_response_msg;
+            m_pickup_response_msg=message::Pickup_command_response_msg();
             return SUCCESS;
 
         }
@@ -220,4 +261,41 @@ Error_manager Terminal_communication::pickup_request(message::Pickup_command_req
     //超时,删除记录,返回错误
     return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace release request timeout");
 
+}
+
+
+/*
+    * 检查入口完成状态
+    */
+Error_manager Terminal_communication::check_store_entrance_statu(int terminal_id,message::Entrance_statu& statu)
+{
+    std::lock_guard<std::mutex> lk(m_statu_lock);
+    auto it=m_entrance_status.mutable_store_msg_map()->find(terminal_id);
+    if(it==m_entrance_status.mutable_store_msg_map()->end())
+    {
+        return ERROR;
+    }
+    else
+    {
+        statu=it->second;
+        return SUCCESS;
+    }
+}
+
+/*
+ * 检查出口完成状态
+ */
+Error_manager Terminal_communication::check_pickup_entrance_statu(int terminal_id,message::Entrance_statu& statu)
+{
+    std::lock_guard<std::mutex> lk(m_statu_lock);
+    auto it=m_entrance_status.mutable_pickup_msg_map()->find(terminal_id);
+    if(it==m_entrance_status.mutable_pickup_msg_map()->end())
+    {
+        return ERROR;
+    }
+    else
+    {
+        statu=it->second;
+        return SUCCESS;
+    }
 }

+ 21 - 7
test/Terminal_communication.h

@@ -4,18 +4,15 @@
 
 #ifndef NNXX_TESTS_TERMINAL_COMMUNICATION_H
 #define NNXX_TESTS_TERMINAL_COMMUNICATION_H
+
+#include <process_message.pb.h>
 #include "../tool/singleton.h"
 #include "../communication/communication_socket_base.h"
 #include "terminal_message.pb.h"
+#include "parkspace_allocation_message.pb.h"
 #include "message_compare.h"
 
 class Terminal_communication :public Singleton<Terminal_communication>, public Communication_socket_base{
-    enum Terminal_statu
-    {
-        eTerminal_ready=0,
-        eTerminal_storing,
-        eTerminal_picking,
-    };
 
     friend class Singleton<Terminal_communication>;
 private:
@@ -41,6 +38,21 @@ public:
      */
     Error_manager pickup_request(message::Pickup_command_request_msg& request,message::Pickup_command_response_msg& response);
 
+    /*
+     * 发送查询车辆信息消息,用于取车终端接收,id表示哪个终端执行取车任务
+     */
+    Error_manager send_pickup_task(message::Parkspace_search_request_msg& msg);
+
+    /*
+     * 检查入口完成状态
+     */
+    Error_manager check_store_entrance_statu(int terminal_id,message::Entrance_statu& statu);
+
+    /*
+     * 检查出口完成状态
+     */
+    Error_manager check_pickup_entrance_statu(int terminal_id,message::Entrance_statu& statu);
+
 protected:
     //重载函数
     virtual Error_manager encapsulate_msg(Communication_message* message);
@@ -57,11 +69,13 @@ protected:
     virtual Error_manager check_executer(Communication_message* p_msg);
 
 private:
-    Terminal_statu                                  m_statu;
     message::Store_command_response_msg             m_store_response_msg;
     message::Pickup_command_response_msg            m_pickup_response_msg;
     int                                             m_terminal_id;
 
+    std::mutex                                      m_statu_lock;
+    message::Entrance_statu_msg                     m_entrance_status;
+
 
 };
 

+ 92 - 0
test/pickup_terminal.cpp

@@ -0,0 +1,92 @@
+//
+// Created by zx on 2020/7/16.
+//
+
+#include "pickup_terminal.h"
+
+#include "Terminal_communication.h"
+pickup_terminal::pickup_terminal(int terminal_id)
+{
+    m_terminal_id=terminal_id;
+    m_thread_safe_queue= nullptr;
+
+}
+
+
+Error_manager pickup_terminal::init(threadsafe_queue<message::Car_info>* queue)
+{
+    m_thread_safe_queue=queue;
+    m_exit_cond.reset(false,false,false);
+    m_pthread=new std::thread(pickup_thread,this);
+    return SUCCESS;
+}
+
+pickup_terminal::~pickup_terminal()
+{
+    m_exit_cond.set_pass_ever(true);
+    if(m_pthread)
+    {
+        if(m_pthread->joinable())
+            m_pthread->join();
+        delete m_pthread;
+        m_pthread=nullptr;
+    }
+}
+
+
+Error_manager pickup_terminal::pickup(message::Car_info& car_info)
+{
+    message::Pickup_command_request_msg request;
+    message::Base_info base_info;
+    base_info.set_msg_type(message::ePickup_command_request_msg);
+    base_info.set_sender(message::eTerminor);
+    base_info.set_receiver(message::eMain);
+    request.mutable_base_info()->CopyFrom(base_info);
+    request.mutable_car_info()->CopyFrom(car_info);
+    request.set_terminal_id(m_terminal_id);
+    //发送停车请求
+    Error_manager code;
+    message::Pickup_command_response_msg response;
+    code=Terminal_communication::get_instance_pointer()->pickup_request(request,response);
+    if(code!=SUCCESS)
+    {
+        std::cout<<"取车请求失败:"<<code.to_string()<<std::endl;
+        return ERROR;
+    }
+    //等待停车完成
+    while(m_exit_cond.wait_for_millisecond(200)==false)
+    {
+        message::Entrance_statu statu;
+        code = Terminal_communication::get_instance_pointer()->check_pickup_entrance_statu(m_terminal_id, statu);
+        if (code != SUCCESS) {
+            continue;
+        }
+
+        if (statu == message::eReady) {
+            return SUCCESS;
+        }
+    }
+    return FAILED;
+}
+
+
+void pickup_terminal::pickup_thread(pickup_terminal* picker)
+{
+    while(picker->m_exit_cond.wait_for_millisecond(100)==false)
+    {
+        message::Car_info car_info;
+        if(picker->m_thread_safe_queue== nullptr)
+            continue;
+        if(picker->m_thread_safe_queue->try_pop(car_info)==true)
+        {
+            if(picker->pickup(car_info)==SUCCESS)
+                LOG(WARNING)<<"取车终端:"<<picker->m_terminal_id<<",车辆:"<<car_info.license().c_str()
+                        <<",已经取车完成,剩余out:"<<picker->m_thread_safe_queue->size();
+            else
+            {
+                LOG(ERROR)<<"取车终端:"<<picker->m_terminal_id<<",车辆:"<<car_info.license().c_str()
+                          <<",已经取车失败,剩余out:"<<picker->m_thread_safe_queue->size();
+            }
+        }
+    }
+}

+ 29 - 0
test/pickup_terminal.h

@@ -0,0 +1,29 @@
+//
+// Created by zx on 2020/7/16.
+//
+
+#ifndef NNXX_TESTS_PICKUP_TERMINAL_H
+#define NNXX_TESTS_PICKUP_TERMINAL_H
+#include <thread>
+#include "threadSafeQueue.h"
+#include "thread_condition.h"
+#include "error_code.h"
+#include "terminal_message.pb.h"
+
+class pickup_terminal {
+public:
+    pickup_terminal(int terminal_id);
+    ~pickup_terminal();
+    Error_manager init(threadsafe_queue<message::Car_info>* queue);
+    Error_manager pickup(message::Car_info& msg);
+protected:
+    static void pickup_thread(pickup_terminal* pPickup);
+    std::thread*                m_pthread;
+    int                         m_terminal_id;
+
+    threadsafe_queue<message::Car_info>*           m_thread_safe_queue;
+    Thread_condition                                m_exit_cond;
+};
+
+
+#endif //NNXX_TESTS_PICKUP_TERMINAL_H

+ 104 - 0
test/store_terminal.cpp

@@ -0,0 +1,104 @@
+//
+// Created by zx on 2020/7/16.
+//
+
+#include "store_terminal.h"
+#include "Terminal_communication.h"
+store_terminal::store_terminal(int terminal_id)
+{
+    m_terminal_id=terminal_id;
+    m_thread_safe_queue= nullptr;
+    m_thread_safe_output_queue=nullptr;
+}
+store_terminal::~store_terminal()
+{
+    m_exit_cond.set_pass_ever(true);
+    if(m_pthread)
+    {
+        if(m_pthread->joinable())
+            m_pthread->join();
+        delete m_pthread;
+        m_pthread=nullptr;
+    }
+}
+
+Error_manager store_terminal::init(threadsafe_queue<message::Car_info>* input,
+                                   threadsafe_queue<message::Car_info>* output)
+{
+    m_thread_safe_queue=input;
+    m_thread_safe_output_queue=output;
+    m_exit_cond.reset(false,false,false);
+    m_pthread=new std::thread(storing_thread,this);
+    return SUCCESS;
+}
+
+Error_manager store_terminal::storing(message::Car_info& car_info)
+{
+    message::Store_command_request_msg request;
+    message::Base_info base_info;
+    base_info.set_msg_type(message::eStore_command_request_msg);
+    base_info.set_sender(message::eTerminor);
+    base_info.set_receiver(message::eMain);
+    request.mutable_base_info()->CopyFrom(base_info);
+    request.mutable_car_info()->CopyFrom(car_info);
+
+    message::Locate_information locate_info;
+    locate_info.set_locate_x(0.9);
+    locate_info.set_locate_y(2.25);
+    locate_info.set_locate_angle(90.0);
+    locate_info.set_locate_wheel_base(2.7);
+    locate_info.set_locate_width(car_info.car_width());
+    locate_info.set_locate_height(car_info.car_height());
+    locate_info.set_locate_correct(true);
+    request.mutable_locate_information()->CopyFrom(locate_info);
+
+    request.set_terminal_id(m_terminal_id);
+    //发送停车请求
+    Error_manager code;
+    message::Store_command_response_msg response;
+    code=Terminal_communication::get_instance_pointer()->store_request(request,response);
+    if(code!=SUCCESS)
+    {
+        std::cout<<"  request_error: "<<code.to_string()<<std::endl;
+        return ERROR;
+    }
+    usleep(1000*100);
+    //等待停车完成
+    while(m_exit_cond.wait_for_millisecond(200)==false) {
+        message::Entrance_statu statu;
+        code = Terminal_communication::get_instance_pointer()->check_store_entrance_statu(m_terminal_id, statu);
+        if (code != SUCCESS) {
+            continue;
+        }
+
+        if (statu == message::eReady) {
+            if (m_thread_safe_output_queue)
+                m_thread_safe_output_queue->push(car_info);
+            return SUCCESS;
+        }
+    }
+    return FAILED;
+}
+
+
+void store_terminal::storing_thread(store_terminal* pStore)
+{
+    while(pStore->m_exit_cond.wait_for_millisecond(100)==false)
+    {
+        message::Car_info car_info;
+        if(pStore->m_thread_safe_queue== nullptr)
+            continue;
+
+        if(pStore->m_thread_safe_queue->try_pop(car_info)==true)
+        {
+            if(pStore->storing(car_info)==SUCCESS)
+                LOG(INFO)<<"停车终端:"<<pStore->m_terminal_id<<",车辆:"<<car_info.license().c_str()<<",已经停车完成,剩余in:"
+                <<pStore->m_thread_safe_queue->size();
+            else
+            {
+                LOG(ERROR)<<"停车终端:"<<pStore->m_terminal_id<<",车辆:"<<car_info.license().c_str()<<",已经停车失败,剩余in:"
+                         <<pStore->m_thread_safe_queue->size();
+            }
+        }
+    }
+}

+ 37 - 0
test/store_terminal.h

@@ -0,0 +1,37 @@
+//
+// Created by zx on 2020/7/16.
+//
+
+#ifndef NNXX_TESTS_STORE_TERMINAL_H
+#define NNXX_TESTS_STORE_TERMINAL_H
+#include <thread>
+#include "threadSafeQueue.h"
+#include "thread_condition.h"
+#include "error_code.h"
+#include "terminal_message.pb.h"
+
+
+
+class store_terminal {
+
+public:
+    store_terminal(int terminal_id);
+    ~store_terminal();
+
+    Error_manager init(threadsafe_queue<message::Car_info>* input,
+                       threadsafe_queue<message::Car_info>* output);
+    Error_manager storing(message::Car_info& msg);
+
+protected:
+    static void storing_thread(store_terminal* pStore);
+    std::thread*                m_pthread;
+    int                         m_terminal_id;
+
+    threadsafe_queue<message::Car_info>*           m_thread_safe_queue;
+    Thread_condition                                m_exit_cond;
+
+    threadsafe_queue<message::Car_info>*           m_thread_safe_output_queue;
+};
+
+
+#endif //NNXX_TESTS_STORE_TERMINAL_H

+ 89 - 72
test/terminal_client.cpp

@@ -15,98 +15,115 @@
 #include <thread>
 #include "threadSafeQueue.h"
 
-std::mutex lock;
-
-threadsafe_queue<std::string> license_queue;
 #include "Terminal_communication.h"
+#include "store_terminal.h"
+#include "pickup_terminal.h"
+#include "pathcreator.h"
 
-void pickup()
+GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
 {
-    message::Base_info base_msg;
-    message::Pickup_command_request_msg request;
-
-    base_msg.set_msg_type(message::ePickup_command_request_msg);
-    base_msg.set_sender(message::eTerminor);
-    base_msg.set_receiver(message::eMain);
-    request.mutable_base_info()->CopyFrom(base_msg);
-
-    int command_id = rand() % 900000 + 100000;
-    char license[255] = {0};
-    sprintf(license, "鄂A%d", command_id);
-
-    request.set_terminal_id(command_id % 6);
-    message::Car_info car_info;
-    car_info.set_license(license);
-    car_info.set_car_height(1.5);
-    car_info.set_car_width(1.7);
-    request.mutable_car_info()->CopyFrom(car_info);
-
-    message::Pickup_command_response_msg response;
-    Error_manager code=Terminal_communication::get_instance_pointer()->pickup_request(request,response);
-    if(code!=SUCCESS)
-    {
-        std::cout<<code.to_string()<<std::endl;
-    }
-    else
-    {
-        if(response.code().error_code()!=0)
-        {
-            std::cout<<response.code().error_description()<<std::endl;
-        }
-    }
+    time_t tt;
+    time( &tt );
+    tt = tt + 8*3600;  // transform the time zone
+    tm* t= gmtime( &tt );
+    char buf[255]={0};
+    sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump_client.txt",
+            t->tm_year + 1900,
+            t->tm_mon + 1,
+            t->tm_mday,
+            t->tm_hour,
+            t->tm_min,
+            t->tm_sec);
+
+    FILE* tp_file=fopen(buf,"w");
+    fprintf(tp_file,data,strlen(data));
+    fclose(tp_file);
 
 }
 
+void init_glog()
+{
+    time_t tt = time(0);//时间cuo
+    struct tm* t = localtime(&tt);
+
+    char strYear[255]={0};
+    char strMonth[255]={0};
+    char strDay[255]={0};
+
+    sprintf(strYear,"%04d", t->tm_year+1900);
+    sprintf(strMonth,"%02d", t->tm_mon+1);
+    sprintf(strDay,"%02d", t->tm_mday);
+
+    char buf[255]={0};
+    getcwd(buf,255);
+    char strdir[255]={0};
+    sprintf(strdir,"%s/log_client/%s/%s/%s", buf,strYear,strMonth,strDay);
+    PathCreator creator;
+    creator.Mkdir(strdir);
+
+    char logPath[255] = { 0 };
+    sprintf(logPath, "%s/", strdir);
+    FLAGS_max_log_size = 100;
+    FLAGS_logbufsecs = 0;
+    google::InitGoogleLogging("LidarMeasurement");
+    google::SetStderrLogging(google::INFO);
+    google::SetLogDestination(0, logPath);
+    google::SetLogFilenameExtension("zxlog");
+    google::InstallFailureSignalHandler();
+    google::InstallFailureWriter(&shut_down_logging);
+    FLAGS_colorlogtostderr = true;        // Set log color
+    FLAGS_logbufsecs = 0;                // Set log output speed(s)
+    FLAGS_max_log_size = 1024;            // Set max log file size(GB)
+    FLAGS_stop_logging_if_full_disk = true;
+}
+
 int main() {
+    init_glog();
     Terminal_communication::get_instance_pointer()->communication_connect("tcp://127.0.0.1:9001");
     Terminal_communication::get_instance_pointer()->communication_run();
+    usleep(2000*1000);
+    threadsafe_queue<message::Car_info> input_queue;
+    threadsafe_queue<message::Car_info> output_queue;
+
+    const int n_input=6;
+    const int n_output=6;
+    std::vector<store_terminal*> input_terminals;
+    std::vector<pickup_terminal*> output_terminals;
+    for(int i=0;i<n_input;++i)
+    {
+        store_terminal* store=new store_terminal(i);
+        store->init(&input_queue,&output_queue);
+        input_terminals.push_back(store);
+    }
+
+    for(int i=0;i<n_output;++i)
+    {
+        pickup_terminal* picker=new pickup_terminal(i);
+        picker->init(&output_queue);
+        output_terminals.push_back(picker);
+    }
 
     int n = 0;
     bool run = true;
     Error_manager code;
-    //std::thread* pth=new std::thread(cancel_thread);
     while (run) {
-        pickup();
-        /*message::Base_info base_msg;
-        message::Store_command_request_msg request;
 
-        base_msg.set_msg_type(message::eStore_command_request_msg);
-        base_msg.set_sender(message::eTerminor);
-        base_msg.set_receiver(message::eMain);
-        request.mutable_base_info()->CopyFrom(base_msg);
-
-        int command_id = rand() % 900000 + 100000;
+        int license_id = rand() % 90000 + 10000;
+        char C=rand()%10+'A';
         char license[255] = {0};
-        sprintf(license, "鄂A%d", command_id);
-
-        request.set_terminal_id(command_id % 6);
+        sprintf(license, "鄂%c%d",C, license_id);
         message::Car_info car_info;
         car_info.set_license(license);
         car_info.set_car_height(1.5);
         car_info.set_car_width(1.7);
-        request.mutable_car_info()->CopyFrom(car_info);
-        message::Locate_information locate_info;
-        locate_info.set_locate_x(0.9);
-        locate_info.set_locate_y(2.25);
-        locate_info.set_locate_angle(90.0);
-        locate_info.set_locate_wheel_base(2.7);
-        locate_info.set_locate_width(1.7);
-        locate_info.set_locate_height(1.5);
-        locate_info.set_locate_correct(true);
-        request.mutable_locate_information()->CopyFrom(locate_info);
-
-        message::Store_command_response_msg response;
-        code=Terminal_communication::get_instance_pointer()->store_request(request,response);
-        if(code!=SUCCESS)
-        {
-            std::cout<<code.to_string()<<std::endl;
-        }
-        else
-        {
-            printf("N:%08d\n",n++);
-        }*/
-        //每两秒发送一次
-        usleep(1000*1000);
+        input_queue.push(car_info);
+
+        int in_count=input_queue.size();
+        int out_count=output_queue.size();
+
+        //暂停 5-15s 代替
+        int delay=in_count*200;
+        usleep(1000*delay+1);
         std::this_thread::yield();
     }
 }

+ 1 - 1
tool/threadSafeQueue.h

@@ -68,9 +68,9 @@ public:
 
     bool try_pop(T& value)//������û�ж���Ԫ��ֱ�ӷ���
     {
+        std::lock_guard<std::mutex> lk(mut);
         if (data_queue.empty())
             return false;
-        std::lock_guard<std::mutex> lk(mut);
         value = data_queue.front();
         data_queue.pop();
         return true;