功能:心跳同时发送到 vsp-service 和 WVP 平台

新增 WVP_API_URL 环境变量,心跳同时上报到两个地址,
解决前端从 WVP 读设备状态显示离线的问题。
This commit is contained in:
2026-03-19 09:58:24 +08:00
parent 9c39913a55
commit 2ea35ad5d3
2 changed files with 17 additions and 8 deletions

View File

@@ -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")),

23
main.py
View File

@@ -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}")