告警间隔时间内不触发

This commit is contained in:
caoqianming 2023-03-10 14:53:11 +08:00
parent be30c4d234
commit 5cb1331000
3 changed files with 41 additions and 28 deletions

View File

@ -250,6 +250,7 @@ def check_not_in_place(opl: Opl):
for i in tds: for i in tds:
if i.code not in macs: if i.code not in macs:
# 触发作业人员未就位事件 # 触发作业人员未就位事件
cate = EventCate.objects.get(code='not_in_place')
event = Event() event = Event()
event.area = area event.area = area
event.obj_cate = 'opl' event.obj_cate = 'opl'
@ -257,7 +258,6 @@ def check_not_in_place(opl: Opl):
event.opl = opl event.opl = opl
event.happen_time = timezone.now() event.happen_time = timezone.now()
event.save() event.save()
cate = EventCate.objects.get(code='not_in_place')
Eventdo.objects.get_or_create(cate=cate, event=event, defaults={ Eventdo.objects.get_or_create(cate=cate, event=event, defaults={
'cate': cate, 'cate': cate,
'event': event 'event': event
@ -434,13 +434,8 @@ def loc_change(data):
if area_fix['stay_minute_max'] > 0 and area_fix['stay_minute_max'] < stay_minute: if area_fix['stay_minute_max'] > 0 and area_fix['stay_minute_max'] < stay_minute:
# 触发超时滞留事件 # 触发超时滞留事件
code_name = 'stand_area' code_name = 'stand_area'
if code_name: # 2分钟不再次触发 if code_name:
last_event = Event.objects.filter( handle_xx_event_2(code_name, ep=blts.employee, area=Area.objects.get(id=area_fix['id']))
cates__code=code_name, employee=blts.employee).order_by('-create_time').first()
if last_event and last_event.create_time + timedelta(minutes=2) > timezone.now():
pass
else:
handle_xx_event_2(code_name, ep=blts.employee, area=Area.objects.get(id=area_fix['id']))
else: else:
ep_loc_dict['time1'] = time2 ep_loc_dict['time1'] = time2
ep_loc_dict['area_fix_id'] = area_fix['id'] if area_fix else None ep_loc_dict['area_fix_id'] = area_fix['id'] if area_fix else None
@ -458,7 +453,8 @@ def handle_xx_event(name: str, data: dict):
# 找到最近未处理同一人发生的事件 # 找到最近未处理同一人发生的事件
if cate: # 5分钟内不再次触发 if cate: # 5分钟内不再次触发
last_event = Event.objects.filter(cates__code=name, employee=blts.employee).order_by('-create_time').first() last_event = Event.objects.filter(cates__code=name, employee=blts.employee).order_by('-create_time').first()
if last_event and last_event.create_time + timedelta(minutes=5) > timezone.now(): same_allow_minute = cate.same_allow_minute
if same_allow_minute >0 and last_event and last_event.create_time + timedelta(minutes=cate.same_allow_minute) > timezone.now():
return return
event = Event() event = Event()
# 查询定位信息 # 查询定位信息
@ -485,26 +481,27 @@ def handle_xx_event(name: str, data: dict):
def handle_xx_event_2(name: str, ep: Employee, area: Area): def handle_xx_event_2(name: str, ep: Employee, area: Area):
# 违规进入事件特殊处理 # 违规进入事件特殊处理
# 找寻该区域下审批和进行的作业, 本厂或相关方人员, 如是就不触发 # 找寻该区域下审批和进行的作业, 本厂或相关方人员, 如是就不触发
if name == 'i_enter' and ep.type in ['employee', 'remployee']:
last_event = Event.objects.filter(
cates__code='i_enter', employee=ep, area=area).order_by('-create_time').first()
if last_event and last_event.create_time + timedelta(minutes=2) > timezone.now(): # 2分钟不再次触发
return
ops = Operation.objects.filter(area=area, state__in=[Operation.OP_AUDIT, Operation.OP_WAIT, Operation.OP_WORK])
if OplWorker.objects.filter(opl__operation__in=ops, worker__employee=ep).exists():
# 如果是作业人员
return
elif ops.filter(coordinator__employee=ep).exists():
# 如果是协调员
return
elif Opl.objects.filter(operation__in=ops, charger__employee=ep).exists():
# 如果是作业负责人
return
elif Opl.objects.filter(operation__in=ops, monitor__employee=ep).exists():
# 如果是作业监护人
return
cate = EventCate.objects.filter(code=name).first() cate = EventCate.objects.filter(code=name).first()
if cate: if cate:
if name == 'i_enter' and ep.type in ['employee', 'remployee']:
last_event = Event.objects.filter(
cates__code='i_enter', employee=ep, area=area).order_by('-create_time').first()
same_allow_minute = cate.same_allow_minute
if same_allow_minute>0 and last_event and last_event.create_time + timedelta(minutes=same_allow_minute) > timezone.now(): # 2分钟不再次触发
return
ops = Operation.objects.filter(area=area, state__in=[Operation.OP_AUDIT, Operation.OP_WAIT, Operation.OP_WORK])
if OplWorker.objects.filter(opl__operation__in=ops, worker__employee=ep).exists():
# 如果是作业人员
return
elif ops.filter(coordinator__employee=ep).exists():
# 如果是协调员
return
elif Opl.objects.filter(operation__in=ops, charger__employee=ep).exists():
# 如果是作业负责人
return
elif Opl.objects.filter(operation__in=ops, monitor__employee=ep).exists():
# 如果是作业监护人
return
event = Event() event = Event()
event.area = area event.area = area
# 查询定位信息 # 查询定位信息
@ -605,10 +602,14 @@ def get_area_from_point(x: int, y: int, floorNo: str, area_fix_id):
def snap_and_analyse(vchannel: TDevice, algo_codes: list, opl: Opl = None): def snap_and_analyse(vchannel: TDevice, algo_codes: list, opl: Opl = None):
global_img_o = dhClient.snap(vchannel.code) global_img_o = dhClient.snap(vchannel.code)
happen_time = timezone.now() happen_time = timezone.now()
if global_img_o is None:
return
ec_codes = ai_analyse_2(algo_codes, global_img=global_img_o) # 算法处理返回的事件结果 ec_codes = ai_analyse_2(algo_codes, global_img=global_img_o) # 算法处理返回的事件结果
if ec_codes: if ec_codes:
# 获取本次所有发生事件种类 # 获取本次所有发生事件种类
ecs = EventCate.objects.filter(code__in=ec_codes.keys()) ecs = EventCate.objects.filter(code__in=ec_codes.keys())
if not ecs.exists():
return
obj_cate = 'opl' if opl else 'other' obj_cate = 'opl' if opl else 'other'
ep = None ep = None
if 'helmet' in ec_codes or 'helmet2' in ec_codes and obj_cate == 'other': if 'helmet' in ec_codes or 'helmet2' in ec_codes and obj_cate == 'other':
@ -619,6 +620,12 @@ def snap_and_analyse(vchannel: TDevice, algo_codes: list, opl: Opl = None):
# if res and res[0]: # if res and res[0]:
# ep = Employee.objects.filter(id_number=res[0]['identity']).first() # ep = Employee.objects.filter(id_number=res[0]['identity']).first()
pass pass
last_event = Event.objects.filter(cates__in=ecs, employee=ep, obj_cate=obj_cate).order_by('-create_time').first()
same_allow_minute = ecs.order_by('same_allow_minute').first().same_allow_minute
if same_allow_minute>0 and last_event and last_event.create_time + timedelta(minutes=same_allow_minute) > timezone.now(): # 告警间隔范围内不再次触发
return
event = Event() event = Event()
event.global_img = ec_codes['global_img'] if ec_codes.get('global_img', None) else save_dahua_pic(global_img_o) event.global_img = ec_codes['global_img'] if ec_codes.get('global_img', None) else save_dahua_pic(global_img_o)
event.vchannel = vchannel event.vchannel = vchannel

View File

@ -19,6 +19,7 @@ import time
from django.core.cache import cache from django.core.cache import cache
from django.conf import settings from django.conf import settings
from apps.utils.tasks import CustomTask from apps.utils.tasks import CustomTask
from datetime import timedelta
@shared_task(base=CustomTask) @shared_task(base=CustomTask)
@ -61,6 +62,11 @@ def update_count_people(i: Area):
def handle_xx_event_3(name: str, area: Area): def handle_xx_event_3(name: str, area: Area):
cate = EventCate.objects.filter(code=name).first() cate = EventCate.objects.filter(code=name).first()
if cate: if cate:
# 告警间隔内不触发
last_event = Event.objects.filter(cates=cate, area=area, obj_cate='area').order_by('-create_time').first()
same_allow_minute = cate.same_allow_minute
if same_allow_minute >0 and last_event and last_event.create_time + timedelta(minutes=cate.same_allow_minute) > timezone.now():
return
event = Event() event = Event()
event.area = area event.area = area
event.obj_cate = 'area' event.obj_cate = 'area'

View File

@ -39,7 +39,7 @@ class EventCateViewSet(CreateModelMixin, UpdateModelMixin, ListModelMixin, Custo
create_serializer_class = EventCateCreateUpdateSerializer create_serializer_class = EventCateCreateUpdateSerializer
update_serializer_class = EventCateCreateUpdateSerializer update_serializer_class = EventCateCreateUpdateSerializer
serializer_class = EventCateListSerializer serializer_class = EventCateListSerializer
filterset_fields = ['self_algo'] filterset_fields = ['self_algo', 'trigger']
ordering = ['priority', 'create_time'] ordering = ['priority', 'create_time']