mqttmsg.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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();
  69. data_=(char*)malloc(length_);
  70. memcpy(data_,json_str.c_str(),length_);
  71. }
  72. bool MqttMsg::toStatu(double &x, double &y, double &theta, double &v, double &vth)
  73. {
  74. std::lock_guard<std::mutex> lock(mutex_);
  75. if(data_== nullptr)
  76. return false;
  77. AGVStatu statu;
  78. util::Status ret;
  79. char* buf=(char*)malloc(length_+1);
  80. memset(buf,0,length_+1);
  81. memcpy(buf,data(),length_);
  82. try
  83. {
  84. ret = util::JsonStringToMessage(buf, &statu);
  85. }
  86. catch (std::exception &e)
  87. {
  88. printf(" exp:%s\n",e.what());
  89. }
  90. free(buf);
  91. if(ret.ok())
  92. {
  93. x=statu.x();
  94. y=statu.y();
  95. theta=statu.theta();
  96. v=statu.v();
  97. vth=statu.vth();
  98. }
  99. return ret.ok();
  100. }
  101. void MqttMsg::fromSpeed(double v, double vth)
  102. {
  103. std::lock_guard<std::mutex> lock(mutex_);
  104. if(data_)
  105. free(data_);
  106. ChangeSpeedCmd cmd;
  107. cmd.set_v(v);
  108. cmd.set_vth(vth);
  109. google::protobuf::util::JsonPrintOptions option;
  110. option.always_print_primitive_fields=true;
  111. std::string json_str;
  112. google::protobuf::util::MessageToJsonString(cmd,&json_str,option);
  113. length_=json_str.size();
  114. data_=(char*)malloc(length_);
  115. memcpy(data_,json_str.c_str(),length_);
  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. char* buf=(char*)malloc(length_+1);
  125. memset(buf,0,length_+1);
  126. memcpy(buf,data(),length_);
  127. try
  128. {
  129. //printf("%s\n",buf);
  130. ret = util::JsonStringToMessage(buf, &cmd);
  131. printf("change speed v:%f, vth:%f\n",v,vth);
  132. }
  133. catch (std::exception &e)
  134. {
  135. printf(" exp:%s\n",e.what());
  136. }
  137. free(buf);
  138. if(ret.ok())
  139. {
  140. v=cmd.v();
  141. vth=cmd.vth();
  142. }
  143. return ret.ok();
  144. }