event.h 4.1 KB

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