feat(aiot): 告警三表结构升级 + 腾讯云COS对象存储集成
1. 新增三表结构: alarm_event(主表), alarm_event_ext(算法扩展), alarm_llm_analysis(大模型分析) 2. 新增 AlarmEventService 服务,支持 MQTT/HTTP 双路创建告警 3. MQTT handler 双写新旧表,平滑过渡 4. 重写 yudao_aiot_alarm 路由,对接新告警服务 5. 集成腾讯云 COS 对象存储:上传、预签名URL、STS临时凭证 6. 新增 storage 路由:upload/presign/upload-url/sts 四个接口 7. COS 未启用时自动降级本地 uploads/ 目录存储 8. 新增数据迁移脚本 migrate_to_alarm_event.py 9. 删除根目录 main.py(非项目入口) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
21
app/main.py
21
app/main.py
@@ -21,12 +21,13 @@ from app.schemas import (
|
||||
DeviceStatisticsResponse,
|
||||
)
|
||||
from app.services.alert_service import alert_service, get_alert_service
|
||||
from app.services.alarm_event_service import alarm_event_service, get_alarm_event_service
|
||||
from app.services.ai_analyzer import trigger_async_analysis
|
||||
from app.services.mqtt_service import get_mqtt_service
|
||||
from app.services.notification_service import get_notification_service
|
||||
from app.services.device_service import get_device_service
|
||||
from app.utils.logger import logger
|
||||
from app.routers import yudao_alert_router, yudao_auth_router, yudao_aiot_alarm_router, yudao_aiot_edge_router
|
||||
from app.routers import yudao_alert_router, yudao_auth_router, yudao_aiot_alarm_router, yudao_aiot_edge_router, yudao_aiot_storage_router
|
||||
from app.yudao_compat import yudao_exception_handler
|
||||
import json
|
||||
|
||||
@@ -38,13 +39,23 @@ device_service = get_device_service()
|
||||
|
||||
|
||||
def handle_mqtt_alert(payload: dict):
|
||||
"""处理 MQTT 告警消息"""
|
||||
"""处理 MQTT 告警消息(双写:旧表 + 新表)"""
|
||||
try:
|
||||
# 1. 写旧表(保持兼容)
|
||||
alert = alert_service.create_alert_from_mqtt(payload)
|
||||
if alert:
|
||||
# 通过 WebSocket 推送新告警
|
||||
logger.info(f"MQTT 告警写入旧表: {alert.alert_no}")
|
||||
|
||||
# 2. 写新表
|
||||
alarm = alarm_event_service.create_from_mqtt(payload)
|
||||
|
||||
# 3. WebSocket 通知(优先使用新表数据)
|
||||
if alarm:
|
||||
notification_service.notify_sync("new_alert", alarm.to_dict())
|
||||
logger.info(f"MQTT 告警已处理并推送: {alarm.alarm_id}")
|
||||
elif alert:
|
||||
notification_service.notify_sync("new_alert", alert.to_dict())
|
||||
logger.info(f"MQTT 告警已处理并推送: {alert.alert_no}")
|
||||
logger.info(f"MQTT 告警已处理并推送(旧表): {alert.alert_no}")
|
||||
except Exception as e:
|
||||
logger.error(f"处理 MQTT 告警失败: {e}")
|
||||
|
||||
@@ -100,6 +111,7 @@ app.add_middleware(
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
|
||||
)
|
||||
|
||||
# ==================== 芋道兼容路由 ====================
|
||||
@@ -111,6 +123,7 @@ app.include_router(yudao_alert_router)
|
||||
# aiot 命名空间下的新路由,与旧路由并存
|
||||
app.include_router(yudao_aiot_alarm_router)
|
||||
app.include_router(yudao_aiot_edge_router)
|
||||
app.include_router(yudao_aiot_storage_router)
|
||||
|
||||
# 注册芋道格式异常处理器
|
||||
app.add_exception_handler(HTTPException, yudao_exception_handler)
|
||||
|
||||
Reference in New Issue
Block a user