This commit is contained in:
quantulr
2024-01-26 11:31:36 +08:00
parent 6b67995704
commit 077076f234
50 changed files with 140 additions and 141 deletions

View File

@ -1,45 +0,0 @@
mapping = {
131329: {"type": '视频丢失', "analyze_key": None},
131330: {"type": '视频遮挡', "analyze_key": None},
131331: {"type": '移动侦测', "analyze_key": None},
131612: {"type": '场景变更', "analyze_key": None},
131613: {"type": '虚焦', "analyze_key": None},
589825: {"type": '报警输入', "analyze_key": None},
196355: {"type": '可视域事件', "analyze_key": None},
851969: {"type": 'GPS采集', "analyze_key": None},
131588: {"type": '区域入侵', "analyze_key": "fielddetection"},
131585: {"type": '越界侦测', "analyze_key": "linedetection"},
131586: {"type": '进入区域', "analyze_key": "regionEntrance"},
131587: {"type": '离开区域', "analyze_key": "regionExiting"},
131590: {"type": '徘徊侦测', "analyze_key": "loitering"},
131593: {"type": '人员聚集', "analyze_key": "group"},
131592: {"type": '快速移动', "analyze_key": "rapidMove"},
131591: {"type": '停车侦测', "analyze_key": "parking"},
131594: {"type": '物品遗留', "analyze_key": "unattendedBaggage"},
131595: {"type": '物品拿取', "analyze_key": "attendedBaggage"},
131664: {"type": '人数异常', "analyze_key": "peoplenumChange"},
131665: {"type": '间距异常', "analyze_key": "spacingChange"},
131596: {"type": '剧烈运动', "analyze_key": "violentMotion"},
131603: {"type": '岗位值守', "analyze_key": "leavePosition"},
131605: {"type": '倒地', "analyze_key": "fallDown"},
131597: {"type": '攀高', "analyze_key": "reachHight"},
131666: {"type": '人员站立', "analyze_key": "standUp"},
131609: {"type": '防风场滞留', "analyze_key": "yardTarry"},
131598: {"type": '起身', "analyze_key": "getUp"},
131599: {"type": '人靠近ATM', "analyze_key": "humanEnter"},
131600: {"type": '操作超时', "analyze_key": "overTime"},
131601: {"type": '贴纸条', "analyze_key": "stickUp"},
131602: {"type": '安装读卡器', "analyze_key": "installScanner"},
131604: {"type": '尾随', "analyze_key": "trail"},
131606: {"type": '声强突变', "analyze_key": "audioAbnormal"},
131607: {"type": '折线攀高', "analyze_key": "advReachHeight"},
131611: {"type": '折线警戒面', "analyze_key": "advTraversePlane"},
192518: {"type": '温差报警', "analyze_key": "thermometryDiff"},
192517: {"type": '温度报警', "analyze_key": "thermometry"},
192516: {"type": '船只检测', "analyze_key": "shipDetection"},
192515: {"type": '火点检测', "analyze_key": "fireDetection"},
192514: {"type": '烟火检测', "analyze_key": "smokeAndFireDetection"},
192513: {"type": '烟雾检测', "analyze_key": "smokeDetection"},
930335: {"type": '高空抛物', "analyze_key": None},
889196545: {"type": '监控点离线', "analyze_key": None},
}

View File

@ -10,18 +10,11 @@ app = FastAPI()
async def add_stream(data: dict):
redis = await aioredis.from_url("redis://localhost", db=0)
await redis.lpush("event", json.dumps(data))
# await redis.xadd("hik-event", {"event": json.dumps(data)})
redis = await aioredis.Redis(host="127.0.0.1", port=6379)
await redis.lpush("hik-sub-event", json.dumps(data))
await redis.close()
@app.post("/upload")
async def upload_file(file: UploadFile):
print(file)
return {"message": "success"}
@app.get("/")
async def root():
return {"message": "Hello World"}
@ -30,7 +23,7 @@ async def root():
@app.post("/eventRcv")
async def event_rcv(data: dict, background_tasks: BackgroundTasks):
background_tasks.add_task(add_stream, data)
return {"data": data}
return {"msg": "success"}
def run_app():

