添加推送时间间隔
This commit is contained in:
@ -6,6 +6,7 @@ from pathlib import Path
|
|||||||
import requests
|
import requests
|
||||||
import yaml
|
import yaml
|
||||||
from redis import asyncio as aioredis
|
from redis import asyncio as aioredis
|
||||||
|
from redis.asyncio import Redis
|
||||||
|
|
||||||
|
|
||||||
def read_config():
|
def read_config():
|
||||||
@ -49,7 +50,7 @@ def push_notification(push_url: str, notify_content: dict):
|
|||||||
logging.error(f"网络错误推送失败: {e}")
|
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"]
|
user_map = config["user_ids"]
|
||||||
device_map = config["devices"]
|
device_map = config["devices"]
|
||||||
event_map = config["event_type"]
|
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, "未知设备")
|
device_name = device_map.get(src_index, "未知设备")
|
||||||
event_detail["deviceName"] = device_name
|
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
|
# 添加regionName
|
||||||
if "srcName" in event_detail:
|
if "srcName" in event_detail:
|
||||||
event_detail["regionName"] = event_detail["srcName"]
|
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"]:
|
if "eventType" in event_detail["data"]:
|
||||||
# 事件的分析结果字段名称
|
# 事件的分析结果字段名称
|
||||||
detection_field_name = event_detail["data"]["eventType"]
|
detection_field_name = event_detail["data"]["eventType"]
|
||||||
logging.info(f"DFF:{detection_field_name}")
|
|
||||||
if detection_field_name in event_detail["data"]:
|
if detection_field_name in event_detail["data"]:
|
||||||
# 不同事件的分析结果字段名称不同,将字段名称替换为_detectionResult
|
# 不同事件的分析结果字段名称不同,将字段名称替换为_detectionResult
|
||||||
event_detail["data"]["_detectionResult"] = (
|
event_detail["data"]["_detectionResult"] = (
|
||||||
@ -131,7 +140,9 @@ async def push_loop():
|
|||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
config = read_config()
|
config = read_config()
|
||||||
redis_client = await aioredis.Redis(
|
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"]
|
push_url = config["push_url"]
|
||||||
while True:
|
while True:
|
||||||
@ -148,13 +159,13 @@ async def push_loop():
|
|||||||
event_detail = event_details[0]
|
event_detail = event_details[0]
|
||||||
event_detail["eventLvl"] = event["eventLvl"]
|
event_detail["eventLvl"] = event["eventLvl"]
|
||||||
event_detail["happenTime"] = event["happenTime"]
|
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:
|
else:
|
||||||
notify_json = handle_event_detail(event, config)
|
notify_json = await handle_event_detail(event, config, redis_client)
|
||||||
if notify_json is not None:
|
if notify_json is not None:
|
||||||
push_notification(push_url, notify_json)
|
push_notification(push_url, notify_json)
|
||||||
else:
|
else:
|
||||||
logging.error("No event detected")
|
logging.error("事件间隔小于80秒")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("error: ", e)
|
logging.error("error: ", e)
|
||||||
continue
|
continue
|
||||||
|
@ -8,7 +8,11 @@ app = FastAPI()
|
|||||||
|
|
||||||
|
|
||||||
async def add_stream(data: dict):
|
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.lpush("hik-sub-event", json.dumps(data))
|
||||||
await redis.close()
|
await redis.close()
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "hik-push"
|
name = "hik-push"
|
||||||
version = "1.0.4"
|
version = "1.0.5"
|
||||||
description = ""
|
description = ""
|
||||||
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
|
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
Reference in New Issue
Block a user