area_business_count_checker.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. int pc=a->m_area->m_person_count.load();
  62. int pc_=a->m_area->m_person_show_count.load();
  63. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
  64. if(pc>limit_val){
  65. lock();
  66. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=true,DT_NORMAL);
  67. unlock();
  68. log_info("person_count_enter:%d,v_count:%d limit:%d",aid,pc,limit_val);
  69. }
  70. if(pc_>limit_val){
  71. lock();
  72. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=true,DT_SPECIAL);
  73. unlock();
  74. log_info("person_count_enter_show:%d,v_count:%d limit:%d",aid,pc_,limit_val);
  75. }
  76. }
  77. else if (c->is_vehicle())
  78. {
  79. int limit_val=a->m_area->m_limit_vehicle_count;
  80. int aid=a->m_area->id();
  81. if(limit_val<=0)
  82. {
  83. log_warn("on_enter_area_id:%d,limit_vehicle_count is 0.so should not run area_business_count_checker...",aid);
  84. return;
  85. }
  86. if(!ptr)
  87. a->m_area->m_vehicle_count ++ ;
  88. if(c->m_display)
  89. a->m_area->m_vehicle_show_count ++ ;
  90. int vc=a->m_area->m_vehicle_count.load();
  91. int vc_=a->m_area->m_vehicle_show_count.load();
  92. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  93. if(vc>limit_val){
  94. lock();
  95. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc,a->m_area->m_event_vehicle_count=true,DT_NORMAL);
  96. unlock();
  97. log_info("vehicle_count_enter:%d,v_count:%d limit:%d",aid,vc,limit_val);
  98. }
  99. if(vc_>limit_val){
  100. lock();
  101. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc_,a->m_area->m_event_vehicle_show_count=true,DT_SPECIAL);
  102. unlock();
  103. log_info("vehicle_count_enter_show:%d,v_count:%d limit:%d",aid,vc_,limit_val);
  104. }
  105. }
  106. c->update_display();
  107. }
  108. void area_business_count_checker::on_hover(const std::shared_ptr<area_hover>&a,
  109. const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
  110. {
  111. if(!c->eq_display())
  112. {
  113. ptr=std::make_shared<business_data>();
  114. if(c->m_display==1)
  115. on_enter(a,c,ptr);
  116. else if(c->m_display==0)
  117. on_leave(a,c,ptr);
  118. c->update_display();
  119. ptr=nullptr;
  120. }
  121. }
  122. //减少计数
  123. void area_business_count_checker::on_leave(const std::shared_ptr<area_hover>&a,
  124. const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
  125. {
  126. if (nullptr == a->m_area )return ;
  127. int flag=c->m_display;
  128. if(!ptr){
  129. if(!c->eq_display()){
  130. c->update_display();
  131. //从0 变成1 之前是0 m_show_count没加1 这里不需要减一
  132. if(c->m_display==1)flag=0;
  133. //从1 变成 0 之前是加1了。 这里需要减去
  134. if(c->m_display==0)flag=1;
  135. }
  136. }
  137. if (c->is_person())
  138. {
  139. if(ptr)
  140. a->m_area->m_person_show_count -- ;
  141. else{
  142. a->m_area->m_person_count -- ;
  143. //if(c->m_display)
  144. if(flag)
  145. a->m_area->m_person_show_count -- ;
  146. }
  147. int pc=a->m_area->m_person_count.load();
  148. int pc_=a->m_area->m_person_show_count.load();
  149. int limit_val=a->m_area->m_limit_person_count;
  150. int aid=a->m_area->id();
  151. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
  152. if(a->m_area->m_event_person_count&& pc <= limit_val){
  153. lock();
  154. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=false,DT_NORMAL);
  155. unlock();
  156. log_info("person_count_leave:%d,v_count:%d limit:%d",aid,pc,limit_val);
  157. }
  158. if(a->m_area->m_event_person_show_count&& pc_ <= limit_val){
  159. lock();
  160. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=false,DT_SPECIAL);
  161. unlock();
  162. log_info("person_count_leave_show:%d,v_count:%d limit:%d",aid,pc,limit_val);
  163. }
  164. }
  165. else if (c->is_vehicle())
  166. {
  167. if(ptr)
  168. a->m_area->m_vehicle_show_count -- ;
  169. else{
  170. a->m_area->m_vehicle_count -- ;
  171. //if(c->m_display)
  172. if(flag)
  173. a->m_area->m_vehicle_show_count -- ;
  174. }
  175. int pc=a->m_area->m_vehicle_count.load();
  176. int pc_=a->m_area->m_vehicle_show_count.load();
  177. int limit_val=a->m_area->m_limit_vehicle_count;
  178. int aid=a->m_area->id();
  179. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  180. if(a->m_area->m_event_vehicle_count&& pc <= limit_val){
  181. lock();
  182. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_vehicle_count=false,DT_NORMAL);
  183. unlock();
  184. log_info("vehicle_count_leave:%d,v_count:%d limit:%d",aid,pc,limit_val);
  185. }
  186. if(a->m_area->m_event_vehicle_show_count&& pc_ <= limit_val){
  187. lock();
  188. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_vehicle_show_count=false,DT_SPECIAL);
  189. unlock();
  190. log_info("vehicle_count_leave_show:%d,v_count:%d limit:%d",aid,pc,limit_val);
  191. }
  192. }
  193. }