This commit is contained in:
2024-02-01 16:52:32 +08:00
parent 7019d231e2
commit 9d3d9a8b48
13 changed files with 1186 additions and 24 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

1
hik_push/rea.json Normal file
View File

@ -0,0 +1 @@
{'data': {'channelID': 1, 'channelName': 'B西~', 'dataProcInterval': '24', 'dataType': 'behavioralAnalysis', 'dateTime': '2024-02-01T11:23:54.000+08:00', 'eventDescription': 'fielddetection', 'eventType': 'fielddetection', 'ipAddress': '192.168.1.11', 'picUploadInterval': '20', 'portNo': 8000, 'recvTime': '2024-02-01T11:23:57.711+08:00', 'sendTime': '2024-02-01T11:23:57.735+08:00', 'userIds': ['fsdf34324', 'dsffee879'], '_detectionResult': [{'detectionTarget': 1, 'duration': 1, 'imageUrl': 'http://192.168.1.250:6120/pic?0d44=de54ida-=o6018p66ea906d-63599cbbb*562s=**113==tp*7417=6807757*3861=6l9*0814=fo916-12*le1-od2620224252', 'rate': 0, 'rectType': 'others', 'regionCoordinatesList': [{'positionX': 0, 'positionY': 0.5210000276565552}, {'positionX': 0.9929999709129332, 'positionY': 0.8059999942779541}, {'positionX': 0.9929999709129332, 'positionY': 0.02500000037252903}, {'positionX': 0.006000000052154064, 'positionY': 0}], 'sensitivityLevel': 4, 'targetAttrs': {'cameraIndexCode': 'c08f3b8317a743069ac18fc1f9d3dfc3', 'deviceIndexCode': '9c6ed401392e454984c8002bdd96c255', 'imageServerCode': '53a3b8cb-b3e6-45ff-ae95-65ab3a157551', 'picServerIndexCode': '53a3b8cb-b3e6-45ff-ae95-65ab3a157551'}, 'targetRect': {'height': 0.4860000014305115, 'positionX': 0.02400000020861626, 'positionY': 0.4329999983310699, 'width': 0.09200000017881392}, 'taskInfo': {'ruleCustomName': '1', 'ruleID': 0, 'ruleName': '1'}}]}, 'eventId': '00091540-B916-8546-8C42-8582BD2F66F3', 'eventType': '', 'happenTime': '2024-02-01T11:23:54.000+08:00', 'srcIndex': 'c08f3b8317a743069ac18fc1f9d3dfc3', 'srcName': 'B西~', 'srcParentIndex': '9c6ed401392e454984c8002bdd96c255', 'srcType': 'camera', 'status': 0, 'timeout': 0}

View File

@ -1,61 +1,71 @@
import asyncio
import json
from pathlib import Path
import logging
import yaml
from hik_push.mapping.event_map import event_map
import requests
from redis import asyncio as aioredis
async def read_event():
logging.basicConfig(level=logging.INFO)
# 读取配置文件
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:
print("请配置user_ids")
logging.error("请配置user_ids")
return
if "devices" not in config:
print("请配置devices")
# if "devices" not in config:
# logging.error("请配置devices")
# return
if "event_type" not in config:
logging.error("请配置event_type")
return
print(config)
if "push_url" not in config:
logging.error("请配置push_url")
return
logging.info(config)
user_map = config['user_ids']
device_map = config['devices']
event_map = config['event_type']
push_url = config['push_url']
redis_client = await aioredis.Redis(host="127.0.0.1", port=7019, password="SMHdFrlK")
while True:
data = await redis_client.brpop("hik-sub-event")
sub_json = json.loads(data[1].decode('utf-8'))
try:
data = await redis_client.brpop("hik-sub-event")
sub_json = json.loads(data[1].decode('utf-8'))
events = sub_json["params"]["events"]
for event in events:
# 将eventType ID 替换为中文字符串
event_type = event['eventType']
logging.info(event_type)
event_type_str = event_map.get(event_type, "未知事件类型")
event['eventType'] = event_type_str
#
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
if "data" in event:
# 如果存在 data 属性
# ip_address
ip_address = event["data"]["ipAddress"]
# 根据 ip_address 获取对应设备名称
device_name = device_map.get(ip_address, "未知设备")
event['data']['deviceName'] = device_name
# 根据 ip_address 获取对应 userid 数组
user_ids = user_map.get(ip_address, [])
event['data']['userIds'] = user_ids
# 替换分析结果字段
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)
# TODO: 请求推送 api
#
print(event)
# 请求推送 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}")
except Exception as e:
print("error: ", e)
logging.error("error: ", e)
continue
finally:
await asyncio.sleep(0.5)

42
hik_push/sub_body.json Normal file
View File

@ -0,0 +1,42 @@
{
"eventTypes": [
131588,
131585,
131586,
131587,
131590,
131593,
131592,
131591,
131594,
131595,
131664,
131665,
131596,
131603,
131605,
131597,
131666,
131609,
131598,
131599,
131600,
131601,
131602,
131604,
131606,
131607,
131611,
192518,
192517,
192516,
192515,
192514,
192513,
930335,
20210724,
20210724
],
"eventDest": "http://127.0.0.1:8000/eventRcv",
"subType": 0
}