event.cpp 26 KB

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