common_tool.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #ifndef COMMON_TOOL_H
  2. #define COMMON_TOOL_H
  3. /**
  4. * @brief 基楚的工具类
  5. * @author 戴月腾
  6. * @date 2018-09-15
  7. */
  8. #include <chrono>
  9. #include <string>
  10. #include <sys/time.h>
  11. #include <rapidjson/document.h>
  12. #include <rapidjson/writer.h>
  13. #include <rapidjson/stringbuffer.h>
  14. #include <rapidjson/prettywriter.h>
  15. #include <boost/function.hpp>
  16. #include <boost/bind.hpp>
  17. #include "websocket/sio/sio_client.h"
  18. #include "common.h"
  19. class tool_other
  20. {
  21. public:
  22. static std::string to13str(std::string& str)
  23. {
  24. uint64_t tmp = std::stoull(str);
  25. return to13str(tmp);
  26. }
  27. static std::string to13str(uint64_t data)
  28. {
  29. char ss[20]={0};
  30. sprintf(ss, "%013ld", data);
  31. return std::string(ss);
  32. }
  33. static uint32_t id64_to_id(std::string& str)
  34. {
  35. return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
  36. }
  37. static int id64_to_type(std::string& str)
  38. {
  39. return std::stoi(to13str(str).substr(0, 3));
  40. }
  41. static int card_id_to_type(const std::string &cardid)
  42. {
  43. return std::stoi(cardid.substr(0,3));
  44. }
  45. static int card_id_to_id(const std::string &cardid)
  46. {
  47. return atoi(cardid.substr(3).c_str());
  48. }
  49. static std::string type_id_to_str(int32_t type,uint32_t id)
  50. {
  51. char sql[15] = {0};
  52. snprintf(sql, 15,"%03d%010d", type, id);
  53. return std::string(sql);
  54. }
  55. static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
  56. {
  57. return type<<32|id;
  58. }
  59. static bool is_person(int32_t type)
  60. {
  61. return CT_PERSON == type;
  62. }
  63. static bool is_vehicle(int32_t type)
  64. {
  65. return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  66. }
  67. static std::string get_string_cardid(uint64_t id)
  68. {
  69. uint64_t type = id>>32;
  70. uint32_t cid = id & (~(type<<32));
  71. return type_id_to_str(type,cid);
  72. }
  73. //是否系统网络字节顺序 true=大端(低位在前,高位在后) false=小端(高位在前,低位在后)
  74. static bool IsBigEnd()
  75. {
  76. static int g_big = -1;
  77. if (g_big == -1)
  78. {
  79. g_big = 1;
  80. int x=0x12345678; /* 305419896 */
  81. unsigned char *p=(unsigned char *)&x;
  82. if (p[0] == 0x78) {
  83. g_big = 0;
  84. }
  85. }
  86. return g_big == 1;
  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. ///采煤机
  93. static bool is_coal(int32_t type)
  94. {
  95. return CT_COAL_CUTTER == type;
  96. }
  97. /// 掘进机
  98. static bool is_driving(int32_t type)
  99. {
  100. return CT_HEADING_MACHINE == type;
  101. }
  102. static bool is_coal_or_driving(int32_t type)
  103. {
  104. return CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  105. }
  106. };
  107. class tool_map
  108. {
  109. public:
  110. static bool try_get_value(sio::message::ptr& out_data,
  111. const char* key, sio::message::ptr const& data)
  112. {
  113. auto map=data->get_map()[key];
  114. if(map && sio::message::flag_object == map->get_flag())
  115. {
  116. out_data = map;
  117. return true;
  118. }
  119. return false;
  120. }
  121. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  122. {
  123. auto map=data->get_map()[key];
  124. if(map && sio::message::flag_integer == map->get_flag())
  125. {
  126. out_data = map->get_int();
  127. return true;
  128. }
  129. return false;
  130. }
  131. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  132. {
  133. auto map=data->get_map()[key];
  134. if(map && sio::message::flag_integer == map->get_flag())
  135. {
  136. out_data = static_cast<uint32_t>(map->get_int());
  137. return true;
  138. }
  139. return false;
  140. }
  141. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  142. {
  143. auto map=data->get_map()[key];
  144. if(map && sio::message::flag_integer == map->get_flag())
  145. {
  146. out_data = static_cast<int>(map->get_int());
  147. return true;
  148. }
  149. return false;
  150. }
  151. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  152. {
  153. auto map=data->get_map()[key];
  154. if(map && sio::message::flag_string == map->get_flag())
  155. {
  156. out_data = map->get_string();
  157. return true;
  158. }
  159. return false;
  160. }
  161. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  162. {
  163. auto map=data->get_map()[key];
  164. if(map && sio::message::flag_double == map->get_flag())
  165. {
  166. out_data = map->get_double();
  167. return true;
  168. }
  169. return false;
  170. }
  171. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  172. const char* key, sio::message::ptr const& data)
  173. {
  174. auto map=data->get_map()[key];
  175. if(map && sio::message::flag_array == map->get_flag())
  176. {
  177. out_data = map->get_vector();
  178. return true;
  179. }
  180. return false;
  181. }
  182. };
  183. class tool_json
  184. {
  185. public:
  186. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  187. rapidjson::Document::AllocatorType& allocator)
  188. {
  189. rapidjson::Value name;
  190. name.SetString(key, allocator);
  191. rapidjson::Value data;
  192. data.SetString(value.c_str(), allocator);
  193. out_data.AddMember(name, data, allocator);
  194. }
  195. static void push_back(rapidjson::Value& out_data, std::string value,
  196. rapidjson::Document::AllocatorType& allocator)
  197. {
  198. rapidjson::Value data;
  199. data.SetString(value.c_str(), allocator);
  200. out_data.PushBack(data, allocator);
  201. }
  202. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  203. rapidjson::Value::ConstMemberIterator& out_iter)
  204. {
  205. if(node.IsObject())
  206. {
  207. out_iter = node.FindMember(key);
  208. if(node.MemberEnd() == out_iter)
  209. {
  210. return false;
  211. }
  212. return true;
  213. }
  214. return false;
  215. }
  216. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  217. {
  218. rapidjson::Value::ConstMemberIterator iter;
  219. if(try_get_iter(key, node, iter))
  220. {
  221. if(iter->value.IsInt())
  222. {
  223. d = iter->value.GetInt();
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  230. {
  231. rapidjson::Value::ConstMemberIterator iter;
  232. if(try_get_iter(key, node, iter))
  233. {
  234. if(iter->value.IsUint64())
  235. {
  236. d = iter->value.GetUint64();
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  243. {
  244. rapidjson::Value::ConstMemberIterator iter;
  245. if(try_get_iter(key, node, iter))
  246. {
  247. if(iter->value.IsDouble())
  248. {
  249. d = iter->value.GetDouble();
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  256. {
  257. rapidjson::Value::ConstMemberIterator iter;
  258. if(try_get_iter(key, node, iter))
  259. {
  260. if(iter->value.IsString())
  261. {
  262. d = iter->value.GetString();
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  269. {
  270. rapidjson::Value::ConstMemberIterator iter;
  271. if(try_get_iter(key, node, iter))
  272. {
  273. if(iter->value.IsInt())
  274. {
  275. return iter->value.GetInt();
  276. }
  277. }
  278. return default_data;
  279. }
  280. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  281. {
  282. rapidjson::Value::ConstMemberIterator iter;
  283. if(try_get_iter(key, node, iter))
  284. {
  285. if(iter->value.IsString())
  286. {
  287. return iter->value.GetString();
  288. }
  289. }
  290. return default_data;
  291. }
  292. static std::string doc_to_json(rapidjson::Document& doc)
  293. {
  294. rapidjson::StringBuffer sb;
  295. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  296. doc.Accept(writer);
  297. return sb.GetString();
  298. }
  299. };
  300. #endif // COMMON_TOOL_H