From 2ea35ad5d34ea2d3d69b0b42f9e8a6b02dabb1fd Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Thu, 19 Mar 2026 09:58:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9A=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E5=8F=91=E9=80=81=E5=88=B0=20vsp-service=20?= =?UTF-8?q?=E5=92=8C=20WVP=20=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 WVP_API_URL 环境变量,心跳同时上报到两个地址, 解决前端从 WVP 读设备状态显示离线的问题。 --- config/settings.py | 2 ++ main.py | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/config/settings.py b/config/settings.py index 7fc58af..d31ae3e 100644 --- a/config/settings.py +++ b/config/settings.py @@ -93,6 +93,7 @@ class COSConfig: class AlarmUploadConfig: """告警上报配置""" cloud_api_url: str = "http://localhost:8000" + wvp_api_url: str = "" # WVP 平台地址(心跳同步用) edge_token: str = "" retry_max: int = 3 retry_interval: int = 5 @@ -263,6 +264,7 @@ class Settings: self.alarm_upload = AlarmUploadConfig( cloud_api_url=os.getenv("CLOUD_API_URL", "http://localhost:8000"), + wvp_api_url=os.getenv("WVP_API_URL", ""), edge_token=os.getenv("EDGE_TOKEN", ""), retry_max=int(os.getenv("ALARM_RETRY_MAX", "3")), retry_interval=int(os.getenv("ALARM_RETRY_INTERVAL", "5")), diff --git a/main.py b/main.py index 2763391..975813d 100644 --- a/main.py +++ b/main.py @@ -284,14 +284,17 @@ class EdgeInferenceService: self._debug_http_thread.start() def _start_heartbeat(self): - """启动心跳守护线程,每 30 秒向 WVP 上报设备状态""" + """启动心跳守护线程,每 30 秒向云端上报设备状态""" def worker(): import requests base_url = self._settings.alarm_upload.cloud_api_url.rstrip("/") - url = f"{base_url}/api/ai/device/heartbeat" + wvp_url = self._settings.alarm_upload.wvp_api_url.rstrip("/") if self._settings.alarm_upload.wvp_api_url else "" + urls = [f"{base_url}/api/ai/device/heartbeat"] + if wvp_url: + urls.append(f"{wvp_url}/api/ai/device/heartbeat") device_id = self._settings.mqtt.device_id - self._logger.info(f"[心跳] 守护线程已启动, 目标: {url}, device_id={device_id}") + self._logger.info(f"[心跳] 守护线程已启动, 目标: {urls}, device_id={device_id}") while not self._stop_event.is_set(): try: @@ -314,11 +317,15 @@ class EdgeInferenceService: }, } - resp = requests.post(url, json=payload, timeout=10) - if resp.status_code == 200: - self._logger.debug(f"[心跳] 上报成功: uptime={int(uptime)}s, streams={stream_count}") - else: - self._logger.warning(f"[心跳] 上报失败: HTTP {resp.status_code}") + for url in urls: + try: + resp = requests.post(url, json=payload, timeout=10) + if resp.status_code == 200: + self._logger.debug(f"[心跳] {url} 上报成功") + else: + self._logger.warning(f"[心跳] {url} 上报失败: HTTP {resp.status_code}") + except Exception as e: + self._logger.warning(f"[心跳] {url} 上报异常: {e}") except Exception as e: self._logger.warning(f"[心跳] 上报异常: {e}")