1
0

common_tool.h 12 KB

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