common_tool.h 9.6 KB

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