From d1dd2b4c70c31956befebe23d78f972a965b9e79 Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Tue, 2 Jun 2026 13:03:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=B0=E5=9B=BE=E5=AE=9E=E6=97=B6=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E6=97=A0=E4=BA=BA=E6=9C=BA=E5=92=8C=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E7=BB=8F=E7=BA=AC=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/devices/DeviceStatusPage.tsx | 49 ++++++++++++++++----- src/components/devices/deviceController.js | 6 ++- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/components/devices/DeviceStatusPage.tsx b/src/components/devices/DeviceStatusPage.tsx index 6822085..ab4f925 100644 --- a/src/components/devices/DeviceStatusPage.tsx +++ b/src/components/devices/DeviceStatusPage.tsx @@ -272,16 +272,7 @@ export default function DeviceStatusPage({ }, [persistentDroneData, persistentHostData]); // 实时位置数据(用于地图) - const realtimePosition = React.useMemo(() => { - if (persistentDroneData?.longitude && persistentDroneData?.latitude) { - return { - lon: persistentDroneData.longitude, - lat: persistentDroneData.latitude, - heading: persistentDroneData.heading - }; - } - return null; - }, [persistentDroneData]); + const [messageApi, contextHolder] = message.useMessage(); @@ -328,6 +319,34 @@ export default function DeviceStatusPage({ ); const getVal = (val) => val === "no_rain" ? "无" : (val || '-'); + const realtimePosition = React.useMemo(() => { + // 1. 无人机:优先用 MQTT 实时数据 + if (isUAV) { + if (currentDroneData?.longitude && currentDroneData?.latitude) { + return { + lon: currentDroneData.longitude, + lat: currentDroneData.latitude, + heading: currentDroneData.heading || 0, + }; + } + return null; + } + + // 2. 机器人:从 wsDeviceInfo / runningStatus 取实时经纬度 + const robotLon = runningStatus.longitude || wsDeviceInfo.longitude; + const robotLat = runningStatus.latitude || wsDeviceInfo.latitude; + + if (robotLon && robotLat) { + return { + lon: robotLon, + lat: robotLat, + heading: runningStatus.yaw || wsDeviceInfo.yaw || 0, + }; + } + + return null; + }, [isUAV, currentDroneData, runningStatus, wsDeviceInfo]); + useEffect(() => { const controller = new DeviceController(userInfo, messageApi); @@ -354,7 +373,7 @@ export default function DeviceStatusPage({ }, [userInfo, messageApi]); useEffect(() => { - if (!device || !controllerRef.current || !device?.serialNumber) return; + if (!device || !controllerRef.current || !device?.serialNumber || isUAV) return; controllerRef.current.connectDevice(device); }, [device?.serialNumber]); @@ -503,6 +522,11 @@ export default function DeviceStatusPage({ return Number(val).toFixed(2); }; + const realtimeType = useMemo(() => { + return isUAV ? 'plane' : 'car'; + }, [isUAV]); + + if (!device) { return ( {contextHolder} @@ -702,7 +727,7 @@ export default function DeviceStatusPage({ devices={[mapDevices]} points={points} realtimePosition={realtimePosition} - realtimeType="plane" + realtimeType={realtimeType} /> diff --git a/src/components/devices/deviceController.js b/src/components/devices/deviceController.js index e9eaa50..43177b2 100644 --- a/src/components/devices/deviceController.js +++ b/src/components/devices/deviceController.js @@ -62,6 +62,7 @@ export default class DeviceController { // ========== 【集成】页面切前台重连 ========== handleVisibilityChange() { if (document.visibilityState === 'visible' && this.currentDevice) { + if (!this.currentDevice?.serialNumber) return; console.log('✅ 页面切回前台,自动重连 WebSocket'); setTimeout(() => { this.connectDevice(this.currentDevice); @@ -75,7 +76,7 @@ export default class DeviceController { if ( this.connectedDeviceSn && this.wsStatus !== 'connected' && - this.currentDevice + this.currentDevice?.serialNumber ) { console.log('🔄 后台保活:尝试重连 WebSocket'); this.connectDevice(this.currentDevice); @@ -116,8 +117,9 @@ export default class DeviceController { // 保存当前设备,用于保活/重连 this.currentDevice = device; + if (!device.serialNumber) return; - const sn = device.serialNumber || device.device_sn; + const sn = device.serialNumber; this.connectedDeviceSn = null; this.wsDeviceInfo = {};