mqttmsg.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /*
  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. if(strlen(data())<length_)
  75. return false;
  76. std::lock_guard<std::mutex> lock(mutex_);
  77. if(data_== nullptr)
  78. return false;
  79. AGVStatu statu;
  80. util::Status ret;
  81. char* buf=(char*)malloc(length_+1);
  82. memset(buf,0,length_+1);
  83. memcpy(buf,data(),length_);
  84. try
  85. {
  86. ret = util::JsonStringToMessage(buf, &statu);
  87. }
  88. catch (std::exception &e)
  89. {
  90. printf(" exp:%s\n",e.what());
  91. }
  92. free(buf);
  93. if(ret.ok())
  94. {
  95. x=statu.x();
  96. y=statu.y();
  97. theta=statu.theta();
  98. v=statu.v();
  99. vth=statu.vth();
  100. }
  101. return ret.ok();
  102. }
  103. void MqttMsg::fromNavSpeed(const NavMessage::Speed& speed)
  104. {
  105. std::lock_guard<std::mutex> lock(mutex_);
  106. if(data_)
  107. free(data_);
  108. google::protobuf::util::JsonPrintOptions option;
  109. option.always_print_primitive_fields=true;
  110. std::string json_str;
  111. google::protobuf::util::MessageToJsonString(speed,&json_str,option);
  112. length_=json_str.size();
  113. data_=(char*)malloc(length_);
  114. memcpy(data_,json_str.c_str(),length_);
  115. }
  116. bool MqttMsg::toNavSpeed(NavMessage::Speed& speed)
  117. {
  118. std::lock_guard<std::mutex> lock(mutex_);
  119. if(data_== nullptr)
  120. return false;
  121. util::Status ret;
  122. char* buf=(char*)malloc(length_+1);
  123. memset(buf,0,length_+1);
  124. memcpy(buf,data(),length_);
  125. try
  126. {
  127. //printf("%s\n",buf);
  128. ret = util::JsonStringToMessage(buf, &speed);
  129. printf("change speed v:%f, vth:%f\n",speed.v(),speed.vth());
  130. }
  131. catch (std::exception &e)
  132. {
  133. printf(" exp:%s\n",e.what());
  134. }
  135. free(buf);
  136. return ret.ok();
  137. }
  138. void MqttMsg::fromNavCmd(const NavMessage::NavCmd& cmd)
  139. {
  140. std::lock_guard<std::mutex> lock(mutex_);
  141. if(data_)
  142. free(data_);
  143. google::protobuf::util::JsonPrintOptions option;
  144. option.always_print_primitive_fields=true;
  145. std::string json_str;
  146. google::protobuf::util::MessageToJsonString(cmd,&json_str,option);
  147. length_=json_str.length();
  148. data_=(char*)malloc(length_);
  149. memcpy(data_,json_str.c_str(),length_);
  150. }
  151. bool MqttMsg::toNavCmd(NavMessage::NavCmd& cmd)
  152. {
  153. if(strlen(data())<length_)
  154. return false;
  155. std::lock_guard<std::mutex> lock(mutex_);
  156. if(data_== nullptr)
  157. return false;
  158. util::Status ret;
  159. char* buf=(char*)malloc(length_+1);
  160. memset(buf,0,length_+1);
  161. memcpy(buf,data(),length_);
  162. try
  163. {
  164. ret = util::JsonStringToMessage(buf, &cmd);
  165. }
  166. catch (std::exception &e)
  167. {
  168. printf(" exp:%s\n",e.what());
  169. return false;
  170. }
  171. free(buf);
  172. return ret.ok();
  173. }
  174. void MqttMsg::fromNavStatu(const NavMessage::NavStatu &statu)
  175. {
  176. std::lock_guard<std::mutex> lock(mutex_);
  177. if(data_)
  178. free(data_);
  179. google::protobuf::util::JsonPrintOptions option;
  180. option.always_print_primitive_fields=true;
  181. std::string json_str;
  182. google::protobuf::util::MessageToJsonString(statu,&json_str,option);
  183. length_=json_str.length();
  184. data_=(char*)malloc(length_);
  185. memcpy(data_,json_str.c_str(),length_);
  186. }
  187. bool MqttMsg::toNavStatu(NavMessage::NavStatu &statu)
  188. {
  189. if(strlen(data())<length_)
  190. return false;
  191. std::lock_guard<std::mutex> lock(mutex_);
  192. if(data_== nullptr)
  193. return false;
  194. util::Status ret;
  195. char* buf=(char*)malloc(length_+1);
  196. memset(buf,0,length_+1);
  197. memcpy(buf,data(),length_);
  198. try
  199. {
  200. ret = util::JsonStringToMessage(buf, &statu);
  201. }
  202. catch (std::exception &e)
  203. {
  204. printf(" exp:%s\n",e.what());
  205. return false;
  206. }
  207. free(buf);
  208. return ret.ok();
  209. }
  210. */
  211. void MqttMsg::fromProtoMessage(const google::protobuf::Message& message)
  212. {
  213. std::lock_guard<std::mutex> lock(mutex_);
  214. if(data_)
  215. free(data_);
  216. google::protobuf::util::JsonPrintOptions option;
  217. option.always_print_primitive_fields=true;
  218. std::string json_str;
  219. google::protobuf::util::MessageToJsonString(message,&json_str,option);
  220. length_=json_str.size();
  221. data_=(char*)malloc(length_);
  222. memcpy(data_,json_str.c_str(),length_);
  223. }
  224. bool MqttMsg::toProtoMessage(google::protobuf::Message& message) const
  225. {
  226. if(data_== nullptr)
  227. return false;
  228. absl::Status ret;
  229. char* buf=(char*)malloc(length_+1);
  230. memset(buf,0,length_+1);
  231. memcpy(buf,data(),length_);
  232. try
  233. {
  234. ret = util::JsonStringToMessage(buf, &message);
  235. }
  236. catch (std::exception &e)
  237. {
  238. printf(" exp:%s\n",e.what());
  239. }
  240. free(buf);
  241. return ret.ok();
  242. }