三个页面无人机mqtt显示成功
This commit is contained in:
@@ -45,11 +45,40 @@ const logs = [
|
||||
{ time: '10:05:22', device: 'RBT-01', type: '事件', desc: '电量较低,建议回充', level: '低电量' },
|
||||
];
|
||||
|
||||
export default function DeviceOverviewPage({ devices, planedevices, devicesAll, onViewStatus, selectedSn, onDeviceSelect }) {
|
||||
export default function DeviceOverviewPage({ devices, planedevices, devicesAll, onViewStatus, selectedSn, onDeviceSelect, mqttDroneData }) {
|
||||
const [zoomedVideo, setZoomedVideo] = useState(null);
|
||||
const [mowerView, setMowerView] = useState('front');
|
||||
const [videoSource, setVideoSource] = useState('drone'); // 恢复 videoSource 状态管理
|
||||
const [uavState, setUavState] = useState(null);
|
||||
const [persistentDroneData, setPersistentDroneData] = 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 sn = selectedSn || (devicesAll?.[0]?.device_sn || devicesAll?.[0]?.serialNumber);
|
||||
@@ -111,14 +140,15 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll,
|
||||
if (onDeviceSelect) onDeviceSelect(id);
|
||||
};
|
||||
|
||||
// 用于视频、状态展示的实时设备数据(只合并一次)
|
||||
// 用于视频、状态展示的实时设备数据(优先使用持久化MQTT数据)
|
||||
const realtimeDevice = useMemo(() => {
|
||||
if (!selectedDevice) return null;
|
||||
return {
|
||||
...selectedDevice,
|
||||
...uavState, // 无人机实时状态覆盖
|
||||
...uavState,
|
||||
...currentDroneData, // 持久化数据最后覆盖,确保优先显示
|
||||
};
|
||||
}, [selectedDevice, uavState]);
|
||||
}, [selectedDevice, uavState, currentDroneData]);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
@@ -140,7 +170,7 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll,
|
||||
}
|
||||
};
|
||||
|
||||
fetchUavState();
|
||||
fetchUavState();
|
||||
|
||||
|
||||
}, [isUAV, selectedDevice]);
|
||||
|
||||
Reference in New Issue
Block a user