123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- #ifndef COMMON_TOOL_H
- #define COMMON_TOOL_H
- /**
- * @brief 基楚的工具类
- * @author 戴月腾
- * @date 2018-09-15
- */
- #include "common.h"
- #include "log.h"
- #include <chrono>
- #include <string>
- #include <sys/time.h>
- #include "db_api/CDBConnPool.h"
- #include "websocket/wsClientMgr.h"
- #include "card.h"
- #include "mine.h"
- class tool_time
- {
- public:
- static uint32_t elapse_seconds(std::chrono::system_clock::time_point &start)
- {
- return static_cast<uint32_t>( std::chrono::duration_cast<std::chrono::seconds>
- (std::chrono::system_clock::now() - start).count() );
- }
- static uint64_t elapse_ms(std::chrono::system_clock::time_point &start)
- {
- return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
- (std::chrono::system_clock::now() - start).count() );
- }
- static uint32_t now_to_seconds()
- {
- return static_cast<uint32_t>( std::chrono::duration_cast<std::chrono::seconds>
- (std::chrono::system_clock::now().time_since_epoch()).count() );
- }
- static uint64_t now_to_ms()
- {
- return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
- (std::chrono::system_clock::now().time_since_epoch()).count() );
- }
- static uint64_t now_to_us()
- {
- return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::microseconds>
- (std::chrono::system_clock::now().time_since_epoch()).count() );
- }
- static uint64_t to_ms(const std::chrono::system_clock::time_point &time)
- {
- return static_cast<uint64_t>( std::chrono::duration_cast<std::chrono::milliseconds>
- (time.time_since_epoch()).count() );
- }
- static std::string to_str(const std::chrono::system_clock::time_point &time)
- {
- char _time[25] = {0};
- time_t tt = std::chrono::system_clock::to_time_t(time);
- struct tm local_time;
- localtime_r(&tt, &local_time);
- strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);
- return std::string(_time);
- }
- static uint64_t morning_of_today_ms()
- {
- std::time_t now = time(0);
- struct tm * loc_t = localtime(&now);
- loc_t->tm_hour=0;loc_t->tm_min=0;loc_t->tm_sec=0;
- now = mktime(loc_t);
- return now*1000;
- }
- //"%Y-%m-%d %H:%M:%S"
- static time_t to_time(std::string str)
- {
- time_t t_;
- tm tm_;
- strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
- t_ = mktime(&tm_); //将tm时间转换为秒时间
- return t_;
- }
- ////"%d-%02d-%02d %02d:%02d:%02d.%03d"
- static std::chrono::system_clock::time_point to_time_ex(std::string str)
- {
- uint64_t pos = str.length()-3;
- time_t t_;
- tm tm_;
- strptime(str.substr(0,pos).c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
- t_ = mktime(&tm_); //将tm时间转换为秒时间
- int milli = std::stoi(str.substr(pos));
- return std::chrono::system_clock::time_point(std::chrono::milliseconds(t_*1000 + milli));
- }
- //"%d-%02d-%02d %02d:%02d:%02d.%03d"
- static std::string to_str_ex(uint64_t ms)
- {
- int32_t mill = ms%1000;
- char _time[25] = {0};
- time_t tt = ms/1000;
- struct tm *local_time=localtime(&tt);
- //strftime(_time, 22, "%Y-%m-%d %H:%M:%S", local_time);
- sprintf(_time, "%d-%02d-%02d %02d:%02d:%02d.%03d", local_time->tm_year+1900,
- local_time->tm_mon+1, local_time->tm_mday, local_time->tm_hour,
- local_time->tm_min, local_time->tm_sec, mill);
- return std::string(_time);
- }
- //"%d-%02d-%02d %02d:%02d:%02d.%03d"
- static std::string to_str_ex(std::chrono::system_clock::time_point time)
- {
- return to_str_ex(to_ms(time));
- }
- static int elapse_seconds(time_t &start)
- {
- time_t now;
- time(&now);
- return static_cast<int>(std::difftime(now, start));
- }
- //"%Y-%m-%d %H:%M:%S"
- static std::string to_str(const std::time_t &time)
- {
- char _time[25] = {0};
- struct tm local_time;
- localtime_r(&time, &local_time);
- strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);
- return std::string(_time);
- }
- };
- class tool_other
- {
- public:
- static void send_json(const std::string& cmd, const std::string& data)
- {
- log_info("发送json: cmd=%s, data=%s\n", cmd.c_str(), data.c_str());
- swsClientMgr.send(cmd, data);
- }
- static std::string to13str(std::string& str)
- {
- uint64_t tmp = std::stoull(str);
- return to13str(tmp);
- }
- static std::string to13str(uint64_t data)
- {
- char ss[20]={0};
- sprintf(ss, "%013ld", data);
- return std::string(ss);
- }
- static uint32_t id64_to_id(std::string& str)
- {
- return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
- }
- static int id64_to_type(std::string& str)
- {
- return std::stoi(to13str(str).substr(0, 3));
- }
- static bool is_person(int32_t type)
- {
- return CT_PERSON == type;
- }
- static bool is_vehicle(int32_t type)
- {
- return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
- }
- // static uint32_t id64_to_id(uint64_t card_id)
- // {
- // return static_cast<uint32_t>(card_id);
- // }
- // static int id64_to_type(uint64_t card_id)
- // {
- // return static_cast<int32_t>(card_id>>32);
- // }
- };
- class tool_db
- {
- public:
- static void PushAsync(char* sql)
- {
- if(!sDBConnPool.PushAsync(sql))
- {
- log_error( "PushAsync记录到队列中失败\n");
- }
- }
- static void save_attendance(const std::shared_ptr<card_location_base>& card_ptr)
- {
- char sql[LENGTH_SQL] = {0};
- std::string call("add_att_staff");
- if(card_ptr->is_vehicle())//车卡
- {
- call="add_att_vehicle";
- }
- auto mine_tool_ptr = card_ptr->get_mine_tool();
- auto start = mine_tool_ptr->m_attendance_start_time;
- auto end = mine_tool_ptr->m_attendance_start_time;
- if(!mine_tool_ptr->is_attendance())//考勤结束时间
- {
- end = std::chrono::system_clock::now();
- }
- std::string start_str = tool_time::to_str(start);
- std::string end_str = tool_time::to_str(end);
- int landmarkid = 0;
- int landmarkdirect=0;
- double landmarkdist=0;
- auto area_hover_ptr = card_ptr->get_area_hover();
- if(area_hover_ptr)
- {
- landmarkid = area_hover_ptr->landmark_id;
- landmarkdirect = area_hover_ptr->landmark_dir;
- landmarkdist = area_hover_ptr->landmark_dis;
- }
- sprintf(sql, "CALL %s(%s, %d, '%s', '%s', %d, %d, %.3f);", call.c_str(),
- card_list::to_id64_str(card_ptr->m_type, card_ptr->m_id).c_str(),
- card_ptr->m_id, start_str.c_str(), end_str.c_str(),
- landmarkid, landmarkdirect, landmarkdist);
- PushAsync(sql);
- }
- };
- class tool_map
- {
- public:
- static bool try_get_value(sio::message::ptr& out_data,
- const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_object == map->get_flag())
- {
- out_data = map;
- return true;
- }
- return false;
- }
- static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_integer == map->get_flag())
- {
- out_data = map->get_int();
- return true;
- }
- return false;
- }
- static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_integer == map->get_flag())
- {
- out_data = static_cast<uint32_t>(map->get_int());
- return true;
- }
- return false;
- }
- static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_integer == map->get_flag())
- {
- out_data = static_cast<int>(map->get_int());
- return true;
- }
- return false;
- }
- static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_string == map->get_flag())
- {
- out_data = map->get_string();
- return true;
- }
- return false;
- }
- static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_double == map->get_flag())
- {
- out_data = map->get_double();
- return true;
- }
- return false;
- }
- static bool try_get_value(std::vector<sio::message::ptr>& out_data,
- const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_array == map->get_flag())
- {
- out_data = map->get_vector();
- return true;
- }
- return false;
- }
- };
- class tool_json
- {
- public:
- static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
- rapidjson::Document::AllocatorType& allocator)
- {
- rapidjson::Value name;
- name.SetString(key, allocator);
- rapidjson::Value data;
- data.SetString(value.c_str(), allocator);
- out_data.AddMember(name, data, allocator);
- }
- static void push_back(rapidjson::Value& out_data, std::string value,
- rapidjson::Document::AllocatorType& allocator)
- {
- rapidjson::Value data;
- data.SetString(value.c_str(), allocator);
- out_data.PushBack(data, allocator);
- }
- static bool try_get_iter(const char* key, const rapidjson::Value& node,
- rapidjson::Value::ConstMemberIterator& out_iter)
- {
- if(node.IsObject())
- {
- out_iter = node.FindMember(key);
- if(node.MemberEnd() == out_iter)
- {
- return false;
- }
- return true;
- }
- return false;
- }
- static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsInt())
- {
- d = iter->value.GetInt();
- return true;
- }
- }
- return false;
- }
- static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsUint64())
- {
- d = iter->value.GetUint64();
- return true;
- }
- }
- return false;
- }
- static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsDouble())
- {
- d = iter->value.GetDouble();
- return true;
- }
- }
- return false;
- }
- static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsString())
- {
- d = iter->value.GetString();
- return true;
- }
- }
- return false;
- }
- static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsInt())
- {
- return iter->value.GetInt();
- }
- }
- return default_data;
- }
- static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsString())
- {
- return iter->value.GetString();
- }
- }
- return default_data;
- }
- static std::string doc_to_json(rapidjson::Document& doc)
- {
- rapidjson::StringBuffer sb;
- rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
- doc.Accept(writer);
- return sb.GetString();
- }
- };
- #endif // COMMON_TOOL_H
|