This commit is contained in:
quantulr
2024-03-15 20:52:13 +08:00
parent e570a4153f
commit c88253e12d
2 changed files with 129 additions and 198 deletions

View File

@ -28,15 +28,6 @@ user_ids:
- "35393" - "35393"
- "26292" - "26292"
- "24857" - "24857"
4GA-供油站:
high:
- "2152"
- "2050"
all:
- "2043"
- "29246"
- "2090"
- "24857"
4DB-供油站: 4DB-供油站:
high: high:
- "2152" - "2152"
@ -46,17 +37,32 @@ user_ids:
- "35393" - "35393"
- "26292" - "26292"
- "24857" - "24857"
4DB-供油站(测温):
high:
- "2152"
- "2050"
all:
- "5791"
- "35393"
- "26292"
- "24857"
4GA-供油站:
high:
- "2152"
- "2050"
all:
- "2043"
- "29246"
- "2090"
- "24857"
4GA-供油站(测温): 4GA-供油站(测温):
high: high:
- "2152" - "2152"
- "2050" - "2050"
all: all:
- "24857" - "2043"
4GB-供油站(测温): - "29246"
high: - "2090"
- "2152"
- "2050"
all:
- "24857" - "24857"
A区_北油库_气体探测器: A区_北油库_气体探测器:
high: high:
@ -78,15 +84,15 @@ user_ids:
- "24857" - "24857"
# b区 # b区
4GB-供油站: 4GB-供油站:
high: [ ] high: []
all: all:
- "1823" - "1823"
- "2164" - "2164"
- "2166" - "2166"
- "30024" - "30024"
- "2078" - "2078"
4DB-供油站(测温): 4GB-供油站(测温:
high: [ ] high: []
all: all:
- "1823" - "1823"
- "2164" - "2164"
@ -94,7 +100,7 @@ user_ids:
- "30024" - "30024"
- "2078" - "2078"
B区油库_气体探测器: B区油库_气体探测器:
high: [ ] high: []
all: all:
- "1823" - "1823"
- "2164" - "2164"
@ -362,135 +368,6 @@ user_ids:
- "1926" - "1926"
- "2097" - "2097"
# T2变电所:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# A区北油库:
# - "5791"
# - "35393"
# - "26292"
# B区西~油库:
# - "1823"
# - "2164"
# - "2166"
# - "30024"
# - "2078"
# 二厂平台~东:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T1变电所:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# A区南~油库:
# - "2043"
# - "29246"
# - "2090"
# T3变电所:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T11变电所:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# 二厂平台~西:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# 二厂平台~中01:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# 二厂平台~中02:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T9变压器:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T6变压器:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# B区西~油库(测温):
# - "1823"
# - "2164"
# - "2166"
# - "30024"
# - "2078"
# T2变电所 (测温):
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# 二厂平台~西(测温):
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# 二厂平台~中01测温:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T1变电所(测温):
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# 二厂平台~东(测温):
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# 二厂平台~中02测温:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T3变电所测温:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T11变电所(测温):
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# A区南~油库(测温):
# - "2043"
# - "29246"
# - "2090"
# T9变压器测温:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# T6变压器测温:
# - "1815"
# - "1899"
# - "1926"
# - "2097"
# A区北油库(测温):
# - "5791"
# - "35393"
# - "26292"
# srcIndex 与设备名称 map # srcIndex 与设备名称 map
devices: devices:
f5ac885b1c39411695d575b99771855d: 4DB供油站8号台架01 f5ac885b1c39411695d575b99771855d: 4DB供油站8号台架01

View File

@ -11,7 +11,9 @@ async def read_event():
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
# 读取配置文件 # 读取配置文件
home_dir = Path.home() home_dir = Path.home()
with open(home_dir / '.config' / 'hik-push' / 'config.yaml', 'r', encoding='utf-8') as f: with open(
home_dir / ".config" / "hik-push" / "config.yaml", "r", encoding="utf-8"
) as f:
config = yaml.safe_load(f) config = yaml.safe_load(f)
if "user_ids" not in config: if "user_ids" not in config:
logging.error("请配置user_ids") logging.error("请配置user_ids")
@ -30,92 +32,133 @@ async def read_event():
return return
logging.info(config) logging.info(config)
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"]
push_url = config['push_url'] push_url = config["push_url"]
event_level = config['event_level'] event_level = config["event_level"]
redis_client = await aioredis.Redis(host="127.0.0.1", port=7019, password="SMHdFrlK") redis_client = await aioredis.Redis(
host="127.0.0.1", port=7019, password="SMHdFrlK"
)
while True: while True:
try: try:
data = await redis_client.brpop("hik-sub-event") data = await redis_client.brpop("hik-sub-event")
sub_json = json.loads(data[1].decode('utf-8')) sub_json = json.loads(data[1].decode("utf-8"))
events = sub_json["params"]["events"] events = sub_json["params"]["events"]
for event in events: for event in events:
# 如果是ai事件 # 如果是ai事件
if event["srcType"] == "eventRule": if event["srcType"] == "eventRule":
event_details = event["eventDetails"] event_details = event["eventDetails"]
for event_detail in event_details: for event_detail in event_details:
# 设置默认事件等级
handle_event_level(
event_detail, event_level, event_detail["eventType"]
)
# 事件类型 id 替换为 str # 事件类型 id 替换为 str
event_type_str = event_map.get(event_detail['eventType'], "未知事件类型") event_type_str = event_map.get(
event_detail['eventType'] = event_type_str event_detail["eventType"], "未知事件类型"
)
event_detail["eventType"] = event_type_str
# 添加设备名称 # 添加设备名称
src_index = event_detail["srcIndex"] src_index = event_detail["srcIndex"]
device_name = device_map.get(src_index, "未知设备") device_name = device_map.get(src_index, "未知设备")
event_detail['deviceName'] = device_name event_detail["deviceName"] = device_name
# 默认事件等级为2
if "eventLvl" not in event_detail:
event_detail['eventLvl'] = 2
# 根据设备名称获取 user_ids # 根据设备名称获取 user_ids
user_all = user_map.get("all", []) # 接受全部通知的用户 user_ids = []
user_ids = user_map.get(device_name, []) # 高级
user_ids = user_ids + user_all if event_detail["eventLvl"] == 3:
event_detail['userIds'] = user_ids user_all = user_map.get("all", {}).get("high", [])
# 接受全部通知的用户
user_ids = user_map.get(device_name, {}).get("high", [])
user_ids = user_ids + user_all
# 中级
else:
user_all = user_map.get("all", {}).get("all", [])
# 接受全部通知的用户
user_ids = user_map.get(device_name, {}).get("all", [])
user_ids = user_ids + user_all
event_detail["userIds"] = user_ids
# 添加其他字段 # 添加其他字段
event_detail["eventLvl"] = event['eventLvl'] event_detail["eventLvl"] = event["eventLvl"]
event_detail["happenTime"] = event['happenTime'] event_detail["happenTime"] = event["happenTime"]
# 如果存在 data 属性 # 如果存在 data 属性
if "data" in event_detail: if "data" in event_detail:
# 替换分析结果字段 # 替换分析结果字段
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"]
if detection_field_name in event_detail['data']: if detection_field_name in event_detail["data"]:
event_detail['data']["_detectionResult"] = event_detail['data'].pop( event_detail["data"]["_detectionResult"] = (
detection_field_name) event_detail["data"].pop(detection_field_name)
if isinstance(event_detail['data']["_detectionResult"], list) and len( )
event_detail['data']["_detectionResult"]): if isinstance(
event_detail['data']["_detectionResult"] = \ event_detail["data"]["_detectionResult"], list
event_detail['data']["_detectionResult"][0] ) and len(event_detail["data"]["_detectionResult"]):
replace_image_host(event_detail['data']["_detectionResult"]) event_detail["data"]["_detectionResult"] = (
event_detail["data"]["_detectionResult"][0]
)
replace_image_host(
event_detail["data"]["_detectionResult"]
)
logging.info(event_detail) logging.info(event_detail)
try: try:
push_resp = requests.post(push_url, json=event_detail).content.decode('utf-8') push_resp = requests.post(
push_url, json=event_detail
).content.decode("utf-8")
logging.info(push_resp) logging.info(push_resp)
except Exception as e: except Exception as e:
logging.error(f"网络错误推送失败: {e}") logging.error(f"网络错误推送失败: {e}")
else: else:
# 设置默认事件等级
handle_event_level(event, event_level, event["eventType"])
# 将 eventType ID 替换为中文字符串 # 将 eventType ID 替换为中文字符串
event_type = event['eventType'] event_type = event["eventType"]
logging.info(event_type) logging.info(event_type)
event_type_str = event_map.get(event_type, "未知事件类型") event_type_str = event_map.get(event_type, "未知事件类型")
event['eventType'] = event_type_str event["eventType"] = event_type_str
# 添加设备名称 # 添加设备名称
src_index = event["srcIndex"] src_index = event["srcIndex"]
device_name = device_map.get(src_index, "未知设备") device_name = device_map.get(src_index, "未知设备")
# 默认事件等级为2 # 默认事件等级为2
if "eventLvl" not in event: if "eventLvl" not in event:
event['eventLvl'] = 2 event["eventLvl"] = 2
# 根据设备名称获取 user_ids # 根据设备名称获取 user_ids
user_all = user_map.get("all", []) # 接受全部通知的用户 user_ids = []
user_ids = user_map.get(device_name, []) # 高级
user_ids = user_ids + user_all if event["eventLvl"] == 3:
event['deviceName'] = device_name user_all = user_map.get("all", {}).get("high", [])
event['userIds'] = user_ids # 接受全部通知的用户
user_ids = user_map.get(device_name, {}).get("high", [])
user_ids = user_ids + user_all
# 中级
else:
user_all = user_map.get("all", {}).get("all", [])
# 接受全部通知的用户
user_ids = user_map.get(device_name, {}).get("all", [])
user_ids = user_ids + user_all
event["deviceName"] = device_name
event["userIds"] = user_ids
if "data" in event: if "data" in event:
# 如果存在 data 属性 # 如果存在 data 属性
# 替换分析结果字段 # 替换分析结果字段
if "eventType" in event['data']: if "eventType" in event["data"]:
detection_field_name = event['data']['eventType'] detection_field_name = event["data"]["eventType"]
if detection_field_name in event['data']: if detection_field_name in event["data"]:
event['data']["_detectionResult"] = event['data'].pop(detection_field_name) event["data"]["_detectionResult"] = event["data"].pop(
if isinstance(event['data']["_detectionResult"], list) and len( detection_field_name
event['data']["_detectionResult"]): )
event['data']["_detectionResult"] = event['data']["_detectionResult"][0] if isinstance(
replace_image_host(event['data']["_detectionResult"]) event["data"]["_detectionResult"], list
) and len(event["data"]["_detectionResult"]):
event["data"]["_detectionResult"] = event["data"][
"_detectionResult"
][0]
replace_image_host(event["data"]["_detectionResult"])
# 请求推送 api # 请求推送 api
logging.info(event) logging.info(event)
try: try:
push_resp = requests.post(push_url, json=event).content.decode('utf-8') push_resp = requests.post(push_url, json=event).content.decode(
"utf-8"
)
logging.info(push_resp) logging.info(push_resp)
except Exception as e: except Exception as e:
logging.error(f"网络错误推送失败: {e}") logging.error(f"网络错误推送失败: {e}")
@ -129,9 +172,20 @@ async def read_event():
def replace_image_host(detection_data): def replace_image_host(detection_data):
if "imageUrl" in detection_data: if "imageUrl" in detection_data:
detection_data["imageUrl"] = detection_data["imageUrl"].replace("192.168.1.250", "192.168.11.180") detection_data["imageUrl"] = detection_data["imageUrl"].replace(
"192.168.1.250", "192.168.11.180"
)
if "visiblePicUrl" in detection_data: if "visiblePicUrl" in detection_data:
detection_data["visiblePicUrl"] = detection_data["visiblePicUrl"].replace("192.168.1.250", "192.168.11.180") detection_data["visiblePicUrl"] = detection_data["visiblePicUrl"].replace(
"192.168.1.250", "192.168.11.180"
)
def handle_event_level(event_detail, event_levels, event_type):
if event_type in event_levels["high"]:
event_detail["eventLvl"] = 3
elif "eventLvl" not in event_detail:
event_detail["eventLvl"] = 2
def run_app(): def run_app():