common_tool.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 int card_id_to_type(const std::string &cardid)
  45. {
  46. return std::stoi(cardid.substr(0,3));
  47. }
  48. static int card_id_to_id(const std::string &cardid)
  49. {
  50. return atoi(cardid.substr(3).c_str());
  51. }
  52. static std::string type_id_to_str(int32_t type,uint32_t id)
  53. {
  54. char sql[15] = {0};
  55. snprintf(sql, 15,"%03d%010d", type, id);
  56. return std::string(sql);
  57. }
  58. static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
  59. {
  60. return type<<32|id;
  61. }
  62. static bool is_person(int32_t type)
  63. {
  64. return CT_PERSON == type;
  65. }
  66. static bool is_vehicle(int32_t type)
  67. {
  68. return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  69. }
  70. static std::string get_string_cardid(uint64_t id)
  71. {
  72. uint64_t type = id>>32;
  73. uint32_t cid = id & (~(type<<32));
  74. return type_id_to_str(type,cid);
  75. }
  76. static uint64_t card_id_to_u64(const std::string & cardid)
  77. {
  78. return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
  79. }
  80. };
  81. class tool_map
  82. {
  83. public:
  84. static bool try_get_value(sio::message::ptr& out_data,
  85. const char* key, sio::message::ptr const& data)
  86. {
  87. auto map=data->get_map()[key];
  88. if(map && sio::message::flag_object == map->get_flag())
  89. {
  90. out_data = map;
  91. return true;
  92. }
  93. return false;
  94. }
  95. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  96. {
  97. auto map=data->get_map()[key];
  98. if(map && sio::message::flag_integer == map->get_flag())
  99. {
  100. out_data = map->get_int();
  101. return true;
  102. }
  103. return false;
  104. }
  105. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  106. {
  107. auto map=data->get_map()[key];
  108. if(map && sio::message::flag_integer == map->get_flag())
  109. {
  110. out_data = static_cast<uint32_t>(map->get_int());
  111. return true;
  112. }
  113. return false;
  114. }
  115. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  116. {
  117. auto map=data->get_map()[key];
  118. if(map && sio::message::flag_integer == map->get_flag())
  119. {
  120. out_data = static_cast<int>(map->get_int());
  121. return true;
  122. }
  123. return false;
  124. }
  125. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  126. {
  127. auto map=data->get_map()[key];
  128. if(map && sio::message::flag_string == map->get_flag())
  129. {
  130. out_data = map->get_string();
  131. return true;
  132. }
  133. return false;
  134. }
  135. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  136. {
  137. auto map=data->get_map()[key];
  138. if(map && sio::message::flag_double == map->get_flag())
  139. {
  140. out_data = map->get_double();
  141. return true;
  142. }
  143. return false;
  144. }
  145. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  146. const char* key, sio::message::ptr const& data)
  147. {
  148. auto map=data->get_map()[key];
  149. if(map && sio::message::flag_array == map->get_flag())
  150. {
  151. out_data = map->get_vector();
  152. return true;
  153. }
  154. return false;
  155. }
  156. };
  157. class tool_json
  158. {
  159. public:
  160. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  161. rapidjson::Document::AllocatorType& allocator)
  162. {
  163. rapidjson::Value name;
  164. name.SetString(key, allocator);
  165. rapidjson::Value data;
  166. data.SetString(value.c_str(), allocator);
  167. out_data.AddMember(name, data, allocator);
  168. }
  169. static void push_back(rapidjson::Value& out_data, std::string value,
  170. rapidjson::Document::AllocatorType& allocator)
  171. {
  172. rapidjson::Value data;
  173. data.SetString(value.c_str(), allocator);
  174. out_data.PushBack(data, allocator);
  175. }
  176. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  177. rapidjson::Value::ConstMemberIterator& out_iter)
  178. {
  179. if(node.IsObject())
  180. {
  181. out_iter = node.FindMember(key);
  182. if(node.MemberEnd() == out_iter)
  183. {
  184. return false;
  185. }
  186. return true;
  187. }
  188. return false;
  189. }
  190. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  191. {
  192. rapidjson::Value::ConstMemberIterator iter;
  193. if(try_get_iter(key, node, iter))
  194. {
  195. if(iter->value.IsInt())
  196. {
  197. d = iter->value.GetInt();
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  204. {
  205. rapidjson::Value::ConstMemberIterator iter;
  206. if(try_get_iter(key, node, iter))
  207. {
  208. if(iter->value.IsUint64())
  209. {
  210. d = iter->value.GetUint64();
  211. return true;
  212. }
  213. }
  214. return false;
  215. }
  216. static bool try_get_value(double& 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.IsDouble())
  222. {
  223. d = iter->value.GetDouble();
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. static bool try_get_value(std::string& 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.IsString())
  235. {
  236. d = iter->value.GetString();
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. static int get_value(const char* key, const int& 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.IsInt())
  248. {
  249. return iter->value.GetInt();
  250. }
  251. }
  252. return default_data;
  253. }
  254. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  255. {
  256. rapidjson::Value::ConstMemberIterator iter;
  257. if(try_get_iter(key, node, iter))
  258. {
  259. if(iter->value.IsString())
  260. {
  261. return iter->value.GetString();
  262. }
  263. }
  264. return default_data;
  265. }
  266. static std::string doc_to_json(rapidjson::Document& doc)
  267. {
  268. rapidjson::StringBuffer sb;
  269. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  270. doc.Accept(writer);
  271. return sb.GetString();
  272. }
  273. };
  274. #endif // COMMON_TOOL_H