event.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //超速告警所在格子
  72. int m_cell_index;
  73. //超速告警的实际速度
  74. double m_speed;
  75. //是否已经发送 ture bu发送, false推送. 推送完置为true
  76. bool m_is_sent;
  77. uint8_t landmark_id=0;
  78. uint8_t landmark_dir=0;
  79. double landmark_dis=0;
  80. bool is_end()
  81. {
  82. return ES_END == m_status;
  83. }
  84. ///作为事件map列表的id,方便查找;
  85. uint64_t get_list_id();
  86. };
  87. class Event;
  88. struct event_tool
  89. {
  90. template <class UnaryPredicate>
  91. 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="")
  92. {
  93. handle_event(ot,et,id,limit_value,cur_value,p(),edt,desc);
  94. }
  95. 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="");
  96. static event_tool * instance();
  97. event_tool()
  98. {
  99. make_event_object();
  100. }
  101. private:
  102. void make_event_object();
  103. std::map<OBJECT_TYPE,std::shared_ptr<Event>> m_map;
  104. };
  105. struct event_list:single_base<event_list,uint64_t,std::shared_ptr<ya_event>>
  106. {
  107. public:
  108. static uint64_t to_list_id(EVENT_TYPE ev_type, OBJECT_TYPE obj_type,uint64_t obj_id,EVENT_DIS_TYPE edt)
  109. {
  110. return (static_cast<uint64_t>(edt)<<58)|(static_cast<uint64_t>(ev_type)<<48)|(static_cast<uint64_t>(obj_type)<<40)|obj_id;
  111. }
  112. 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);
  113. std::shared_ptr<ya_event> get_event_area(int32_t area_id, EVENT_TYPE ev_type,EVENT_DIS_TYPE edt)
  114. {
  115. return base::get(to_list_id(ev_type, OT_AREA, static_cast<uint64_t>(area_id),edt));
  116. }
  117. std::shared_ptr<ya_event> get_event_reader(int32_t reader_id, EVENT_TYPE ev_type,EVENT_DIS_TYPE edt=DT_COMMON)
  118. {
  119. return base::get(to_list_id(ev_type,OT_DEVICE_READER, static_cast<uint64_t>(reader_id),edt));
  120. }
  121. static void save_event(const std::shared_ptr<ya_event> &event_ptr);
  122. static void save_event_v(const std::shared_ptr<ya_event>& event_ptr);
  123. void load_his_data_from_db(bool init=true);
  124. void load_his_data_v_from_db();
  125. ~event_list(){}
  126. static std::string ev_to_json(std::shared_ptr<ya_event> ev_ptr)
  127. {
  128. std::vector<std::shared_ptr<ya_event>> evs;
  129. evs.push_back(ev_ptr);
  130. return evs_to_json(evs);
  131. }
  132. static std::string evs_to_json_v(std::vector<std::shared_ptr<ya_event>> arr);
  133. static void _ev_to_node_v(std::shared_ptr<ya_event> ev_ptr, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& out_data);
  134. static std::string evs_to_json(std::vector<std::shared_ptr<ya_event>> arr);
  135. private:
  136. static void _ev_to_node(std::shared_ptr<ya_event> ev_ptr,
  137. rapidjson::Document::AllocatorType& allocator,
  138. rapidjson::Value& out_data);
  139. };
  140. #endif