修复:send-card 群聊通知三个问题
1. 告警告警重复:用 alarm_type_code 映射而非 IoT 的 title 2. 摄像头未知:从告警表查 device_id 获取摄像头名称 3. 截图403:从告警表取 object key 生成预签名 URL(IoT 传的永久URL无签名)
This commit is contained in:
@@ -93,17 +93,51 @@ async def send_card(request: Request):
|
||||
# 群聊通知
|
||||
group_chat_id = settings.wechat.group_chat_id
|
||||
if group_chat_id:
|
||||
# 截图预签名URL
|
||||
# 截图:从告警表获取 object key 生成预签名 URL
|
||||
from app.services.notify_dispatch import _get_presigned_url
|
||||
presigned_url = _get_presigned_url(req.snapshotUrl or "")
|
||||
snapshot_url = req.snapshotUrl or ""
|
||||
# 如果是完整 COS URL,从告警表取 object key 重新生成预签名
|
||||
if not snapshot_url or "403" in snapshot_url:
|
||||
snapshot_url = ""
|
||||
presigned_url = ""
|
||||
if snapshot_url:
|
||||
presigned_url = _get_presigned_url(snapshot_url)
|
||||
|
||||
# 从告警表补全摄像头名称和截图(IoT 可能没传或传了无效数据)
|
||||
camera_name = req.cameraName or ""
|
||||
alarm_type_code = ""
|
||||
alarm_snapshot_key = ""
|
||||
from app.models import get_session, AlarmEvent
|
||||
from app.services.camera_name_service import get_camera_name_service
|
||||
db = get_session()
|
||||
try:
|
||||
alarm = db.query(AlarmEvent).filter(AlarmEvent.alarm_id == req.alarmId).first()
|
||||
if alarm:
|
||||
alarm_type_code = alarm.alarm_type or ""
|
||||
alarm_snapshot_key = alarm.snapshot_url or ""
|
||||
if not camera_name or camera_name == "未知":
|
||||
if alarm.device_id:
|
||||
camera_service = get_camera_name_service()
|
||||
camera_info = await camera_service.get_camera_info(alarm.device_id)
|
||||
camera_name = camera_service.format_display_name(alarm.device_id, camera_info)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# 截图预签名:优先用告警表的 object key(能正确签名)
|
||||
if not presigned_url or not snapshot_url:
|
||||
if alarm_snapshot_key:
|
||||
presigned_url = _get_presigned_url(alarm_snapshot_key)
|
||||
|
||||
# alarm_type: 用 alarm_type_code 避免"告警告警"
|
||||
actual_alarm_type = alarm_type_code or req.title
|
||||
|
||||
await wechat.send_group_alarm_combo(
|
||||
chat_id=group_chat_id,
|
||||
alarm_id=req.alarmId,
|
||||
alarm_type=req.title,
|
||||
alarm_type=actual_alarm_type,
|
||||
area_name=req.areaName or "",
|
||||
camera_name=req.cameraName or "",
|
||||
description=f"工单编号:{req.orderId}",
|
||||
camera_name=camera_name,
|
||||
description=req.title,
|
||||
event_time=req.eventTime or "",
|
||||
alarm_level=req.level or 2,
|
||||
snapshot_url=presigned_url,
|
||||
@@ -111,12 +145,22 @@ async def send_card(request: Request):
|
||||
)
|
||||
|
||||
# 私发卡片
|
||||
# 从告警表获取 alarm_type_code(避免重复"告警")
|
||||
if not alarm_type_code:
|
||||
from app.models import get_session, AlarmEvent
|
||||
db = get_session()
|
||||
try:
|
||||
alarm = db.query(AlarmEvent).filter(AlarmEvent.alarm_id == req.alarmId).first()
|
||||
alarm_type_code = alarm.alarm_type if alarm else ""
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
sent = await wechat.send_alarm_card(
|
||||
user_ids=req.userIds,
|
||||
alarm_id=req.alarmId,
|
||||
alarm_type=req.title,
|
||||
alarm_type=alarm_type_code or req.title,
|
||||
area_name=req.areaName or "",
|
||||
camera_name=req.cameraName or "",
|
||||
camera_name=camera_name if 'camera_name' in dir() else (req.cameraName or ""),
|
||||
description=f"工单编号:{req.orderId}",
|
||||
event_time=req.eventTime or "",
|
||||
alarm_level=req.level or 2,
|
||||
|
||||
Reference in New Issue
Block a user