From 766ee6a69aef56c3e1430aa616f5fe70ae8bf409 Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Mon, 9 Mar 2026 16:27:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=9F=E4=B8=80=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=97=B6=E9=97=B4=E4=B8=BA=E5=8C=97=E4=BA=AC?= =?UTF-8?q?=E6=97=B6=E9=97=B4=20+=20=E5=A4=84=E7=90=86/=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E6=97=B6=E8=AE=A1=E7=AE=97=E5=91=8A=E8=AD=A6=E6=97=B6=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - notify_dispatch: _mark_false_alarm 使用 beijing_now() + 计算 duration_ms - alarm_event_service: handle_alarm 处理时自动计算 duration_ms 和 last_frame_time - notification_service: datetime.utcnow() 替换为 beijing_now() - device_service: datetime.now(timezone.utc) 替换为 beijing_now() Co-Authored-By: Claude Opus 4.6 --- app/services/alarm_event_service.py | 18 ++++++++++++++++-- app/services/device_service.py | 5 +++-- app/services/notification_service.py | 11 ++++++----- app/services/notify_dispatch.py | 13 ++++++++++++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/app/services/alarm_event_service.py b/app/services/alarm_event_service.py index f5674dc..0674ac1 100644 --- a/app/services/alarm_event_service.py +++ b/app/services/alarm_event_service.py @@ -415,8 +415,22 @@ class AlarmEventService: alarm.handle_remark = remark if handler: alarm.handler = handler - alarm.handled_at = beijing_now() - alarm.updated_at = beijing_now() + + now = beijing_now() + alarm.handled_at = now + alarm.updated_at = now + + # 如果没有 duration_ms 且有 event_time,计算告警时长 + if alarm.duration_ms is None and alarm.event_time: + try: + delta = now - alarm.event_time + alarm.duration_ms = int(delta.total_seconds() * 1000) + except Exception: + pass + + # 如果没有 last_frame_time,设置为当前处理时间 + if alarm.last_frame_time is None: + alarm.last_frame_time = now db.commit() db.refresh(alarm) diff --git a/app/services/device_service.py b/app/services/device_service.py index f70e7af..4a7f7b5 100644 --- a/app/services/device_service.py +++ b/app/services/device_service.py @@ -7,6 +7,7 @@ from typing import Dict, Any, List, Optional from app.models import EdgeDevice, DeviceStatus, get_session from app.utils.logger import logger +from app.utils.timezone import beijing_now class DeviceService: @@ -32,7 +33,7 @@ class DeviceService: """检查离线设备""" db = get_session() try: - threshold = datetime.now(timezone.utc) - timedelta(seconds=self.OFFLINE_TIMEOUT) + threshold = beijing_now() - timedelta(seconds=self.OFFLINE_TIMEOUT) # 查找需要标记为离线的设备 devices = db.query(EdgeDevice).filter( @@ -43,7 +44,7 @@ class DeviceService: offline_devices = [] for device in devices: device.status = DeviceStatus.OFFLINE - device.updated_at = datetime.now(timezone.utc) + device.updated_at = beijing_now() offline_devices.append(device) logger.info(f"设备离线: {device.device_id}") diff --git a/app/services/notification_service.py b/app/services/notification_service.py index c5e2ce3..f527fee 100644 --- a/app/services/notification_service.py +++ b/app/services/notification_service.py @@ -9,6 +9,7 @@ from typing import Dict, Any, List, Set from fastapi import WebSocket from app.utils.logger import logger +from app.utils.timezone import beijing_now class ConnectionManager: @@ -80,7 +81,7 @@ class NotificationService: message = { "event": "new_alert", "data": alert_data, - "timestamp": datetime.utcnow().isoformat(), + "timestamp": beijing_now().isoformat(), } await self._manager.broadcast(message) logger.debug(f"已广播新告警通知: {alert_data.get('alert_no', 'N/A')}") @@ -90,7 +91,7 @@ class NotificationService: message = { "event": "alert_updated", "data": alert_data, - "timestamp": datetime.utcnow().isoformat(), + "timestamp": beijing_now().isoformat(), } await self._manager.broadcast(message) @@ -99,7 +100,7 @@ class NotificationService: message = { "event": "device_status", "data": device_data, - "timestamp": datetime.utcnow().isoformat(), + "timestamp": beijing_now().isoformat(), } await self._manager.broadcast(message) @@ -108,7 +109,7 @@ class NotificationService: message = { "event": f"work_order_{event_type}", "data": order_data, - "timestamp": datetime.utcnow().isoformat(), + "timestamp": beijing_now().isoformat(), } await self._manager.broadcast(message) @@ -121,7 +122,7 @@ class NotificationService: message = { "event": event, "data": data, - "timestamp": datetime.utcnow().isoformat(), + "timestamp": beijing_now().isoformat(), } async def _broadcast(): diff --git a/app/services/notify_dispatch.py b/app/services/notify_dispatch.py index 53d0c8a..5c7c36f 100644 --- a/app/services/notify_dispatch.py +++ b/app/services/notify_dispatch.py @@ -22,6 +22,7 @@ from app.config import settings from app.services.vlm_service import get_vlm_service from app.services.wechat_service import get_wechat_service from app.utils.logger import logger +from app.utils.timezone import beijing_now async def process_alarm_notification(alarm_data: Dict): @@ -151,10 +152,20 @@ def _mark_false_alarm(alarm_id: str): try: alarm = db.query(AlarmEvent).filter(AlarmEvent.alarm_id == alarm_id).first() if alarm: + now = beijing_now() alarm.alarm_status = "FALSE" alarm.handle_status = "DONE" alarm.handle_remark = "VLM复核判定误报" - alarm.handled_at = datetime.now() + alarm.handled_at = now + # 计算告警时长(VLM复核时间 - 事件时间) + if alarm.duration_ms is None and alarm.event_time: + try: + delta = now - alarm.event_time + alarm.duration_ms = int(delta.total_seconds() * 1000) + except Exception: + pass + if alarm.last_frame_time is None: + alarm.last_frame_time = now db.commit() logger.info(f"告警已标记误报: {alarm_id}") except Exception as e: