1
0

event.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef __EVENT_HPP__
  2. #define __EVENT_HPP__
  3. #include <memory>
  4. #include <chrono>
  5. #include<map>
  6. #include<rapidjson/document.h>
  7. #include<rapidjson/writer.h>
  8. #include<rapidjson/stringbuffer.h>
  9. #include<rapidjson/prettywriter.h>
  10. #include<write-copy.h>
  11. #include "common.h"
  12. struct ya_event
  13. {
  14. private:
  15. uint64_t m_ev_id;
  16. public:
  17. ya_event(uint64_t e_id):m_cur_time(std::chrono::system_clock::now())
  18. {
  19. m_ev_id = e_id;
  20. m_obj_id = "";
  21. m_map_id = 0;
  22. m_area_id = 0;
  23. x = 0;
  24. y = 0;
  25. m_limit_value = 0;
  26. m_cur_value = 0;
  27. m_desc = "";
  28. m_status=ES_START;
  29. m_is_display=true;
  30. m_is_sent=false;
  31. }
  32. ~ya_event(){}
  33. public:
  34. //key of event_list.
  35. uint64_t m_id;
  36. ///告警状态,开始、结束
  37. EVENT_STATUS m_status;
  38. ///告警类型
  39. EVENT_TYPE m_ev_type;
  40. ///告警对象类型
  41. OBJECT_TYPE m_obj_type;
  42. /// 告警对象编号,与告警对象类型对应,如告警对象类型为分站,此字段为分站编号
  43. std::string m_obj_id;
  44. ///当前时间,为告警事件的触发时间,如果状态为开始,则表示开始时间,否则为结束时间
  45. std::chrono::system_clock::time_point m_cur_time;
  46. ///告警所在地图
  47. int m_map_id;
  48. ///告警所在区域
  49. int m_area_id;
  50. ///位置
  51. double x;
  52. ///位置
  53. double y;
  54. ///告警阈值
  55. double m_limit_value;
  56. ///当前值
  57. double m_cur_value;
  58. ///描述
  59. std::string m_desc;
  60. bool m_is_display;
  61. //是否已经发送 ture bu发送, false推送. 推送完置为true
  62. bool m_is_sent;
  63. bool is_end()
  64. {
  65. return ES_END == m_status;
  66. }
  67. ///作为事件map列表的id,方便查找;
  68. uint64_t get_list_id();
  69. uint64_t get_id()
  70. {
  71. return m_ev_id;
  72. }
  73. };
  74. class Event;
  75. struct event_tool
  76. {
  77. template <class UnaryPredicate>
  78. void handle_event(OBJECT_TYPE ot,EVENT_TYPE et,uint64_t id,double limit_value,double cur_value,UnaryPredicate p,const std::string &desc="")
  79. {
  80. handle_event(ot,et,id,limit_value,cur_value,p(),desc);
  81. }
  82. void handle_event(OBJECT_TYPE ot,EVENT_TYPE et,uint64_t id,double limit_value,double cur_value,bool f,const std::string &desc="");
  83. static event_tool * instance();
  84. private:
  85. event_tool()
  86. {
  87. make_event_object();
  88. }
  89. void make_event_object();
  90. std::map<OBJECT_TYPE,std::shared_ptr<Event>> m_map;
  91. };
  92. struct event_list:single_base<event_list,uint64_t,std::shared_ptr<ya_event>>
  93. {
  94. public:
  95. static uint64_t to_list_id(EVENT_TYPE ev_type, OBJECT_TYPE obj_type,uint64_t obj_id)
  96. {
  97. return (static_cast<uint64_t>(ev_type)<<48)|(static_cast<uint64_t>(obj_type)<<40)|obj_id;
  98. }
  99. std::shared_ptr<ya_event> get_event_card(uint32_t card_id, int card_type, EVENT_TYPE ev_type);
  100. std::shared_ptr<ya_event> get_event_area(int32_t area_id, EVENT_TYPE ev_type)
  101. {
  102. return base::get(to_list_id(ev_type, OT_AREA, static_cast<uint64_t>(area_id)));
  103. }
  104. std::shared_ptr<ya_event> get_event_reader(int32_t reader_id, EVENT_TYPE ev_type)
  105. {
  106. return base::get(to_list_id(ev_type,OT_DEVICE_READER, static_cast<uint64_t>(reader_id)));
  107. }
  108. static void save_event(std::shared_ptr<ya_event> event_ptr);
  109. void load_his_data_from_db();
  110. ~event_list(){}
  111. static std::string ev_to_json(std::shared_ptr<ya_event> ev_ptr)
  112. {
  113. std::vector<std::shared_ptr<ya_event>> evs;
  114. evs.push_back(ev_ptr);
  115. return evs_to_json(evs);
  116. }
  117. static std::string evs_to_json(std::vector<std::shared_ptr<ya_event>> arr);
  118. private:
  119. static void _ev_to_node(std::shared_ptr<ya_event> ev_ptr,
  120. rapidjson::Document::AllocatorType& allocator,
  121. rapidjson::Value& out_data);
  122. };
  123. #endif