area_business_count_checker.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "event.h"
  2. #include "area.h"
  3. #include "card_base.h"
  4. #include "common_tool.h"
  5. #include "log.h"
  6. #include "area_business_count_checker.h"
  7. /*
  8. 判断当前区域a中的人数是否超过设定人数,超过后告警
  9. 区域内实时人数存储在area对象中,在当前类on_enter/on_leave中进行更新
  10. 整个井下的超员和某个区域的超员都使用这个代码
  11. */
  12. static void check_event(bool &event_flag,EVENT_TYPE et,EVENT_DIS_TYPE edt,int id)
  13. {
  14. if(!event_flag)
  15. {
  16. auto ev_p = event_list::instance()->get_event_area(id,et,edt);
  17. if (ev_p && !ev_p->is_end())
  18. event_flag = true;
  19. }
  20. }
  21. //服务器重启加载数据
  22. void area_business_count_checker::on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr)
  23. {
  24. if (nullptr == a->m_area )
  25. return ;
  26. if (c->is_person())
  27. {
  28. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
  29. check_event(a->m_area->m_event_person_count,ev,DT_NORMAL,a->m_area->id());
  30. check_event(a->m_area->m_event_person_show_count,ev,DT_SPECIAL,a->m_area->id());
  31. }
  32. else if (c->is_vehicle())
  33. {
  34. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  35. check_event(a->m_area->m_event_vehicle_count,ev,DT_NORMAL,a->m_area->id());
  36. check_event(a->m_area->m_event_vehicle_show_count,ev,DT_SPECIAL,a->m_area->id());
  37. }
  38. on_enter(a,c,ptr);
  39. }
  40. //增加计数,并进行判断
  41. //多线程环境有可能同时产生相同的告警。
  42. //如果多个线程同一时间触发。
  43. void area_business_count_checker::on_enter(const std::shared_ptr<area_hover>&a,
  44. const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr)
  45. {
  46. if (nullptr == a->m_area )
  47. return ;
  48. if (c->is_person())
  49. {
  50. int limit_val=a->m_area->m_limit_person_count;
  51. int aid=a->m_area->id();
  52. if(limit_val<=0)
  53. {
  54. log_warn("on_enter_area_id:%d,limit_person_count is 0.so should not run area_business_count_checker...",aid);
  55. return;
  56. }
  57. if(!ptr)
  58. a->m_area->m_person_count ++;
  59. if(c->m_display)
  60. a->m_area->m_person_show_count ++ ;
  61. _add_person_count_event(a,limit_val,"person_count_enter");
  62. }
  63. else if (c->is_vehicle())
  64. {
  65. int limit_val=a->m_area->m_limit_vehicle_count;
  66. int aid=a->m_area->id();
  67. if(limit_val<=0)
  68. {
  69. log_warn("on_enter_area_id:%d,limit_vehicle_count is 0.so should not run area_business_count_checker...",aid);
  70. return;
  71. }
  72. if(!ptr)
  73. a->m_area->m_vehicle_count ++ ;
  74. if(c->m_display)
  75. a->m_area->m_vehicle_show_count ++ ;
  76. _add_vehicle_count_event(a,limit_val);
  77. }
  78. c->update_display();
  79. }
  80. void area_business_count_checker::on_hover(const std::shared_ptr<area_hover>&a,
  81. const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
  82. {
  83. if(!c->eq_display())
  84. {
  85. ptr=std::make_shared<business_data>();
  86. if(c->m_display==1)
  87. on_enter(a,c,ptr);
  88. else if(c->m_display==0)
  89. on_leave(a,c,ptr);
  90. c->update_display();
  91. ptr=nullptr;
  92. }
  93. if (c->is_person() && a->m_area->get_limit_person_count())
  94. {
  95. int limit_val = a->m_area->m_limit_person_count;
  96. _remove_person_count_event(a,limit_val,"area_business_count_checker::on_hover");
  97. _add_person_count_event(a,limit_val,"area_business_count_checker::on_hover");
  98. }
  99. }
  100. //减少计数
  101. void area_business_count_checker::on_leave(const std::shared_ptr<area_hover>&a,
  102. const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
  103. {
  104. if (nullptr == a->m_area )return ;
  105. int flag=c->m_display;
  106. if(!ptr){
  107. if(!c->eq_display()){
  108. c->update_display();
  109. //从0 变成1 之前是0 m_show_count没加1 这里不需要减一
  110. if(c->m_display==1)flag=0;
  111. //从1 变成 0 之前是加1了。 这里需要减去
  112. if(c->m_display==0)flag=1;
  113. }
  114. }
  115. if (c->is_person())
  116. {
  117. if(ptr)
  118. a->m_area->m_person_show_count -- ;
  119. else{
  120. a->m_area->m_person_count -- ;
  121. //if(c->m_display)
  122. if(flag)
  123. a->m_area->m_person_show_count -- ;
  124. }
  125. _remove_person_count_event(a , a->m_area->m_limit_person_count ,"person_count_leave");
  126. }
  127. else if (c->is_vehicle())
  128. {
  129. if(ptr)
  130. a->m_area->m_vehicle_show_count -- ;
  131. else{
  132. a->m_area->m_vehicle_count -- ;
  133. //if(c->m_display)
  134. if(flag)
  135. a->m_area->m_vehicle_show_count -- ;
  136. }
  137. _remove_vehicle_count_event(a,a->m_area->m_limit_vehicle_count);
  138. }
  139. }
  140. //添加人员告警
  141. void area_business_count_checker::_add_person_count_event(const std::shared_ptr<area_hover>&a
  142. ,int limit_val,const std::string &desc)
  143. {
  144. int aid = a->m_area->id();
  145. int pc = a->m_area->m_person_count.load();
  146. int pc_ = a->m_area->m_person_show_count.load();
  147. EVENT_TYPE ev = a->m_area->is_mine() ? EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON;
  148. if(pc > limit_val )
  149. {
  150. lock();
  151. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=true,DT_NORMAL);
  152. unlock();
  153. log_info("%s:%d,v_count:%d limit:%d",desc.c_str(),aid,pc,limit_val);
  154. }
  155. if(pc_ > limit_val )
  156. {
  157. lock();
  158. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=true,DT_SPECIAL);
  159. unlock();
  160. log_info("%s:%d,v_count:%d limit:%d",desc.c_str(),aid,pc_,limit_val);
  161. }
  162. }
  163. //移除人员告警
  164. void area_business_count_checker::_remove_person_count_event(const std::shared_ptr<area_hover>&a
  165. ,int limit_val,const std::string &desc)
  166. {
  167. int aid = a->m_area->id();
  168. int pc = a->m_area->m_person_count.load();
  169. int pc_ = a->m_area->m_person_show_count.load();
  170. EVENT_TYPE ev = a->m_area->is_mine() ? EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON;
  171. if(pc <= limit_val && a->m_area->m_event_person_count)
  172. {
  173. lock();
  174. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=false,DT_NORMAL);
  175. unlock();
  176. log_info("%s :%d,v_count:%d limit:%d",desc.c_str(),aid,pc,limit_val);
  177. }
  178. if(pc_ <= limit_val && a->m_area->m_event_person_show_count)
  179. {
  180. lock();
  181. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=false,DT_SPECIAL);
  182. unlock();
  183. log_info("%s :%d,v_count:%d limit:%d",desc.c_str(),aid,pc_,limit_val);
  184. }
  185. }
  186. //添加车数量超出限制告警
  187. void area_business_count_checker::_add_vehicle_count_event(const std::shared_ptr<area_hover>&a
  188. ,int limit_val,const std::string &desc)
  189. {
  190. int aid = a->m_area->id();
  191. int vc=a->m_area->m_vehicle_count.load();
  192. int vc_=a->m_area->m_vehicle_show_count.load();
  193. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  194. if(vc>limit_val){
  195. lock();
  196. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc,a->m_area->m_event_vehicle_count=true,DT_NORMAL);
  197. unlock();
  198. log_info("vehicle_count_enter:%d,v_count:%d limit:%d",aid,vc,limit_val);
  199. }
  200. if(vc_>limit_val){
  201. lock();
  202. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc_,a->m_area->m_event_vehicle_show_count=true,DT_SPECIAL);
  203. unlock();
  204. log_info("vehicle_count_enter_show:%d,v_count:%d limit:%d",aid,vc_,limit_val);
  205. }
  206. }
  207. //移除车数量超出告警
  208. void area_business_count_checker::_remove_vehicle_count_event(const std::shared_ptr<area_hover>&a
  209. ,int limit_val,const std::string &desc)
  210. {
  211. int pc=a->m_area->m_vehicle_count.load();
  212. int pc_=a->m_area->m_vehicle_show_count.load();
  213. int aid=a->m_area->id();
  214. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  215. if(a->m_area->m_event_vehicle_count&& pc <= limit_val){
  216. lock();
  217. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_vehicle_count=false,DT_NORMAL);
  218. unlock();
  219. log_info("vehicle_count_leave:%d,v_count:%d limit:%d",aid,pc,limit_val);
  220. }
  221. if(a->m_area->m_event_vehicle_show_count&& pc_ <= limit_val){
  222. lock();
  223. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_vehicle_show_count=false,DT_SPECIAL);
  224. unlock();
  225. log_info("vehicle_count_leave_show:%d,v_count:%d limit:%d",aid,pc,limit_val);
  226. }
  227. }