event.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include "event.h"
  2. #include<string>
  3. #include "ant.h"
  4. #include "area.h"
  5. #include "card.h"
  6. #include "common_tool.h"
  7. #include "websocket/constdef.h"
  8. #include "db/db_api/CDBSingletonDefine.h"
  9. #include"tool_time.h"
  10. #include"ant.h"
  11. #include"db/db_tool.h"
  12. #include "mine.h"
  13. uint64_t ya_event::get_list_id()
  14. {
  15. return m_id;
  16. }
  17. struct Event
  18. {
  19. OBJECT_TYPE m_oid;
  20. Event(OBJECT_TYPE oid)
  21. :m_oid(oid)
  22. {}
  23. 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);
  24. virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f)=0;
  25. std::shared_ptr<ya_event> create_event(const std::string&obj_id,EVENT_TYPE ev_type);
  26. virtual ~Event(){}
  27. };
  28. struct mine_event:Event
  29. {
  30. mine_event()
  31. :Event(OT_MORE_CARD)
  32. {}
  33. virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
  34. };
  35. struct area_event:Event
  36. {
  37. area_event()
  38. :Event(OT_AREA)
  39. {}
  40. virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
  41. };
  42. struct device_reader_event:Event
  43. {
  44. device_reader_event()
  45. :Event(OT_DEVICE_READER)
  46. {}
  47. virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
  48. };
  49. struct card_event:Event
  50. {
  51. card_event()
  52. :Event(OT_CARD)
  53. {}
  54. virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
  55. };
  56. static event_tool et;
  57. event_tool * event_tool::instance()
  58. {
  59. return &et;
  60. }
  61. void event_tool::make_event_object()
  62. {
  63. m_map[OT_MORE_CARD] = std::make_shared<mine_event>();
  64. m_map[OT_AREA] = std::make_shared<area_event>();
  65. m_map[OT_DEVICE_READER] = std::make_shared<device_reader_event>();
  66. m_map[OT_CARD] = std::make_shared<card_event>();
  67. }
  68. 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/*=""*/)
  69. {
  70. m_map[ot]->handle_alarm_event(et,id,limit_value,cur_value,f,edt,desc);
  71. }
  72. std::shared_ptr<ya_event> Event::create_event(const std::string&obj_id,EVENT_TYPE ev_type)
  73. {
  74. auto ev_ptr = std::make_shared<ya_event>(tool_time::now_to_us());
  75. ev_ptr->m_ev_type = ev_type;
  76. ev_ptr->m_obj_type=m_oid;
  77. ev_ptr->m_obj_id = obj_id;
  78. return ev_ptr;
  79. }
  80. 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)
  81. {
  82. std::shared_ptr<ya_event> ev_ptr = nullptr;
  83. uint64_t eid = event_list::to_list_id(et,m_oid,id,edt);
  84. auto event_ptr = event_list::instance()->get(eid);
  85. if(f)
  86. {
  87. if(event_ptr)
  88. {
  89. //已经存在告警
  90. event_ptr->m_cur_value = cur_value;
  91. if(et!=ET_READER_ERROR)
  92. event_ptr->m_is_sent=false;
  93. }
  94. else
  95. {
  96. uint64_t _id=id;
  97. log_info("Create_Event:desc[%s],id:%d,et:%d",desc.c_str(),id,et);
  98. if((et==ET_UWB_MORE_CARD||et == ET_VEHICLE_REAR_END)&&!desc.empty())//防追尾告警特殊处理
  99. {
  100. std::string cardid = desc.substr(0,desc.find_first_of('&'));
  101. _id = tool_other::card_id_to_u64(cardid);
  102. }
  103. ev_ptr=on_message(et,_id,f);
  104. if(ev_ptr){
  105. ev_ptr->m_cur_value=cur_value;
  106. ev_ptr->m_limit_value=limit_value;
  107. ev_ptr->m_desc=desc;
  108. ev_ptr->m_id=eid;
  109. ev_ptr->m_dis_type=edt;
  110. if(et == ET_VEHICLE_REAR_END||et==ET_UWB_MORE_CARD)//一人多卡特殊处理
  111. {
  112. ev_ptr->m_obj_id=desc;
  113. //ev_ptr->m_cur_time=std::chrono::system_clock::time_point(std::chrono::milliseconds((time_t)limit_value * 1000));
  114. }
  115. //保存告警信息
  116. event_list::instance()->add(eid,ev_ptr);
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if(event_ptr && !event_ptr->is_end())
  123. {
  124. //取消告警
  125. event_ptr->m_cur_time = std::chrono::system_clock::now();
  126. event_ptr->m_status = ES_END;
  127. event_ptr->m_cur_value = cur_value;
  128. event_ptr->m_desc=desc;
  129. event_ptr->m_is_sent=false;
  130. ev_ptr=event_ptr;
  131. }
  132. }
  133. if(ev_ptr)
  134. {
  135. event_list::save_event(ev_ptr);
  136. }
  137. }
  138. std::shared_ptr<ya_event> mine_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  139. {
  140. std::shared_ptr<ya_event> event_ptr=nullptr;
  141. if(f){
  142. event_ptr=create_event(std::to_string(id), et);
  143. auto card_ptr = card_list::instance()->get(id);
  144. if(!card_ptr)
  145. {
  146. event_ptr->m_map_id=5;
  147. return event_ptr;
  148. }
  149. if(auto site_ptr=card_ptr->get_area_tool()->m_site)
  150. {
  151. event_ptr->m_area_id = site_ptr->m_area_id;
  152. event_ptr->m_map_id = site_ptr->m_map_id;
  153. }
  154. }
  155. return event_ptr;
  156. }
  157. std::shared_ptr<ya_event> area_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  158. {
  159. std::shared_ptr<ya_event> event_ptr=nullptr;
  160. if(f)
  161. {
  162. auto area_ptr = area_list::instance()->get(id);
  163. if(!area_ptr)
  164. {
  165. log_error("create_event .can not find area ..%d",id);
  166. return event_ptr;
  167. }
  168. event_ptr=create_event(std::to_string(id), et);
  169. event_ptr->m_area_id = id;
  170. event_ptr->m_map_id = area_ptr->mapid();
  171. }
  172. return event_ptr;
  173. }
  174. std::shared_ptr<ya_event> device_reader_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  175. {
  176. std::shared_ptr<ya_event> event_ptr=nullptr;
  177. if(f)
  178. {
  179. auto site_ptr = sit_list::instance()->get(id);
  180. if(!site_ptr)
  181. {
  182. log_info("create_event.can not find site :%d",id);
  183. return event_ptr;
  184. }
  185. event_ptr=create_event(std::to_string(id),et);
  186. event_ptr->m_area_id = site_ptr->m_area_id;
  187. event_ptr->m_map_id = site_ptr->m_map_id;
  188. event_ptr->x = site_ptr->x;
  189. event_ptr->y = site_ptr->y;
  190. }
  191. return event_ptr;
  192. }
  193. std::shared_ptr<ya_event> card_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  194. {
  195. std::shared_ptr<ya_event> event_ptr=nullptr;
  196. if(f)
  197. {
  198. auto card_ptr = card_list::instance()->get(id);
  199. std::string card_id = tool_other::get_string_cardid(id);
  200. if(!card_ptr)
  201. {
  202. log_error("Create_event.can not find card:%s",card_id.c_str());
  203. return event_ptr;
  204. }
  205. event_ptr=create_event(card_id,et);
  206. event_ptr->x = card_ptr->x;
  207. event_ptr->y = card_ptr->y;
  208. //event_ptr->m_is_display = card_ptr->m_display;
  209. if(const auto area_ptr=card_ptr->get_area_tool()) {
  210. const auto area_info = area_ptr->m_area_info;
  211. if(!area_info.empty()){
  212. const auto x=area_info.cbegin()->second;
  213. event_ptr->landmark_id=std::get<1>(x);
  214. event_ptr->landmark_dir=std::get<2>(x);
  215. event_ptr->landmark_dis=std::get<3>(x);
  216. }
  217. if(const auto site_ptr=area_ptr->m_site){
  218. event_ptr->m_area_id = site_ptr->m_area_id;
  219. event_ptr->m_map_id = site_ptr->m_map_id;
  220. }
  221. }
  222. }
  223. return event_ptr;
  224. }
  225. 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)
  226. {
  227. uint64_t id64 = tool_other::type_id_to_u64(card_type, card_id);
  228. return base::get(to_list_id(ev_type, OT_CARD, id64,edt));
  229. }
  230. void event_list::save_event(const std::shared_ptr<ya_event> &ev_ptr)
  231. {
  232. char sql[LENGTH_SQL] = {0};
  233. std::string _time = tool_time::to_str_ex(ev_ptr->m_cur_time);
  234. sprintf(sql,
  235. "INSERT IGNORE INTO his_event_data(event_id,id, stat, event_type_id, obj_type_id, obj_id, dis_type,\
  236. map_id, area_id, limit_value, cur_value, x, y, cur_time, description,landmark_id,landmark_dir,landmark_dist) \
  237. VALUES(%ld,%ld, %d, %d, %d, %s, %d, %d, %d, %.2f, %.2f, %f, %f, '%s', '%s',%d,%d,%.2f);",
  238. 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,
  239. 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,
  240. _time.c_str(), ev_ptr->m_desc.c_str(),ev_ptr->landmark_id,ev_ptr->landmark_dir,ev_ptr->landmark_dis);
  241. db_tool::PushAsync(sql);
  242. }
  243. void event_list::load_his_data_from_db(bool init /*=true*/)
  244. {
  245. static std::time_t s_last_time=0;
  246. if(!init){
  247. std::time_t t=time(0);
  248. if(t-s_last_time<15)return;
  249. s_last_time=t;
  250. }
  251. std::unordered_map<uint64_t, std::shared_ptr<ya_event>> map;
  252. std::string sql("SELECT event_id, id,stat,event_type_id,obj_type_id,obj_id,dis_type,map_id,area_id,\
  253. limit_value,cur_value,x,y, cur_time FROM his_event_data \
  254. WHERE event_id IN (SELECT MAX(event_id) FROM his_event_data \
  255. WHERE cur_time > (CASE obj_type_id \
  256. WHEN 1 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
  257. WHEN 4 THEN DATE_SUB(NOW(),INTERVAL 1 MONTH)\
  258. WHEN 9 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
  259. WHEN 10 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
  260. END) \
  261. AND cur_time < NOW()\
  262. GROUP BY event_type_id, obj_id,dis_type) \
  263. AND event_id NOT IN ( SELECT event_id FROM his_event_data WHERE stat=100)\
  264. AND event_type_id NOT IN (21,22,31,36) and obj_type_id !=2;");
  265. if(!init)
  266. 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;";
  267. std::string Error;
  268. YADB::CDBResultSet DBRes;
  269. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  270. if(!Error.empty())
  271. log_error("初始化事件列表 Error,%s",Error.c_str());
  272. uint64_t nCount = DBRes.GetRecordCount( Error );
  273. if (nCount > 0)
  274. {
  275. log_info( "init_event_list. The record count=%ld\n", nCount );
  276. while ( DBRes.GetNextRecod(Error) )
  277. {
  278. long long int event_id = 0;
  279. DBRes.GetField( "event_id",event_id, Error );
  280. long long int id = 0;
  281. DBRes.GetField( "id",id, Error );
  282. int event_type_id = 0;
  283. DBRes.GetField( "event_type_id",event_type_id, Error );
  284. int obj_type_id = 0;
  285. DBRes.GetField( "obj_type_id",obj_type_id, Error );
  286. std::string obj_id = "";
  287. DBRes.GetField( "obj_id",obj_id, Error );
  288. int dis_type = 0;
  289. DBRes.GetField( "dis_type",dis_type, Error );
  290. int map_id = 0;
  291. DBRes.GetField( "map_id",map_id, Error );
  292. int area_id = 0;
  293. DBRes.GetField( "area_id",area_id, Error );
  294. double limit_value = 0;
  295. DBRes.GetField( "limit_value",limit_value, Error );
  296. double cur_value = 0;
  297. DBRes.GetField( "cur_value",cur_value, Error );
  298. double x = 0;
  299. DBRes.GetField( "x",x, Error );
  300. double y = 0;
  301. DBRes.GetField( "y",y, Error );
  302. std::string cur_time = "";
  303. DBRes.GetField( "cur_time",cur_time, Error );
  304. int stat = 0;
  305. DBRes.GetField( "stat",stat, Error );
  306. //增加虚拟告警处理逻辑
  307. std::shared_ptr<ya_event> ev=nullptr;
  308. bool flag=false;
  309. if(!init){
  310. ev=event_list::instance()->get(id);
  311. if(!ev){
  312. ev = std::make_shared<ya_event>(event_id);
  313. }else{
  314. flag=true;
  315. if(stat==100)ev->m_is_sent=false;
  316. }
  317. }else{
  318. ev = std::make_shared<ya_event>(event_id);
  319. }
  320. ev->m_status = static_cast<EVENT_STATUS>(stat);
  321. ev->m_ev_type = static_cast<EVENT_TYPE>(event_type_id);
  322. ev->m_obj_type = static_cast<OBJECT_TYPE>(obj_type_id);
  323. ev->m_dis_type = static_cast<EVENT_DIS_TYPE>(dis_type);
  324. ev->m_obj_id = obj_id;
  325. ev->m_cur_time = tool_time::to_time_ex(cur_time);
  326. ev->m_cur_value = cur_value;
  327. ev->m_limit_value = limit_value;
  328. ev->m_map_id = map_id;
  329. ev->m_area_id = area_id;
  330. ev->x = x;
  331. ev->y = y;
  332. ev->m_id = id;
  333. //这里当是卡告警的时候,对m_event数据进行赋值
  334. //方便清理
  335. //备注防追尾告警和一人多卡告警可能不适用,后续整理
  336. if(ev->m_obj_type==OT_CARD){
  337. uint64_t c_id= tool_other::card_id_to_u64(ev->m_obj_id);
  338. if(auto c=card_list::instance()->get(c_id)){
  339. if(c->m_type==CT_PERSON){
  340. auto mine_tool_ptr = c->get_mine_tool();
  341. if(!mine_tool_ptr->m_is_attendance) {
  342. log_warn("person_not_att:%s",obj_id.c_str());
  343. continue;
  344. }
  345. }
  346. c->set_event_flag(ev->m_ev_type);
  347. if(event_type_id==ET_CARD_LOW_POWER_SERIOUS)
  348. c->m_pwr_stat=STATUS_POWER_LOWER_SERIOUS;
  349. } else {
  350. log_warn("load_evnet_history:card_id:%s not exist...",obj_id.c_str());
  351. continue;
  352. }
  353. }else if(ev->m_obj_type==OT_DEVICE_READER){
  354. auto sit_ptr=sit_list::instance()->get(std::stoi(obj_id));
  355. if(!sit_ptr){
  356. log_warn("load_evnet_history:site_id:%s not exist...",obj_id.c_str());
  357. continue;
  358. }
  359. if(ev->m_ev_type==ET_READER_POWER_BY_BATTERY)
  360. sit_ptr->m_power_ac_down=true;
  361. if(ev->m_ev_type==ET_READER_ERROR)
  362. sit_ptr->m_time=tool_time::to_ms(ev->m_cur_time)/1000;
  363. }
  364. if(!flag)
  365. map.insert(std::make_pair(id, ev));
  366. log_info("event_list %lld,%lld,%d,%d,%d,%s,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%s"
  367. , 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
  368. ,ev->m_map_id,ev->m_area_id,ev->m_limit_value,ev->m_cur_value
  369. ,ev->x,ev->y
  370. ,tool_time::to_str_ex(ev->m_cur_time).c_str());
  371. }
  372. if(!map.empty())
  373. event_list::instance()->add(map);
  374. }
  375. }
  376. std::string event_list::evs_to_json(std::vector<std::shared_ptr<ya_event>> arr)
  377. {
  378. rapidjson::Document doc(rapidjson::kObjectType);
  379. rapidjson::Value data(rapidjson::kArrayType);
  380. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  381. auto it=arr.begin();
  382. for(;it!=arr.end();++it)
  383. {
  384. _ev_to_node(*it, allocator, data);
  385. }
  386. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_EVENT, allocator);
  387. doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  388. doc.AddMember(JSON_ROOT_KEY_DATA,data, allocator);
  389. rapidjson::StringBuffer sb;
  390. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  391. doc.Accept(writer);
  392. return sb.GetString();
  393. }
  394. void event_list::_ev_to_node(std::shared_ptr<ya_event> ev_ptr,
  395. rapidjson::Document::AllocatorType& allocator,
  396. rapidjson::Value& out_data)
  397. {
  398. rapidjson::Value ev(rapidjson::kObjectType);
  399. //ev.AddMember(JSON_KEY_EVENT_EVENT_ID,ev_ptr->m_id, allocator);
  400. tool_json::add_member(ev, JSON_KEY_EVENT_EVENT_ID, std::to_string(ev_ptr->m_id), allocator);
  401. ev.AddMember(JSON_KEY_EVENT_STATUS,ev_ptr->m_status, allocator);
  402. ev.AddMember(JSON_KEY_EVENT_TYPE_ID,ev_ptr->m_ev_type, allocator);
  403. ev.AddMember(JSON_KEY_EVENT_OBJ_TYPE_ID,ev_ptr->m_obj_type, allocator);
  404. tool_json::add_member(ev, JSON_KEY_EVENT_OBJ_ID, ev_ptr->m_obj_id, allocator);
  405. ev.AddMember(JSON_KEY_EVENT_DIS_TYPE_ID,ev_ptr->m_dis_type, allocator);
  406. ev.AddMember(JSON_KEY_EVENT_MAP_ID,ev_ptr->m_map_id, allocator);
  407. ev.AddMember(JSON_KEY_EVENT_AREA_ID,ev_ptr->m_area_id, allocator);
  408. ev.AddMember(JSON_KEY_EVENT_X,ev_ptr->x, allocator);
  409. ev.AddMember(JSON_KEY_EVENT_Y,ev_ptr->y, allocator);
  410. ev.AddMember(JSON_KEY_EVENT_LIMIT_VALUE,ev_ptr->m_limit_value, allocator);
  411. ev.AddMember(JSON_KEY_EVENT_CUR_VALUE,ev_ptr->m_cur_value, allocator);
  412. ev.AddMember(JSON_KEY_EVENT_CUR_TIME,tool_time::to_ms(ev_ptr->m_cur_time), allocator);
  413. out_data.PushBack(ev, allocator);
  414. }
  415. //template<> std::shared_ptr<event_list> single_base<event_list, uint64_t, std::shared_ptr<ya_event>>::m_instance=std::make_shared<event_list>();