航线 设备状态 装备总览界面无人机和机场实时状态接入,并且地图上就可以实时显示无人机定位,根据航向角转角度

This commit is contained in:
mmc
2026-06-01 17:14:55 +08:00
parent 9a5df3658c
commit 11817fa2d8
6 changed files with 557 additions and 186 deletions

View File

@@ -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;
}
// ====================================

View File

@@ -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,
<div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 4 }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 4 }}>
<span style={{ color: '#52c41a' }}><ThunderboltOutlined /></span>
<span style={{ fontSize: 12 }}>电量 {fix2((uavState || selectedDevice).capacity_percent || 100)}%</span>
<span style={{ fontSize: 12 }}>电量 {fix2(realtimeDevice?.capacity_percent || 100)}%</span>
</div>
{(uavState || selectedDevice).environment_temperature && (
{realtimeDevice?.environment_temperature && (
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
<span style={{ fontSize: 12 }}>温度</span>
<span style={{ color: '#faad14' }}>{fix2((uavState || selectedDevice).environment_temperature || 25)}℃</span>
<span style={{ color: '#faad14' }}>{fix2(realtimeDevice.environment_temperature || 25)}℃</span>
</div>
)}
</div>
@@ -331,22 +446,21 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll,
<>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ color: '#666', fontSize: 12 }}>飞行高度</span>
<span style={{ fontSize: 12 }}>{fix2((uavState || selectedDevice).height)} m</span>
<span style={{ fontSize: 12 }}>{fix2(realtimeDevice?.height)} m</span>
</div>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ color: '#666', fontSize: 12 }}>飞行风速</span>
<span style={{ fontSize: 12 }}>{fix2((uavState || selectedDevice).wind_speed)} m/s</span>
<span style={{ fontSize: 12 }}>{fix2(realtimeDevice?.wind_speed)} m/s</span>
</div>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ color: '#666', fontSize: 12 }}>rtk数量</span>
<span style={{ fontSize: 12 }}>
{/*<Badge count={(uavState || selectedDevice).position_state?.gps_number || 0} style={{ backgroundColor: '#1890ff', transform: 'scale(0.8)' }} />
<span style={{ margin: '0 2px' }}>/</span>*/}
<Badge
showZero={true}
count={(uavState || selectedDevice)?.position_state?.rtk_number || 0}
count={realtimeDevice?.position_state?.rtk_number || 0}
style={{ backgroundColor: '#52c41a', transform: 'scale(0.8)' }}
/> </span>
/>
</span>
</div>
</>
)}
@@ -379,13 +493,12 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll,
)}
</div>
{/* 视频容器 */}
{/* ✅ 关键:videoDevice 绝不随MQTT更新,视频永不闪烁 */}
<div style={{ width: '100%', height: 160, borderRadius: 8, position: 'relative', overflow: 'hidden', background: '#000' }}>
{isUAV ? (
<DroneVideoPlayer
device={realtimeDevice}
device={videoDevice}
height={160}
showSourceSelect={false}
videoSource={videoSource}
onSourceChange={setVideoSource}
@@ -452,10 +565,10 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll,
<Card title="环境与飞行条件" bordered={false} style={{ borderRadius: 12, marginBottom: 8 }}>
<div style={{ display: "flex", justifyContent: "space-between", gap: 12, alignItems: "center" }}>
<div style={{ display: "flex", border: "1px solid #e8e8e8", borderRadius: 12, overflow: "hidden", flex: 1 }}>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px", borderRight: "1px solid #e8e8e8" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>风速</div><div style={{ fontSize: 14, fontWeight: 700, color: '#222', marginTop: 2 }}>{fix2((uavState || selectedDevice)?.wind_speed)} m/s</div></div>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px", borderRight: "1px solid #e8e8e8" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>温度</div><div style={{ fontSize: 14, fontWeight: 700, color: '#222', marginTop: 2 }}>{fix2((uavState || selectedDevice)?.environment_temperature)}℃</div></div>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px", borderRight: "1px solid #e8e8e8" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>降雨</div><div style={{ fontSize: 14, fontWeight: 700, color: '#222', marginTop: 2 }}>{getVal((uavState || selectedDevice)?.rainfall)}</div></div>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>RTK</div><div style={{ fontSize: 14, fontWeight: 700, color: '#52c41a', marginTop: 2 }}>{(uavState || selectedDevice)?.position_state?.is_fixed === 'fixing_successful' ? '固定' : '未固定'}</div></div>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px", borderRight: "1px solid #e8e8e8" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>风速</div><div style={{ fontSize: 14, fontWeight: 700, color: '#222', marginTop: 2 }}>{fix2(realtimeDevice?.wind_speed)} m/s</div></div>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px", borderRight: "1px solid #e8e8e8" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>温度</div><div style={{ fontSize: 14, fontWeight: 700, color: '#222', marginTop: 2 }}>{fix2(realtimeDevice?.environment_temperature)}℃</div></div>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px", borderRight: "1px solid #e8e8e8" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>降雨</div><div style={{ fontSize: 14, fontWeight: 700, color: '#222', marginTop: 2 }}>{getVal(realtimeDevice?.rainfall)}</div></div>
<div style={{ flex: 1, textAlign: 'center', whiteSpace: "nowrap", padding: "12px 4px" }}><div style={{ fontSize: 11, color: '#666', fontWeight: 600 }}>RTK</div><div style={{ fontSize: 14, fontWeight: 700, color: '#52c41a', marginTop: 2 }}>{realtimeDevice?.position_state?.is_fixed === 'fixing_successful' ? '固定' : '未固定'}</div></div>
</div>
<div style={{ width: 110, padding: '12px 4px', borderRadius: 8, background: '#f6ffed', border: '1px solid #b7eb8f', textAlign: 'center', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 4 }}>
<div className="flex items-center gap-1">

View File

@@ -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<any>(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,
}}
>
<CesiumMap devices={[mapDevices]} points={points} />
<CesiumMap
devices={[mapDevices]}
points={points}
realtimePosition={realtimePosition}
realtimeType="plane"
/>
</Card>
</Col>
@@ -544,8 +674,18 @@ export default function DeviceStatusPage({
<Descriptions.Item label={<><EnvironmentOutlined /> 飞行高度</>}>
<Text strong style={{ fontSize: 16 }}>{fix2((currentDroneData || uavState || device).height)} m</Text>
</Descriptions.Item>
<Descriptions.Item label="环境风速"><Text>{fix2((currentDroneData || uavState || device).wind_speed)} m/s</Text></Descriptions.Item>
<Descriptions.Item label="环境温度"><Text>{fix2((currentDroneData || uavState || device).environment_temperature)} ℃</Text></Descriptions.Item>
<Descriptions.Item label="水平速度">
<Text>{fix2(currentDroneData?.horizontal_speed) || '-'} m/s</Text>
</Descriptions.Item>
<Descriptions.Item label="垂直速度">
<Text>{fix2(currentDroneData?.vertical_speed) || '-'} m/s</Text>
</Descriptions.Item>
<Descriptions.Item label="环境风速">
<Text>{fix2((currentDroneData || uavState || device).wind_speed)} m/s</Text>
</Descriptions.Item>
<Descriptions.Item label="环境温度">
<Text>{fix2((currentDroneData || uavState || device).environment_temperature)} ℃</Text>
</Descriptions.Item>
<Descriptions.Item label="环境湿度">
<Text>{(currentDroneData?.humidity) || '-'}{currentDroneData?.humidity ? '%' : ''}</Text>
</Descriptions.Item>
@@ -555,10 +695,16 @@ export default function DeviceStatusPage({
<Descriptions.Item label="降雨状态">
<Tag color={(currentDroneData || uavState || device).rainfall === 'no_rain' ? 'green' : 'red'}>{getVal((currentDroneData || uavState || device).rainfall)}</Tag>
</Descriptions.Item>
<Descriptions.Item label="返航距离"><Text>{fix2((currentDroneData || uavState || device).home_distance)} m</Text></Descriptions.Item>
<Descriptions.Item label="航向角">
<Text>{fix2(currentDroneData?.heading)}°</Text>
</Descriptions.Item>
<Descriptions.Item label="飞行模式">
<Tag color="blue">
{currentDroneData?.mode_code !== undefined ? `模式 ${currentDroneData.mode_code}` : '-'}
</Tag>
</Descriptions.Item>
<Descriptions.Item label="无人机位置">
<Tag color="blue">
{currentDroneData?.drone_in_dock === 1 ? '在库' : '外出'}
@@ -580,11 +726,17 @@ export default function DeviceStatusPage({
<Descriptions.Item label="RTK 卫星数">
<Badge count={(currentDroneData || uavState || device).position_state?.rtk_number || 0} style={{ backgroundColor: '#52c41a' }} />
</Descriptions.Item>
<Descriptions.Item label="RTK 状态" span={2}>
<Tag color={(currentDroneData || uavState || device).position_state?.is_fixed === 'fixing_successful' ? 'success' : 'warning'}>
{(currentDroneData || uavState || device).position_state?.is_fixed === 'fixing_successful' ? 'RTK 已固定' : '未固定'}
<Descriptions.Item label="RTK 状态">
<Tag color={(currentDroneData || uavState || device).position_state?.is_fixed === 2 || (currentDroneData || uavState || device).position_state?.is_fixed === 'fixing_successful' ? 'success' : 'warning'}>
{(currentDroneData || uavState || device).position_state?.is_fixed === 2 || (currentDroneData || uavState || device).position_state?.is_fixed === 'fixing_successful' ? 'RTK 已固定' : '未固定'}
</Tag>
</Descriptions.Item>
<Descriptions.Item label="实时经纬度" span={2}>
<Text style={{ fontSize: '12px' }}>
{currentDroneData?.latitude && currentDroneData?.longitude ?
`${currentDroneData.latitude.toFixed(6)}, ${currentDroneData.longitude.toFixed(6)}` : '-'}
</Text>
</Descriptions.Item>
</Descriptions>
) : (
<Descriptions column={2} size="small" layout="vertical">

View File

@@ -293,8 +293,8 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
padding: '4px 8px',
fontSize: 10
}}>
<span>高度 {Number(device?.height)?.toFixed(2) || '-'}m</span>
<span>风速 {Number(device?.wind_speed?.toFixed(2) || '0')}m/s</span>
{/*<span>高度 {Number(device?.height)?.toFixed(2) || '-'}m</span>
<span>风速 {Number(device?.wind_speed?.toFixed(2) || '0')}m/s</span>*/}
</div>
)}
</div>

View File

@@ -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<any>(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(() => {