添加推送时间间隔

This commit is contained in:
quantulr
2024-03-22 15:24:20 +08:00
parent b1549787df
commit 048b587396
3 changed files with 23 additions and 8 deletions

View File

@ -6,6 +6,7 @@ from pathlib import Path
import requests
import yaml
from redis import asyncio as aioredis
from redis.asyncio import Redis
def read_config():
@ -49,7 +50,7 @@ def push_notification(push_url: str, notify_content: dict):
logging.error(f"网络错误推送失败: {e}")
def handle_event_detail(event_detail: dict, config: dict):
async def handle_event_detail(event_detail: dict, config: dict, redis: Redis):
user_map = config["user_ids"]
device_map = config["devices"]
event_map = config["event_type"]
@ -67,6 +68,15 @@ def handle_event_detail(event_detail: dict, config: dict):
device_name = device_map.get(src_index, "未知设备")
event_detail["deviceName"] = device_name
# 从redis中查询最近是否发生过该事件
happen_recent = await redis.get(name=f"hik-push-interval:{device_name}:{event_type}")
# 如果同一地点最近发生过同一事件则不推送
if happen_recent is not None:
return
# 如果事件类型为区域入侵或吸烟or打电话
if event_type == 131588 or event_type == 422400002:
await redis.set(name=f"hik-push-interval:{device_name}:{event_type}", value=event_detail["happenTime"], ex=80)
# 添加regionName
if "srcName" in event_detail:
event_detail["regionName"] = event_detail["srcName"]
@ -100,7 +110,6 @@ def handle_event_detail(event_detail: dict, config: dict):
if "eventType" in event_detail["data"]:
# 事件的分析结果字段名称
detection_field_name = event_detail["data"]["eventType"]
logging.info(f"DFF{detection_field_name}")
if detection_field_name in event_detail["data"]:
# 不同事件的分析结果字段名称不同将字段名称替换为_detectionResult
event_detail["data"]["_detectionResult"] = (
@ -131,7 +140,9 @@ async def push_loop():
logging.basicConfig(level=logging.INFO)
config = read_config()
redis_client = await aioredis.Redis(
host="127.0.0.1", port=7019, password="SMHdFrlK"
host="127.0.0.1",
port=7019,
password="SMHdFrlK"
)
push_url = config["push_url"]
while True:
@ -148,13 +159,13 @@ async def push_loop():
event_detail = event_details[0]
event_detail["eventLvl"] = event["eventLvl"]
event_detail["happenTime"] = event["happenTime"]
notify_json = handle_event_detail(event_detail, config)
notify_json = await handle_event_detail(event_detail, config, redis_client)
else:
notify_json = handle_event_detail(event, config)
notify_json = await handle_event_detail(event, config, redis_client)
if notify_json is not None:
push_notification(push_url, notify_json)
else:
logging.error("No event detected")
logging.error("事件间隔小于80秒")
except Exception as e:
logging.error("error: ", e)
continue

View File

@ -8,7 +8,11 @@ app = FastAPI()
async def add_stream(data: dict):
redis = await aioredis.Redis(host="127.0.0.1", port=7019, password="SMHdFrlK")
redis = await aioredis.Redis(
host="127.0.0.1",
port=7019,
password="SMHdFrlK"
)
await redis.lpush("hik-sub-event", json.dumps(data))
await redis.close()

View File

@ -1,6 +1,6 @@
[tool.poetry]
name = "hik-push"
version = "1.0.4"
version = "1.0.5"
description = ""
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
readme = "README.md"