area_business_motionless_persion.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "area_business_motionless_persion.h"
  2. #include <chrono>
  3. #include "common_tool.h"
  4. #include "log.h"
  5. #include "area.h"
  6. #include "event.h"
  7. #include "tool_time.h"
  8. #include "area_business.h"
  9. #include "card.h"
  10. #include "config_file.h"
  11. struct motionless_data:business_data
  12. {
  13. motionless_data()
  14. :m_acc_start_time(0)
  15. ,m_acc_0count(0)
  16. ,m_is_warning(false)
  17. {
  18. }
  19. ///;检测到多少次之后定位完全静止
  20. // std::atomic<int> m_acc_0count;
  21. ///检测到多久之后告警
  22. time_t m_acc_start_time;
  23. int m_acc_0count; //连续静止数量
  24. bool m_is_warning; //
  25. };
  26. void area_business_motionless_persion::init(config_file& config)
  27. {
  28. // sleep_ms = std::stoi(config.get("service.motionless_thread_sleep_ms","5000"));
  29. _acc_0count_limit = std::stoi(config.get("service.motionless_acc_0count_limit","40"));
  30. _acc_seconds_limit = std::stoi(config.get("service.motionless_acc_seconds_limit","120"));
  31. }
  32. void area_business_motionless_persion::on_load_his(const std::shared_ptr<area_hover>&area_hover_ptr,
  33. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
  34. {
  35. on_enter(area_hover_ptr, card_ptr, ptr);
  36. }
  37. void area_business_motionless_persion::on_enter(const std::shared_ptr<area_hover>&area_hover_ptr,
  38. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
  39. {
  40. if(!card_ptr->is_person())
  41. {
  42. return;
  43. }
  44. auto ptr_temp = std::make_shared<motionless_data>();
  45. ptr = ptr_temp;
  46. auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_CARD_MOTIONLESS,DT_COMMON);
  47. ptr_temp->m_is_warning = (nullptr != ev_ptr_temp && !ev_ptr_temp->is_end());
  48. }
  49. void area_business_motionless_persion::on_hover(const std::shared_ptr<area_hover>&area_hover_ptr,
  50. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
  51. {
  52. if(!card_ptr->is_person())
  53. {
  54. return;
  55. }
  56. if(nullptr == ptr)
  57. {
  58. log_error("area_business_motionless_persion::on_hover:nullptr == ptr");
  59. return;
  60. }
  61. auto ptr_temp = static_cast<motionless_data*>(ptr.get());
  62. if(0 == static_cast<int>(card_ptr->m_acc))
  63. {
  64. ++ptr_temp->m_acc_0count;
  65. }
  66. else
  67. {
  68. ptr_temp->m_acc_0count=0;
  69. ptr_temp->m_acc_start_time = 0;
  70. }
  71. if(ptr_temp->m_acc_0count >= _acc_0count_limit)
  72. {
  73. ptr_temp->m_acc_0count = _acc_0count_limit;
  74. if(ptr_temp->m_acc_start_time == 0)
  75. {
  76. time(&ptr_temp->m_acc_start_time);
  77. }
  78. if(!ptr_temp->m_is_warning
  79. && tool_time::elapse_seconds(ptr_temp->m_acc_start_time) >= _acc_seconds_limit)
  80. {
  81. ptr_temp->m_is_warning = true;
  82. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  83. event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, true);
  84. }
  85. }
  86. else
  87. {
  88. if(ptr_temp->m_is_warning)
  89. {
  90. ptr_temp->m_is_warning = false;
  91. ptr_temp->m_acc_start_time = 0;
  92. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  93. event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, false);
  94. }
  95. }
  96. }
  97. void area_business_motionless_persion::on_leave(const std::shared_ptr<area_hover>&area_hover_ptr,
  98. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
  99. {
  100. if(!card_ptr->is_person())
  101. {
  102. return;
  103. }
  104. if(nullptr == ptr)
  105. {
  106. log_error("area_business_motionless_persion::on_leave:nullptr == ptr");
  107. return;
  108. }
  109. auto ptr_temp = static_cast<motionless_data*>(ptr.get());
  110. if(ptr_temp->m_is_warning)//如果正在告警,则发送取消告警的信息
  111. {
  112. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  113. event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, false);
  114. }
  115. }
  116. int area_business_motionless_persion::_acc_0count_limit=20;
  117. int area_business_motionless_persion::_acc_seconds_limit=120;
  118. //void module_motionless_persion::deal_alarm(std::shared_ptr<card_location_base>& card_ptr)
  119. //{
  120. // auto area_hover_ptr = card_ptr->get_area_hover();
  121. // if(!area_hover_ptr)
  122. // {
  123. // log_error("if(!area_hover_ptr)==true");
  124. // return;
  125. // }
  126. // auto arr = area_hover_ptr->m_data;
  127. // int index = 1;
  128. // if(arr.size()<2 || !arr[index])
  129. // {
  130. // return;
  131. // }
  132. // auto ptr = static_cast<motionless_data*>(area_hover_ptr->m_data[index].get());
  133. // if(ptr->m_acc_0count >= _acc_0count_limit)
  134. // {
  135. // ptr->m_acc_0count = _acc_0count_limit;
  136. // if(0 == ptr->m_acc_start_time)
  137. // {
  138. // ptr->m_acc_start_time = tool_time::now_to_seconds();
  139. // }
  140. // }
  141. // else
  142. // {
  143. // ptr->m_acc_start_time=0;
  144. // }
  145. // bool is_alarm = (0 != ptr->m_acc_start_time
  146. // &&tool_time::elapse_seconds(ptr->m_acc_start_time)> _acc_seconds_limit);
  147. // uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  148. // event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, is_alarm);
  149. //}