123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- #include "event.h"
- #include <string>
- #include "ant.h"
- #include "area.h"
- #include "card.h"
- #include "common_tool.h"
- #include "websocket/constdef.h"
- #include "db/db_api/CDBSingletonDefine.h"
- #include "tool_time.h"
- #include "ant.h"
- #include "db/db_tool.h"
- #include "mine.h"
- #include "module_service/module_traffic_light_manager.h"
- uint64_t ya_event::get_list_id()
- {
- return m_id;
- }
- struct Event
- {
- OBJECT_TYPE m_oid;
- Event(OBJECT_TYPE oid)
- :m_oid(oid)
- {}
- void handle_alarm_event(EVENT_TYPE et,uint64_t id,double limit_value,double cur_value,bool f,EVENT_DIS_TYPE edt,const std::string &desc);
- virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f)=0;
- std::shared_ptr<ya_event> create_event(const std::string&obj_id,EVENT_TYPE ev_type);
- virtual ~Event(){}
- };
- struct mine_event:Event
- {
- mine_event()
- :Event(OT_MORE_CARD)
- {}
- virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
- };
- struct area_event:Event
- {
- area_event()
- :Event(OT_AREA)
- {}
- virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
- };
- struct device_reader_event:Event
- {
- device_reader_event()
- :Event(OT_DEVICE_READER)
- {}
- virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
- };
- struct card_event:Event
- {
- card_event()
- :Event(OT_CARD)
- {}
- virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
- };
- struct light_event:Event{
- light_event()
- : Event(OT_DEVICE_LIGHT)
- {}
- virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et, uint64_t id, bool f);
- };
- static event_tool et;
- event_tool * event_tool::instance()
- {
- return &et;
- }
- void event_tool::make_event_object()
- {
- m_map[OT_MORE_CARD] = std::make_shared<mine_event>();
- m_map[OT_AREA] = std::make_shared<area_event>();
- m_map[OT_DEVICE_READER] = std::make_shared<device_reader_event>();
- m_map[OT_CARD] = std::make_shared<card_event>();
- m_map[OT_DEVICE_LIGHT] = std::make_shared<light_event>();
- }
- void event_tool::handle_event(OBJECT_TYPE ot,EVENT_TYPE et,uint64_t id,double limit_value,double cur_value,bool f,EVENT_DIS_TYPE edt/*=DT_COMMON*/,const std::string &desc/*=""*/)
- {
- m_map[ot]->handle_alarm_event(et,id,limit_value,cur_value,f,edt,desc);
- }
- std::shared_ptr<ya_event> Event::create_event(const std::string&obj_id,EVENT_TYPE ev_type)
- {
- auto ev_ptr = std::make_shared<ya_event>(tool_time::now_to_us());
- ev_ptr->m_ev_type = ev_type;
- ev_ptr->m_obj_type=m_oid;
- ev_ptr->m_obj_id = obj_id;
- return ev_ptr;
- }
- /*
- * @breif 构建开始或者取消告警事件
- * @param EVENT_TYPE et 事件设备类型
- * @param uint64_t id
- * @param double limit_value 阈值
- * @param double cur_value 当前值
- * @param bool f 告警标志,true表示开始告警,false表示取消告警
- * @param EVENT_DIS_TYPE edt
- * @param const std::string& desc 描述
- * @return 无
- * @note
- * @bug
- * @warning
- * */
- void Event::handle_alarm_event(EVENT_TYPE et,uint64_t id,double limit_value,double cur_value,bool f,EVENT_DIS_TYPE edt,const std::string &desc)
- {
- std::shared_ptr<ya_event> ev_ptr = nullptr;
- uint64_t eid = event_list::to_list_id(et,m_oid,id,edt);
- auto event_ptr = event_list::instance()->get(eid);
- if(f)
- {
- if(event_ptr)
- {
- //已经存在告警
- event_ptr->m_cur_value = cur_value;
- if(et!=ET_READER_ERROR)
- event_ptr->m_is_sent=false;
- }
- else
- {
- uint64_t _id=id;
- //log_info("Create_Event:desc[%s],id:%d,et:%d",desc.c_str(),id,et);
- if((et == ET_UWB_MORE_CARD||et == ET_VEHICLE_REAR_END) && !desc.empty())//防追尾告警特殊处理
- {
- std::string cardid = desc.substr(0,desc.find_first_of('&'));
- _id = tool_other::card_id_to_u64(cardid);
- }
- ev_ptr=on_message(et,_id,f);
- if(ev_ptr){
- ev_ptr->m_cur_value=cur_value;
- ev_ptr->m_limit_value=limit_value;
- ev_ptr->m_desc=desc;
- ev_ptr->m_id=eid;
- ev_ptr->m_dis_type=edt;
- if(et == ET_VEHICLE_REAR_END||et==ET_UWB_MORE_CARD)//一人多卡特殊处理
- {
- ev_ptr->m_obj_id=desc;
- //ev_ptr->m_cur_time=std::chrono::system_clock::time_point(std::chrono::milliseconds((time_t)limit_value * 1000));
- }
- //保存告警信息
- event_list::instance()->add(eid,ev_ptr);
- }
- }
- }
- else
- {
- if(event_ptr && !event_ptr->is_end())
- {
- //取消告警
- event_ptr->m_cur_time = std::chrono::system_clock::now();
- event_ptr->m_status = ES_END;
- event_ptr->m_cur_value = cur_value;
- event_ptr->m_desc=desc;
- event_ptr->m_is_sent=false;
- ev_ptr=event_ptr;
- }
- }
- if(ev_ptr)
- {
- event_list::save_event(ev_ptr);
- }
- }
- std::shared_ptr<ya_event> mine_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
- {
- std::shared_ptr<ya_event> event_ptr=nullptr;
- if(f){
- event_ptr=create_event(std::to_string(id), et);
- auto card_ptr = card_list::instance()->get(id);
- if(!card_ptr)
- {
- event_ptr->m_map_id=5;
- return event_ptr;
- }
- if(auto site_ptr=card_ptr->get_area_tool()->m_site)
- {
- event_ptr->m_area_id = site_ptr->m_area_id;
- event_ptr->m_map_id = site_ptr->m_map_id;
- }
- }
- return event_ptr;
- }
- std::shared_ptr<ya_event> area_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
- {
- std::shared_ptr<ya_event> event_ptr=nullptr;
- if(f)
- {
- auto area_ptr = area_list::instance()->get(id);
- if(!area_ptr)
- {
- log_error("create_event .can not find area ..%d",id);
- return event_ptr;
- }
- event_ptr=create_event(std::to_string(id), et);
- event_ptr->m_area_id = id;
- event_ptr->m_map_id = area_ptr->mapid();
- }
- return event_ptr;
- }
- std::shared_ptr<ya_event> device_reader_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
- {
- std::shared_ptr<ya_event> event_ptr=nullptr;
- if(f)
- {
- auto site_ptr = sit_list::instance()->get(id);
- if(!site_ptr)
- {
- log_info("create_event.can not find site :%d",id);
- return event_ptr;
- }
- if(site_ptr->m_special){
- //log_info()
- return event_ptr;
- }
- event_ptr=create_event(std::to_string(id),et);
- event_ptr->m_area_id = site_ptr->m_area_id;
- event_ptr->m_map_id = site_ptr->m_map_id;
- event_ptr->x = site_ptr->x;
- event_ptr->y = site_ptr->y;
- }
- return event_ptr;
- }
- std::shared_ptr<ya_event> card_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
- {
- std::shared_ptr<ya_event> event_ptr=nullptr;
- if(f)
- {
- auto card_ptr = card_list::instance()->get(id);
- std::string card_id = tool_other::get_string_cardid(id);
- if(!card_ptr)
- {
- log_error("Create_event.can not find card:%s",card_id.c_str());
- return event_ptr;
- }
- event_ptr=create_event(card_id,et);
- event_ptr->x = card_ptr->x;
- event_ptr->y = card_ptr->y;
- //event_ptr->m_is_display = card_ptr->m_display;
-
- if(const auto area_ptr=card_ptr->get_area_tool()) {
- const auto area_info = area_ptr->m_area_info;
- if(!area_info.empty()){
- const auto x=area_info.cbegin()->second;
- event_ptr->landmark_id=std::get<1>(x);
- event_ptr->landmark_dir=std::get<2>(x);
- event_ptr->landmark_dis=std::get<3>(x);
- }
- if(const auto site_ptr=area_ptr->m_site){
- event_ptr->m_area_id = site_ptr->m_area_id;
- event_ptr->m_map_id = site_ptr->m_map_id;
- }
- }
- }
- return event_ptr;
- }
- std::shared_ptr<ya_event> light_event::on_message(EVENT_TYPE et, uint64_t id, bool f)
- {
- std::shared_ptr<ya_event> event_ptr = nullptr;
- if(f){
- auto light_ptr = traffic_light_manager::instance()->get(id);
- if(!light_ptr)
- {
- log_info("create_event.can not find light :%d",id);
- return event_ptr;
- }
- event_ptr = create_event(std::to_string(id), et);
- event_ptr->m_area_id = light_ptr->m_area_id;
- event_ptr->m_map_id = light_ptr->m_map_id;
- event_ptr->x = light_ptr->x;
- event_ptr->y = light_ptr->y;
-
- }
- return event_ptr;
- }
- std::shared_ptr<ya_event> event_list::get_event_card(uint32_t card_id, int card_type, EVENT_TYPE ev_type,EVENT_DIS_TYPE edt)
- {
- uint64_t id64 = tool_other::type_id_to_u64(card_type, card_id);
- return base::get(to_list_id(ev_type, OT_CARD, id64,edt));
- }
- void event_list::save_event(const std::shared_ptr<ya_event> &ev_ptr)
- {
- char sql[LENGTH_SQL] = {0};
- std::string _time = tool_time::to_str_ex(ev_ptr->m_cur_time);
- sprintf(sql,
- "INSERT IGNORE INTO his_event_data(event_id,id, stat, event_type_id, obj_type_id, obj_id, dis_type,\
- map_id, area_id, limit_value, cur_value, x, y, cur_time, description,landmark_id,landmark_dir,landmark_dist) \
- VALUES(%ld,%ld, %d, %d, %d, %s, %d, %d, %d, %.2f, %.2f, %f, %f, '%s', '%s',%d,%d,%.2f);",
- ev_ptr->m_ev_id,ev_ptr->m_id,ev_ptr->m_status, ev_ptr->m_ev_type, ev_ptr->m_obj_type, ev_ptr->m_obj_id.c_str(),ev_ptr->m_dis_type,
- ev_ptr->m_map_id, ev_ptr->m_area_id, ev_ptr->m_limit_value, ev_ptr->m_cur_value, ev_ptr->x, ev_ptr->y,
- _time.c_str(), ev_ptr->m_desc.c_str(),ev_ptr->landmark_id,ev_ptr->landmark_dir,ev_ptr->landmark_dis);
- db_tool::PushAsync(sql);
- }
- void event_list::load_his_data_from_db(bool init /*=true*/)
- {
- static std::time_t s_last_time=0;
- if(!init){
- std::time_t t=time(0);
- if(t-s_last_time<15)return;
- s_last_time=t;
- }
- std::unordered_map<uint64_t, std::shared_ptr<ya_event>> map;
- std::string sql("SELECT event_id, id,stat,event_type_id,obj_type_id,obj_id,dis_type,map_id,area_id,\
- limit_value,cur_value,x,y, cur_time FROM his_event_data \
- WHERE event_id IN (SELECT MAX(event_id) FROM his_event_data \
- WHERE cur_time > (CASE obj_type_id \
- WHEN 1 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
- WHEN 4 THEN DATE_SUB(NOW(),INTERVAL 1 MONTH)\
- WHEN 9 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
- WHEN 10 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
- END) \
- AND cur_time < NOW()\
- GROUP BY event_type_id, obj_id,dis_type) \
- AND event_id NOT IN ( SELECT event_id FROM his_event_data WHERE stat=100)\
- AND event_type_id NOT IN (21,22,31,36) and obj_type_id !=2;");
- if(!init)
- sql="SELECT event_id, id,stat,event_type_id,obj_type_id,obj_id,dis_type,map_id,area_id,limit_value,cur_value,x,y, cur_time FROM his_event_data WHERE cur_time > date_sub(NOW(),interval 20 second) and source=1 order by stat;";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- if(!Error.empty())
- log_error("初始化事件列表 Error,%s",Error.c_str());
- uint64_t nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "init_event_list. The record count=%ld\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- long long int event_id = 0;
- DBRes.GetField( "event_id",event_id, Error );
- long long int id = 0;
- DBRes.GetField( "id",id, Error );
- int event_type_id = 0;
- DBRes.GetField( "event_type_id",event_type_id, Error );
- int obj_type_id = 0;
- DBRes.GetField( "obj_type_id",obj_type_id, Error );
- std::string obj_id = "";
- DBRes.GetField( "obj_id",obj_id, Error );
- int dis_type = 0;
- DBRes.GetField( "dis_type",dis_type, Error );
- int map_id = 0;
- DBRes.GetField( "map_id",map_id, Error );
- int area_id = 0;
- DBRes.GetField( "area_id",area_id, Error );
- double limit_value = 0;
- DBRes.GetField( "limit_value",limit_value, Error );
- double cur_value = 0;
- DBRes.GetField( "cur_value",cur_value, Error );
- double x = 0;
- DBRes.GetField( "x",x, Error );
- double y = 0;
- DBRes.GetField( "y",y, Error );
- std::string cur_time = "";
- DBRes.GetField( "cur_time",cur_time, Error );
- int stat = 0;
- DBRes.GetField( "stat",stat, Error );
- //增加虚拟告警处理逻辑
- std::shared_ptr<ya_event> ev=nullptr;
- bool flag=false;
- if(!init){
- ev=event_list::instance()->get(id);
- if(!ev){
- ev = std::make_shared<ya_event>(event_id);
- }else{
- flag=true;
- if(stat==100)ev->m_is_sent=false;
- }
- }else{
- ev = std::make_shared<ya_event>(event_id);
- }
- ev->m_status = static_cast<EVENT_STATUS>(stat);
- ev->m_ev_type = static_cast<EVENT_TYPE>(event_type_id);
- ev->m_obj_type = static_cast<OBJECT_TYPE>(obj_type_id);
- ev->m_dis_type = static_cast<EVENT_DIS_TYPE>(dis_type);
- ev->m_obj_id = obj_id;
- ev->m_cur_time = tool_time::to_time_ex(cur_time);
- ev->m_cur_value = cur_value;
- ev->m_limit_value = limit_value;
- ev->m_map_id = map_id;
- ev->m_area_id = area_id;
- ev->x = x;
- ev->y = y;
- ev->m_id = id;
- //这里当是卡告警的时候,对m_event数据进行赋值
- //方便清理
- //备注防追尾告警和一人多卡告警可能不适用,后续整理
- if(ev->m_obj_type==OT_CARD){
- uint64_t c_id= tool_other::card_id_to_u64(ev->m_obj_id);
- if(auto c=card_list::instance()->get(c_id)){
- if(c->m_type==CT_PERSON){
- auto mine_tool_ptr = c->get_mine_tool();
- if(!mine_tool_ptr->m_is_attendance) {
- log_warn("person_not_att:%s",obj_id.c_str());
- continue;
- }
- }
- c->set_event_flag(ev->m_ev_type);
- if(event_type_id==ET_CARD_LOW_POWER_SERIOUS)
- c->m_pwr_stat=STATUS_POWER_LOWER_SERIOUS;
- } else {
- log_warn("load_evnet_history:card_id:%s not exist...",obj_id.c_str());
- continue;
- }
- }else if(ev->m_obj_type==OT_DEVICE_READER){
- auto sit_ptr=sit_list::instance()->get(std::stoi(obj_id));
- if(!sit_ptr){
- log_warn("load_evnet_history:site_id:%s not exist...",obj_id.c_str());
- continue;
- }
- if(ev->m_ev_type==ET_READER_POWER_BY_BATTERY)
- sit_ptr->m_power_ac_down=true;
- if(ev->m_ev_type==ET_READER_ERROR)
- sit_ptr->m_time=tool_time::to_ms(ev->m_cur_time)/1000;
- }
- if(!flag)
- map.insert(std::make_pair(id, ev));
- log_info("event_list %lld,%lld,%d,%d,%d,%s,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%s"
- , ev->m_ev_id,ev->m_id,ev->m_status,ev->m_ev_type,ev->m_obj_type,ev->m_obj_id.c_str(),ev->m_dis_type
- ,ev->m_map_id,ev->m_area_id,ev->m_limit_value,ev->m_cur_value
- ,ev->x,ev->y
- ,tool_time::to_str_ex(ev->m_cur_time).c_str());
- }
- if(!map.empty())
- event_list::instance()->add(map);
- }
- }
- std::string event_list::evs_to_json(std::vector<std::shared_ptr<ya_event>> arr)
- {
- rapidjson::Document doc(rapidjson::kObjectType);
- rapidjson::Value data(rapidjson::kArrayType);
- rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
- auto it=arr.begin();
- for(;it!=arr.end();++it)
- {
- _ev_to_node(*it, allocator, data);
- }
- doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_EVENT, allocator);
- doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
- doc.AddMember(JSON_ROOT_KEY_DATA,data, allocator);
- rapidjson::StringBuffer sb;
- rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
- doc.Accept(writer);
- return sb.GetString();
- }
- void event_list::_ev_to_node(std::shared_ptr<ya_event> ev_ptr,
- rapidjson::Document::AllocatorType& allocator,
- rapidjson::Value& out_data)
- {
- rapidjson::Value ev(rapidjson::kObjectType);
- //ev.AddMember(JSON_KEY_EVENT_EVENT_ID,ev_ptr->m_id, allocator);
- tool_json::add_member(ev, JSON_KEY_EVENT_EVENT_ID, std::to_string(ev_ptr->m_id), allocator);
- ev.AddMember(JSON_KEY_EVENT_STATUS,ev_ptr->m_status, allocator);
- ev.AddMember(JSON_KEY_EVENT_TYPE_ID,ev_ptr->m_ev_type, allocator);
- ev.AddMember(JSON_KEY_EVENT_OBJ_TYPE_ID,ev_ptr->m_obj_type, allocator);
- tool_json::add_member(ev, JSON_KEY_EVENT_OBJ_ID, ev_ptr->m_obj_id, allocator);
- ev.AddMember(JSON_KEY_EVENT_DIS_TYPE_ID,ev_ptr->m_dis_type, allocator);
- ev.AddMember(JSON_KEY_EVENT_MAP_ID,ev_ptr->m_map_id, allocator);
- ev.AddMember(JSON_KEY_EVENT_AREA_ID,ev_ptr->m_area_id, allocator);
- ev.AddMember(JSON_KEY_EVENT_X,ev_ptr->x, allocator);
- ev.AddMember(JSON_KEY_EVENT_Y,ev_ptr->y, allocator);
- ev.AddMember(JSON_KEY_EVENT_LIMIT_VALUE,ev_ptr->m_limit_value, allocator);
- ev.AddMember(JSON_KEY_EVENT_CUR_VALUE,ev_ptr->m_cur_value, allocator);
- ev.AddMember(JSON_KEY_EVENT_CUR_TIME,tool_time::to_ms(ev_ptr->m_cur_time), allocator);
- out_data.PushBack(ev, allocator);
- }
- //template<> std::shared_ptr<event_list> single_base<event_list, uint64_t, std::shared_ptr<ya_event>>::m_instance=std::make_shared<event_list>();
|