common_tool.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 double round(double dVal, int iPlaces)
  23. {
  24. double dRetval;
  25. double dMod = 0.0000001;
  26. if (dVal<0.0) dMod=-0.0000001;
  27. dRetval=dVal;
  28. dRetval+=(5.0/pow(10.0,iPlaces+1.0));
  29. dRetval*=pow(10.0,iPlaces);
  30. dRetval=floor(dRetval+dMod);
  31. dRetval/=pow(10.0,iPlaces);
  32. return(dRetval);
  33. }
  34. static std::string to13str(std::string& str)
  35. {
  36. uint64_t tmp = std::stoull(str);
  37. return to13str(tmp);
  38. }
  39. static std::string to13str(uint64_t data)
  40. {
  41. char ss[20]={0};
  42. sprintf(ss, "%013ld", data);
  43. return std::string(ss);
  44. }
  45. static uint32_t id64_to_id(std::string& str)
  46. {
  47. return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
  48. }
  49. static int id64_to_type(std::string& str)
  50. {
  51. return std::stoi(to13str(str).substr(0, 3));
  52. }
  53. static int card_id_to_type(const std::string &cardid)
  54. {
  55. return std::stoi(cardid.substr(0,3));
  56. }
  57. static int card_id_to_id(const std::string &cardid)
  58. {
  59. return atoi(cardid.substr(3).c_str());
  60. }
  61. static std::string type_id_to_str(int32_t type,uint32_t id)
  62. {
  63. char sql[15] = {0};
  64. snprintf(sql, 15,"%03d%010d", type, id);
  65. return std::string(sql);
  66. }
  67. static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
  68. {
  69. return type<<32|id;
  70. }
  71. static bool is_person(int32_t type)
  72. {
  73. return CT_PERSON == type;
  74. }
  75. static bool is_vehicle(int32_t type)
  76. {
  77. return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  78. }
  79. static std::string get_string_cardid(uint64_t id)
  80. {
  81. uint64_t type = id>>32;
  82. uint32_t cid = id & (~(type<<32));
  83. return type_id_to_str(type,cid);
  84. }
  85. static uint64_t card_id_to_u64(const std::string & cardid)
  86. {
  87. return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
  88. }
  89. ///采煤机
  90. static bool is_coal(int32_t type)
  91. {
  92. return CT_COAL_CUTTER == type;
  93. }
  94. /// 掘进机
  95. static bool is_driving(int32_t type)
  96. {
  97. return CT_HEADING_MACHINE == type;
  98. }
  99. static bool is_coal_or_driving(int32_t type)
  100. {
  101. return CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  102. }
  103. };
  104. class tool_map
  105. {
  106. public:
  107. static bool try_get_value(sio::message::ptr& out_data,
  108. const char* key, sio::message::ptr const& data)
  109. {
  110. auto map=data->get_map()[key];
  111. if(map && sio::message::flag_object == map->get_flag())
  112. {
  113. out_data = map;
  114. return true;
  115. }
  116. return false;
  117. }
  118. static bool try_get_value(int64_t& 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 = map->get_int();
  124. return true;
  125. }
  126. return false;
  127. }
  128. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  129. {
  130. auto map=data->get_map()[key];
  131. if(map && sio::message::flag_integer == map->get_flag())
  132. {
  133. out_data = static_cast<uint32_t>(map->get_int());
  134. return true;
  135. }
  136. return false;
  137. }
  138. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  139. {
  140. auto map=data->get_map()[key];
  141. if(map && sio::message::flag_integer == map->get_flag())
  142. {
  143. out_data = static_cast<int>(map->get_int());
  144. return true;
  145. }
  146. return false;
  147. }
  148. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  149. {
  150. auto map=data->get_map()[key];
  151. if(map && sio::message::flag_string == map->get_flag())
  152. {
  153. out_data = map->get_string();
  154. return true;
  155. }
  156. return false;
  157. }
  158. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  159. {
  160. auto map=data->get_map()[key];
  161. if(map && sio::message::flag_double == map->get_flag())
  162. {
  163. out_data = map->get_double();
  164. return true;
  165. }
  166. return false;
  167. }
  168. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  169. const char* key, sio::message::ptr const& data)
  170. {
  171. auto map=data->get_map()[key];
  172. if(map && sio::message::flag_array == map->get_flag())
  173. {
  174. out_data = map->get_vector();
  175. return true;
  176. }
  177. return false;
  178. }
  179. };
  180. class tool_json
  181. {
  182. public:
  183. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  184. rapidjson::Document::AllocatorType& allocator)
  185. {
  186. rapidjson::Value name;
  187. name.SetString(key, allocator);
  188. rapidjson::Value data;
  189. data.SetString(value.c_str(), allocator);
  190. out_data.AddMember(name, data, allocator);
  191. }
  192. static void push_back(rapidjson::Value& out_data, std::string value,
  193. rapidjson::Document::AllocatorType& allocator)
  194. {
  195. rapidjson::Value data;
  196. data.SetString(value.c_str(), allocator);
  197. out_data.PushBack(data, allocator);
  198. }
  199. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  200. rapidjson::Value::ConstMemberIterator& out_iter)
  201. {
  202. if(node.IsObject())
  203. {
  204. out_iter = node.FindMember(key);
  205. if(node.MemberEnd() == out_iter)
  206. {
  207. return false;
  208. }
  209. return true;
  210. }
  211. return false;
  212. }
  213. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  214. {
  215. rapidjson::Value::ConstMemberIterator iter;
  216. if(try_get_iter(key, node, iter))
  217. {
  218. if(iter->value.IsInt())
  219. {
  220. d = iter->value.GetInt();
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  227. {
  228. rapidjson::Value::ConstMemberIterator iter;
  229. if(try_get_iter(key, node, iter))
  230. {
  231. if(iter->value.IsUint64())
  232. {
  233. d = iter->value.GetUint64();
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  240. {
  241. rapidjson::Value::ConstMemberIterator iter;
  242. if(try_get_iter(key, node, iter))
  243. {
  244. if(iter->value.IsDouble())
  245. {
  246. d = iter->value.GetDouble();
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  253. {
  254. rapidjson::Value::ConstMemberIterator iter;
  255. if(try_get_iter(key, node, iter))
  256. {
  257. if(iter->value.IsString())
  258. {
  259. d = iter->value.GetString();
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. static int get_value(const char* key, const int& 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.IsInt())
  271. {
  272. return iter->value.GetInt();
  273. }
  274. }
  275. return default_data;
  276. }
  277. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  278. {
  279. rapidjson::Value::ConstMemberIterator iter;
  280. if(try_get_iter(key, node, iter))
  281. {
  282. if(iter->value.IsString())
  283. {
  284. return iter->value.GetString();
  285. }
  286. }
  287. return default_data;
  288. }
  289. static std::string doc_to_json(rapidjson::Document& doc)
  290. {
  291. rapidjson::StringBuffer sb;
  292. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  293. doc.Accept(writer);
  294. return sb.GetString();
  295. }
  296. };
  297. #endif // COMMON_TOOL_H