event.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. /*
  69. * @brief
  70. * 告警分类:一人带多卡告警、区域告警、基站告警、卡告警、红绿灯告警
  71. * @param
  72. * 无
  73. * @return
  74. * 无
  75. * @note
  76. * @warning
  77. * @bug
  78. *
  79. * */
  80. void event_tool::make_event_object()
  81. {
  82. m_map[OT_MORE_CARD] = std::make_shared<mine_event>();
  83. m_map[OT_AREA] = std::make_shared<area_event>();
  84. m_map[OT_DEVICE_READER] = std::make_shared<device_reader_event>();
  85. m_map[OT_CARD] = std::make_shared<card_event>();
  86. m_map[OT_DEVICE_LIGHT] = std::make_shared<light_event>();
  87. }
  88. 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/*=""*/)
  89. {
  90. m_map[ot]->handle_alarm_event(et,id,limit_value,cur_value,f,edt,desc);
  91. }
  92. std::shared_ptr<ya_event> Event::create_event(const std::string&obj_id,EVENT_TYPE ev_type)
  93. {
  94. auto ev_ptr = std::make_shared<ya_event>(tool_time::now_to_us());
  95. ev_ptr->m_ev_type = ev_type;
  96. ev_ptr->m_obj_type=m_oid;
  97. ev_ptr->m_obj_id = obj_id;
  98. return ev_ptr;
  99. }
  100. /*
  101. * @breif 构建开始或者取消告警事件,
  102. * 告警事件入口,生成事件对象,保存在内存中。
  103. * end_time为零表示开始
  104. * start_time为零表示结束
  105. * 事件的唯一根据id判断。
  106. * @param EVENT_TYPE et 事件设备类型
  107. * @param uint64_t id
  108. * @param double limit_value 阈值
  109. * @param double cur_value 当前值
  110. * @param bool f 告警标志,true表示开始告警,false表示取消告警
  111. * @param EVENT_DIS_TYPE edt
  112. * @param const std::string& desc 描述
  113. * @return 无
  114. * @note
  115. * @bug
  116. * @warning
  117. * */
  118. 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)
  119. {
  120. std::shared_ptr<ya_event> ev_ptr = nullptr;
  121. uint64_t eid = event_list::to_list_id(et, m_oid, id, edt);
  122. //eid是告警的唯一id,如果存在,查找eid来找到的已经存在的事件
  123. auto event_ptr = event_list::instance()->get(eid);
  124. if(f)
  125. {
  126. if(event_ptr)
  127. {
  128. //已经存在告警
  129. event_ptr->m_cur_value = cur_value;
  130. if(et!=ET_READER_ERROR)
  131. event_ptr->m_is_sent=false;
  132. }
  133. else
  134. {
  135. uint64_t _id = id;
  136. //log_info("Create_Event:desc[%s],id:%d,et:%d",desc.c_str(),id,et);
  137. if((et == ET_UWB_MORE_CARD||et == ET_VEHICLE_REAR_END) && !desc.empty())//防追尾告警特殊处理
  138. {
  139. std::string cardid = desc.substr(0, desc.find_first_of('&'));
  140. _id = tool_other::card_id_to_u64(cardid);
  141. }
  142. ev_ptr = on_message(et,_id,f);
  143. if(ev_ptr){
  144. ev_ptr->m_cur_value = cur_value;
  145. ev_ptr->m_limit_value = limit_value;
  146. ev_ptr->m_desc = desc;
  147. ev_ptr->m_id = eid;
  148. ev_ptr->m_dis_type = edt;
  149. if(et == ET_VEHICLE_REAR_END|| et == ET_UWB_MORE_CARD)//一人多卡特殊处理
  150. {
  151. ev_ptr->m_obj_id = desc;
  152. //ev_ptr->m_cur_time=std::chrono::system_clock::time_point(std::chrono::milliseconds((time_t)limit_value * 1000));
  153. }
  154. //保存告警信息
  155. event_list::instance()->add(eid, ev_ptr);
  156. }
  157. }
  158. }
  159. else
  160. {
  161. if(event_ptr && !event_ptr->is_end())
  162. {
  163. //取消告警
  164. event_ptr->m_cur_time = std::chrono::system_clock::now();
  165. event_ptr->m_status = ES_END;
  166. event_ptr->m_cur_value = cur_value;
  167. event_ptr->m_desc = desc;
  168. event_ptr->m_is_sent = false;
  169. ev_ptr=event_ptr;
  170. }
  171. }
  172. if(ev_ptr)
  173. {
  174. //event_list::save_event(ev_ptr);
  175. //基站失联告警:人员和车辆都要有
  176. if(ev_ptr->m_ev_type == 6){
  177. event_list::save_event(ev_ptr);
  178. event_list::save_event_v(ev_ptr);
  179. }else if(ev_ptr->m_ev_type == 8 || ev_ptr->m_ev_type == 21 || ev_ptr->m_ev_type == 41){
  180. // 红绿灯失联、人车防碰撞、超速告警这三类保存到his_event_data_v表中
  181. event_list::save_event_v(ev_ptr);
  182. }else{
  183. //其他的人员相关报警保存到his_event_data表中
  184. event_list::save_event(ev_ptr);
  185. }
  186. }
  187. }
  188. std::shared_ptr<ya_event> mine_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  189. {
  190. std::shared_ptr<ya_event> event_ptr=nullptr;
  191. if(f){
  192. event_ptr=create_event(std::to_string(id), et);
  193. auto card_ptr = card_list::instance()->get(id);
  194. if(!card_ptr)
  195. {
  196. event_ptr->m_map_id=5;
  197. return event_ptr;
  198. }
  199. if(auto site_ptr=card_ptr->get_area_tool()->m_site)
  200. {
  201. event_ptr->m_area_id = site_ptr->m_area_id;
  202. event_ptr->m_map_id = site_ptr->m_map_id;
  203. }
  204. }
  205. return event_ptr;
  206. }
  207. std::shared_ptr<ya_event> area_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  208. {
  209. std::shared_ptr<ya_event> event_ptr=nullptr;
  210. if(f)
  211. {
  212. auto area_ptr = area_list::instance()->get(id);
  213. if(!area_ptr)
  214. {
  215. log_error("create_event .can not find area ..%d",id);
  216. return event_ptr;
  217. }
  218. event_ptr=create_event(std::to_string(id), et);
  219. event_ptr->m_area_id = id;
  220. event_ptr->m_map_id = area_ptr->mapid();
  221. }
  222. return event_ptr;
  223. }
  224. std::shared_ptr<ya_event> device_reader_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  225. {
  226. std::shared_ptr<ya_event> event_ptr=nullptr;
  227. if(f)
  228. {
  229. auto site_ptr = sit_list::instance()->get(id);
  230. if(!site_ptr)
  231. {
  232. log_info("create_event.can not find site :%d",id);
  233. return event_ptr;
  234. }
  235. if(site_ptr->m_special){
  236. //log_info()
  237. return event_ptr;
  238. }
  239. event_ptr=create_event(std::to_string(id),et);
  240. event_ptr->m_area_id = site_ptr->m_area_id;
  241. event_ptr->m_map_id = site_ptr->m_map_id;
  242. event_ptr->x = site_ptr->x;
  243. event_ptr->y = site_ptr->y;
  244. }
  245. return event_ptr;
  246. }
  247. std::shared_ptr<ya_event> card_event::on_message(EVENT_TYPE et,uint64_t id,bool f)
  248. {
  249. std::shared_ptr<ya_event> event_ptr=nullptr;
  250. if(f)
  251. {
  252. auto card_ptr = card_list::instance()->get(id);
  253. std::string card_id = tool_other::get_string_cardid(id);
  254. if(!card_ptr)
  255. {
  256. log_error("Create_event.can not find card:%s",card_id.c_str());
  257. return event_ptr;
  258. }
  259. event_ptr=create_event(card_id,et);
  260. event_ptr->x = card_ptr->x;
  261. event_ptr->y = card_ptr->y;
  262. //event_ptr->m_is_display = card_ptr->m_display;
  263. if(const auto area_ptr=card_ptr->get_area_tool()) {
  264. const auto area_info = area_ptr->m_area_info;
  265. if(!area_info.empty()){
  266. const auto x=area_info.cbegin()->second;
  267. event_ptr->landmark_id=std::get<1>(x);
  268. event_ptr->landmark_dir=std::get<2>(x);
  269. event_ptr->landmark_dis=std::get<3>(x);
  270. }
  271. if(const auto site_ptr=area_ptr->m_site){
  272. event_ptr->m_area_id = site_ptr->m_area_id;
  273. event_ptr->m_map_id = site_ptr->m_map_id;
  274. }
  275. }
  276. }
  277. return event_ptr;
  278. }
  279. std::shared_ptr<ya_event> light_event::on_message(EVENT_TYPE et, uint64_t id, bool f)
  280. {
  281. std::shared_ptr<ya_event> event_ptr = nullptr;
  282. if(f){
  283. auto light_ptr = traffic_light_manager::instance()->get(id);
  284. if(!light_ptr)
  285. {
  286. log_info("create_event.can not find light :%d",id);
  287. return event_ptr;
  288. }
  289. event_ptr = create_event(std::to_string(id), et);
  290. event_ptr->m_area_id = light_ptr->m_area_id;
  291. event_ptr->m_map_id = light_ptr->m_map_id;
  292. event_ptr->x = light_ptr->x;
  293. event_ptr->y = light_ptr->y;
  294. }
  295. return event_ptr;
  296. }
  297. 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)
  298. {
  299. uint64_t id64 = tool_other::type_id_to_u64(card_type, card_id);
  300. return base::get(to_list_id(ev_type, OT_CARD, id64,edt));
  301. }
  302. void event_list::save_event(const std::shared_ptr<ya_event> &ev_ptr)
  303. {
  304. char sql[LENGTH_SQL] = {0};
  305. std::string _time = tool_time::to_str_ex(ev_ptr->m_cur_time);
  306. sprintf(sql,
  307. "INSERT IGNORE INTO his_event_data(event_id,id, stat, event_type_id, obj_type_id, obj_id, dis_type,\
  308. map_id, area_id, limit_value, cur_value, x, y, cur_time, description,landmark_id,landmark_dir,landmark_dist) \
  309. VALUES(%ld,%ld, %d, %d, %d, %s, %d, %d, %d, %.2f, %.2f, %f, %f, '%s', '%s',%d,%d,%.2f);",
  310. 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,
  311. 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,
  312. _time.c_str(), ev_ptr->m_desc.c_str(),ev_ptr->landmark_id,ev_ptr->landmark_dir,ev_ptr->landmark_dis);
  313. db_tool::PushAsync(sql);
  314. }
  315. /*
  316. * @brief
  317. * 保存告警事件
  318. * @param
  319. * const std::shared_ptr<ya_event>& ev_ptr 告警对象
  320. * int type 告警开始结束标志,开始为0,结束为1
  321. * @return
  322. * 无
  323. * @note
  324. * @warning
  325. * @bug
  326. * */
  327. void event_list::save_event_v(const std::shared_ptr<ya_event> &ev_ptr)
  328. {
  329. char sql[LENGTH_SQL] = {0};
  330. std::string _time = tool_time::to_str_ex(ev_ptr->m_cur_time);
  331. switch(ev_ptr->m_status){
  332. case 0:
  333. sprintf(sql, "INSERT IGNORE INTO his_event_data_v(id, event_type_id, obj_id, x, y ,start_time) VALUES(%ld, %d, '%s', %.2f, %.2f, '%s');",
  334. ev_ptr->m_ev_id, ev_ptr->m_ev_type, ev_ptr->m_obj_id.c_str(), ev_ptr->x, ev_ptr->y, _time.c_str());
  335. break;
  336. case 100:
  337. sprintf(sql, "update his_event_data_v set end_time='%s' where id=%ld;", _time.c_str(), ev_ptr->m_ev_id);
  338. break;
  339. default:
  340. break;
  341. }
  342. db_tool::PushAsync(sql);
  343. }
  344. void event_list::load_his_data_from_db(bool init /*=true*/)
  345. {
  346. static std::time_t s_last_time=0;
  347. if(!init){
  348. std::time_t t=time(0);
  349. if(t-s_last_time<15)return;
  350. s_last_time=t;
  351. }
  352. std::unordered_map<uint64_t, std::shared_ptr<ya_event>> map;
  353. std::string sql("SELECT event_id, id,stat,event_type_id,obj_type_id,obj_id,dis_type,map_id,area_id,\
  354. limit_value,cur_value,x,y, cur_time FROM his_event_data \
  355. WHERE event_id IN (SELECT MAX(event_id) FROM his_event_data \
  356. WHERE cur_time > (CASE obj_type_id \
  357. WHEN 1 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
  358. WHEN 4 THEN DATE_SUB(NOW(),INTERVAL 1 MONTH)\
  359. WHEN 9 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
  360. WHEN 10 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
  361. END) \
  362. AND cur_time < NOW()\
  363. GROUP BY event_type_id, obj_id,dis_type) \
  364. AND event_id NOT IN ( SELECT event_id FROM his_event_data WHERE stat=100)\
  365. AND event_type_id NOT IN (21,22,31,36) and obj_type_id !=2;");
  366. if(!init)
  367. 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;";
  368. std::string Error;
  369. YADB::CDBResultSet DBRes;
  370. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  371. if(!Error.empty())
  372. log_error("初始化事件列表 Error,%s",Error.c_str());
  373. uint64_t nCount = DBRes.GetRecordCount( Error );
  374. if (nCount > 0)
  375. {
  376. log_info( "init_event_list. The record count=%ld\n", nCount );
  377. while ( DBRes.GetNextRecod(Error) )
  378. {
  379. long long int event_id = 0;
  380. DBRes.GetField( "event_id",event_id, Error );
  381. long long int id = 0;
  382. DBRes.GetField( "id",id, Error );
  383. int event_type_id = 0;
  384. DBRes.GetField( "event_type_id",event_type_id, Error );
  385. int obj_type_id = 0;
  386. DBRes.GetField( "obj_type_id",obj_type_id, Error );
  387. std::string obj_id = "";
  388. DBRes.GetField( "obj_id",obj_id, Error );
  389. int dis_type = 0;
  390. DBRes.GetField( "dis_type",dis_type, Error );
  391. int map_id = 0;
  392. DBRes.GetField( "map_id",map_id, Error );
  393. int area_id = 0;
  394. DBRes.GetField( "area_id",area_id, Error );
  395. double limit_value = 0;
  396. DBRes.GetField( "limit_value",limit_value, Error );
  397. double cur_value = 0;
  398. DBRes.GetField( "cur_value",cur_value, Error );
  399. double x = 0;
  400. DBRes.GetField( "x",x, Error );
  401. double y = 0;
  402. DBRes.GetField( "y",y, Error );
  403. std::string cur_time = "";
  404. DBRes.GetField( "cur_time",cur_time, Error );
  405. int stat = 0;
  406. DBRes.GetField( "stat",stat, Error );
  407. //增加虚拟告警处理逻辑
  408. std::shared_ptr<ya_event> ev=nullptr;
  409. bool flag=false;
  410. if(!init){
  411. ev=event_list::instance()->get(id);
  412. if(!ev){
  413. ev = std::make_shared<ya_event>(event_id);
  414. }else{
  415. flag=true;
  416. if(stat==100)ev->m_is_sent=false;
  417. }
  418. }else{
  419. ev = std::make_shared<ya_event>(event_id);
  420. }
  421. ev->m_status = static_cast<EVENT_STATUS>(stat);
  422. ev->m_ev_type = static_cast<EVENT_TYPE>(event_type_id);
  423. ev->m_obj_type = static_cast<OBJECT_TYPE>(obj_type_id);
  424. ev->m_dis_type = static_cast<EVENT_DIS_TYPE>(dis_type);
  425. ev->m_obj_id = obj_id;
  426. ev->m_cur_time = tool_time::to_time_ex(cur_time);
  427. ev->m_cur_value = cur_value;
  428. ev->m_limit_value = limit_value;
  429. ev->m_map_id = map_id;
  430. ev->m_area_id = area_id;
  431. ev->x = x;
  432. ev->y = y;
  433. ev->m_id = id;
  434. //这里当是卡告警的时候,对m_event数据进行赋值
  435. //方便清理
  436. //备注防追尾告警和一人多卡告警可能不适用,后续整理
  437. if(ev->m_obj_type==OT_CARD){
  438. uint64_t c_id= tool_other::card_id_to_u64(ev->m_obj_id);
  439. if(auto c=card_list::instance()->get(c_id)){
  440. if(c->m_type==CT_PERSON){
  441. auto mine_tool_ptr = c->get_mine_tool();
  442. if(!mine_tool_ptr->m_is_attendance) {
  443. log_warn("person_not_att:%s",obj_id.c_str());
  444. continue;
  445. }
  446. }
  447. c->set_event_flag(ev->m_ev_type);
  448. if(event_type_id==ET_CARD_LOW_POWER_SERIOUS)
  449. c->m_pwr_stat=STATUS_POWER_LOWER_SERIOUS;
  450. } else {
  451. log_warn("load_evnet_history:card_id:%s not exist...",obj_id.c_str());
  452. continue;
  453. }
  454. }else if(ev->m_obj_type==OT_DEVICE_READER){
  455. auto sit_ptr=sit_list::instance()->get(std::stoi(obj_id));
  456. if(!sit_ptr){
  457. log_warn("load_evnet_history:site_id:%s not exist...",obj_id.c_str());
  458. continue;
  459. }
  460. if(ev->m_ev_type==ET_READER_POWER_BY_BATTERY)
  461. sit_ptr->m_power_ac_down=true;
  462. if(ev->m_ev_type==ET_READER_ERROR)
  463. sit_ptr->m_time=tool_time::to_ms(ev->m_cur_time)/1000;
  464. }
  465. if(!flag)
  466. map.insert(std::make_pair(id, ev));
  467. log_info("event_list %lld,%lld,%d,%d,%d,%s,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%s"
  468. , 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
  469. ,ev->m_map_id,ev->m_area_id,ev->m_limit_value,ev->m_cur_value
  470. ,ev->x,ev->y
  471. ,tool_time::to_str_ex(ev->m_cur_time).c_str());
  472. }
  473. if(!map.empty())
  474. event_list::instance()->add(map);
  475. }
  476. }
  477. /*
  478. * @brief
  479. * 加载车辆历史告警表
  480. * @param
  481. *
  482. * @return
  483. * 无
  484. * @note
  485. * @warning
  486. * @bug
  487. * */
  488. void event_list::load_his_data_v_from_db()
  489. {
  490. static std::time_t s_last_time=0;
  491. if(!init){
  492. std::time_t t=time(0);
  493. if(t-s_last_time<15)return;
  494. s_last_time=t;
  495. }
  496. std::unordered_map<uint64_t, std::shared_ptr<ya_event>> map;
  497. std::string sql("SELECT id, obj_id, event_type_id, x, y, start_time from his_event_data_v where id in (select max(id), obj_id, event_type_id, x, y, start_time from his_event_data_v where end_time is null and start_time > DATE_SUB(NOW(), INTERVAL 1 DAY) GROUP BY obj_id_id, event_type_id);");
  498. std::string Error;
  499. YADB::CDBResultSet DBRes;
  500. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  501. if(!Error.empty())
  502. log_error("初始化事件列表 Error,%s",Error.c_str());
  503. uint64_t nCount = DBRes.GetRecordCount( Error );
  504. if (nCount > 0)
  505. {
  506. log_info( "init_event_list. The record count=%ld\n", nCount );
  507. while ( DBRes.GetNextRecod(Error) )
  508. {
  509. long long int event_id = 0;
  510. DBRes.GetField( "id",event_id, Error );
  511. long long int id = 0;
  512. id = event_id;
  513. int event_type_id = 0;
  514. DBRes.GetField( "event_type_id",event_type_id, Error );
  515. int obj_type_id = 0;
  516. std::string obj_id = "";
  517. DBRes.GetField( "obj_id",obj_id, Error );
  518. int dis_type = 0;
  519. int map_id = 0;
  520. int area_id = 0;
  521. double limit_value = 0;
  522. double cur_value = 0;
  523. double x = 0;
  524. DBRes.GetField( "x",x, Error );
  525. double y = 0;
  526. DBRes.GetField( "y",y, Error );
  527. std::string cur_time = "";
  528. DBRes.GetField( "start_time",cur_time, Error );
  529. int stat = 0;
  530. //增加虚拟告警处理逻辑
  531. std::shared_ptr<ya_event> ev=nullptr;
  532. bool flag=false;
  533. if(!init){
  534. ev=event_list::instance()->get(id);
  535. if(!ev){
  536. ev = std::make_shared<ya_event>(event_id);
  537. }else{
  538. flag=true;
  539. if(stat==100)ev->m_is_sent=false;
  540. }
  541. }else{
  542. ev = std::make_shared<ya_event>(event_id);
  543. }
  544. ev->m_status = static_cast<EVENT_STATUS>(stat);
  545. ev->m_ev_type = static_cast<EVENT_TYPE>(event_type_id);
  546. ev->m_obj_type = static_cast<OBJECT_TYPE>(obj_type_id);
  547. ev->m_dis_type = static_cast<EVENT_DIS_TYPE>(dis_type);
  548. ev->m_obj_id = obj_id;
  549. ev->m_cur_time = tool_time::to_time_ex(cur_time);
  550. ev->m_cur_value = cur_value;
  551. ev->m_limit_value = limit_value;
  552. ev->m_map_id = map_id;
  553. ev->m_area_id = area_id;
  554. ev->x = x;
  555. ev->y = y;
  556. ev->m_id = id;
  557. //这里当是卡告警的时候,对m_event数据进行赋值
  558. //方便清理
  559. //备注防追尾告警和一人多卡告警可能不适用,后续整理
  560. if(!flag)
  561. map.insert(std::make_pair(id, ev));
  562. log_info("event_list %lld,%lld,%d,%d,%d,%s,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%s"
  563. , 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
  564. ,ev->m_map_id,ev->m_area_id,ev->m_limit_value,ev->m_cur_value
  565. ,ev->x,ev->y
  566. ,tool_time::to_str_ex(ev->m_cur_time).c_str());
  567. }
  568. if(!map.empty())
  569. event_list::instance()->add(map);
  570. }
  571. }
  572. /*
  573. * @brief
  574. * 事件转为json
  575. * @param
  576. * std::vector<std::shared_ptr<ya_event>> arr 事件列表
  577. * @return
  578. * json字符串
  579. * @note
  580. * @warning
  581. * @bug
  582. * */
  583. std::string event_list::evs_to_json(std::vector<std::shared_ptr<ya_event>> arr)
  584. {
  585. rapidjson::Document doc(rapidjson::kObjectType);
  586. rapidjson::Value data(rapidjson::kArrayType);
  587. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  588. auto it=arr.begin();
  589. for(;it!=arr.end();++it)
  590. {
  591. _ev_to_node(*it, allocator, data);
  592. }
  593. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_EVENT, allocator);
  594. doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  595. doc.AddMember(JSON_ROOT_KEY_DATA,data, allocator);
  596. rapidjson::StringBuffer sb;
  597. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  598. doc.Accept(writer);
  599. return sb.GetString();
  600. }
  601. /*
  602. * @brief
  603. * 构造alarm的json字符串
  604. * @param
  605. * std::vector<std::shared_ptr<ya_event>> arr 告警事件列表
  606. * @return
  607. * alarm的json字符串
  608. * @note
  609. * @warning
  610. * @bug
  611. *
  612. * */
  613. std::string event_list::evs_to_json_v(std::vector<std::shared_ptr<ya_event>> arr)
  614. {
  615. rapidjson::Document doc(rapidjson::kObjectType);
  616. rapidjson::Value data(rapidjson::kArrayType);
  617. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  618. auto it=arr.begin();
  619. for(;it!=arr.end();++it)
  620. {
  621. _ev_to_node_v(*it, allocator, data);
  622. }
  623. doc.AddMember(JSON_ROOT_KEY_CMD, "alarm", allocator);
  624. doc.AddMember(JSON_ROOT_KEY_DATA, data, allocator);
  625. rapidjson::StringBuffer sb;
  626. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  627. doc.Accept(writer);
  628. return sb.GetString();
  629. }
  630. void event_list::_ev_to_node(std::shared_ptr<ya_event> ev_ptr,
  631. rapidjson::Document::AllocatorType& allocator,
  632. rapidjson::Value& out_data)
  633. {
  634. rapidjson::Value ev(rapidjson::kObjectType);
  635. //ev.AddMember(JSON_KEY_EVENT_EVENT_ID,ev_ptr->m_id, allocator);
  636. tool_json::add_member(ev, JSON_KEY_EVENT_EVENT_ID, std::to_string(ev_ptr->m_id), allocator);
  637. ev.AddMember(JSON_KEY_EVENT_STATUS,ev_ptr->m_status, allocator);
  638. ev.AddMember(JSON_KEY_EVENT_TYPE_ID,ev_ptr->m_ev_type, allocator);
  639. ev.AddMember(JSON_KEY_EVENT_OBJ_TYPE_ID,ev_ptr->m_obj_type, allocator);
  640. tool_json::add_member(ev, JSON_KEY_EVENT_OBJ_ID, ev_ptr->m_obj_id, allocator);
  641. ev.AddMember(JSON_KEY_EVENT_DIS_TYPE_ID,ev_ptr->m_dis_type, allocator);
  642. ev.AddMember(JSON_KEY_EVENT_MAP_ID,ev_ptr->m_map_id, allocator);
  643. ev.AddMember(JSON_KEY_EVENT_AREA_ID,ev_ptr->m_area_id, allocator);
  644. ev.AddMember(JSON_KEY_EVENT_X,ev_ptr->x, allocator);
  645. ev.AddMember(JSON_KEY_EVENT_Y,ev_ptr->y, allocator);
  646. ev.AddMember(JSON_KEY_EVENT_LIMIT_VALUE,ev_ptr->m_limit_value, allocator);
  647. ev.AddMember(JSON_KEY_EVENT_CUR_VALUE,ev_ptr->m_cur_value, allocator);
  648. ev.AddMember(JSON_KEY_EVENT_CUR_TIME,tool_time::to_ms(ev_ptr->m_cur_time), allocator);
  649. out_data.PushBack(ev, allocator);
  650. }
  651. /*
  652. * @brief
  653. * 构造每个具体告警的数据
  654. * @param
  655. * std::shared_ptr<ya_event> ev_ptr //告警对象
  656. * rapidjson::Document::AllocatorType& allocator //json的allocator
  657. * rapidjson::Value& out_data //输出的json value对象
  658. * @return
  659. * @note
  660. * @warning
  661. * @bug
  662. * */
  663. void event_list::_ev_to_node_v(std::shared_ptr<ya_event> ev_ptr,
  664. rapidjson::Document::AllocatorType& allocator,
  665. rapidjson::Value& out_data)
  666. {
  667. rapidjson::Value ev(rapidjson::kObjectType);
  668. //ev.AddMember(JSON_KEY_EVENT_EVENT_ID,ev_ptr->m_id, allocator);
  669. tool_json::add_member(ev, JSON_KEY_EVENT_EVENT_ID, std::to_string(ev_ptr->m_id), allocator);
  670. ev.AddMember(JSON_KEY_EVENT_TYPE_ID,ev_ptr->m_ev_type, allocator);
  671. tool_json::add_member(ev, JSON_KEY_EVENT_OBJ_ID, ev_ptr->m_obj_id, allocator);
  672. ev.AddMember(JSON_KEY_EVENT_X,ev_ptr->x, allocator);
  673. ev.AddMember(JSON_KEY_EVENT_Y,ev_ptr->y, allocator);
  674. switch(ev_ptr->m_status){
  675. case 0:
  676. ev.AddMember("start_time", tool_time::to_ms(ev_ptr->m_cur_time), allocator);
  677. break;
  678. case 100:
  679. ev.AddMember("end_time", tool_time::to_ms(ev_ptr->m_cur_time), allocator);
  680. break;
  681. default:
  682. break;
  683. }
  684. out_data.PushBack(ev, allocator);
  685. }
  686. //template<> std::shared_ptr<event_list> single_base<event_list, uint64_t, std::shared_ptr<ya_event>>::m_instance=std::make_shared<event_list>();