common_tool.h 9.6 KB

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