2024-01-26 11:34:46 +08:00
|
|
|
import asyncio
|
|
|
|
import json
|
2024-01-30 17:25:58 +08:00
|
|
|
from pathlib import Path
|
2024-02-01 16:52:32 +08:00
|
|
|
import logging
|
2024-01-30 17:25:58 +08:00
|
|
|
import yaml
|
2024-02-01 16:52:32 +08:00
|
|
|
import requests
|
2024-01-26 11:34:46 +08:00
|
|
|
from redis import asyncio as aioredis
|
|
|
|
|
|
|
|
|
|
|
|
async def read_event():
|
2024-02-01 16:52:32 +08:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
2024-01-30 17:25:58 +08:00
|
|
|
# 读取配置文件
|
|
|
|
home_dir = Path.home()
|
|
|
|
with open(home_dir / '.config' / 'hik-push' / 'config.yaml', 'r', encoding='utf-8') as f:
|
|
|
|
config = yaml.safe_load(f)
|
|
|
|
if "user_ids" not in config:
|
2024-02-01 16:52:32 +08:00
|
|
|
logging.error("请配置user_ids")
|
|
|
|
return
|
|
|
|
# if "devices" not in config:
|
|
|
|
# logging.error("请配置devices")
|
|
|
|
# return
|
|
|
|
if "event_type" not in config:
|
|
|
|
logging.error("请配置event_type")
|
2024-01-30 17:25:58 +08:00
|
|
|
return
|
2024-02-01 16:52:32 +08:00
|
|
|
if "push_url" not in config:
|
|
|
|
logging.error("请配置push_url")
|
2024-01-30 17:25:58 +08:00
|
|
|
return
|
2024-02-01 16:52:32 +08:00
|
|
|
|
|
|
|
logging.info(config)
|
2024-01-30 17:25:58 +08:00
|
|
|
user_map = config['user_ids']
|
|
|
|
device_map = config['devices']
|
2024-02-01 16:52:32 +08:00
|
|
|
event_map = config['event_type']
|
|
|
|
push_url = config['push_url']
|
2024-01-31 09:18:18 +08:00
|
|
|
redis_client = await aioredis.Redis(host="127.0.0.1", port=7019, password="SMHdFrlK")
|
2024-01-26 11:34:46 +08:00
|
|
|
while True:
|
|
|
|
try:
|
2024-02-01 16:52:32 +08:00
|
|
|
data = await redis_client.brpop("hik-sub-event")
|
|
|
|
sub_json = json.loads(data[1].decode('utf-8'))
|
2024-01-26 11:34:46 +08:00
|
|
|
events = sub_json["params"]["events"]
|
|
|
|
for event in events:
|
|
|
|
# 将eventType ID 替换为中文字符串
|
|
|
|
event_type = event['eventType']
|
2024-02-01 16:52:32 +08:00
|
|
|
logging.info(event_type)
|
2024-01-30 17:25:58 +08:00
|
|
|
event_type_str = event_map.get(event_type, "未知事件类型")
|
2024-01-26 11:34:46 +08:00
|
|
|
event['eventType'] = event_type_str
|
2024-02-01 16:52:32 +08:00
|
|
|
#
|
|
|
|
src_index = event["srcIndex"]
|
|
|
|
device_name = device_map.get(src_index, "未知设备")
|
|
|
|
user_ids = user_map.get(device_name, [])
|
|
|
|
event['deviceName'] = device_name
|
|
|
|
event['userIds'] = user_ids
|
2024-01-30 13:06:27 +08:00
|
|
|
if "data" in event:
|
|
|
|
# 如果存在 data 属性
|
|
|
|
# 替换分析结果字段
|
|
|
|
if "eventType" in event['data']:
|
|
|
|
detection_field_name = event['data']['eventType']
|
|
|
|
if detection_field_name in event['data']:
|
|
|
|
event['data']["_detectionResult"] = event['data'].pop(detection_field_name)
|
2024-02-01 16:52:32 +08:00
|
|
|
# 请求推送 api
|
|
|
|
logging.info(event)
|
|
|
|
try:
|
|
|
|
push_resp = requests.post(push_url, json=event).content.decode('utf-8')
|
|
|
|
logging.info(push_resp)
|
|
|
|
except Exception as e:
|
|
|
|
logging.error(f"网络错误推送失败: {e}")
|
2024-01-26 11:34:46 +08:00
|
|
|
|
|
|
|
except Exception as e:
|
2024-02-01 16:52:32 +08:00
|
|
|
logging.error("error: ", e)
|
2024-01-26 11:34:46 +08:00
|
|
|
continue
|
2024-01-30 17:25:58 +08:00
|
|
|
finally:
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
|
|
def run_app():
|
|
|
|
asyncio.run(read_event())
|
2024-01-26 11:34:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(read_event())
|