common_tool.h 13 KB

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