common_tool.h 9.6 KB

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