mqttmsg.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Created by zx on 23-2-22.
  3. //
  4. #include "mqttmsg.h"
  5. #include <string.h>
  6. #include "message.pb.h"
  7. #include <google/protobuf/util/json_util.h>
  8. using namespace google::protobuf;
  9. MqttMsg::MqttMsg()
  10. {
  11. data_= nullptr;
  12. length_=0;
  13. }
  14. MqttMsg::MqttMsg(char* data,const int length)
  15. {
  16. data_=(char*)malloc(length);
  17. memcpy(data_,data,length);
  18. length_=length;
  19. }
  20. MqttMsg::MqttMsg(const MqttMsg& msg)
  21. {
  22. std::lock_guard<std::mutex> lock(mutex_);
  23. if(data_)
  24. free(data_);
  25. data_=(char*)malloc(msg.length_);
  26. memcpy(data_,msg.data_,msg.length_);
  27. length_=msg.length_;
  28. }
  29. MqttMsg& MqttMsg::operator=(const MqttMsg& msg)
  30. {
  31. std::lock_guard<std::mutex> lock(mutex_);
  32. if(data_)
  33. free(data_);
  34. data_=(char*)malloc(msg.length_);
  35. memcpy(data_,msg.data_,msg.length_);
  36. length_=msg.length_;
  37. return *this;
  38. }
  39. char* MqttMsg::data()const{
  40. return data_;
  41. }
  42. int MqttMsg::length()const{
  43. return length_;
  44. }
  45. MqttMsg::~MqttMsg(){
  46. if(data_!= nullptr)
  47. {
  48. free(data_);
  49. data_ = nullptr;
  50. }
  51. length_=0;
  52. }
  53. void MqttMsg::fromStatu(double x, double y, double theta, double v, double vth)
  54. {
  55. std::lock_guard<std::mutex> lock(mutex_);
  56. if(data_)
  57. free(data_);
  58. AGVStatu statu;
  59. statu.set_x(x);
  60. statu.set_y(y);
  61. statu.set_theta(theta);
  62. statu.set_v(v);
  63. statu.set_vth(vth);
  64. google::protobuf::util::JsonPrintOptions option;
  65. option.always_print_primitive_fields=true;
  66. std::string json_str;
  67. google::protobuf::util::MessageToJsonString(statu,&json_str,option);
  68. length_=json_str.length()+1;
  69. data_=(char*)malloc(length_);
  70. sprintf(data_,"%s",json_str.c_str());
  71. //memcpy(data_,json_str.c_str(),length_-1);
  72. data_[length_-1]=0;
  73. }
  74. bool MqttMsg::toStatu(double &x, double &y, double &theta, double &v, double &vth)
  75. {
  76. std::lock_guard<std::mutex> lock(mutex_);
  77. if(data_== nullptr)
  78. return false;
  79. AGVStatu statu;
  80. util::Status ret;
  81. try
  82. {
  83. ret = util::JsonStringToMessage(data(), &statu);
  84. }
  85. catch (std::exception &e)
  86. {
  87. printf(" exp:%s\n",e.what());
  88. }
  89. if(ret.ok())
  90. {
  91. x=statu.x();
  92. y=statu.y();
  93. theta=statu.theta();
  94. v=statu.v();
  95. vth=statu.vth();
  96. }
  97. return ret.ok();
  98. }
  99. void MqttMsg::fromSpeed(double v, double vth)
  100. {
  101. std::lock_guard<std::mutex> lock(mutex_);
  102. if(data_)
  103. free(data_);
  104. ChangeSpeedCmd cmd;
  105. cmd.set_v(v);
  106. cmd.set_vth(vth);
  107. google::protobuf::util::JsonPrintOptions option;
  108. option.always_print_primitive_fields=true;
  109. std::string json_str;
  110. google::protobuf::util::MessageToJsonString(cmd,&json_str,option);
  111. length_=json_str.size()+1;
  112. data_=(char*)malloc(length_);
  113. //sprintf(data_,"%s",json_str.c_str());
  114. memcpy(data_,json_str.c_str(),length_);
  115. data_[length_-1]='\0';
  116. }
  117. bool MqttMsg::toSpeed(double &v, double &vth)
  118. {
  119. std::lock_guard<std::mutex> lock(mutex_);
  120. if(data_== nullptr)
  121. return false;
  122. ChangeSpeedCmd cmd;
  123. util::Status ret;
  124. try
  125. {
  126. printf("%s\n",data());
  127. ret = util::JsonStringToMessage(data(), &cmd);
  128. printf("v:%f, vth:%f\n",v,vth);
  129. }
  130. catch (std::exception &e)
  131. {
  132. printf(" exp:%s\n",e.what());
  133. }
  134. if(ret.ok())
  135. {
  136. v=cmd.v();
  137. vth=cmd.vth();
  138. }
  139. return ret.ok();
  140. }