View File

Binary file not shown.

View File

@ -0,0 +1,15 @@
device_map = {
"192.168.1.12": "4GB-供油站",
"192.168.1.13": "T2变电所",
"192.168.1.14": "二厂平台~西",
"192.168.1.15": "二厂平台~中o1",
"192.168.1.16": "T1变电所",
"192.168.1.17": "二厂平台~东",
"192.168.1.18": "二厂平台~中o2",
"192.168.1.19": "T3变电所",
"192.168.1.20": "T11变电所",
"192.168.1.21": "4GA-供油站",
"192.168.1.22": "T9-变压器",
"192.168.1.23": "T6-变压器",
"192.168.1.24": "4DB-供油站",
}

View File

@ -0,0 +1,45 @@
mapping = {
131329: '视频丢失',
131330: '视频遮挡',
131331: '移动侦测',
131612: '场景变更',
131613: '虚焦',
589825: '报警输入',
196355: '可视域事件',
851969: 'GPS采集',
131588: '区域入侵',
131585: '越界侦测',
131586: '进入区域',
131587: '离开区域',
131590: '徘徊侦测',
131593: '人员聚集',
131592: '快速移动',
131591: '停车侦测',
131594: '物品遗留',
131595: '物品拿取',
131664: '人数异常',
131665: '间距异常',
131596: '剧烈运动',
131603: '岗位值守',
131605: '倒地',
131597: '攀高',
131666: '人员站立',
131609: '防风场滞留',
131598: '起身',
131599: '人靠近ATM',
131600: '操作超时',
131601: '贴纸条',
131602: '安装读卡器',
131604: '尾随',
131606: '声强突变',
131607: '折线攀高',
131611: '折线警戒面',
192518: '温差报警',
192517: '温度报警',
192516: '船只检测',
192515: '火点检测',
192514: '烟火检测',
192513: '烟雾检测',
930335: '高空抛物',
889196545: '监控点离线',
}

View File

