123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- //
- // Created by huli on 2020/9/16.
- //
- #include "predict_with_network.h"
- #include "../system/system_executor.h"
- #include "../system/system_communication.h"
- Predict_with_network::Predict_with_network()
- {
- }
- Predict_with_network::~Predict_with_network()
- {
- for (auto iter = m_locate_sift_request_msg_map.begin(); iter != m_locate_sift_request_msg_map.end(); ++iter)
- {
- delete(iter->second);
- }
- m_locate_sift_request_msg_map.clear();
- for (auto iter = m_locate_sift_response_msg_map.begin(); iter != m_locate_sift_response_msg_map.end(); ++iter)
- {
- delete(iter->second);
- }
- m_locate_sift_response_msg_map.clear();
- }
- //预测, 发送socket通信, 在服务器上进行点云算法
- //data_in:输入数据,大小为 输入点数*3
- //data_out:输出数据,大小为 输入点数*类别数
- Error_manager Predict_with_network::predict_for_send(int data_id, float* data_in)
- {
- if ( data_in == NULL )
- {
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
- " POINTER IS NULL ");
- }
- Error_manager t_error;
- std::unique_lock<std::mutex> t_lock(m_message_lock);
- //查重, 把原有的清空就好了.
- if ( m_locate_sift_request_msg_map.find(data_id) != m_locate_sift_request_msg_map.end() )
- {
- delete(m_locate_sift_request_msg_map[data_id]);
- m_locate_sift_request_msg_map.erase(data_id);
- }
- if ( m_locate_sift_response_msg_map.find(data_id) != m_locate_sift_response_msg_map.end() )
- {
- delete(m_locate_sift_response_msg_map[data_id]);
- m_locate_sift_response_msg_map.erase(data_id);
- }
- //封装一条消息,
- message::Locate_sift_request_msg* tp_locate_sift_request_msg = new message::Locate_sift_request_msg;
- tp_locate_sift_request_msg->mutable_base_info()->set_msg_type(message::Message_type::eLocate_sift_request_msg);
- tp_locate_sift_request_msg->mutable_base_info()->set_timeout_ms(5000);
- tp_locate_sift_request_msg->mutable_base_info()->set_sender(message::Communicator::eMeasurer);
- tp_locate_sift_request_msg->mutable_base_info()->set_receiver(message::Communicator::eMeasurer_sift_server);
- int t_terminal_id = System_executor::get_instance_references().get_terminal_id();
- tp_locate_sift_request_msg->set_command_key("");
- tp_locate_sift_request_msg->set_terminal_id(t_terminal_id);
- tp_locate_sift_request_msg->set_lidar_id(data_id);
- for (int i = 0; i < PREDICT_CLOUD_SIZE; ++i)
- {
- message::Cloud_coordinate* tp_cloud_coordinate = tp_locate_sift_request_msg->add_cloud_coordinates();
- tp_cloud_coordinate->set_x(data_in[i*3+0]);
- tp_cloud_coordinate->set_y(data_in[i*3+1]);
- tp_cloud_coordinate->set_z(data_in[i*3+2]);
- }
- //发送消息
- std::string t_msg = tp_locate_sift_request_msg->SerializeAsString();
- t_error = System_communication::get_instance_references().encapsulate_msg(t_msg);
- if ( t_error != Error_code::SUCCESS )
- {
- delete(tp_locate_sift_request_msg);
- return t_error;
- }
- else
- {
- //将发送消息保存到map
- //内存的权限转交给map
- m_locate_sift_request_msg_map[data_id] = tp_locate_sift_request_msg;
- }
- return Error_code::SUCCESS;
- }
- //预测, 接受预测结果, 算法时间较长, 请在外部循环接受. (方便中途放弃)
- //data_in:输入数据,大小为 输入点数*3
- //data_out:输出数据,大小为 输入点数*类别数
- //return:返回成功表示数据已经获取到了, 返回 NODATA 表示没有收到数据
- Error_manager Predict_with_network::predict_for_receive(int data_id, int* data_out)
- {
- if ( data_out == NULL )
- {
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
- " POINTER IS NULL ");
- }
- //查询指定的id
- std::unique_lock<std::mutex> t_lock(m_message_lock);
- if ( m_locate_sift_response_msg_map.find(data_id) != m_locate_sift_response_msg_map.end() )
- {
- if ( m_locate_sift_response_msg_map[data_id]->error_manager().error_code() == 0 )
- {
- int t_cloud_size = m_locate_sift_response_msg_map[data_id]->cloud_type_size();
- if ( t_cloud_size == PREDICT_CLOUD_SIZE )
- {
- for (int i = 0; i < PREDICT_CLOUD_SIZE; ++i)
- {
- data_out[i] = m_locate_sift_response_msg_map[data_id]->cloud_type(i).type();
- }
- return Error_code::SUCCESS;
- }
- else
- {
- std::string t_string = m_locate_sift_response_msg_map[data_id]->error_manager().error_description();
- return Error_manager(
- (Error_code)(m_locate_sift_response_msg_map[data_id]->error_manager().error_code()),
- (Error_level)(m_locate_sift_response_msg_map[data_id]->error_manager().error_level()),
- t_string);
- }
- }
- else
- {
- return Error_manager(LOCATER_SIFT_PREDICT_FAILED,MINOR_ERROR,"pointSIFT predict ERROR");
- }
- }
- else
- {
- return Error_manager(Error_code::NODATA, Error_level::NEGLIGIBLE_ERROR,
- " fun error ");
- }
- return Error_code::SUCCESS;
- }
- //签收预测的答复消息, 给通信模块调用, 将收到的预测答复消息, 存入 m_locate_sift_response_msg_map
- Error_manager Predict_with_network::execute_for_predict(std::string& message_string)
- {
- message::Locate_sift_response_msg* tp_locate_sift_response_msg = new message::Locate_sift_response_msg;
- //针对消息类型, 对消息进行二次解析
- if (tp_locate_sift_response_msg->ParseFromString(message_string) )
- {
- std::unique_lock<std::mutex> t_lock(m_message_lock);
- //对比请求和答复消息, 一致性
- int index = tp_locate_sift_response_msg->lidar_id();
- if ( m_locate_sift_request_msg_map.find(index) != m_locate_sift_request_msg_map.end())
- {
- if ( m_locate_sift_request_msg_map[index]->command_key() == tp_locate_sift_response_msg->command_key() &&
- m_locate_sift_request_msg_map[index]->terminal_id() == tp_locate_sift_response_msg->terminal_id() &&
- m_locate_sift_request_msg_map[index]->lidar_id() == tp_locate_sift_response_msg->lidar_id() )
- {
- //校验成功, 直接存入 m_locate_sift_response_msg_map
- if ( m_locate_sift_response_msg_map.find(index) != m_locate_sift_response_msg_map.end() )
- {
- delete(m_locate_sift_response_msg_map[index]);
- }
- //内存的权限转交给map
- m_locate_sift_response_msg_map[index] = tp_locate_sift_response_msg;
- return Error_code::SUCCESS;
- }
- }
- else
- {
- delete(tp_locate_sift_response_msg);
- return Error_manager(Error_code::INVALID_MESSAGE, Error_level::MINOR_ERROR,
- " Predict_with_network::execute_for_predict error ");
- }
- }
- else
- {
- delete(tp_locate_sift_response_msg);
- return Error_manager(Error_code::PARSE_FAILED, Error_level::MINOR_ERROR,
- " Predict_with_network::execute_for_predict error ");
- }
- return Error_code::SUCCESS;
- }
|