From 11817fa2d85eee27b45bbd07294bf268603de084 Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Mon, 1 Jun 2026 17:14:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=88=AA=E7=BA=BF=20=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=8A=B6=E6=80=81=20=E8=A3=85=E5=A4=87=E6=80=BB=E8=A7=88?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E6=97=A0=E4=BA=BA=E6=9C=BA=E5=92=8C=E6=9C=BA?= =?UTF-8?q?=E5=9C=BA=E5=AE=9E=E6=97=B6=E7=8A=B6=E6=80=81=E6=8E=A5=E5=85=A5?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E5=9C=B0=E5=9B=BE=E4=B8=8A=E5=B0=B1?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=AE=9E=E6=97=B6=E6=98=BE=E7=A4=BA=E6=97=A0?= =?UTF-8?q?=E4=BA=BA=E6=9C=BA=E5=AE=9A=E4=BD=8D=EF=BC=8C=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E8=88=AA=E5=90=91=E8=A7=92=E8=BD=AC=E8=A7=92=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Map.jsx | 43 ++- src/components/devices/DeviceOverviewPage.tsx | 253 +++++++++++++----- src/components/devices/DeviceStatusPage.tsx | 252 +++++++++++++---- src/components/devices/DroneVideoPlayer.tsx | 4 +- src/components/devices/WayLinePage.tsx | 162 +++++++++-- src/pages/Devices.tsx | 29 +- 6 files changed, 557 insertions(+), 186 deletions(-) diff --git a/src/components/Map.jsx b/src/components/Map.jsx index cc4eb02..66cf558 100644 --- a/src/components/Map.jsx +++ b/src/components/Map.jsx @@ -25,7 +25,7 @@ const CYAN_GLOW_COLOR = new window.Cesium.Color( ); window.Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmNjk0NjI1MC0wZGYyLTQ1YzItYjJmZi0wY2M5ODEwODllYzIiLCJpZCI6Mzk3MTQxLCJpYXQiOjE3NzI1MjkwMjV9.U6qmakhDgXJVXwLFyWzSxAUOFZRrDkIsRUPP6CCdnKg"; -export default function CesiumMap({ realtimePosition, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], onPathChange, from, onClearDraw }) { +export default function CesiumMap({ realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], onPathChange, from, onClearDraw }) { const containerRef = useRef(null); const viewerRef = useRef(null); const tooltipRef = useRef(null); @@ -1288,29 +1288,46 @@ export default function CesiumMap({ realtimePosition, pathPoints = [], devices = } // ==================================== - // 创建实时小车(只创建一次) + // 创建实时设备图标(只创建一次) // ==================================== + // 判断应该用什么图标(飞机优先) + const isPlane = realtimeType === 'plane' || realtimeType === 'drone' || devices.some(d => d.type === 'plane' || d.type === 'drone'); + const img = isPlane ? planeImg : carImg; + const imgWidth = isPlane ? 70 : 60; + const imgHeight = isPlane ? 70 : 60; + + // 如果有航向角,设置旋转 + let rotation = 0; + if (realtimePosition?.heading !== undefined && !isNaN(Number(realtimePosition.heading))) { + // 航向通常是地理坐标系(0=北, 90=东, 顺时针) + // Cesium billboard 的 rotation 是0=右(东),顺时针为正 + // 需要将地理航向转换为 Cesium rotation + // 地理 0° 北 → Cesium 需要 -90° 或者 270°(因为 Cesium 0° 是右/东) + let headingDeg = Number(realtimePosition.heading); + // 将地理航向转换:北(0°) → 上(270° 或 -90°) + rotation = Cesium.Math.toRadians(headingDeg - 90); + } + if (!realtimeCarEntityRef.current) { - realtimeCarEntityRef.current = viewer.entities.add({ - position: position, - billboard: { - image: carImg, - width: 60, - height: 60, - verticalOrigin: Cesium.VerticalOrigin.BOTTOM, + image: img, + width: imgWidth, + height: imgHeight, + verticalOrigin: Cesium.VerticalOrigin.CENTER, + rotation: rotation, disableDepthTestDistance: Number.POSITIVE_INFINITY, } - }); - } else { - + // 更新位置、图标和旋转 realtimeCarEntityRef.current.position = position; - + realtimeCarEntityRef.current.billboard.image = img; + realtimeCarEntityRef.current.billboard.width = imgWidth; + realtimeCarEntityRef.current.billboard.height = imgHeight; + realtimeCarEntityRef.current.billboard.rotation = rotation; } // ==================================== diff --git a/src/components/devices/DeviceOverviewPage.tsx b/src/components/devices/DeviceOverviewPage.tsx index b38183a..d76acff 100644 --- a/src/components/devices/DeviceOverviewPage.tsx +++ b/src/components/devices/DeviceOverviewPage.tsx @@ -23,6 +23,8 @@ import { useWebRTCPlayer } from '../useWebRTCStream.tsx'; import VideoPlayer from '../VideoPlayer.tsx.tsx'; import CesiumMap from '../Map.jsx'; import { getUAVState } from '@/api/device.ts'; +import { useMqtt } from '@/hooks/useMqtt'; +import envConfig from '../../../env'; const { Content } = Layout; const { Text, Title } = Typography; @@ -45,54 +47,181 @@ const logs = [ { time: '10:05:22', device: 'RBT-01', type: '事件', desc: '电量较低,建议回充', level: '低电量' }, ]; -export default function DeviceOverviewPage({ devices, planedevices, devicesAll, onViewStatus, selectedSn, onDeviceSelect, mqttDroneData }) { +export default function DeviceOverviewPage({ devices, planedevices, devicesAll, onViewStatus, selectedSn, onDeviceSelect }) { const [zoomedVideo, setZoomedVideo] = useState(null); const [mowerView, setMowerView] = useState('front'); - const [videoSource, setVideoSource] = useState('drone'); // 恢复 videoSource 状态管理 + const [videoSource, setVideoSource] = useState('drone'); const [uavState, setUavState] = useState(null); - const [persistentDroneData, setPersistentDroneData] = useState(null); // 持久化无人机数据 + const [persistentDroneData, setPersistentDroneData] = useState(null); + const [persistentHostData, setPersistentHostData] = useState(null); - // 当MQTT数据更新时,更新持久化数据(只在有新数据时更新) - useEffect(() => { - if (mqttDroneData && mqttDroneData.host) { - const { host } = mqttDroneData; - setPersistentDroneData({ - // 无人机状态数据映射 - capacity_percent: host.drone_charge_state?.capacity_percent, - environment_temperature: host.environment_temperature, - height: host.height, - wind_speed: host.wind_speed, - rainfall: host.rainfall === 0 ? 'no_rain' : 'rain', - position_state: host.position_state, - longitude: host.longitude, - latitude: host.latitude, - heading: host.heading, - humidity: host.humidity, - drone_in_dock: host.drone_in_dock, - cover_state: host.cover_state, - temperature: host.temperature, - }); - } - }, [mqttDroneData]); - - // 优先使用持久化数据,然后是API数据 - const currentDroneData = React.useMemo(() => { - return persistentDroneData; // 总是返回持久化数据(不会变回null) - }, [persistentDroneData]); - - const selectedDevice = React.useMemo(() => { + // 当前选中设备(切换时才变) + const selectedDevice = useMemo(() => { const sn = selectedSn || (devicesAll?.[0]?.device_sn || devicesAll?.[0]?.serialNumber); return devicesAll?.find(item => (item.device_sn || item.serialNumber) === sn) || null; }, [devicesAll, selectedSn]); const isUAV = !!selectedDevice?.gateway_sn; + // ======================== 核心优化:视频用设备对象(绝不随MQTT刷新)======================== + const videoDevice = useMemo(() => { + if (!selectedDevice) return null; + return { ...selectedDevice }; + }, [selectedDevice]); // 只有切设备才变 → 视频不闪 + + // ======================== UI显示用(MQTT实时更新)======================== + const realtimeDevice = useMemo(() => { + if (!selectedDevice) return null; + return { + ...selectedDevice, + ...uavState, + ...persistentHostData, + ...persistentDroneData, + }; + }, [selectedDevice, uavState, persistentHostData, persistentDroneData?.wind_speed]); + console.log('实时设备数据:', realtimeDevice); + + // MQTT消息处理 + const handleMqttMessage = (topic, message) => { + console.log('DeviceOverviewPage MQTT Topic:', topic); + console.log('DeviceOverviewPage MQTT Message:', message); + console.log('实时设备数据:', realtimeDevice); + + try { + const parsed = JSON.parse(message); + console.log('MQTT解析结果:', parsed); + if (parsed.data?.host) { + const { host } = parsed.data; + + // 判断是无人机数据还是机场数据:无人机数据有 battery 字段 + const isDroneData = host.battery !== undefined; + + if (isDroneData) { + // 处理无人机数据 + setPersistentDroneData(prevData => { + const newData = { ...prevData }; + + // 无人机特有字段 + if (host.battery?.capacity_percent !== undefined) { + newData.capacity_percent = host.battery.capacity_percent; + } + if (host.attitude_head !== undefined) { + newData.heading = host.attitude_head; + } + if (host.horizontal_speed !== undefined) { + newData.horizontal_speed = host.horizontal_speed; + } + if (host.vertical_speed !== undefined) { + newData.vertical_speed = host.vertical_speed; + } + if (host.total_flight_time !== undefined) { + newData.total_flight_time = host.total_flight_time; + } + if (host.total_flight_sorties !== undefined) { + newData.total_flight_sorties = host.total_flight_sorties; + } + if (host.mode_code !== undefined) { + newData.mode_code = host.mode_code; + } + + // 通用字段 + if (host.height !== undefined) { + newData.height = host.height; + } + if (host.wind_speed !== undefined) { + newData.wind_speed = host.wind_speed; + } + if (host.position_state !== undefined) { + newData.position_state = host.position_state; + } + if (host.longitude !== undefined) { + newData.longitude = host.longitude; + } + if (host.latitude !== undefined) { + newData.latitude = host.latitude; + } + + return newData; + }); + } else { + // 处理机场数据 + setPersistentHostData(prevData => { + const newData = { ...prevData }; + + // 机场特有字段 + if (host.drone_charge_state?.capacity_percent !== undefined) { + newData.capacity_percent = host.drone_charge_state.capacity_percent; + } + if (host.air_conditioner?.air_conditioner_state !== undefined) { + newData.air_conditioner_state = host.air_conditioner.air_conditioner_state; + } + if (host.air_conditioner_state !== undefined) { + newData.air_conditioner_state = host.air_conditioner_state; + } + + // 通用字段 + if (host.environment_temperature !== undefined) { + newData.environment_temperature = host.environment_temperature; + } + if (host.wind_speed !== undefined) { + newData.wind_speed = host.wind_speed; + } + if (host.rainfall !== undefined) { + newData.rainfall = host.rainfall === 0 ? 'no_rain' : 'rain'; + } + if (host.humidity !== undefined) { + newData.humidity = host.humidity; + } + if (host.drone_in_dock !== undefined) { + newData.drone_in_dock = host.drone_in_dock; + } + if (host.cover_state !== undefined) { + newData.cover_state = host.cover_state; + } + if (host.temperature !== undefined) { + newData.temperature = host.temperature; + } + if (host.home_distance !== undefined) { + newData.home_distance = host.home_distance; + } + + return newData; + }); + } + } + } catch (err) { + console.error('MQTT解析失败', err); + } + }; + + // MQTT订阅 + const mqttTopics = useMemo(() => { + if (!isUAV || !selectedDevice) return []; + const topics = []; + if (selectedDevice.device_sn) { + topics.push(`thing/product/${selectedDevice.device_sn}/osd`); + } + if (selectedDevice.gateway_sn && selectedDevice.gateway_sn !== selectedDevice.device_sn) { + topics.push(`thing/product/${selectedDevice.gateway_sn}/osd`); + } + console.log('DeviceOverviewPage 订阅的MQTT主题:', topics); + return topics; + }, [selectedDevice]); + + useMqtt({ + url: envConfig.mqtt_url, + topic: mqttTopics, + onMessage: handleMqttMessage, + }); + + // 机器人视频 const { video: robotVideo, error, ready } = useWebRTCPlayer( !isUAV ? selectedDevice?.serialNumber || '' : '', `webrtc://1.95.137.212/live/livestream/${selectedDevice?.serialNumber}`, ); - const dynamicTopCards = React.useMemo(() => { + // 顶部统计卡片 + const dynamicTopCards = useMemo(() => { const totalDrones = planedevices?.length || 0; const onlineDrones = planedevices?.filter(d => d.onlineStatus === 1).length || 0; const totalRobots = devicesAll?.filter(d => d.productName?.includes('巡检')).length || 0; @@ -109,18 +238,19 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll, ]; }, [devicesAll, planedevices]); - const dynamicScheduleList = React.useMemo(() => { - return devicesAll.map((d, index) => ({ + // 设备调度列表 + const dynamicScheduleList = useMemo(() => { + return devicesAll?.map((d, index) => ({ id: d.deviceId || index, name: d.deviceAlias || d.deviceName || d.callsign || '未知设备', type: d.productName || '未知类型', status: d.onlineStatus === 1 ? '在线' : '离线', mode: d.status === 4 ? '待命' : '作业中', - battery: d.capacity_percent || 100, + capacity_percent: d.capacity_percent || 100, position: d.position || '站内', task: d.status === 4 ? '待命' : '作业中', comm: d.rssi ? `${d.rssi}dBm` : '良好' - })); + })) || []; }, [devicesAll]); const handleStart = () => message.success('任务已启动'); @@ -140,17 +270,7 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll, if (onDeviceSelect) onDeviceSelect(id); }; - // 用于视频、状态展示的实时设备数据(优先使用持久化MQTT数据) - const realtimeDevice = useMemo(() => { - if (!selectedDevice) return null; - return { - ...selectedDevice, - ...uavState, - ...currentDroneData, // 持久化数据最后覆盖,确保优先显示 - }; - }, [selectedDevice, uavState, currentDroneData]); - - + // 获取无人机状态 useEffect(() => { const fetchUavState = async () => { if (isUAV && selectedDevice?.gateway_sn && selectedDevice?.device_sn) { @@ -159,9 +279,7 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll, sn: selectedDevice.gateway_sn, droneSn: selectedDevice.device_sn }); - if (res.code === 200) { - setUavState(res.data); - } + if (res.code === 200) setUavState(res.data); } catch (err) { console.error('获取无人机状态失败', err); } @@ -169,10 +287,7 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll, setUavState(null); } }; - fetchUavState(); - - }, [isUAV, selectedDevice]); return ( @@ -292,12 +407,12 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll,
- 电量 {fix2((uavState || selectedDevice).capacity_percent || 100)}% + 电量 {fix2(realtimeDevice?.capacity_percent || 100)}%
- {(uavState || selectedDevice).environment_temperature && ( + {realtimeDevice?.environment_temperature && (
温度 - {fix2((uavState || selectedDevice).environment_temperature || 25)}℃ + {fix2(realtimeDevice.environment_temperature || 25)}℃
)}
@@ -331,22 +446,21 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll, <>
飞行高度 - {fix2((uavState || selectedDevice).height)} m + {fix2(realtimeDevice?.height)} m
飞行风速 - {fix2((uavState || selectedDevice).wind_speed)} m/s + {fix2(realtimeDevice?.wind_speed)} m/s
rtk数量 - {/* - /*/} + /> +
)} @@ -379,13 +493,12 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll, )} - {/* 视频容器 */} + {/* ✅ 关键:videoDevice 绝不随MQTT更新,视频永不闪烁 */}
{isUAV ? (
-
风速
{fix2((uavState || selectedDevice)?.wind_speed)} m/s
-
温度
{fix2((uavState || selectedDevice)?.environment_temperature)}℃
-
降雨
{getVal((uavState || selectedDevice)?.rainfall)}
-
RTK
{(uavState || selectedDevice)?.position_state?.is_fixed === 'fixing_successful' ? '固定' : '未固定'}
+
风速
{fix2(realtimeDevice?.wind_speed)} m/s
+
温度
{fix2(realtimeDevice?.environment_temperature)}℃
+
降雨
{getVal(realtimeDevice?.rainfall)}
+
RTK
{realtimeDevice?.position_state?.is_fixed === 'fixing_successful' ? '固定' : '未固定'}
diff --git a/src/components/devices/DeviceStatusPage.tsx b/src/components/devices/DeviceStatusPage.tsx index dcfab91..6635fa5 100644 --- a/src/components/devices/DeviceStatusPage.tsx +++ b/src/components/devices/DeviceStatusPage.tsx @@ -50,6 +50,8 @@ import VideoPlayer from '../VideoPlayer.tsx'; import DeviceController from './DeviceController'; import { DatePicker } from 'antd'; import dayjs from 'dayjs'; +import { useMqtt } from '@/hooks/useMqtt'; +import envConfig from '../../../env'; const { RangePicker } = DatePicker; const { Text } = Typography; @@ -58,39 +60,179 @@ export default function DeviceStatusPage({ devicesAll, serialNumber, onDeviceChange, - mqttDroneData, }) { const { userInfo } = useSelector((state: RootState) => state.user); const [persistentDroneData, setPersistentDroneData] = useState(null); // 持久化无人机数据 + const [persistentHostData, setPersistentHostData] = useState(null); // 持久化机场数据 + + // 获取当前选中的无人机设备 + const device = useMemo(() => { + const sn = + serialNumber || + devicesAll?.[0]?.device_sn || + devicesAll?.[0]?.serialNumber; + + if (!sn) return null; + + return devicesAll?.find( + (d) => (d.device_sn || d.serialNumber) === sn, + ); + }, [devicesAll, serialNumber]); + + const isUAV = useMemo(() => { + return !!device?.device_sn; + }, [device]); // 当MQTT数据更新时,更新持久化数据(只在有新数据时更新) - useEffect(() => { - if (mqttDroneData && mqttDroneData.host) { - const { host } = mqttDroneData; - setPersistentDroneData({ - // 无人机状态数据映射 - capacity_percent: host.drone_charge_state?.capacity_percent, - environment_temperature: host.environment_temperature, - height: host.height, - wind_speed: host.wind_speed, - rainfall: host.rainfall === 0 ? 'no_rain' : 'rain', - position_state: host.position_state, - longitude: host.longitude, - latitude: host.latitude, - heading: host.heading, - humidity: host.humidity, - drone_in_dock: host.drone_in_dock, - cover_state: host.cover_state, - temperature: host.temperature, - home_distance: host.home_distance, - air_conditioner_state: host.air_conditioner_state, - }); - } - }, [mqttDroneData]); + const handleMqttMessage = (topic, message) => { + console.log('DeviceStatusPage MQTT 消息到达 - Topic:', topic); + console.log('DeviceStatusPage MQTT 消息内容:', message); + try { + const parsedMessage = JSON.parse(message); + console.log('DeviceStatusPage 解析后的消息:', parsedMessage); - // 优先使用持久化数据,保持显示稳定 + if (parsedMessage.data && parsedMessage.data.host) { + const { host } = parsedMessage.data; + + // 判断是无人机数据还是机场数据:无人机数据有 battery 字段 + const isDroneData = host.battery !== undefined; + + if (isDroneData) { + // 处理无人机数据 + setPersistentDroneData(prevData => { + const newData = { ...prevData }; + + // 无人机特有字段 + if (host.battery?.capacity_percent !== undefined) { + newData.capacity_percent = host.battery.capacity_percent; + } + if (host.attitude_head !== undefined) { + newData.heading = host.attitude_head; + } + if (host.horizontal_speed !== undefined) { + newData.horizontal_speed = host.horizontal_speed; + } + if (host.vertical_speed !== undefined) { + newData.vertical_speed = host.vertical_speed; + } + if (host.total_flight_time !== undefined) { + newData.total_flight_time = host.total_flight_time; + } + if (host.total_flight_sorties !== undefined) { + newData.total_flight_sorties = host.total_flight_sorties; + } + if (host.mode_code !== undefined) { + newData.mode_code = host.mode_code; + } + + // 通用字段 + if (host.height !== undefined) { + newData.height = host.height; + } + if (host.wind_speed !== undefined) { + newData.wind_speed = host.wind_speed; + } + if (host.position_state !== undefined) { + newData.position_state = host.position_state; + } + if (host.longitude !== undefined) { + newData.longitude = host.longitude; + } + if (host.latitude !== undefined) { + newData.latitude = host.latitude; + } + + return newData; + }); + } else { + // 处理机场数据 + setPersistentHostData(prevData => { + const newData = { ...prevData }; + + // 机场特有字段 + if (host.drone_charge_state?.capacity_percent !== undefined) { + newData.capacity_percent = host.drone_charge_state.capacity_percent; + } + if (host.air_conditioner?.air_conditioner_state !== undefined) { + newData.air_conditioner_state = host.air_conditioner.air_conditioner_state; + } + if (host.air_conditioner_state !== undefined) { + newData.air_conditioner_state = host.air_conditioner_state; + } + + // 通用字段 + if (host.environment_temperature !== undefined) { + newData.environment_temperature = host.environment_temperature; + } + if (host.wind_speed !== undefined) { + newData.wind_speed = host.wind_speed; + } + if (host.rainfall !== undefined) { + newData.rainfall = host.rainfall === 0 ? 'no_rain' : 'rain'; + } + if (host.humidity !== undefined) { + newData.humidity = host.humidity; + } + if (host.drone_in_dock !== undefined) { + newData.drone_in_dock = host.drone_in_dock; + } + if (host.cover_state !== undefined) { + newData.cover_state = host.cover_state; + } + if (host.temperature !== undefined) { + newData.temperature = host.temperature; + } + if (host.home_distance !== undefined) { + newData.home_distance = host.home_distance; + } + + return newData; + }); + } + } + } catch (err) { + console.error('DeviceStatusPage MQTT 消息解析失败:', err); + } + }; + + // 根据选中设备动态构建MQTT主题 + const mqttTopics = useMemo(() => { + if (!isUAV || !device) { + return []; + } + const topics = []; + if (device.device_sn) { + topics.push(`thing/product/${device.device_sn}/osd`); + } + if (device.gateway_sn && device.gateway_sn !== device.device_sn) { + topics.push(`thing/product/${device.gateway_sn}/osd`); + } + console.log('DeviceStatusPage 订阅的MQTT主题:', topics); + return topics; + }, [isUAV, device]); + + // 订阅MQTT消息 + useMqtt({ + url: envConfig.mqtt_url, + topic: mqttTopics, + onMessage: handleMqttMessage, + }); + + // 合并无人机和机场数据 const currentDroneData = React.useMemo(() => { - return persistentDroneData; + return { ...persistentHostData, ...persistentDroneData }; + }, [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(); @@ -121,23 +263,6 @@ export default function DeviceStatusPage({ const [points, setPoints] = useState([]); const timerRef = useRef(null); - const device = useMemo(() => { - const sn = - serialNumber || - devicesAll?.[0]?.device_sn || - devicesAll?.[0]?.serialNumber; - - if (!sn) return null; - - return devicesAll?.find( - (d) => (d.device_sn || d.serialNumber) === sn, - ); - }, [devicesAll, serialNumber]); - - const isUAV = useMemo(() => { - return !!device?.device_sn; - }, [device]); - const runningStatus = useMemo(() => { const base = device?.lastRunningStatus || {}; @@ -525,7 +650,12 @@ export default function DeviceStatusPage({ height: 400, }} > - + @@ -544,8 +674,18 @@ export default function DeviceStatusPage({ 飞行高度}> {fix2((currentDroneData || uavState || device).height)} m - {fix2((currentDroneData || uavState || device).wind_speed)} m/s - {fix2((currentDroneData || uavState || device).environment_temperature)} ℃ + + {fix2(currentDroneData?.horizontal_speed) || '-'} m/s + + + {fix2(currentDroneData?.vertical_speed) || '-'} m/s + + + {fix2((currentDroneData || uavState || device).wind_speed)} m/s + + + {fix2((currentDroneData || uavState || device).environment_temperature)} ℃ + {(currentDroneData?.humidity) || '-'}{currentDroneData?.humidity ? '%' : ''} @@ -555,10 +695,16 @@ export default function DeviceStatusPage({ {getVal((currentDroneData || uavState || device).rainfall)} - {fix2((currentDroneData || uavState || device).home_distance)} m + {fix2(currentDroneData?.heading)}° + + + {currentDroneData?.mode_code !== undefined ? `模式 ${currentDroneData.mode_code}` : '-'} + + + {currentDroneData?.drone_in_dock === 1 ? '在库' : '外出'} @@ -580,11 +726,17 @@ export default function DeviceStatusPage({ - - - {(currentDroneData || uavState || device).position_state?.is_fixed === 'fixing_successful' ? 'RTK 已固定' : '未固定'} + + + {(currentDroneData || uavState || device).position_state?.is_fixed === 2 || (currentDroneData || uavState || device).position_state?.is_fixed === 'fixing_successful' ? 'RTK 已固定' : '未固定'} + + + {currentDroneData?.latitude && currentDroneData?.longitude ? + `${currentDroneData.latitude.toFixed(6)}, ${currentDroneData.longitude.toFixed(6)}` : '-'} + + ) : ( diff --git a/src/components/devices/DroneVideoPlayer.tsx b/src/components/devices/DroneVideoPlayer.tsx index 416c4c5..c0d29ef 100644 --- a/src/components/devices/DroneVideoPlayer.tsx +++ b/src/components/devices/DroneVideoPlayer.tsx @@ -293,8 +293,8 @@ const DroneVideoPlayer: React.FC = ({ padding: '4px 8px', fontSize: 10 }}> - 高度 {Number(device?.height)?.toFixed(2) || '-'}m - 风速 {Number(device?.wind_speed?.toFixed(2) || '0')}m/s + {/*高度 {Number(device?.height)?.toFixed(2) || '-'}m + 风速 {Number(device?.wind_speed?.toFixed(2) || '0')}m/s*/}
)}
diff --git a/src/components/devices/WayLinePage.tsx b/src/components/devices/WayLinePage.tsx index d43df3c..de303e8 100644 --- a/src/components/devices/WayLinePage.tsx +++ b/src/components/devices/WayLinePage.tsx @@ -25,11 +25,13 @@ import utils from '@/lib/utils'; import CesiumMap from '../Map'; import { getUAVState } from '@/api/device'; import { fix2, getVal } from '@/lib/utils'; +import { useMqtt } from '@/hooks/useMqtt'; +import envConfig from '../../../env'; const { Text, Title } = Typography; const { RangePicker } = DatePicker; -export default function WayLinePage({ planedevices, onRefresh, mqttDroneData }) { +export default function WayLinePage({ planedevices, onRefresh }) { const fontSmall = 'clamp(10px, 0.7vw, 11px)'; const fontNormal = 'clamp(12px, 0.75vw, 13px)'; const fontTitle = 'clamp(14px, 0.85vw, 16px)'; @@ -43,36 +45,150 @@ export default function WayLinePage({ planedevices, onRefresh, mqttDroneData }) const [listDateRange, setListDateRange] = useState(null); const [taskList, setTaskList] = useState([]); const [persistentDroneData, setPersistentDroneData] = useState(null); // 持久化无人机数据 + const [persistentHostData, setPersistentHostData] = useState(null); // 持久化机场数据 const listDroneTimerRef = useRef(null); // 当MQTT数据更新时,更新持久化数据(只在有新数据时更新) - useEffect(() => { - if (mqttDroneData && mqttDroneData.host) { - const { host } = mqttDroneData; - setPersistentDroneData({ - // 无人机状态数据映射 - capacity_percent: host.drone_charge_state?.capacity_percent, - environment_temperature: host.environment_temperature, - height: host.height, - wind_speed: host.wind_speed, - rainfall: host.rainfall === 0 ? 'no_rain' : 'rain', - position_state: host.position_state, - longitude: host.longitude, - latitude: host.latitude, - heading: host.heading, - humidity: host.humidity, - drone_in_dock: host.drone_in_dock, - cover_state: host.cover_state, - temperature: host.temperature, - }); + const handleMqttMessage = (topic, message) => { + console.log('WayLinePage MQTT 消息到达 - Topic:', topic); + console.log('WayLinePage MQTT 消息内容:', message); + try { + const parsedMessage = JSON.parse(message); + console.log('WayLinePage 解析后的消息:', parsedMessage); + + if (parsedMessage.data && parsedMessage.data.host) { + const { host } = parsedMessage.data; + if (host?.job_number) { return; } + + // 判断是无人机数据还是机场数据:无人机数据有 battery 字段 + const isDroneData = host.battery !== undefined; + + if (isDroneData) { + // 处理无人机数据 + setPersistentDroneData(prevData => { + const newData = { ...prevData }; + + // 无人机特有字段 + if (host.battery?.capacity_percent !== undefined) { + newData.capacity_percent = host.battery.capacity_percent; + } + if (host.attitude_head !== undefined) { + newData.heading = host.attitude_head; + } + if (host.horizontal_speed !== undefined) { + newData.horizontal_speed = host.horizontal_speed; + } + if (host.vertical_speed !== undefined) { + newData.vertical_speed = host.vertical_speed; + } + if (host.total_flight_time !== undefined) { + newData.total_flight_time = host.total_flight_time; + } + if (host.total_flight_sorties !== undefined) { + newData.total_flight_sorties = host.total_flight_sorties; + } + if (host.mode_code !== undefined) { + newData.mode_code = host.mode_code; + } + + // 通用字段 + if (host.height !== undefined) { + newData.height = host.height; + } + if (host.wind_speed !== undefined) { + newData.wind_speed = host.wind_speed; + } + if (host.position_state !== undefined) { + newData.position_state = host.position_state; + } + if (host.longitude !== undefined) { + newData.longitude = host.longitude; + } + if (host.latitude !== undefined) { + newData.latitude = host.latitude; + } + + return newData; + }); + } else { + // 处理机场数据 + setPersistentHostData(prevData => { + const newData = { ...prevData }; + + // 机场特有字段 + if (host.drone_charge_state?.capacity_percent !== undefined) { + newData.capacity_percent = host.drone_charge_state.capacity_percent; + } + if (host.air_conditioner?.air_conditioner_state !== undefined) { + newData.air_conditioner_state = host.air_conditioner.air_conditioner_state; + } + if (host.air_conditioner_state !== undefined) { + newData.air_conditioner_state = host.air_conditioner_state; + } + + // 通用字段 + if (host.environment_temperature !== undefined) { + newData.environment_temperature = host.environment_temperature; + } + if (host.wind_speed !== undefined) { + newData.wind_speed = host.wind_speed; + } + if (host.rainfall !== undefined) { + newData.rainfall = host.rainfall === 0 ? 'no_rain' : 'rain'; + } + if (host.humidity !== undefined) { + newData.humidity = host.humidity; + } + if (host.drone_in_dock !== undefined) { + newData.drone_in_dock = host.drone_in_dock; + } + if (host.cover_state !== undefined) { + newData.cover_state = host.cover_state; + } + if (host.temperature !== undefined) { + newData.temperature = host.temperature; + } + if (host.home_distance !== undefined) { + newData.home_distance = host.home_distance; + } + + return newData; + }); + } + } + } catch (err) { + console.error('WayLinePage MQTT 消息解析失败:', err); } - }, [mqttDroneData]); + }; + + // 根据选中设备动态构建MQTT主题 + const mqttTopics = React.useMemo(() => { + if (!listDrone) { + return []; + } + const topics = []; + if (listDrone.device_sn) { + topics.push(`thing/product/${listDrone.device_sn}/osd`); + } + if (listDrone.gateway_sn && listDrone.gateway_sn !== listDrone.device_sn) { + topics.push(`thing/product/${listDrone.gateway_sn}/osd`); + } + console.log('WayLinePage 订阅的MQTT主题:', topics); + return topics; + }, [listDrone]); + + // 订阅MQTT消息 + useMqtt({ + url: envConfig.mqtt_url, + topic: mqttTopics, + onMessage: handleMqttMessage, + }); // 优先使用持久化数据,保持显示稳定 const mqttDerivedData = React.useMemo(() => { - return persistentDroneData; - }, [persistentDroneData]); + return { ...persistentHostData, ...persistentDroneData }; + }, [persistentDroneData, persistentHostData]); // 定时获取任务列表所选无人机的实时状态 useEffect(() => { diff --git a/src/pages/Devices.tsx b/src/pages/Devices.tsx index dc60184..9cd786a 100644 --- a/src/pages/Devices.tsx +++ b/src/pages/Devices.tsx @@ -8,8 +8,6 @@ import { useSelector } from 'react-redux'; import { RootState } from '../store'; import WayLinePage from '../components/devices/WayLinePage'; import RobotTaskPage from '../components/devices/RobotTaskPage'; -import { useMqtt } from '../hooks/useMqtt'; -import envConfig from '../../env'; const { Header, Content } = Layout; const { Text } = Typography; @@ -23,8 +21,6 @@ export default function DeviceIndexPage() { const [devices, setDevices] = useState([]); const [planedevices, setPlaneDevices] = useState([]); const [devicesAll, setDevicesAll] = useState([]); - // MQTT实时数据状态 - const [mqttDroneData, setMqttDroneData] = useState(null); const tabs = [ { key: 'overview', label: '装备总览', icon: }, @@ -33,27 +29,6 @@ export default function DeviceIndexPage() { { key: 'robotTask', label: '机器人任务', icon: }, ]; - useMqtt({ - url: envConfig.mqtt_url, - topic: 'thing/product/8UUXN4200A04HZ/osd', - onMessage: (_, message) => { - console.log('收到 MQTT 消息:', message); - try { - const parsedMessage = JSON.parse(message); - console.log('解析后的消息对象:', parsedMessage); - // 更新MQTT无人机数据 - if (parsedMessage.data) { - if (parsedMessage.data?.host?.humidity) { - setMqttDroneData(parsedMessage.data); - } - - } - } catch (error) { - console.error('解析消息失败:', error); - } - }, - }); - // 👇 核心:给无人机补全 lastRunningStatus + type: 'plane' const fetchAllData = async () => { @@ -122,7 +97,6 @@ export default function DeviceIndexPage() { onViewStatus={handleViewStatus} selectedSn={selectedSn} onDeviceSelect={(sn) => setSelectedSn(sn)} - mqttDroneData={mqttDroneData} />; } if (activeTab === 'status') { @@ -130,10 +104,9 @@ export default function DeviceIndexPage() { devicesAll={devicesAll} serialNumber={selectedSn} onDeviceChange={(sn) => setSelectedSn(sn)} - mqttDroneData={mqttDroneData} />; } - if (activeTab === 'wayLineTask') return ; + if (activeTab === 'wayLineTask') return ; if (activeTab === 'robotTask') return ; return null; };