@ -0,0 +1,17 @@
user_map = {
"192.168.1.12": [
],
"192.168.1.13": [],
"192.168.1.14": [],
"192.168.1.15": [],
"192.168.1.16": [],
"192.168.1.17": [],
"192.168.1.18": [],
"192.168.1.19": [],
"192.168.1.20": [],
"192.168.1.21": [],
"192.168.1.22": [],
"192.168.1.23": [],
"192.168.1.24": [],
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,7 +5,9 @@ from io import BytesIO
import requests
from custom_exception import FeishuAuthException
from event_map_mapping import mapping
from mapping.event_map_mapping import mapping
from mapping.device_map import device_map
from mapping.user_map import user_map
from redis import asyncio as aioredis
@ -27,7 +29,7 @@ def get_access_token(api_id, secret):
# 获取事件的图片
def get_image_url(event):
analysis_key = mapping.get(event['eventType'])['analyze_key']
analysis_key = mapping.get(event['eventType'])['detection_key']
if analysis_key is None:
return None
images = []
@ -65,7 +67,6 @@ def upload_image(url):
# 推送到飞书
def push_to_feishu(data):
push_url = "https://open.feishu.cn/open-apis/message/v4/batch_send/"
# push_url = "http://192.168.20.115:8000/ipaasuat/engine_company/anycross/trigger/callback/MmRhZTE4OTRiYjVkZDQ5YWNmOGRmZDI0NjQ1MTBlODUw/1.0.0"
data = json.dumps(data)
r = requests.post(push_url, data=data, headers=headers).content.decode('utf-8')
data = json.loads(r)
@ -76,92 +77,34 @@ def push_to_feishu(data):
async def read_event():
access_token = get_access_token(app_id, app_secret)
headers['Authorization'] = f"Bearer {access_token}"
redis_client = await aioredis.from_url("redis://localhost")
redis_client = await aioredis.Redis(host="127.0.0.1", port=6379)
while True:
data = await redis_client.brpop("event")
event_json = json.loads(data[1].decode('utf-8'))
data = await redis_client.brpop("hik-sub-event")
sub_json = json.loads(data[1].decode('utf-8'))
try:
events = event_json["params"]["events"]
events = sub_json["params"]["events"]
for event in events:
event_type = mapping.get(event['eventType'], {"type": "未知事件类型"})
print(event_type)
# TODO: 上传图片到飞书链接
images = get_image_url(event)
feishu_image_key = None
if images is not None:
feishu_image_key = upload_image(images[0])
print(feishu_image_key)
# TODO: 根据ip_address获取对应userid
#
# 将eventType ID 替换为中文字符串
event_type = event['eventType']
event_type_str = mapping.get(event_type, "未知事件类型")
event['eventType'] = event_type_str
# ip address
ip_address = event["data"]["ipAddress"]
# channel_name = get_value_by_nested_key(event, ("data", "channelName"))
# 根据 ip_address 获取对应设备名称
device_name = device_map.get(ip_address, "未知设备")
event['deviceName'] = device_name
# print(event['data'][event['data']['eventType']])
# TODO: 根据 ip_address 获取对应 userid
user_ids = user_map.get(ip_address, [])
event['userIds'] = user_ids
# TODO: 请求推送 api
#
print(event, device_name)
# TODO: 向飞书推送消息
push_data = {
"user_ids": [
"8dg79c18"
],
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": True
},
"elements": [
{
"tag": "markdown",
"content": "这里是卡片文本支持使用markdown标签设置文本格式。例如\n*斜体* 、**粗体**、~~删除线~~、[文字链接]("
"https://www.feishu.cn)、<at id=all></at>、<font color='red'> 彩色文本 </font>"
},
{
"alt": {
"content": "",
"tag": "plain_text"
},
"img_key": feishu_image_key,
"tag": "img",
"mode": "fit_horizontal",
"compact_width": False
},
{
"tag": "action",
"actions": [
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": "这是跳转按钮"
},
"type": "primary",
"url": "https://feishu.cn"
}
]
}
],
"header": {
"template": "blue",
"title": {
"content": event_type["type"],
"tag": "plain_text"
}
}
}
}
push_to_feishu(push_data)
print(event_type["type"], ip_address)
except FeishuAuthException:
print("飞书登录失败")
# TODO: 重新获取 access token
access_token = get_access_token(app_id, app_secret)
headers['Authorization'] = f"Bearer {access_token}"
await redis_client.lpush("event", json.dumps(event_json))
continue
except Exception as e:
print("error: ", e)
continue
await asyncio.sleep(0.5)
if __name__ == "__main__":

36
hik_push/requirements.txt Normal file
View File

@ -0,0 +1,36 @@
annotated-types==0.6.0
anyio==4.2.0
certifi==2023.11.17
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
dnspython==2.5.0
email-validator==2.1.0.post1
fastapi==0.109.0
h11==0.14.0
-e git+http://101.43.70.124:3000/cxc/hik-push.git@6b679957040979034a03da39c78011a26285dc7e#egg=hik_push
httpcore==1.0.2
httptools==0.6.1
httpx==0.26.0
idna==3.6
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.4
orjson==3.9.12
pydantic==2.5.3
pydantic-extra-types==2.4.1
pydantic-settings==2.1.0
pydantic_core==2.14.6
python-dotenv==1.0.0
python-multipart==0.0.6
PyYAML==6.0.1
redis==5.0.1
requests==2.31.0
sniffio==1.3.0
starlette==0.35.1
typing_extensions==4.9.0
ujson==5.9.0
urllib3==2.1.0
uvicorn==0.26.0
watchfiles==0.21.0
websockets==12.0

View File

@ -1,5 +0,0 @@
user_map = {
"10.19.134.11": [
"8dg79c18"
]
}