123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //
- // Created by zx on 2020/6/18.
- //
- #include "measure_excutor.h"
- #include "uniq_key.h"
- Measure_excutor::Measure_excutor()
- {
- }
- Measure_excutor::~Measure_excutor()
- {
- }
- Error_manager Measure_excutor::locate_request(message::Measure_request_msg& request,
- message::Measure_response_msg& result,Thread_condition& cancel_condition)
- {
- /*
- * 检查request合法性,以及模块状态
- */
- if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eMeasurer)
- return Error_manager(LOCATER_MSG_REQUEST_INVALID,MINOR_ERROR,"measure request invalid");
- request.set_command_key(create_key());
- if(m_response_table.find(request.command_key())==true)
- return Error_manager(LOCATER_MSG_REQUEST_REPEATED,MAJOR_ERROR," measure reques repeated");
- //设置超时,若没有设置,默认3000
- int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():3000;
- //向测量节点发送测量请求,并记录请求
- Error_manager code;
- Communication_message message;
- message::Base_info base_msg;
- base_msg.set_msg_type(message::eLocate_request_msg);
- base_msg.set_sender(message::eMain);
- base_msg.set_receiver(message::eMeasurer);
- base_msg.set_timeout_ms(timeout);
- message.reset(base_msg,request.SerializeAsString());
- m_response_table[request.command_key()]=message::Measure_response_msg();
- //发送请求
- code= Message_communicator::get_instance_pointer()->send_msg(&message);
- if(code!=SUCCESS)
- {
- m_response_table.erase(request.command_key());
- return code;
- }
- //循环查询请求是否被处理
- auto start_time=std::chrono::system_clock::now();
- double time=0;
- do{
- //查询到记录
- message::Measure_response_msg response;
- ///查询是否存在,并且删除该记录,
- if(m_response_table.find(request.command_key(),response))
- {
- //判断是否接收到回应,若回应信息被赋值则证明有回应
- if (response.has_base_info() && response.has_command_key())
- {
- message::Base_info response_base = response.base_info();
- //检查类型是否匹配
- if (response_base.msg_type() != message::eLocate_response_msg) {
- return Error_manager(LOCATER_MSG_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
- "measure response msg type error");
- }
- //检查基本信息是否匹配
- if (response_base.sender() != message::eMeasurer ||
- response_base.receiver() != message::eMain ||
- !(response.command_key() == request.command_key())) {
- return Error_manager(LOCATER_MSG_RESPONSE_INFO_ERROR, MAJOR_ERROR,
- "measure response msg info error");
- }
- result = response;
- m_response_table.erase(request.command_key());
- return SUCCESS;
- }
- }
- else
- {
- //未查询到记录,任务已经被提前取消,记录被删除
- return Error_manager(TASK_CANCEL,MINOR_ERROR,"measure request canceled");
- }
- auto end_time=std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
- time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
- std::this_thread::yield();
- if(time>double(timeout))
- {
- m_response_table.erase(request.command_key());
- return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"measure request timeout");
- }
- }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
- m_response_table.erase(request.command_key());
- return Error_manager(TASK_CANCEL,MINOR_ERROR,"measure request timeout");
- }
- Error_manager Measure_excutor::check_statu(int terminal_id)
- {
- if(m_measure_statu_msg_map.find(terminal_id)==false || m_statu_recv_time_map.find(terminal_id)==false)
- return Error_manager(FAILED,MINOR_ERROR,"测量节点状态不存在");
- std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
- auto durantion=time_now-m_statu_recv_time_map[terminal_id];
- if(m_measure_statu_msg_map[terminal_id].has_base_info()== false
- || durantion>std::chrono::seconds(5))
- {
- return Error_manager(DISCONNECT,MINOR_ERROR,"测量节点通讯断开");
- }
- if(m_measure_statu_msg_map[terminal_id].laser_manager_status()==message::LASER_MANAGER_FAULT)
- {
- return Error_manager(ERROR,MINOR_ERROR,"测量模块扫描控制故障");
- }
- for(int i=0;i<m_measure_statu_msg_map[terminal_id].laser_statu_vector_size();++i)
- {
- message::Laser_statu laser_statu=m_measure_statu_msg_map[terminal_id].laser_statu_vector(i);
- if(laser_statu==message::LASER_FAULT )
- {
- return Error_manager(ERROR,MINOR_ERROR,"测量模块雷达故障");
- }
- if(laser_statu==message::LASER_DISCONNECT)
- {
- return Error_manager(DISCONNECT,MINOR_ERROR,"测量模块雷达断线");
- }
- }
- return SUCCESS;
- }
- Error_manager Measure_excutor::consume_msg(Communication_message* p_msg)
- {
- if(p_msg== nullptr)
- return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"measure response msg pointer is null");
- //测量response消息
- switch (p_msg->get_message_type())
- {
- ///测量结果反馈消息
- case Communication_message::eLocate_response_msg:
- {
- message::Measure_response_msg response;
- response.ParseFromString(p_msg->get_message_buf());
- ///查询请求表是否存在,并且更新
- if(m_response_table.find_update(response.command_key(),response)==false)
- {
- return Error_manager(LOCATER_MSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"measure response without request");
- }
- break;
- }
- ///测量系统状态
- case Communication_message::eLocate_status_msg:
- {
- message::Measure_status_msg statu_msg;
- if(statu_msg.ParseFromString(p_msg->get_message_buf())==false)
- return Error_manager(ERROR,CRITICAL_ERROR,"measure statu msg parse failed, CRITICAL_ERROR");
- m_measure_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
- m_statu_recv_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
- break;
- }
- }
- return SUCCESS;
- }
- Error_manager Measure_excutor::cancel_request(message::Measure_request_msg& request)
- {
- if(m_response_table.find(request.command_key())==true)
- m_response_table.erase(request.command_key());
- return SUCCESS;
- }
|