mqtt_communication.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "mqtt_communication.h"
  2. #include "tool/time.hpp"
  3. void Tof3DMqttAsyncClient::init(const std::string &file) {
  4. m_message_arrived_callback_ = CloudDataArrived;
  5. MqttAsyncClient::init_from_proto(file);
  6. condit = new Thread_condition();
  7. t = new std::thread(&Tof3DMqttAsyncClient::run, this);
  8. condit->notify_all(true);
  9. }
  10. // 识别结果数据包含识别结果、时间序号、具体识别信息
  11. int Tof3DMqttAsyncClient::CloudDataArrived(void *client, char *topicName, int topicLen,
  12. MQTTAsync_message *message) {
  13. std::string topic = topicName;
  14. if (topic == "tof/seg") {
  15. JetStream::LabelYolo seg_results;
  16. seg_results.ParseFromArray(message->payload, message->payloadlen);
  17. LabelYoloDataBuffer::iter()->lock_exe(&LabelYoloDataBuffer::set, seg_results);
  18. int lable = TimeTool::timeDropoutLL(TimeTool::TemporalPrecision::TemporalPrecisionMs, TimeTool::TemporalPrecision::TemporalPrecisionHour);
  19. // LOG(INFO) << "after " << (lable - seg_results.label()) << "ms";
  20. }
  21. return 1;
  22. }
  23. void Tof3DMqttAsyncClient::run() {
  24. std::this_thread::sleep_for(std::chrono::milliseconds(3000));
  25. auto t_start_time = std::chrono::steady_clock::now();
  26. std::chrono::duration<double> cost = std::chrono::steady_clock::now() - t_start_time;
  27. while (condit->is_alive()) {
  28. condit->wait();
  29. cost = std::chrono::steady_clock::now() - t_start_time;
  30. std::this_thread::sleep_for(std::chrono::milliseconds(100 - std::min<int>(99, cost.count() * 1000)));
  31. t_start_time = std::chrono::steady_clock::now();
  32. if (condit->is_alive()) {
  33. int lable = TimeTool::timeDropoutLL(TimeTool::TemporalPrecision::TemporalPrecisionMs, TimeTool::TemporalPrecision::TemporalPrecisionHour);
  34. // 1、获取图片并合并
  35. cv::Mat merge_mat = cv::Mat::zeros(480 * 2, 640 * 2, CV_8UC1);
  36. DeviceTof3D::DeviceTof3DSaveInfo devices_info[DeviceAzimuth::DEVICE_AZIMUTH_MAX];
  37. for (int device_index = 0; device_index < DeviceAzimuth::DEVICE_AZIMUTH_MAX; device_index++) {
  38. devices_info[device_index] = DeviceTof3D::iter()->getDeviceSaveInfo((DeviceAzimuth) device_index);
  39. MatDataBuffer::iter()->lock_exe(&MatDataBuffer::set, device_index, lable, devices_info[device_index].pointMat);
  40. devices_info[device_index].irMat.copyTo(merge_mat(cv::Rect((device_index & 0x1) * 640,
  41. ((device_index & 0x2) >> 1) * 480, 640, 480)));
  42. }
  43. MatDataBuffer::iter()->lock_exe(&MatDataBuffer::setIr, lable, merge_mat);
  44. // cv::imwrite(ETC_PATH PROJECT_NAME "/failed/merge_mat_" + std::to_string(lable) + ".jpg", merge_mat);
  45. JetStream::LabelImage testimage;
  46. NetMessageTrans::lableMat2Proto(lable, merge_mat, testimage, true);
  47. int size_1 = testimage.ByteSizeLong();
  48. unsigned char data[size_1];
  49. testimage.SerializeToArray((void *)data, size_1);
  50. SendMessage("tof/ir", data, size_1);
  51. // int test_lable = 0;
  52. // cv::Mat pic;
  53. // NetMessageTrans::Proto2lableMat(testimage, test_lable, pic);
  54. // cv::imshow("pic", pic);
  55. // cv::waitKey(1);
  56. }
  57. }
  58. }