mqttmsg.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Created by zx on 23-2-22.
  3. //
  4. #include "mqttmsg.h"
  5. #include <string.h>
  6. #include <google/protobuf/util/json_util.h>
  7. using namespace google::protobuf;
  8. MqttMsg::MqttMsg()
  9. {
  10. data_= nullptr;
  11. length_=0;
  12. }
  13. MqttMsg::MqttMsg(char* data,const int length)
  14. {
  15. data_=(char*)malloc(length);
  16. memcpy(data_,data,length);
  17. length_=length;
  18. }
  19. MqttMsg::MqttMsg(const MqttMsg& msg)
  20. {
  21. std::lock_guard<std::mutex> lock(mutex_);
  22. if(data_)
  23. free(data_);
  24. data_=(char*)malloc(msg.length_);
  25. memcpy(data_,msg.data_,msg.length_);
  26. length_=msg.length_;
  27. }
  28. MqttMsg& MqttMsg::operator=(const MqttMsg& msg)
  29. {
  30. std::lock_guard<std::mutex> lock(mutex_);
  31. if(data_)
  32. free(data_);
  33. data_=(char*)malloc(msg.length_);
  34. memcpy(data_,msg.data_,msg.length_);
  35. length_=msg.length_;
  36. return *this;
  37. }
  38. char* MqttMsg::data()const{
  39. return data_;
  40. }
  41. int MqttMsg::length()const{
  42. return length_;
  43. }
  44. MqttMsg::~MqttMsg(){
  45. if(data_!= nullptr)
  46. {
  47. free(data_);
  48. data_ = nullptr;
  49. }
  50. length_=0;
  51. }
  52. void MqttMsg::fromStatu(double x, double y, double theta, double v, double vth)
  53. {
  54. std::lock_guard<std::mutex> lock(mutex_);
  55. if(data_)
  56. free(data_);
  57. AGVStatu statu;
  58. statu.set_x(x);
  59. statu.set_y(y);
  60. statu.set_theta(theta);
  61. statu.set_v(v);
  62. statu.set_vth(vth);
  63. google::protobuf::util::JsonPrintOptions option;
  64. option.always_print_primitive_fields=true;
  65. std::string json_str;
  66. google::protobuf::util::MessageToJsonString(statu,&json_str,option);
  67. length_=json_str.length();
  68. data_=(char*)malloc(length_);
  69. memcpy(data_,json_str.c_str(),length_);
  70. }
  71. bool MqttMsg::toStatu(double &x, double &y, double &theta, double &v, double &vth)
  72. {
  73. if(strlen(data())<length_)
  74. return false;
  75. std::lock_guard<std::mutex> lock(mutex_);
  76. if(data_== nullptr)
  77. return false;
  78. AGVStatu statu;
  79. util::Status ret;
  80. char* buf=(char*)malloc(length_+1);
  81. memset(buf,0,length_+1);
  82. memcpy(buf,data(),length_);
  83. try
  84. {
  85. ret = util::JsonStringToMessage(buf, &statu);
  86. }
  87. catch (std::exception &e)
  88. {
  89. printf(" exp:%s\n",e.what());
  90. }
  91. free(buf);
  92. if(ret.ok())
  93. {
  94. x=statu.x();
  95. y=statu.y();
  96. theta=statu.theta();
  97. v=statu.v();
  98. vth=statu.vth();
  99. }
  100. return ret.ok();
  101. }
  102. void MqttMsg::fromSpeed(double v, double vth)
  103. {
  104. std::lock_guard<std::mutex> lock(mutex_);
  105. if(data_)
  106. free(data_);
  107. ChangeSpeedCmd cmd;
  108. cmd.set_v(v);
  109. cmd.set_vth(vth);
  110. google::protobuf::util::JsonPrintOptions option;
  111. option.always_print_primitive_fields=true;
  112. std::string json_str;
  113. google::protobuf::util::MessageToJsonString(cmd,&json_str,option);
  114. length_=json_str.size();
  115. data_=(char*)malloc(length_);
  116. memcpy(data_,json_str.c_str(),length_);
  117. }
  118. bool MqttMsg::toSpeed(double &v, double &vth)
  119. {
  120. std::lock_guard<std::mutex> lock(mutex_);
  121. if(data_== nullptr)
  122. return false;
  123. ChangeSpeedCmd cmd;
  124. util::Status ret;
  125. char* buf=(char*)malloc(length_+1);
  126. memset(buf,0,length_+1);
  127. memcpy(buf,data(),length_);
  128. try
  129. {
  130. //printf("%s\n",buf);
  131. ret = util::JsonStringToMessage(buf, &cmd);
  132. printf("change speed v:%f, vth:%f\n",v,vth);
  133. }
  134. catch (std::exception &e)
  135. {
  136. printf(" exp:%s\n",e.what());
  137. }
  138. free(buf);
  139. if(ret.ok())
  140. {
  141. v=cmd.v();
  142. vth=cmd.vth();
  143. }
  144. return ret.ok();
  145. }
  146. void MqttMsg::fromNavCmd(const NavCmd& cmd)
  147. {
  148. std::lock_guard<std::mutex> lock(mutex_);
  149. if(data_)
  150. free(data_);
  151. google::protobuf::util::JsonPrintOptions option;
  152. option.always_print_primitive_fields=true;
  153. std::string json_str;
  154. google::protobuf::util::MessageToJsonString(cmd,&json_str,option);
  155. length_=json_str.length();
  156. data_=(char*)malloc(length_);
  157. memcpy(data_,json_str.c_str(),length_);
  158. }
  159. bool MqttMsg::toNavCmd(NavCmd& cmd)
  160. {
  161. if(strlen(data())<length_)
  162. return false;
  163. std::lock_guard<std::mutex> lock(mutex_);
  164. if(data_== nullptr)
  165. return false;
  166. util::Status ret;
  167. char* buf=(char*)malloc(length_+1);
  168. memset(buf,0,length_+1);
  169. memcpy(buf,data(),length_);
  170. try
  171. {
  172. ret = util::JsonStringToMessage(buf, &cmd);
  173. }
  174. catch (std::exception &e)
  175. {
  176. printf(" exp:%s\n",e.what());
  177. return false;
  178. }
  179. free(buf);
  180. return ret.ok();
  181. }
  182. void MqttMsg::fromNavStatu(const NavStatu &statu)
  183. {
  184. std::lock_guard<std::mutex> lock(mutex_);
  185. if(data_)
  186. free(data_);
  187. google::protobuf::util::JsonPrintOptions option;
  188. option.always_print_primitive_fields=true;
  189. std::string json_str;
  190. google::protobuf::util::MessageToJsonString(statu,&json_str,option);
  191. length_=json_str.length();
  192. data_=(char*)malloc(length_);
  193. memcpy(data_,json_str.c_str(),length_);
  194. }
  195. bool MqttMsg::toNavStatu(NavStatu &statu)
  196. {
  197. if(strlen(data())<length_)
  198. return false;
  199. std::lock_guard<std::mutex> lock(mutex_);
  200. if(data_== nullptr)
  201. return false;
  202. util::Status ret;
  203. char* buf=(char*)malloc(length_+1);
  204. memset(buf,0,length_+1);
  205. memcpy(buf,data(),length_);
  206. try
  207. {
  208. ret = util::JsonStringToMessage(buf, &statu);
  209. }
  210. catch (std::exception &e)
  211. {
  212. printf(" exp:%s\n",e.what());
  213. return false;
  214. }
  215. free(buf);
  216. return ret.ok();
  217. }