common_tool.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. static uint64_t card_id_to_u64(const std::string & cardid)
  74. {
  75. return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
  76. }
  77. ///采煤机
  78. static bool is_coal(int32_t type)
  79. {
  80. return CT_COAL_CUTTER == type;
  81. }
  82. /// 掘进机
  83. static bool is_driving(int32_t type)
  84. {
  85. return CT_HEADING_MACHINE == type;
  86. }
  87. static bool is_coal_or_driving(int32_t type)
  88. {
  89. return CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  90. }
  91. };
  92. class tool_map
  93. {
  94. public:
  95. static bool try_get_value(sio::message::ptr& out_data,
  96. const char* key, sio::message::ptr const& data)
  97. {
  98. auto map=data->get_map()[key];
  99. if(map && sio::message::flag_object == map->get_flag())
  100. {
  101. out_data = map;
  102. return true;
  103. }
  104. return false;
  105. }
  106. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  107. {
  108. auto map=data->get_map()[key];
  109. if(map && sio::message::flag_integer == map->get_flag())
  110. {
  111. out_data = map->get_int();
  112. return true;
  113. }
  114. return false;
  115. }
  116. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  117. {
  118. auto map=data->get_map()[key];
  119. if(map && sio::message::flag_integer == map->get_flag())
  120. {
  121. out_data = static_cast<uint32_t>(map->get_int());
  122. return true;
  123. }
  124. return false;
  125. }
  126. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  127. {
  128. auto map=data->get_map()[key];
  129. if(map && sio::message::flag_integer == map->get_flag())
  130. {
  131. out_data = static_cast<int>(map->get_int());
  132. return true;
  133. }
  134. return false;
  135. }
  136. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  137. {
  138. auto map=data->get_map()[key];
  139. if(map && sio::message::flag_string == map->get_flag())
  140. {
  141. out_data = map->get_string();
  142. return true;
  143. }
  144. return false;
  145. }
  146. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  147. {
  148. auto map=data->get_map()[key];
  149. if(map && sio::message::flag_double == map->get_flag())
  150. {
  151. out_data = map->get_double();
  152. return true;
  153. }
  154. return false;
  155. }
  156. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  157. const char* key, sio::message::ptr const& data)
  158. {
  159. auto map=data->get_map()[key];
  160. if(map && sio::message::flag_array == map->get_flag())
  161. {
  162. out_data = map->get_vector();
  163. return true;
  164. }
  165. return false;
  166. }
  167. };
  168. class tool_json
  169. {
  170. public:
  171. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  172. rapidjson::Document::AllocatorType& allocator)
  173. {
  174. rapidjson::Value name;
  175. name.SetString(key, allocator);
  176. rapidjson::Value data;
  177. data.SetString(value.c_str(), allocator);
  178. out_data.AddMember(name, data, allocator);
  179. }
  180. static void push_back(rapidjson::Value& out_data, std::string value,
  181. rapidjson::Document::AllocatorType& allocator)
  182. {
  183. rapidjson::Value data;
  184. data.SetString(value.c_str(), allocator);
  185. out_data.PushBack(data, allocator);
  186. }
  187. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  188. rapidjson::Value::ConstMemberIterator& out_iter)
  189. {
  190. if(node.IsObject())
  191. {
  192. out_iter = node.FindMember(key);
  193. if(node.MemberEnd() == out_iter)
  194. {
  195. return false;
  196. }
  197. return true;
  198. }
  199. return false;
  200. }
  201. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  202. {
  203. rapidjson::Value::ConstMemberIterator iter;
  204. if(try_get_iter(key, node, iter))
  205. {
  206. if(iter->value.IsInt())
  207. {
  208. d = iter->value.GetInt();
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  215. {
  216. rapidjson::Value::ConstMemberIterator iter;
  217. if(try_get_iter(key, node, iter))
  218. {
  219. if(iter->value.IsUint64())
  220. {
  221. d = iter->value.GetUint64();
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  228. {
  229. rapidjson::Value::ConstMemberIterator iter;
  230. if(try_get_iter(key, node, iter))
  231. {
  232. if(iter->value.IsDouble())
  233. {
  234. d = iter->value.GetDouble();
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  241. {
  242. rapidjson::Value::ConstMemberIterator iter;
  243. if(try_get_iter(key, node, iter))
  244. {
  245. if(iter->value.IsString())
  246. {
  247. d = iter->value.GetString();
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  254. {
  255. rapidjson::Value::ConstMemberIterator iter;
  256. if(try_get_iter(key, node, iter))
  257. {
  258. if(iter->value.IsInt())
  259. {
  260. return iter->value.GetInt();
  261. }
  262. }
  263. return default_data;
  264. }
  265. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  266. {
  267. rapidjson::Value::ConstMemberIterator iter;
  268. if(try_get_iter(key, node, iter))
  269. {
  270. if(iter->value.IsString())
  271. {
  272. return iter->value.GetString();
  273. }
  274. }
  275. return default_data;
  276. }
  277. static std::string doc_to_json(rapidjson::Document& doc)
  278. {
  279. rapidjson::StringBuffer sb;
  280. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  281. doc.Accept(writer);
  282. return sb.GetString();
  283. }
  284. };
  285. #endif // COMMON_TOOL_H