common_tool.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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"card.h"
  11. #include<chrono>
  12. #include<string>
  13. #include<sys/time.h>
  14. #include "db_api/CDBConnPool.h"
  15. #include "websocket/wsClientMgr.h"
  16. class tool_time
  17. {
  18. public:
  19. static uint32_t elapse_seconds(std::chrono::system_clock::time_point &start)
  20. {
  21. return static_cast<uint32_t>( std::chrono::duration_cast<std::chrono::seconds>
  22. (std::chrono::system_clock::now() - start).count() );
  23. }
  24. static uint64_t elapse_ms(std::chrono::system_clock::time_point &start)
  25. {
  26. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
  27. (std::chrono::system_clock::now() - start).count() );
  28. }
  29. static uint32_t now_to_seconds()
  30. {
  31. return static_cast<uint32_t>( std::chrono::duration_cast<std::chrono::seconds>
  32. (std::chrono::system_clock::now().time_since_epoch()).count() );
  33. }
  34. static uint64_t now_to_ms()
  35. {
  36. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
  37. (std::chrono::system_clock::now().time_since_epoch()).count() );
  38. }
  39. static uint64_t now_to_us()
  40. {
  41. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::microseconds>
  42. (std::chrono::system_clock::now().time_since_epoch()).count() );
  43. }
  44. static uint64_t to_ms(const std::chrono::system_clock::time_point &time)
  45. {
  46. return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
  47. (time.time_since_epoch()).count() );
  48. }
  49. static std::string to_str(const std::chrono::system_clock::time_point &time)
  50. {
  51. char _time[25] = {0};
  52. time_t tt = std::chrono::system_clock::to_time_t(time);
  53. struct tm local_time;
  54. localtime_r(&tt, &local_time);
  55. strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);
  56. return std::string(_time);
  57. }
  58. //"%Y-%m-%d %H:%M:%S"
  59. static time_t to_time(std::string str)
  60. {
  61. time_t t_;
  62. tm tm_;
  63. strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
  64. t_ = mktime(&tm_); //将tm时间转换为秒时间
  65. return t_;
  66. }
  67. ////"%d-%02d-%02d %02d:%02d:%02d.%03d"
  68. static std::chrono::system_clock::time_point to_time_ex(std::string str)
  69. {
  70. uint64_t pos = str.length()-3;
  71. time_t t_;
  72. tm tm_;
  73. strptime(str.substr(0,pos).c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
  74. t_ = mktime(&tm_); //将tm时间转换为秒时间
  75. int milli = std::stoi(str.substr(pos));
  76. return std::chrono::system_clock::time_point(std::chrono::milliseconds(t_*1000 + milli));
  77. }
  78. //"%d-%02d-%02d %02d:%02d:%02d.%03d"
  79. static std::string to_str_ex(uint64_t ms)
  80. {
  81. int32_t mill = ms%1000;
  82. char _time[25] = {0};
  83. time_t tt = ms/1000;
  84. struct tm *local_time=localtime(&tt);
  85. //strftime(_time, 22, "%Y-%m-%d %H:%M:%S", local_time);
  86. sprintf(_time, "%d-%02d-%02d %02d:%02d:%02d.%03d", local_time->tm_year+1900,
  87. local_time->tm_mon+1, local_time->tm_mday, local_time->tm_hour,
  88. local_time->tm_min, local_time->tm_sec, mill);
  89. return std::string(_time);
  90. }
  91. //"%d-%02d-%02d %02d:%02d:%02d.%03d"
  92. static std::string to_str_ex(std::chrono::system_clock::time_point time)
  93. {
  94. return to_str_ex(to_ms(time));
  95. }
  96. static int elapse_seconds(time_t &start)
  97. {
  98. time_t now;
  99. time(&now);
  100. return static_cast<int>(std::difftime(now, start));
  101. }
  102. //"%Y-%m-%d %H:%M:%S"
  103. static std::string to_str(const std::time_t &time)
  104. {
  105. char _time[25] = {0};
  106. struct tm local_time;
  107. localtime_r(&time, &local_time);
  108. strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);
  109. return std::string(_time);
  110. }
  111. };
  112. class tool_other
  113. {
  114. public:
  115. static void send_json(const std::string& cmd, const std::string& data)
  116. {
  117. std_debug("发送json: cmd=%s, data=%s\n", cmd.c_str(), data.c_str());
  118. log_info("发送json: cmd=%s, data=%s\n", cmd.c_str(), data.c_str());
  119. swsClientMgr.send(cmd, data);
  120. }
  121. static std::string to13str(std::string& str)
  122. {
  123. uint64_t tmp = std::stoull(str);
  124. char ss[20]={0};
  125. sprintf(ss, "%013ld", tmp);
  126. return std::string(ss);
  127. }
  128. static uint32_t id64_to_id(std::string& str)
  129. {
  130. return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
  131. }
  132. static int id64_to_type(std::string& str)
  133. {
  134. return std::stoi(to13str(str).substr(0, 3));
  135. }
  136. };
  137. class tool_db
  138. {
  139. public:
  140. static void PushAsync(char* sql)
  141. {
  142. //std_debug("PushAsync记录到队列中:%s\n", sql);
  143. //log_info("PushAsync记录到队列中:%s\n", sql);
  144. if(!sDBConnPool.PushAsync(sql))
  145. {
  146. log_error( "PushAsync记录到队列中失败\n");
  147. }
  148. }
  149. static void save_attendance(const std::shared_ptr<card_location_base> card_ptr)
  150. {
  151. char sql[LENGTH_SQL] = {0};
  152. std::string call("add_att_staff");
  153. if(card_ptr->is_vehicle())//车卡
  154. {
  155. call="add_att_vehicle";
  156. }
  157. auto mine_tool_ptr = card_ptr->get_mine_tool();
  158. auto start = mine_tool_ptr->m_attendance_start_time;
  159. auto end = mine_tool_ptr->m_attendance_start_time;
  160. if(!mine_tool_ptr->is_attendance())//考勤结束时间
  161. {
  162. end = std::chrono::system_clock::time_point(std::chrono::milliseconds(card_ptr->m_time));
  163. }
  164. std::string start_str = tool_time::to_str(start);
  165. std::string end_str = tool_time::to_str(end);
  166. int landmarkid = 0;
  167. int landmarkdirect=0;
  168. double landmarkdist=0;
  169. auto area_hover_ptr = card_ptr->get_area_hover();
  170. if(area_hover_ptr)
  171. {
  172. landmarkid = area_hover_ptr->landmark_id;
  173. landmarkdirect = area_hover_ptr->landmark_dir;
  174. landmarkdist = area_hover_ptr->landmark_dis;
  175. }
  176. sprintf(sql, "CALL %s(%s, %d, '%s', '%s', %d, %d, %.3f);", call.c_str(),
  177. card_list::to_id64_str(card_ptr->m_type, card_ptr->m_id).c_str(),
  178. card_ptr->m_id, start_str.c_str(), end_str.c_str(),
  179. landmarkid, landmarkdirect, landmarkdist);
  180. PushAsync(sql);
  181. }
  182. };
  183. class tool_map
  184. {
  185. public:
  186. static bool try_get_value(sio::message::ptr& out_data,
  187. const char* key, sio::message::ptr const& data)
  188. {
  189. auto map=data->get_map()[key];
  190. if(map && sio::message::flag_object == map->get_flag())
  191. {
  192. out_data = map;
  193. return true;
  194. }
  195. return false;
  196. }
  197. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  198. {
  199. auto map=data->get_map()[key];
  200. if(map && sio::message::flag_integer == map->get_flag())
  201. {
  202. out_data = map->get_int();
  203. return true;
  204. }
  205. return false;
  206. }
  207. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  208. {
  209. auto map=data->get_map()[key];
  210. if(map && sio::message::flag_integer == map->get_flag())
  211. {
  212. out_data = static_cast<uint32_t>(map->get_int());
  213. return true;
  214. }
  215. return false;
  216. }
  217. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  218. {
  219. auto map=data->get_map()[key];
  220. if(map && sio::message::flag_integer == map->get_flag())
  221. {
  222. out_data = static_cast<int>(map->get_int());
  223. return true;
  224. }
  225. return false;
  226. }
  227. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  228. {
  229. auto map=data->get_map()[key];
  230. if(map && sio::message::flag_string == map->get_flag())
  231. {
  232. out_data = map->get_string();
  233. return true;
  234. }
  235. return false;
  236. }
  237. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  238. {
  239. auto map=data->get_map()[key];
  240. if(map && sio::message::flag_double == map->get_flag())
  241. {
  242. out_data = map->get_double();
  243. return true;
  244. }
  245. return false;
  246. }
  247. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  248. const char* key, sio::message::ptr const& data)
  249. {
  250. auto map=data->get_map()[key];
  251. if(map && sio::message::flag_array == map->get_flag())
  252. {
  253. out_data = map->get_vector();
  254. return true;
  255. }
  256. return false;
  257. }
  258. };
  259. class tool_json
  260. {
  261. public:
  262. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  263. rapidjson::Document::AllocatorType& allocator)
  264. {
  265. rapidjson::Value name;
  266. name.SetString(key, allocator);
  267. rapidjson::Value data;
  268. data.SetString(value.c_str(), allocator);
  269. out_data.AddMember(name, data, allocator);
  270. }
  271. static void push_back(rapidjson::Value& out_data, std::string value,
  272. rapidjson::Document::AllocatorType& allocator)
  273. {
  274. rapidjson::Value data;
  275. data.SetString(value.c_str(), allocator);
  276. out_data.PushBack(data, allocator);
  277. }
  278. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  279. rapidjson::Value::ConstMemberIterator& out_iter)
  280. {
  281. if(node.IsObject())
  282. {
  283. out_iter = node.FindMember(key);
  284. if(node.MemberEnd() == out_iter)
  285. {
  286. return false;
  287. }
  288. return true;
  289. }
  290. return false;
  291. }
  292. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  293. {
  294. rapidjson::Value::ConstMemberIterator iter;
  295. if(try_get_iter(key, node, iter))
  296. {
  297. if(iter->value.IsInt())
  298. {
  299. d = iter->value.GetInt();
  300. return true;
  301. }
  302. }
  303. return false;
  304. }
  305. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  306. {
  307. rapidjson::Value::ConstMemberIterator iter;
  308. if(try_get_iter(key, node, iter))
  309. {
  310. if(iter->value.IsUint64())
  311. {
  312. d = iter->value.GetUint64();
  313. return true;
  314. }
  315. }
  316. return false;
  317. }
  318. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  319. {
  320. rapidjson::Value::ConstMemberIterator iter;
  321. if(try_get_iter(key, node, iter))
  322. {
  323. if(iter->value.IsDouble())
  324. {
  325. d = iter->value.GetDouble();
  326. return true;
  327. }
  328. }
  329. return false;
  330. }
  331. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  332. {
  333. rapidjson::Value::ConstMemberIterator iter;
  334. if(try_get_iter(key, node, iter))
  335. {
  336. if(iter->value.IsString())
  337. {
  338. d = iter->value.GetString();
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  345. {
  346. rapidjson::Value::ConstMemberIterator iter;
  347. if(try_get_iter(key, node, iter))
  348. {
  349. if(iter->value.IsInt())
  350. {
  351. return iter->value.GetInt();
  352. }
  353. }
  354. return default_data;
  355. }
  356. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  357. {
  358. rapidjson::Value::ConstMemberIterator iter;
  359. if(try_get_iter(key, node, iter))
  360. {
  361. if(iter->value.IsString())
  362. {
  363. return iter->value.GetString();
  364. }
  365. }
  366. return default_data;
  367. }
  368. static std::string doc_to_json(rapidjson::Document& doc)
  369. {
  370. rapidjson::StringBuffer sb;
  371. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  372. doc.Accept(writer);
  373. return sb.GetString();
  374. }
  375. };
  376. #endif // COMMON_TOOL_H