common_tool.h 13 KB

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