common_tool.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #ifndef COMMON_TOOL_H
  2. #define COMMON_TOOL_H
  3. /**
  4. * @brief 基楚的工具类
  5. * @author 戴月腾
  6. * @date 2018-09-15
  7. */
  8. #include "common.h"
  9. #include "log.h"
  10. #include <chrono>
  11. #include <string>
  12. #include <sys/time.h>
  13. #include "db_api/CDBSingletonDefine.h"
  14. #include "websocket/wsClientMgr.h"
  15. #include "card.h"
  16. #include "mine.h"
  17. class tool_other
  18. {
  19. public:
  20. static void send_json(const std::string& cmd, const std::string& data)
  21. {
  22. //log_info("发送json: cmd=%s, data=%s\n", cmd.c_str(), data.c_str());
  23. swsClientMgr.send(cmd, data);
  24. }
  25. static std::string to13str(std::string& str)
  26. {
  27. uint64_t tmp = std::stoull(str);
  28. return to13str(tmp);
  29. }
  30. static std::string to13str(uint64_t data)
  31. {
  32. char ss[20]={0};
  33. sprintf(ss, "%013ld", data);
  34. return std::string(ss);
  35. }
  36. static uint32_t id64_to_id(std::string& str)
  37. {
  38. return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
  39. }
  40. static int id64_to_type(std::string& str)
  41. {
  42. return std::stoi(to13str(str).substr(0, 3));
  43. }
  44. static std::string type_id_to_str(int32_t type,uint32_t id)
  45. {
  46. char sql[15] = {0};
  47. snprintf(sql, 15,"%03d%010d", type, id);
  48. return std::string(sql);
  49. }
  50. static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
  51. {
  52. return type<<32|id;
  53. }
  54. static bool is_person(int32_t type)
  55. {
  56. return CT_PERSON == type;
  57. }
  58. static bool is_vehicle(int32_t type)
  59. {
  60. return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  61. }
  62. static std::string get_string_cardid(uint64_t id)
  63. {
  64. uint64_t type = id>>32;
  65. uint32_t cid = id & (~(type<<32));
  66. return type_id_to_str(type,cid);
  67. }
  68. };
  69. class tool_map
  70. {
  71. public:
  72. static bool try_get_value(sio::message::ptr& out_data,
  73. const char* key, sio::message::ptr const& data)
  74. {
  75. auto map=data->get_map()[key];
  76. if(map && sio::message::flag_object == map->get_flag())
  77. {
  78. out_data = map;
  79. return true;
  80. }
  81. return false;
  82. }
  83. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  84. {
  85. auto map=data->get_map()[key];
  86. if(map && sio::message::flag_integer == map->get_flag())
  87. {
  88. out_data = map->get_int();
  89. return true;
  90. }
  91. return false;
  92. }
  93. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  94. {
  95. auto map=data->get_map()[key];
  96. if(map && sio::message::flag_integer == map->get_flag())
  97. {
  98. out_data = static_cast<uint32_t>(map->get_int());
  99. return true;
  100. }
  101. return false;
  102. }
  103. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  104. {
  105. auto map=data->get_map()[key];
  106. if(map && sio::message::flag_integer == map->get_flag())
  107. {
  108. out_data = static_cast<int>(map->get_int());
  109. return true;
  110. }
  111. return false;
  112. }
  113. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  114. {
  115. auto map=data->get_map()[key];
  116. if(map && sio::message::flag_string == map->get_flag())
  117. {
  118. out_data = map->get_string();
  119. return true;
  120. }
  121. return false;
  122. }
  123. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  124. {
  125. auto map=data->get_map()[key];
  126. if(map && sio::message::flag_double == map->get_flag())
  127. {
  128. out_data = map->get_double();
  129. return true;
  130. }
  131. return false;
  132. }
  133. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  134. const char* key, sio::message::ptr const& data)
  135. {
  136. auto map=data->get_map()[key];
  137. if(map && sio::message::flag_array == map->get_flag())
  138. {
  139. out_data = map->get_vector();
  140. return true;
  141. }
  142. return false;
  143. }
  144. };
  145. class tool_json
  146. {
  147. public:
  148. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  149. rapidjson::Document::AllocatorType& allocator)
  150. {
  151. rapidjson::Value name;
  152. name.SetString(key, allocator);
  153. rapidjson::Value data;
  154. data.SetString(value.c_str(), allocator);
  155. out_data.AddMember(name, data, allocator);
  156. }
  157. static void push_back(rapidjson::Value& out_data, std::string value,
  158. rapidjson::Document::AllocatorType& allocator)
  159. {
  160. rapidjson::Value data;
  161. data.SetString(value.c_str(), allocator);
  162. out_data.PushBack(data, allocator);
  163. }
  164. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  165. rapidjson::Value::ConstMemberIterator& out_iter)
  166. {
  167. if(node.IsObject())
  168. {
  169. out_iter = node.FindMember(key);
  170. if(node.MemberEnd() == out_iter)
  171. {
  172. return false;
  173. }
  174. return true;
  175. }
  176. return false;
  177. }
  178. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  179. {
  180. rapidjson::Value::ConstMemberIterator iter;
  181. if(try_get_iter(key, node, iter))
  182. {
  183. if(iter->value.IsInt())
  184. {
  185. d = iter->value.GetInt();
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  192. {
  193. rapidjson::Value::ConstMemberIterator iter;
  194. if(try_get_iter(key, node, iter))
  195. {
  196. if(iter->value.IsUint64())
  197. {
  198. d = iter->value.GetUint64();
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  205. {
  206. rapidjson::Value::ConstMemberIterator iter;
  207. if(try_get_iter(key, node, iter))
  208. {
  209. if(iter->value.IsDouble())
  210. {
  211. d = iter->value.GetDouble();
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  218. {
  219. rapidjson::Value::ConstMemberIterator iter;
  220. if(try_get_iter(key, node, iter))
  221. {
  222. if(iter->value.IsString())
  223. {
  224. d = iter->value.GetString();
  225. return true;
  226. }
  227. }
  228. return false;
  229. }
  230. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  231. {
  232. rapidjson::Value::ConstMemberIterator iter;
  233. if(try_get_iter(key, node, iter))
  234. {
  235. if(iter->value.IsInt())
  236. {
  237. return iter->value.GetInt();
  238. }
  239. }
  240. return default_data;
  241. }
  242. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  243. {
  244. rapidjson::Value::ConstMemberIterator iter;
  245. if(try_get_iter(key, node, iter))
  246. {
  247. if(iter->value.IsString())
  248. {
  249. return iter->value.GetString();
  250. }
  251. }
  252. return default_data;
  253. }
  254. static std::string doc_to_json(rapidjson::Document& doc)
  255. {
  256. rapidjson::StringBuffer sb;
  257. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  258. doc.Accept(writer);
  259. return sb.GetString();
  260. }
  261. };
  262. #endif // COMMON_TOOL_H