event.cpp 18 KB

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