mqtt_communication.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "mqtt_communication.h"
  2. void Tof3DMqttAsyncClient::init(const std::string &file) {
  3. m_message_arrived_callback_ = CloudDataArrived;
  4. MqttAsyncClient::init_from_proto(file);
  5. condit = new Thread_condition();
  6. t = new std::thread(&Tof3DMqttAsyncClient::run, this);
  7. condit->notify_all(true);
  8. }
  9. // 识别结果数据包含识别结果、时间序号、具体识别信息
  10. int Tof3DMqttAsyncClient::CloudDataArrived(void *client, char *topicName, int topicLen,
  11. MQTTAsync_message *message) {
  12. std::string topic = topicName;
  13. if (topic == "tof/seg") {
  14. JetStream::LabelYolo seg_results;
  15. seg_results.ParseFromArray(message->payload, message->payloadlen);
  16. LabelYoloDataBuffer::iter()->lock_exe(&LabelYoloDataBuffer::set, seg_results);
  17. }
  18. return 1;
  19. }
  20. void Tof3DMqttAsyncClient::run() {
  21. std::this_thread::sleep_for(std::chrono::milliseconds(3000));
  22. auto t_start_time = std::chrono::steady_clock::now();
  23. std::chrono::duration<double> cost = std::chrono::steady_clock::now() - t_start_time;
  24. while (condit->is_alive()) {
  25. condit->wait();
  26. cost = std::chrono::steady_clock::now() - t_start_time;
  27. std::this_thread::sleep_for(std::chrono::milliseconds(100 - std::min<int>(99, cost.count() * 1000)));
  28. t_start_time = std::chrono::steady_clock::now();
  29. if (condit->is_alive()) {
  30. static int lable = 0;
  31. if (lable++ > 1000) {
  32. lable = 0;
  33. }
  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. JetStream::LabelImage testimage;
  44. NetMessageTrans::lableMat2Proto(lable, merge_mat, testimage);
  45. int size_1 = testimage.ByteSizeLong();
  46. unsigned char data[size_1];
  47. testimage.SerializeToArray((void *)data, size_1);
  48. SendMessage("tof/ir", data, size_1);
  49. }
  50. }
  51. }