predict_with_network.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // Created by huli on 2020/9/16.
  3. //
  4. #include "predict_with_network.h"
  5. #include "../system/system_executor.h"
  6. #include "../system/system_communication.h"
  7. Predict_with_network::Predict_with_network()
  8. {
  9. }
  10. Predict_with_network::~Predict_with_network()
  11. {
  12. for (auto iter = m_locate_sift_request_msg_map.begin(); iter != m_locate_sift_request_msg_map.end(); ++iter)
  13. {
  14. delete(iter->second);
  15. }
  16. m_locate_sift_request_msg_map.clear();
  17. for (auto iter = m_locate_sift_response_msg_map.begin(); iter != m_locate_sift_response_msg_map.end(); ++iter)
  18. {
  19. delete(iter->second);
  20. }
  21. m_locate_sift_response_msg_map.clear();
  22. }
  23. //预测, 发送socket通信, 在服务器上进行点云算法
  24. //data_in:输入数据,大小为 输入点数*3
  25. //data_out:输出数据,大小为 输入点数*类别数
  26. Error_manager Predict_with_network::predict_for_send(int data_id, float* data_in)
  27. {
  28. if ( data_in == NULL )
  29. {
  30. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  31. " POINTER IS NULL ");
  32. }
  33. Error_manager t_error;
  34. std::unique_lock<std::mutex> t_lock(m_message_lock);
  35. //查重, 把原有的清空就好了.
  36. if ( m_locate_sift_request_msg_map.find(data_id) != m_locate_sift_request_msg_map.end() )
  37. {
  38. delete(m_locate_sift_request_msg_map[data_id]);
  39. m_locate_sift_request_msg_map.erase(data_id);
  40. }
  41. if ( m_locate_sift_response_msg_map.find(data_id) != m_locate_sift_response_msg_map.end() )
  42. {
  43. delete(m_locate_sift_response_msg_map[data_id]);
  44. m_locate_sift_response_msg_map.erase(data_id);
  45. }
  46. //封装一条消息,
  47. message::Locate_sift_request_msg* tp_locate_sift_request_msg = new message::Locate_sift_request_msg;
  48. tp_locate_sift_request_msg->mutable_base_info()->set_msg_type(message::Message_type::eLocate_sift_request_msg);
  49. tp_locate_sift_request_msg->mutable_base_info()->set_timeout_ms(5000);
  50. tp_locate_sift_request_msg->mutable_base_info()->set_sender(message::Communicator::eMeasurer);
  51. tp_locate_sift_request_msg->mutable_base_info()->set_receiver(message::Communicator::eMeasurer_sift_server);
  52. int t_terminal_id = System_executor::get_instance_references().get_terminal_id();
  53. tp_locate_sift_request_msg->set_command_key("");
  54. tp_locate_sift_request_msg->set_terminal_id(t_terminal_id);
  55. tp_locate_sift_request_msg->set_lidar_id(data_id);
  56. for (int i = 0; i < PREDICT_CLOUD_SIZE; ++i)
  57. {
  58. message::Cloud_coordinate* tp_cloud_coordinate = tp_locate_sift_request_msg->add_cloud_coordinates();
  59. tp_cloud_coordinate->set_x(data_in[i*3+0]);
  60. tp_cloud_coordinate->set_y(data_in[i*3+1]);
  61. tp_cloud_coordinate->set_z(data_in[i*3+2]);
  62. }
  63. //发送消息
  64. std::string t_msg = tp_locate_sift_request_msg->SerializeAsString();
  65. t_error = System_communication::get_instance_references().encapsulate_msg(t_msg);
  66. if ( t_error != Error_code::SUCCESS )
  67. {
  68. delete(tp_locate_sift_request_msg);
  69. return t_error;
  70. }
  71. else
  72. {
  73. //将发送消息保存到map
  74. //内存的权限转交给map
  75. m_locate_sift_request_msg_map[data_id] = tp_locate_sift_request_msg;
  76. }
  77. return Error_code::SUCCESS;
  78. }
  79. //预测, 接受预测结果, 算法时间较长, 请在外部循环接受. (方便中途放弃)
  80. //data_in:输入数据,大小为 输入点数*3
  81. //data_out:输出数据,大小为 输入点数*类别数
  82. //return:返回成功表示数据已经获取到了, 返回 NODATA 表示没有收到数据
  83. Error_manager Predict_with_network::predict_for_receive(int data_id, int* data_out)
  84. {
  85. if ( data_out == NULL )
  86. {
  87. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  88. " POINTER IS NULL ");
  89. }
  90. //查询指定的id
  91. std::unique_lock<std::mutex> t_lock(m_message_lock);
  92. if ( m_locate_sift_response_msg_map.find(data_id) != m_locate_sift_response_msg_map.end() )
  93. {
  94. if ( m_locate_sift_response_msg_map[data_id]->error_manager().error_code() == 0 )
  95. {
  96. int t_cloud_size = m_locate_sift_response_msg_map[data_id]->cloud_type_size();
  97. if ( t_cloud_size == PREDICT_CLOUD_SIZE )
  98. {
  99. for (int i = 0; i < PREDICT_CLOUD_SIZE; ++i)
  100. {
  101. data_out[i] = m_locate_sift_response_msg_map[data_id]->cloud_type(i).type();
  102. }
  103. return Error_code::SUCCESS;
  104. }
  105. else
  106. {
  107. std::string t_string = m_locate_sift_response_msg_map[data_id]->error_manager().error_description();
  108. return Error_manager(
  109. (Error_code)(m_locate_sift_response_msg_map[data_id]->error_manager().error_code()),
  110. (Error_level)(m_locate_sift_response_msg_map[data_id]->error_manager().error_level()),
  111. t_string);
  112. }
  113. }
  114. else
  115. {
  116. return Error_manager(LOCATER_SIFT_PREDICT_FAILED,MINOR_ERROR,"pointSIFT predict ERROR");
  117. }
  118. }
  119. else
  120. {
  121. return Error_manager(Error_code::NODATA, Error_level::NEGLIGIBLE_ERROR,
  122. " fun error ");
  123. }
  124. return Error_code::SUCCESS;
  125. }
  126. //签收预测的答复消息, 给通信模块调用, 将收到的预测答复消息, 存入 m_locate_sift_response_msg_map
  127. Error_manager Predict_with_network::execute_for_predict(std::string& message_string)
  128. {
  129. message::Locate_sift_response_msg* tp_locate_sift_response_msg = new message::Locate_sift_response_msg;
  130. //针对消息类型, 对消息进行二次解析
  131. if (tp_locate_sift_response_msg->ParseFromString(message_string) )
  132. {
  133. std::unique_lock<std::mutex> t_lock(m_message_lock);
  134. //对比请求和答复消息, 一致性
  135. int index = tp_locate_sift_response_msg->lidar_id();
  136. if ( m_locate_sift_request_msg_map.find(index) != m_locate_sift_request_msg_map.end())
  137. {
  138. if ( m_locate_sift_request_msg_map[index]->command_key() == tp_locate_sift_response_msg->command_key() &&
  139. m_locate_sift_request_msg_map[index]->terminal_id() == tp_locate_sift_response_msg->terminal_id() &&
  140. m_locate_sift_request_msg_map[index]->lidar_id() == tp_locate_sift_response_msg->lidar_id() )
  141. {
  142. //校验成功, 直接存入 m_locate_sift_response_msg_map
  143. if ( m_locate_sift_response_msg_map.find(index) != m_locate_sift_response_msg_map.end() )
  144. {
  145. delete(m_locate_sift_response_msg_map[index]);
  146. }
  147. //内存的权限转交给map
  148. m_locate_sift_response_msg_map[index] = tp_locate_sift_response_msg;
  149. return Error_code::SUCCESS;
  150. }
  151. }
  152. else
  153. {
  154. delete(tp_locate_sift_response_msg);
  155. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::MINOR_ERROR,
  156. " Predict_with_network::execute_for_predict error ");
  157. }
  158. }
  159. else
  160. {
  161. delete(tp_locate_sift_response_msg);
  162. return Error_manager(Error_code::PARSE_FAILED, Error_level::MINOR_ERROR,
  163. " Predict_with_network::execute_for_predict error ");
  164. }
  165. return Error_code::SUCCESS;
  166. }