common_tool.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. char ss[20]={0};
  133. sprintf(ss, "%013ld", tmp);
  134. return std::string(ss);
  135. }
  136. static uint32_t id64_to_id(std::string& str)
  137. {
  138. return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
  139. }
  140. static int id64_to_type(std::string& str)
  141. {
  142. return std::stoi(to13str(str).substr(0, 3));
  143. }
  144. };
  145. class tool_db
  146. {
  147. public:
  148. static void PushAsync(char* sql)
  149. {
  150. if(!sDBConnPool.PushAsync(sql))
  151. {
  152. log_error( "PushAsync记录到队列中失败\n");
  153. }
  154. }
  155. static void save_attendance(const std::shared_ptr<card_location_base>& card_ptr)
  156. {
  157. char sql[LENGTH_SQL] = {0};
  158. std::string call("add_att_staff");
  159. if(card_ptr->is_vehicle())//车卡
  160. {
  161. call="add_att_vehicle";
  162. }
  163. auto mine_tool_ptr = card_ptr->get_mine_tool();
  164. auto start = mine_tool_ptr->m_attendance_start_time;
  165. auto end = mine_tool_ptr->m_attendance_start_time;
  166. if(!mine_tool_ptr->is_attendance())//考勤结束时间
  167. {
  168. end = std::chrono::system_clock::time_point(std::chrono::milliseconds(card_ptr->m_time));
  169. }
  170. std::string start_str = tool_time::to_str(start);
  171. std::string end_str = tool_time::to_str(end);
  172. int landmarkid = 0;
  173. int landmarkdirect=0;
  174. double landmarkdist=0;
  175. auto area_hover_ptr = card_ptr->get_area_hover();
  176. if(area_hover_ptr)
  177. {
  178. landmarkid = area_hover_ptr->landmark_id;
  179. landmarkdirect = area_hover_ptr->landmark_dir;
  180. landmarkdist = area_hover_ptr->landmark_dis;
  181. }
  182. sprintf(sql, "CALL %s(%s, %d, '%s', '%s', %d, %d, %.3f);", call.c_str(),
  183. card_list::to_id64_str(card_ptr->m_type, card_ptr->m_id).c_str(),
  184. card_ptr->m_id, start_str.c_str(), end_str.c_str(),
  185. landmarkid, landmarkdirect, landmarkdist);
  186. PushAsync(sql);
  187. }
  188. };
  189. class tool_map
  190. {
  191. public:
  192. static bool try_get_value(sio::message::ptr& out_data,
  193. const char* key, sio::message::ptr const& data)
  194. {
  195. auto map=data->get_map()[key];
  196. if(map && sio::message::flag_object == map->get_flag())
  197. {
  198. out_data = map;
  199. return true;
  200. }
  201. return false;
  202. }
  203. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  204. {
  205. auto map=data->get_map()[key];
  206. if(map && sio::message::flag_integer == map->get_flag())
  207. {
  208. out_data = map->get_int();
  209. return true;
  210. }
  211. return false;
  212. }
  213. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  214. {
  215. auto map=data->get_map()[key];
  216. if(map && sio::message::flag_integer == map->get_flag())
  217. {
  218. out_data = static_cast<uint32_t>(map->get_int());
  219. return true;
  220. }
  221. return false;
  222. }
  223. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  224. {
  225. auto map=data->get_map()[key];
  226. if(map && sio::message::flag_integer == map->get_flag())
  227. {
  228. out_data = static_cast<int>(map->get_int());
  229. return true;
  230. }
  231. return false;
  232. }
  233. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  234. {
  235. auto map=data->get_map()[key];
  236. if(map && sio::message::flag_string == map->get_flag())
  237. {
  238. out_data = map->get_string();
  239. return true;
  240. }
  241. return false;
  242. }
  243. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  244. {
  245. auto map=data->get_map()[key];
  246. if(map && sio::message::flag_double == map->get_flag())
  247. {
  248. out_data = map->get_double();
  249. return true;
  250. }
  251. return false;
  252. }
  253. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  254. const char* key, sio::message::ptr const& data)
  255. {
  256. auto map=data->get_map()[key];
  257. if(map && sio::message::flag_array == map->get_flag())
  258. {
  259. out_data = map->get_vector();
  260. return true;
  261. }
  262. return false;
  263. }
  264. };
  265. class tool_json
  266. {
  267. public:
  268. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  269. rapidjson::Document::AllocatorType& allocator)
  270. {
  271. rapidjson::Value name;
  272. name.SetString(key, allocator);
  273. rapidjson::Value data;
  274. data.SetString(value.c_str(), allocator);
  275. out_data.AddMember(name, data, allocator);
  276. }
  277. static void push_back(rapidjson::Value& out_data, std::string value,
  278. rapidjson::Document::AllocatorType& allocator)
  279. {
  280. rapidjson::Value data;
  281. data.SetString(value.c_str(), allocator);
  282. out_data.PushBack(data, allocator);
  283. }
  284. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  285. rapidjson::Value::ConstMemberIterator& out_iter)
  286. {
  287. if(node.IsObject())
  288. {
  289. out_iter = node.FindMember(key);
  290. if(node.MemberEnd() == out_iter)
  291. {
  292. return false;
  293. }
  294. return true;
  295. }
  296. return false;
  297. }
  298. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  299. {
  300. rapidjson::Value::ConstMemberIterator iter;
  301. if(try_get_iter(key, node, iter))
  302. {
  303. if(iter->value.IsInt())
  304. {
  305. d = iter->value.GetInt();
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  312. {
  313. rapidjson::Value::ConstMemberIterator iter;
  314. if(try_get_iter(key, node, iter))
  315. {
  316. if(iter->value.IsUint64())
  317. {
  318. d = iter->value.GetUint64();
  319. return true;
  320. }
  321. }
  322. return false;
  323. }
  324. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  325. {
  326. rapidjson::Value::ConstMemberIterator iter;
  327. if(try_get_iter(key, node, iter))
  328. {
  329. if(iter->value.IsDouble())
  330. {
  331. d = iter->value.GetDouble();
  332. return true;
  333. }
  334. }
  335. return false;
  336. }
  337. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  338. {
  339. rapidjson::Value::ConstMemberIterator iter;
  340. if(try_get_iter(key, node, iter))
  341. {
  342. if(iter->value.IsString())
  343. {
  344. d = iter->value.GetString();
  345. return true;
  346. }
  347. }
  348. return false;
  349. }
  350. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  351. {
  352. rapidjson::Value::ConstMemberIterator iter;
  353. if(try_get_iter(key, node, iter))
  354. {
  355. if(iter->value.IsInt())
  356. {
  357. return iter->value.GetInt();
  358. }
  359. }
  360. return default_data;
  361. }
  362. static std::string get_value(const char* key, const std::string& 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.IsString())
  368. {
  369. return iter->value.GetString();
  370. }
  371. }
  372. return default_data;
  373. }
  374. static std::string doc_to_json(rapidjson::Document& doc)
  375. {
  376. rapidjson::StringBuffer sb;
  377. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  378. doc.Accept(writer);
  379. return sb.GetString();
  380. }
  381. };
  382. #endif // COMMON_TOOL_H