area_business_count_checker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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->get_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. /*int pc=a->m_area->m_person_count.load();
  63. int pc_=a->m_area->m_person_show_count.load();
  64. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
  65. if(pc>limit_val){
  66. lock();
  67. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=true,DT_NORMAL);
  68. unlock();
  69. log_info("person_count_enter:%d,v_count:%d limit:%d",aid,pc,limit_val);
  70. }
  71. if(pc_>limit_val){
  72. lock();
  73. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=true,DT_SPECIAL);
  74. unlock();
  75. log_info("person_count_enter_show:%d,v_count:%d limit:%d",aid,pc_,limit_val);
  76. }*/
  77. }
  78. else if (c->is_vehicle())
  79. {
  80. int limit_val=a->m_area->m_limit_vehicle_count;
  81. int aid=a->m_area->id();
  82. if(limit_val<=0)
  83. {
  84. log_warn("on_enter_area_id:%d,limit_vehicle_count is 0.so should not run area_business_count_checker...",aid);
  85. return;
  86. }
  87. if(!ptr)
  88. a->m_area->m_vehicle_count ++ ;
  89. if(c->m_display)
  90. a->m_area->m_vehicle_show_count ++ ;
  91. _add_vehicle_count_event(a,limit_val);
  92. /*int vc=a->m_area->m_vehicle_count.load();
  93. int vc_=a->m_area->m_vehicle_show_count.load();
  94. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  95. if(vc>limit_val){
  96. lock();
  97. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc,a->m_area->m_event_vehicle_count=true,DT_NORMAL);
  98. unlock();
  99. log_info("vehicle_count_enter:%d,v_count:%d limit:%d",aid,vc,limit_val);
  100. }
  101. if(vc_>limit_val){
  102. lock();
  103. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc_,a->m_area->m_event_vehicle_show_count=true,DT_SPECIAL);
  104. unlock();
  105. log_info("vehicle_count_enter_show:%d,v_count:%d limit:%d",aid,vc_,limit_val);
  106. }*/
  107. }
  108. c->update_display();
  109. }
  110. void area_business_count_checker::on_hover(const std::shared_ptr<area_hover>&a,
  111. const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
  112. {
  113. if(!c->eq_display())
  114. {
  115. ptr=std::make_shared<business_data>();
  116. if(c->m_display==1)
  117. on_enter(a,c,ptr);
  118. else if(c->m_display==0)
  119. on_leave(a,c,ptr);
  120. c->update_display();
  121. ptr=nullptr;
  122. return;
  123. }
  124. if (c->is_person() && a->m_area->is_time_person_thre())
  125. {
  126. int limit_val = 0;
  127. if (!a->m_area->is_limit_person_count_check(limit_val) || limit_val <= 0)
  128. {
  129. return;
  130. }
  131. _remove_person_count_event(a,limit_val,"area_business_count_checker::on_hover");
  132. _add_person_count_event(a,limit_val,"area_business_count_checker::on_hover");
  133. }
  134. }
  135. //减少计数
  136. void area_business_count_checker::on_leave(const std::shared_ptr<area_hover>&a,
  137. const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
  138. {
  139. if (nullptr == a->m_area )return ;
  140. int flag=c->m_display;
  141. if(!ptr){
  142. if(!c->eq_display()){
  143. c->update_display();
  144. //从0 变成1 之前是0 m_show_count没加1 这里不需要减一
  145. if(c->m_display==1)flag=0;
  146. //从1 变成 0 之前是加1了。 这里需要减去
  147. if(c->m_display==0)flag=1;
  148. }
  149. }
  150. if (c->is_person())
  151. {
  152. if(ptr)
  153. a->m_area->m_person_show_count -- ;
  154. else{
  155. a->m_area->m_person_count -- ;
  156. //if(c->m_display)
  157. if(flag)
  158. a->m_area->m_person_show_count -- ;
  159. }
  160. int limit_val=a->m_area->get_limit_person_count();
  161. _remove_person_count_event(a,limit_val,"person_count_leave");
  162. /*int pc=a->m_area->m_person_count.load();
  163. int pc_=a->m_area->m_person_show_count.load();
  164. int aid=a->m_area->id();
  165. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
  166. if(a->m_area->m_event_person_count&& pc <= limit_val){
  167. lock();
  168. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=false,DT_NORMAL);
  169. unlock();
  170. log_info("person_count_leave:%d,v_count:%d limit:%d",aid,pc,limit_val);
  171. }
  172. if(a->m_area->m_event_person_show_count&& pc_ <= limit_val){
  173. lock();
  174. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=false,DT_SPECIAL);
  175. unlock();
  176. log_info("person_count_leave_show:%d,v_count:%d limit:%d",aid,pc,limit_val);
  177. }
  178. */
  179. }
  180. else if (c->is_vehicle())
  181. {
  182. if(ptr)
  183. a->m_area->m_vehicle_show_count -- ;
  184. else{
  185. a->m_area->m_vehicle_count -- ;
  186. //if(c->m_display)
  187. if(flag)
  188. a->m_area->m_vehicle_show_count -- ;
  189. }
  190. int limit_val=a->m_area->m_limit_vehicle_count;
  191. _remove_vehicle_count_event(a,limit_val);
  192. /*int pc=a->m_area->m_vehicle_count.load();
  193. int pc_=a->m_area->m_vehicle_show_count.load();
  194. int aid=a->m_area->id();
  195. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  196. if(a->m_area->m_event_vehicle_count&& pc <= limit_val){
  197. lock();
  198. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_vehicle_count=false,DT_NORMAL);
  199. unlock();
  200. log_info("vehicle_count_leave:%d,v_count:%d limit:%d",aid,pc,limit_val);
  201. }
  202. if(a->m_area->m_event_vehicle_show_count&& pc_ <= limit_val){
  203. lock();
  204. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_vehicle_show_count=false,DT_SPECIAL);
  205. unlock();
  206. log_info("vehicle_count_leave_show:%d,v_count:%d limit:%d",aid,pc,limit_val);
  207. }*/
  208. }
  209. }
  210. //添加人员告警
  211. void area_business_count_checker::_add_person_count_event(const std::shared_ptr<area_hover>&a
  212. ,int limit_val,const std::string &desc)
  213. {
  214. int aid = a->m_area->id();
  215. int pc = a->m_area->m_person_count.load();
  216. int pc_ = a->m_area->m_person_show_count.load();
  217. EVENT_TYPE ev = a->m_area->is_mine() ? EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON;
  218. if(pc >= limit_val )
  219. {
  220. lock();
  221. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=true,DT_NORMAL);
  222. unlock();
  223. log_info("%s:%d,v_count:%d limit:%d",desc.c_str(),aid,pc,limit_val);
  224. }
  225. if(pc_ >= limit_val )
  226. {
  227. lock();
  228. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=true,DT_SPECIAL);
  229. unlock();
  230. log_info("%s:%d,v_count:%d limit:%d",desc.c_str(),aid,pc_,limit_val);
  231. }
  232. }
  233. //移除人员告警
  234. void area_business_count_checker::_remove_person_count_event(const std::shared_ptr<area_hover>&a
  235. ,int limit_val,const std::string &desc)
  236. {
  237. int aid = a->m_area->id();
  238. int pc = a->m_area->m_person_count.load();
  239. int pc_ = a->m_area->m_person_show_count.load();
  240. EVENT_TYPE ev = a->m_area->is_mine() ? EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON;
  241. if(pc <= limit_val && a->m_area->m_event_person_count)
  242. {
  243. lock();
  244. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_person_count=false,DT_NORMAL);
  245. unlock();
  246. log_info("%s :%d,v_count:%d limit:%d",desc.c_str(),aid,pc,limit_val);
  247. }
  248. if(pc_ <= limit_val && a->m_area->m_event_person_show_count)
  249. {
  250. lock();
  251. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_person_show_count=false,DT_SPECIAL);
  252. unlock();
  253. log_info("%s :%d,v_count:%d limit:%d",desc.c_str(),aid,pc_,limit_val);
  254. }
  255. }
  256. //添加车数量超出限制告警
  257. void area_business_count_checker::_add_vehicle_count_event(const std::shared_ptr<area_hover>&a
  258. ,int limit_val,const std::string &desc)
  259. {
  260. int aid = a->m_area->id();
  261. int vc=a->m_area->m_vehicle_count.load();
  262. int vc_=a->m_area->m_vehicle_show_count.load();
  263. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  264. if(vc>limit_val){
  265. lock();
  266. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc,a->m_area->m_event_vehicle_count=true,DT_NORMAL);
  267. unlock();
  268. log_info("vehicle_count_enter:%d,v_count:%d limit:%d",aid,vc,limit_val);
  269. }
  270. if(vc_>limit_val){
  271. lock();
  272. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,vc_,a->m_area->m_event_vehicle_show_count=true,DT_SPECIAL);
  273. unlock();
  274. log_info("vehicle_count_enter_show:%d,v_count:%d limit:%d",aid,vc_,limit_val);
  275. }
  276. }
  277. //移除车数量超出告警
  278. void area_business_count_checker::_remove_vehicle_count_event(const std::shared_ptr<area_hover>&a
  279. ,int limit_val,const std::string &desc)
  280. {
  281. int pc=a->m_area->m_vehicle_count.load();
  282. int pc_=a->m_area->m_vehicle_show_count.load();
  283. int aid=a->m_area->id();
  284. EVENT_TYPE ev = a->m_area->is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
  285. if(a->m_area->m_event_vehicle_count&& pc <= limit_val){
  286. lock();
  287. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc,a->m_area->m_event_vehicle_count=false,DT_NORMAL);
  288. unlock();
  289. log_info("vehicle_count_leave:%d,v_count:%d limit:%d",aid,pc,limit_val);
  290. }
  291. if(a->m_area->m_event_vehicle_show_count&& pc_ <= limit_val){
  292. lock();
  293. event_tool::instance()->handle_event(OT_AREA,ev,aid,limit_val,pc_,a->m_area->m_event_vehicle_show_count=false,DT_SPECIAL);
  294. unlock();
  295. log_info("vehicle_count_leave_show:%d,v_count:%d limit:%d",aid,pc,limit_val);
  296. }
  297. }