measure_excutor.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Created by zx on 2020/6/18.
  3. //
  4. #include "measure_excutor.h"
  5. #include "uniq_key.h"
  6. Measure_excutor::Measure_excutor()
  7. {
  8. }
  9. Measure_excutor::~Measure_excutor()
  10. {
  11. }
  12. Error_manager Measure_excutor::locate_request(message::Measure_request_msg& request,
  13. message::Measure_response_msg& result,Thread_condition& cancel_condition)
  14. {
  15. /*
  16. * 检查request合法性,以及模块状态
  17. */
  18. if(request.base_info().sender()!=message::eMain||request.base_info().receiver()!=message::eMeasurer)
  19. return Error_manager(LOCATER_MSG_REQUEST_INVALID,MINOR_ERROR,"measure request invalid");
  20. request.set_command_key(create_key());
  21. if(m_response_table.find(request.command_key())==true)
  22. return Error_manager(LOCATER_MSG_REQUEST_REPEATED,MAJOR_ERROR," measure reques repeated");
  23. //设置超时,若没有设置,默认3000
  24. int timeout=request.base_info().has_timeout_ms()?request.base_info().timeout_ms():3000;
  25. //向测量节点发送测量请求,并记录请求
  26. Error_manager code;
  27. Communication_message message;
  28. message::Base_info base_msg;
  29. base_msg.set_msg_type(message::eLocate_request_msg);
  30. base_msg.set_sender(message::eMain);
  31. base_msg.set_receiver(message::eMeasurer);
  32. base_msg.set_timeout_ms(timeout);
  33. message.reset(base_msg,request.SerializeAsString());
  34. m_response_table[request.command_key()]=message::Measure_response_msg();
  35. //发送请求
  36. code= Message_communicator::get_instance_pointer()->send_msg(&message);
  37. if(code!=SUCCESS)
  38. {
  39. m_response_table.erase(request.command_key());
  40. return code;
  41. }
  42. //循环查询请求是否被处理
  43. auto start_time=std::chrono::system_clock::now();
  44. double time=0;
  45. do{
  46. //查询到记录
  47. message::Measure_response_msg response;
  48. ///查询是否存在,并且删除该记录,
  49. if(m_response_table.find(request.command_key(),response))
  50. {
  51. //判断是否接收到回应,若回应信息被赋值则证明有回应
  52. if (response.has_base_info() && response.has_command_key())
  53. {
  54. message::Base_info response_base = response.base_info();
  55. //检查类型是否匹配
  56. if (response_base.msg_type() != message::eLocate_response_msg) {
  57. return Error_manager(LOCATER_MSG_RESPONSE_TYPE_ERROR, MAJOR_ERROR,
  58. "measure response msg type error");
  59. }
  60. //检查基本信息是否匹配
  61. if (response_base.sender() != message::eMeasurer ||
  62. response_base.receiver() != message::eMain ||
  63. !(response.command_key() == request.command_key())) {
  64. return Error_manager(LOCATER_MSG_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  65. "measure response msg info error");
  66. }
  67. result = response;
  68. m_response_table.erase(request.command_key());
  69. return SUCCESS;
  70. }
  71. }
  72. else
  73. {
  74. //未查询到记录,任务已经被提前取消,记录被删除
  75. return Error_manager(TASK_CANCEL,MINOR_ERROR,"measure request canceled");
  76. }
  77. auto end_time=std::chrono::system_clock::now();
  78. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  79. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  80. std::this_thread::yield();
  81. if(time>double(timeout))
  82. {
  83. m_response_table.erase(request.command_key());
  84. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"measure request timeout");
  85. }
  86. }while(cancel_condition.wait_for_ex(std::chrono::milliseconds(1))==false);
  87. m_response_table.erase(request.command_key());
  88. return Error_manager(TASK_CANCEL,MINOR_ERROR,"measure request timeout");
  89. }
  90. Error_manager Measure_excutor::check_statu(int terminal_id)
  91. {
  92. if(m_measure_statu_msg_map.find(terminal_id)==false || m_statu_recv_time_map.find(terminal_id)==false)
  93. return Error_manager(FAILED,MINOR_ERROR,"测量节点状态不存在");
  94. std::chrono::system_clock::time_point time_now=std::chrono::system_clock::now();
  95. auto durantion=time_now-m_statu_recv_time_map[terminal_id];
  96. if(m_measure_statu_msg_map[terminal_id].has_base_info()== false
  97. || durantion>std::chrono::seconds(5))
  98. {
  99. return Error_manager(DISCONNECT,MINOR_ERROR,"测量节点通讯断开");
  100. }
  101. if(m_measure_statu_msg_map[terminal_id].laser_manager_status()==message::LASER_MANAGER_FAULT)
  102. {
  103. return Error_manager(ERROR,MINOR_ERROR,"测量模块扫描控制故障");
  104. }
  105. for(int i=0;i<m_measure_statu_msg_map[terminal_id].laser_statu_vector_size();++i)
  106. {
  107. message::Laser_statu laser_statu=m_measure_statu_msg_map[terminal_id].laser_statu_vector(i);
  108. if(laser_statu==message::LASER_FAULT )
  109. {
  110. return Error_manager(ERROR,MINOR_ERROR,"测量模块雷达故障");
  111. }
  112. if(laser_statu==message::LASER_DISCONNECT)
  113. {
  114. return Error_manager(DISCONNECT,MINOR_ERROR,"测量模块雷达断线");
  115. }
  116. }
  117. return SUCCESS;
  118. }
  119. Error_manager Measure_excutor::consume_msg(Communication_message* p_msg)
  120. {
  121. if(p_msg== nullptr)
  122. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"measure response msg pointer is null");
  123. //测量response消息
  124. switch (p_msg->get_message_type())
  125. {
  126. ///测量结果反馈消息
  127. case Communication_message::eLocate_response_msg:
  128. {
  129. message::Measure_response_msg response;
  130. response.ParseFromString(p_msg->get_message_buf());
  131. ///查询请求表是否存在,并且更新
  132. if(m_response_table.find_update(response.command_key(),response)==false)
  133. {
  134. return Error_manager(LOCATER_MSG_RESPONSE_HAS_NO_REQUEST,NEGLIGIBLE_ERROR,"measure response without request");
  135. }
  136. break;
  137. }
  138. ///测量系统状态
  139. case Communication_message::eLocate_status_msg:
  140. {
  141. message::Measure_status_msg statu_msg;
  142. if(statu_msg.ParseFromString(p_msg->get_message_buf())==false)
  143. return Error_manager(ERROR,CRITICAL_ERROR,"measure statu msg parse failed, CRITICAL_ERROR");
  144. m_measure_statu_msg_map[statu_msg.terminal_id()]=statu_msg;
  145. m_statu_recv_time_map[statu_msg.terminal_id()]=std::chrono::system_clock::now();
  146. break;
  147. }
  148. }
  149. return SUCCESS;
  150. }
  151. Error_manager Measure_excutor::cancel_request(message::Measure_request_msg& request)
  152. {
  153. if(m_response_table.find(request.command_key())==true)
  154. m_response_table.erase(request.command_key());
  155. return SUCCESS;
  156. }