common_tool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #ifndef COMMON_TOOL_H
  2. #define COMMON_TOOL_H
  3. #include <chrono>
  4. #include <string>
  5. #include <sys/time.h>
  6. #include <rapidjson/document.h>
  7. #include <rapidjson/writer.h>
  8. #include <rapidjson/stringbuffer.h>
  9. #include <rapidjson/prettywriter.h>
  10. #include <boost/function.hpp>
  11. #include <boost/bind.hpp>
  12. #include <boost/algorithm/string.hpp>
  13. #include "websocket/sio/sio_client.h"
  14. #include "common.h"
  15. class tool_other
  16. {
  17. public:
  18. static double round(double dVal, int iPlaces)
  19. {
  20. double dRetval;
  21. double dMod = 0.0000001;
  22. if (dVal<0.0) dMod=-0.0000001;
  23. dRetval=dVal;
  24. dRetval+=(5.0/pow(10.0,iPlaces+1.0));
  25. dRetval*=pow(10.0,iPlaces);
  26. dRetval=floor(dRetval+dMod);
  27. dRetval/=pow(10.0,iPlaces);
  28. return(dRetval);
  29. }
  30. static std::string to13str(std::string& str)
  31. {
  32. if(str.size()<13)
  33. str.insert(0,13-str.size(),'0');
  34. return str;
  35. }
  36. static std::string to_cid(int ctype, int cid)
  37. {
  38. char buf[20] = {0};
  39. sprintf(buf, "%.3d%.10d", ctype, cid);
  40. return std::string(buf);
  41. }
  42. static std::string to13str(uint64_t data)
  43. {
  44. char ss[20]={0};
  45. sprintf(ss, "%013ld", data);
  46. return std::string(ss);
  47. }
  48. static uint32_t id64_to_id(std::string& str)
  49. {
  50. return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
  51. }
  52. static int id64_to_type(std::string& str)
  53. {
  54. return std::stoi(to13str(str).substr(0, 3));
  55. }
  56. static int card_id_to_type(const std::string &cardid)
  57. {
  58. return std::stoi(cardid.substr(0,3));
  59. }
  60. static int card_id_to_id(const std::string &cardid)
  61. {
  62. return atoi(cardid.substr(3).c_str());
  63. }
  64. static std::string type_id_to_str(int32_t type,uint32_t id)
  65. {
  66. char sql[15] = {0};
  67. snprintf(sql, 15,"%03d%010d", type, id);
  68. return std::string(sql);
  69. }
  70. static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
  71. {
  72. return type<<32|id;
  73. }
  74. static bool is_person(int32_t type)
  75. {
  76. return CT_PERSON == type;
  77. }
  78. static bool is_vehicle(int32_t type)
  79. {
  80. return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  81. }
  82. static std::string get_string_cardid(uint64_t id)
  83. {
  84. uint64_t type = id>>32;
  85. uint32_t cid = id & (~(type<<32));
  86. return type_id_to_str(type,cid);
  87. }
  88. static uint64_t card_id_to_u64(const std::string & cardid)
  89. {
  90. return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
  91. }
  92. static bool is_coal(int32_t type,int32_t vtype)
  93. {
  94. return CT_COAL_CUTTER == type || vtype==VT_COAL_CUTTER;
  95. }
  96. static bool is_driving(int32_t type,int32_t vtype)
  97. {
  98. return CT_HEADING_MACHINE == type || vtype==VT_HEADING_MACHINE;
  99. }
  100. static bool is_coal_or_driving(int32_t type,int32_t vtype)
  101. {
  102. return is_coal(type,vtype) || is_driving(type,vtype);
  103. }
  104. static double get_pdoa(float poa[], const double& offset)
  105. {
  106. if(poa == nullptr)
  107. {
  108. return -10.0;
  109. }
  110. float pdoa = poa[1] - poa[0] - offset;
  111. while(pdoa >= TPI){
  112. pdoa -= TPI;
  113. }
  114. while(pdoa < 0){
  115. pdoa += TPI;
  116. }
  117. pdoa -= PI;
  118. return pdoa;
  119. }
  120. template<class T>
  121. static void memcpy_int(std::vector<char>& msg, T val)
  122. {
  123. for(int i = sizeof(T)-1; i >=0; --i){
  124. msg.push_back((val>>(i*8))&0xFF);
  125. }
  126. }
  127. /*
  128. * @brief
  129. * @param
  130. * @return
  131. * @note
  132. * @warning
  133. * @bug
  134. * */
  135. static std::string replace(std::string val, std::string d, std::string n)
  136. {
  137. boost::replace_all(val, d, n);
  138. return val;
  139. }
  140. };
  141. class tool_map
  142. {
  143. public:
  144. static bool try_get_value(sio::message::ptr& out_data,
  145. const char* key, sio::message::ptr const& data)
  146. {
  147. auto map=data->get_map()[key];
  148. if(map && sio::message::flag_object == map->get_flag())
  149. {
  150. out_data = map;
  151. return true;
  152. }
  153. return false;
  154. }
  155. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  156. {
  157. auto map=data->get_map()[key];
  158. if(map && sio::message::flag_integer == map->get_flag())
  159. {
  160. out_data = map->get_int();
  161. return true;
  162. }
  163. return false;
  164. }
  165. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  166. {
  167. auto map=data->get_map()[key];
  168. if(map && sio::message::flag_integer == map->get_flag())
  169. {
  170. out_data = static_cast<uint32_t>(map->get_int());
  171. return true;
  172. }
  173. return false;
  174. }
  175. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  176. {
  177. auto map=data->get_map()[key];
  178. if(map && sio::message::flag_integer == map->get_flag())
  179. {
  180. out_data = static_cast<int>(map->get_int());
  181. return true;
  182. }
  183. return false;
  184. }
  185. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  186. {
  187. auto map=data->get_map()[key];
  188. if(map && sio::message::flag_string == map->get_flag())
  189. {
  190. out_data = map->get_string();
  191. return true;
  192. }
  193. return false;
  194. }
  195. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  196. {
  197. auto map=data->get_map()[key];
  198. if(map && sio::message::flag_double == map->get_flag())
  199. {
  200. out_data = map->get_double();
  201. return true;
  202. }
  203. return false;
  204. }
  205. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  206. const char* key, sio::message::ptr const& data)
  207. {
  208. auto map=data->get_map()[key];
  209. if(map && sio::message::flag_array == map->get_flag())
  210. {
  211. out_data = map->get_vector();
  212. return true;
  213. }
  214. return false;
  215. }
  216. };
  217. class tool_json
  218. {
  219. public:
  220. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  221. rapidjson::Document::AllocatorType& allocator)
  222. {
  223. rapidjson::Value name;
  224. name.SetString(key, allocator);
  225. rapidjson::Value data;
  226. data.SetString(value.c_str(), allocator);
  227. out_data.AddMember(name, data, allocator);
  228. }
  229. static void push_back(rapidjson::Value& out_data, std::string value,
  230. rapidjson::Document::AllocatorType& allocator)
  231. {
  232. rapidjson::Value data;
  233. data.SetString(value.c_str(), allocator);
  234. out_data.PushBack(data, allocator);
  235. }
  236. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  237. rapidjson::Value::ConstMemberIterator& out_iter)
  238. {
  239. if(node.IsObject())
  240. {
  241. out_iter = node.FindMember(key);
  242. if(node.MemberEnd() == out_iter)
  243. {
  244. return false;
  245. }
  246. return true;
  247. }
  248. return false;
  249. }
  250. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  251. {
  252. rapidjson::Value::ConstMemberIterator iter;
  253. if(try_get_iter(key, node, iter))
  254. {
  255. if(iter->value.IsInt())
  256. {
  257. d = iter->value.GetInt();
  258. return true;
  259. }
  260. }
  261. return false;
  262. }
  263. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  264. {
  265. rapidjson::Value::ConstMemberIterator iter;
  266. if(try_get_iter(key, node, iter))
  267. {
  268. if(iter->value.IsUint64())
  269. {
  270. d = iter->value.GetUint64();
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  277. {
  278. rapidjson::Value::ConstMemberIterator iter;
  279. if(try_get_iter(key, node, iter))
  280. {
  281. if(iter->value.IsDouble())
  282. {
  283. d = iter->value.GetDouble();
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  290. {
  291. rapidjson::Value::ConstMemberIterator iter;
  292. if(try_get_iter(key, node, iter))
  293. {
  294. if(iter->value.IsString())
  295. {
  296. d = iter->value.GetString();
  297. return true;
  298. }
  299. }
  300. return false;
  301. }
  302. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  303. {
  304. rapidjson::Value::ConstMemberIterator iter;
  305. if(try_get_iter(key, node, iter))
  306. {
  307. if(iter->value.IsInt())
  308. {
  309. return iter->value.GetInt();
  310. }
  311. }
  312. return default_data;
  313. }
  314. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  315. {
  316. rapidjson::Value::ConstMemberIterator iter;
  317. if(try_get_iter(key, node, iter))
  318. {
  319. if(iter->value.IsString())
  320. {
  321. return iter->value.GetString();
  322. }
  323. }
  324. return default_data;
  325. }
  326. static std::string doc_to_json(rapidjson::Document& doc)
  327. {
  328. rapidjson::StringBuffer sb;
  329. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  330. doc.Accept(writer);
  331. return sb.GetString();
  332. }
  333. };
  334. #endif // COMMON_TOOL_H