123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- //
- // 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)
- {
- LOG(WARNING)<<" request_error: "<<code.to_string()<<std::endl;
- return ERROR;
- }
- if(response.code().error_code()!=0)
- {
- LOG(WARNING)<<" store request response code error: "<<response.code().error_description();
- return ERROR;
- }
- //等待停车完成
- bool last_signal=false;
- message::Storing_process_statu_msg last_msg;
- while(m_exit_cond.wait_for_millisecond(50)==false)
- {
- message::Storing_process_statu_msg msg;
- code = Terminal_communication::get_instance_pointer()->get_storing_statu(car_info.license(), msg);
- //未找到
- if (code != SUCCESS) {
- if(last_signal==true )
- {
- if(last_msg.completed()==true) {
- if (m_thread_safe_output_queue)
- m_thread_safe_output_queue->push(car_info);
- }
- return SUCCESS;
- }
- }
- else
- {
- last_msg=msg;
- last_signal=true;
- }
- }
- return FAILED;
- }
- void store_terminal::storing_thread(store_terminal* pStore)
- {
- while(pStore->m_exit_cond.wait_for_millisecond(1)==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)
- {
- LOG(INFO)<<"车辆:"<<car_info.license().c_str()<<",停车开始---"<<pStore->m_terminal_id<<"---,剩余in:"
- <<pStore->m_thread_safe_queue->size();
- Error_manager code=pStore->storing(car_info);
- LOG_IF(INFO,code==SUCCESS)<<"车辆:"<<car_info.license().c_str()<<",停车结束---"<<pStore->m_terminal_id<<"----------,剩余in:"
- <<pStore->m_thread_safe_queue->size();
- LOG_IF(ERROR,code!=SUCCESS)<<"车辆:"<<car_info.license().c_str()<<",停车结束---"<<pStore->m_terminal_id<<"---------,剩余in:"
- <<pStore->m_thread_safe_queue->size();
- }
- }
- }
|