common_tool.h 8.1 KB

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