common_tool.h 13 KB

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