1
0

event.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. public:
  15. ya_event(uint64_t e_id):m_cur_time(std::chrono::system_clock::now())
  16. {
  17. m_ev_id = e_id;
  18. m_obj_id = "";
  19. m_map_id = 0;
  20. m_area_id = 0;
  21. x = 0;
  22. y = 0;
  23. m_limit_value = 0;
  24. m_cur_value = 0;
  25. m_desc = "";
  26. m_status=ES_START;
  27. //m_is_display=true;
  28. m_is_sent=false;
  29. }
  30. ~ya_event(){}
  31. public:
  32. //key of event_list.
  33. uint64_t m_ev_id;
  34. uint64_t m_id;
  35. ///告警状态,开始、结束
  36. EVENT_STATUS m_status;
  37. ///告警类型
  38. EVENT_TYPE m_ev_type;
  39. ///告警对象类型
  40. OBJECT_TYPE m_obj_type;
  41. EVENT_DIS_TYPE m_dis_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. //是否已经发送 ture bu发送, false推送. 推送完置为true
  61. bool m_is_sent;
  62. uint8_t landmark_id=0;
  63. uint8_t landmark_dir=0;
  64. double landmark_dis=0;
  65. bool is_end()
  66. {
  67. return ES_END == m_status;
  68. }
  69. ///作为事件map列表的id,方便查找;
  70. uint64_t get_list_id();
  71. };
  72. class Event;
  73. struct event_tool
  74. {
  75. template <class UnaryPredicate>
  76. 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="")
  77. {
  78. handle_event(ot,et,id,limit_value,cur_value,p(),edt,desc);
  79. }
  80. 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="");
  81. static event_tool * instance();
  82. event_tool()
  83. {
  84. make_event_object();
  85. }
  86. private:
  87. void make_event_object();
  88. std::map<OBJECT_TYPE,std::shared_ptr<Event>> m_map;
  89. };
  90. struct event_list:single_base<event_list,uint64_t,std::shared_ptr<ya_event>>
  91. {
  92. public:
  93. static uint64_t to_list_id(EVENT_TYPE ev_type, OBJECT_TYPE obj_type,uint64_t obj_id,EVENT_DIS_TYPE edt)
  94. {
  95. return (static_cast<uint64_t>(edt)<<58)|(static_cast<uint64_t>(ev_type)<<48)|(static_cast<uint64_t>(obj_type)<<40)|obj_id;
  96. }
  97. 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);
  98. std::shared_ptr<ya_event> get_event_area(int32_t area_id, EVENT_TYPE ev_type,EVENT_DIS_TYPE edt)
  99. {
  100. return base::get(to_list_id(ev_type, OT_AREA, static_cast<uint64_t>(area_id),edt));
  101. }
  102. std::shared_ptr<ya_event> get_event_reader(int32_t reader_id, EVENT_TYPE ev_type,EVENT_DIS_TYPE edt=DT_COMMON)
  103. {
  104. return base::get(to_list_id(ev_type,OT_DEVICE_READER, static_cast<uint64_t>(reader_id),edt));
  105. }
  106. static void save_event(const std::shared_ptr<ya_event> &event_ptr);
  107. void load_his_data_from_db(bool init=true);
  108. ~event_list(){}
  109. static std::string ev_to_json(std::shared_ptr<ya_event> ev_ptr)
  110. {
  111. std::vector<std::shared_ptr<ya_event>> evs;
  112. evs.push_back(ev_ptr);
  113. return evs_to_json(evs);
  114. }
  115. static std::string evs_to_json(std::vector<std::shared_ptr<ya_event>> arr);
  116. private:
  117. static void _ev_to_node(std::shared_ptr<ya_event> ev_ptr,
  118. rapidjson::Document::AllocatorType& allocator,
  119. rapidjson::Value& out_data);
  120. };
  121. #endif