main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <fstream>
  4. #include <thread>
  5. // #include "device_factory.hpp"
  6. #include "tool/point_tool.h"
  7. #include "conversion.hpp"
  8. #include "pahoc/mqtt_async.hpp"
  9. // int main(int argc, char** argv) {
  10. // std::fstream txtFile;
  11. // // LidarDevice* lidar = DeviceFactory::createLidar(DeviceFactory::LIDAR_RSLIDAR);
  12. // pcl::shared_ptr<pcl::PointCloud<pcl::PointXYZ>> cloud = ReadTxtCloud("../cloud.txt");
  13. // printf("---Debug Get cloud.txt\n");
  14. // std::string data;
  15. // Conversion::PackStringPointCloud(cloud, data);
  16. // // cloud->clear();
  17. // // Conversion.UnPackStringPointCloud(data, cloud);
  18. // unsigned int length = cloud->size() * sizeof(float) * 3 + 4;
  19. // // unsigned char result[length] = {};
  20. // unsigned char hex_data[length - 4] = {};
  21. // // Conversion.PackHexPointCloud(cloud, result, sizeof(result));
  22. // Conversion::String2Hex(data, hex_data, sizeof(hex_data));
  23. // printf("---Debug length = %d", length);
  24. // printf("---Debug hex_data[0] = %#X ", hex_data[0]);
  25. // printf("---Debug hex_data[1] = %#X ", hex_data[1]);
  26. // printf("---Debug hex_data[2] = %#X\n", hex_data[2]);
  27. // cloud->clear();
  28. // if (!Conversion::UnPackHexPointCloud(hex_data, cloud)) {
  29. // txtFile.open("txtFile", std::ios_base::out | std::ios::app);
  30. // txtFile.write((char*)hex_data, sizeof(hex_data));
  31. // txtFile.close();
  32. // }
  33. // // Conversion.PointCloud(cloud, result, length);
  34. // printf("---Debug End cloud.txt\n");
  35. // return 0;
  36. // }
  37. const std::string address = "tcp://localhost:1883";
  38. // const std::string address = "mqtt://192.178.2.132";
  39. const std::string send_id = "send";
  40. const std::string receive_id = "receive";
  41. const int port = 1883;
  42. const std::string topic = "data/time";
  43. int call_back(void* context, char* topicName, int topicLen, MQTTAsync_message* message) {
  44. std::fstream txtFile;
  45. txtFile.open("txtFile", std::ios_base::out | std::ios::app);
  46. txtFile.write((const char*)message->payload, message->payloadlen);
  47. txtFile.close();
  48. return 1;
  49. }
  50. void send_func() {
  51. MqttAsyncClient client;
  52. client.onConnect(address, send_id, port);
  53. while (true)
  54. {
  55. sleep(1);
  56. pcl::shared_ptr<pcl::PointCloud<pcl::PointXYZ>> cloud = ReadTxtCloud("../cloud.txt");
  57. unsigned int length = cloud->size() * sizeof(float) * 3 + 4;
  58. unsigned char result[length] = {};
  59. Conversion::PackHexPointCloud(cloud, result, sizeof(result));
  60. client.onSendMessage(topic.c_str(), result, sizeof(result));
  61. }
  62. }
  63. void receive_func() {
  64. MqttAsyncClient client;
  65. client.onConnect(address, receive_id, port);
  66. while(client.onSubscribe(topic.c_str()) == false) {
  67. sleep(1);
  68. }
  69. client.setCallback(call_back);
  70. while (true)
  71. {
  72. sleep(1);
  73. }
  74. }
  75. int main(int argc, char** argv) {
  76. printf("Begain.\n");
  77. std::thread send(send_func);
  78. std::thread receive(receive_func);
  79. while (true)
  80. {
  81. sleep(1);
  82. }
  83. }