feat: 3d控制指令
This commit is contained in:
+635
-78
@@ -1,7 +1,6 @@
|
||||
import React from 'react';
|
||||
import React, { useMemo, useState, useCallback, useEffect, useRef } from 'react';
|
||||
import {
|
||||
CloseOutlined,
|
||||
RocketOutlined,
|
||||
VideoCameraOutlined,
|
||||
ArrowUpOutlined,
|
||||
ArrowDownOutlined,
|
||||
@@ -14,10 +13,20 @@ import {
|
||||
RotateRightOutlined,
|
||||
VerticalAlignTopOutlined,
|
||||
VerticalAlignBottomOutlined,
|
||||
DashboardOutlined,
|
||||
CompassOutlined
|
||||
CompassOutlined,
|
||||
WifiOutlined,
|
||||
LoadingOutlined,
|
||||
PlayCircleOutlined,
|
||||
PauseCircleOutlined,
|
||||
LinkOutlined,
|
||||
DisconnectOutlined,
|
||||
CaretRightOutlined,
|
||||
SwapOutlined,
|
||||
AimOutlined
|
||||
} from '@ant-design/icons';
|
||||
import { message } from 'antd';
|
||||
import { message, Tooltip, Modal, Switch, Tag } from 'antd';
|
||||
import { deviceApi } from '@/services/device';
|
||||
import { deviceControlWs } from '@/services/deviceControlWs';
|
||||
import styles from './LeftControlPanel.less';
|
||||
|
||||
interface LeftControlPanelProps {
|
||||
@@ -27,6 +36,18 @@ interface LeftControlPanelProps {
|
||||
realtimeData: any;
|
||||
}
|
||||
|
||||
// 飞行模式映射
|
||||
const FLIGHT_MODE_MAP: Record<number, string> = {
|
||||
0: '待机',
|
||||
1: '起飞',
|
||||
2: '降落',
|
||||
3: '悬停',
|
||||
4: '航点飞行',
|
||||
5: '返航',
|
||||
6: '手动控制',
|
||||
7: '紧急制动',
|
||||
};
|
||||
|
||||
const LeftControlPanel: React.FC<LeftControlPanelProps> = ({
|
||||
visible,
|
||||
onClose,
|
||||
@@ -34,26 +55,471 @@ const LeftControlPanel: React.FC<LeftControlPanelProps> = ({
|
||||
realtimeData
|
||||
}) => {
|
||||
const dockData = realtimeData?.rawData?.dockData;
|
||||
|
||||
// 模拟详细飞行数据 (Placeholder Data)
|
||||
const flightData = {
|
||||
pitch: -2.5,
|
||||
roll: 0.1,
|
||||
yaw: 125.4,
|
||||
hSpeed: 0.0,
|
||||
vSpeed: 0.0,
|
||||
altitude: realtimeData?.gps?.altitude?.toFixed(1) || '0.0',
|
||||
distance: 12.5, // 离家距离
|
||||
homeAlt: 0,
|
||||
satellites: dockData?.position_state?.gps_number || 12,
|
||||
rtkStatus: 'FIX',
|
||||
signal: 95,
|
||||
rcSignal: 100
|
||||
};
|
||||
const droneData = realtimeData?.rawData?.droneData;
|
||||
|
||||
const handleControl = (action: string) => {
|
||||
message.info(`发送指令: ${action}`);
|
||||
};
|
||||
// 从 droneData (msg_alarm) 获取无人机实时飞行数据
|
||||
// droneData 结构: 根级别有 latitude, longitude, height, attitude_pitch 等
|
||||
// 云台数据在 payload 索引下,如 droneData["98-0-0"]
|
||||
const flightData = useMemo(() => {
|
||||
// 调试日志 - 查看 droneData 的实际结构
|
||||
if (droneData) {
|
||||
console.log('[FlightData] droneData keys:', Object.keys(droneData));
|
||||
console.log('[FlightData] droneData.latitude:', droneData?.latitude);
|
||||
console.log('[FlightData] droneData.longitude:', droneData?.longitude);
|
||||
}
|
||||
|
||||
// 获取云台payload数据 - 遍历查找带有 gimbal_pitch 的子对象
|
||||
let gimbalPayload: any = {};
|
||||
if (droneData) {
|
||||
// 常见的 payload 索引
|
||||
const payloadKeys = ['98-0-0', '39-0-0', '42-0-0', '43-0-0', '52-0-0', '53-0-0', '165-0-0'];
|
||||
for (const key of payloadKeys) {
|
||||
if (droneData[key] && (droneData[key].gimbal_pitch !== undefined || droneData[key].gimbal_yaw !== undefined)) {
|
||||
gimbalPayload = droneData[key];
|
||||
console.log('[FlightData] 找到云台数据在 key:', key, gimbalPayload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 如果常见索引没找到,遍历所有数字格式的key
|
||||
if (!gimbalPayload.gimbal_pitch && !gimbalPayload.gimbal_yaw) {
|
||||
for (const key of Object.keys(droneData)) {
|
||||
if (/^\d+-\d+-\d+$/.test(key) && droneData[key]?.gimbal_pitch !== undefined) {
|
||||
gimbalPayload = droneData[key];
|
||||
console.log('[FlightData] 遍历找到云台数据在 key:', key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const pitch = droneData?.attitude_pitch ?? 0;
|
||||
const roll = droneData?.attitude_roll ?? 0;
|
||||
const yaw = droneData?.attitude_head ?? 0;
|
||||
const hSpeed = droneData?.horizontal_speed ?? droneData?.ground_speed ?? 0;
|
||||
const vSpeed = droneData?.vertical_speed ?? 0;
|
||||
const altitude = droneData?.height ?? droneData?.elevation ?? realtimeData?.gps?.altitude ?? 0;
|
||||
const distance = droneData?.home_distance ?? 0;
|
||||
|
||||
// GPS/RTK卫星数据
|
||||
const satellites = droneData?.position_state?.gps_number ?? dockData?.position_state?.gps_number ?? 0;
|
||||
const rtkSatellites = droneData?.position_state?.rtk_number ?? dockData?.position_state?.rtk_number ?? 0;
|
||||
const isFixed = droneData?.position_state?.is_fixed ?? dockData?.position_state?.is_fixed ?? 0;
|
||||
const rtkStatus = isFixed === 2 ? 'FIX' : (isFixed === 1 ? 'FLOAT' : (rtkSatellites > 0 ? 'FLOAT' : 'NONE'));
|
||||
|
||||
// 电池数据
|
||||
const batteryPercent = droneData?.battery?.capacity_percent ?? realtimeData?.battery ?? 0;
|
||||
const batteryVoltage = droneData?.battery?.voltage ?? 0;
|
||||
const batteryTemp = droneData?.battery?.temperature ?? 0;
|
||||
const remainFlightTime = droneData?.battery?.remain_flight_time ?? 0;
|
||||
|
||||
// 信号强度
|
||||
const signal = dockData?.wireless_link?.dongle_number ?? dockData?.sdr?.upward_quality ?? 0;
|
||||
|
||||
// 飞行模式
|
||||
const modeCode = droneData?.mode_code ?? 0;
|
||||
const flightMode = FLIGHT_MODE_MAP[modeCode] || `模式${modeCode}`;
|
||||
|
||||
// 云台角度 - 优先从 payload 中获取,否则从根级别获取
|
||||
const gimbalPitch = gimbalPayload?.gimbal_pitch ?? droneData?.gimbal_pitch ?? 0;
|
||||
const gimbalYaw = gimbalPayload?.gimbal_yaw ?? droneData?.gimbal_yaw ?? 0;
|
||||
const windSpeed = droneData?.wind_speed ?? 0;
|
||||
|
||||
// 无人机实时经纬度 - 直接从 droneData 根级别获取
|
||||
const droneLat = droneData?.latitude ?? 0;
|
||||
const droneLng = droneData?.longitude ?? 0;
|
||||
|
||||
return {
|
||||
pitch: Number(pitch).toFixed(1),
|
||||
roll: Number(roll).toFixed(1),
|
||||
yaw: Number(yaw).toFixed(1),
|
||||
hSpeed: Number(hSpeed).toFixed(1),
|
||||
vSpeed: Number(vSpeed).toFixed(1),
|
||||
altitude: Number(altitude).toFixed(1),
|
||||
distance: Number(distance).toFixed(1),
|
||||
satellites,
|
||||
rtkSatellites,
|
||||
rtkStatus,
|
||||
signal,
|
||||
batteryPercent,
|
||||
batteryVoltage: (batteryVoltage / 1000).toFixed(2),
|
||||
batteryTemp,
|
||||
remainFlightTime: Math.floor(remainFlightTime / 60),
|
||||
flightMode,
|
||||
modeCode,
|
||||
gimbalPitch: Number(gimbalPitch).toFixed(1),
|
||||
gimbalYaw: Number(gimbalYaw).toFixed(1),
|
||||
windSpeed: Number(windSpeed).toFixed(1),
|
||||
// 无人机实时位置
|
||||
latitude: Number(droneLat).toFixed(6),
|
||||
longitude: Number(droneLng).toFixed(6),
|
||||
};
|
||||
}, [droneData, dockData, realtimeData]);
|
||||
|
||||
// 指令发送状态
|
||||
const [loading, setLoading] = useState<string | null>(null);
|
||||
const [isRecording, setIsRecording] = useState(false);
|
||||
const [cameraMode, setCameraMode] = useState<'photo' | 'video'>('photo'); // 相机模式
|
||||
|
||||
// WebSocket连接状态
|
||||
const [wsConnected, setWsConnected] = useState(false);
|
||||
|
||||
// DRC模式状态 - 只需进入一次即可持续控制
|
||||
const [drcMode, setDrcMode] = useState(false);
|
||||
const drcModeRef = useRef(false); // 用于在回调中访问最新状态
|
||||
|
||||
// 获取设备SN
|
||||
const deviceSn = device?.deviceSn;
|
||||
|
||||
// WebSocket连接管理 + 无人机遥测数据订阅
|
||||
useEffect(() => {
|
||||
if (visible && deviceSn) {
|
||||
// 1. 连接实时控制WebSocket
|
||||
deviceControlWs.connect(deviceSn);
|
||||
|
||||
// 2. 订阅无人机遥测数据(msg_alarm)
|
||||
// 只有进入操控台时才订阅,因为大部分时间无人机不在线
|
||||
console.log('[LeftControlPanel] 订阅无人机遥测数据:', deviceSn);
|
||||
deviceApi.subscribeDroneTelemetry(deviceSn).then((res) => {
|
||||
if (res.success) {
|
||||
console.log('[LeftControlPanel] 无人机遥测数据订阅成功');
|
||||
} else {
|
||||
console.warn('[LeftControlPanel] 无人机遥测数据订阅失败:', res.message);
|
||||
}
|
||||
});
|
||||
|
||||
const unsubscribe = deviceControlWs.onStatusChange((connected) => {
|
||||
setWsConnected(connected);
|
||||
if (connected) {
|
||||
console.log('[LeftControlPanel] 实时控制WebSocket已连接');
|
||||
} else {
|
||||
console.log('[LeftControlPanel] 实时控制WebSocket已断开');
|
||||
// 断开时重置DRC模式状态
|
||||
setDrcMode(false);
|
||||
drcModeRef.current = false;
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
deviceControlWs.disconnect();
|
||||
// 清理DRC模式状态
|
||||
setDrcMode(false);
|
||||
drcModeRef.current = false;
|
||||
|
||||
// 3. 取消订阅无人机遥测数据
|
||||
console.log('[LeftControlPanel] 取消订阅无人机遥测数据:', deviceSn);
|
||||
deviceApi.unsubscribeDroneTelemetry(deviceSn).then((res) => {
|
||||
if (res.success) {
|
||||
console.log('[LeftControlPanel] 无人机遥测数据已取消订阅');
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}, [visible, deviceSn]);
|
||||
|
||||
// 进入DRC模式(只需调用一次)
|
||||
const enterDrcMode = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (drcModeRef.current) {
|
||||
return true; // 已经在DRC模式中
|
||||
}
|
||||
|
||||
setLoading('drc');
|
||||
try {
|
||||
const res = await deviceApi.enterDrcMode(deviceSn);
|
||||
if (res.code === 0) {
|
||||
setDrcMode(true);
|
||||
drcModeRef.current = true;
|
||||
message.success('已进入指令飞行模式');
|
||||
return true;
|
||||
} else {
|
||||
message.error(res.msg || '进入指令飞行模式失败');
|
||||
return false;
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '进入指令飞行模式失败');
|
||||
return false;
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn]);
|
||||
|
||||
// 一键返航
|
||||
const handleReturnHome = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
Modal.confirm({
|
||||
title: '确认返航',
|
||||
content: '确定要让无人机执行一键返航吗?',
|
||||
onOk: async () => {
|
||||
setLoading('return');
|
||||
try {
|
||||
const res = await deviceApi.returnHome(deviceSn);
|
||||
if (res.code === 0) {
|
||||
message.success('返航命令已发送');
|
||||
} else {
|
||||
message.error(res.msg || '返航命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '返航命令发送失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
},
|
||||
});
|
||||
}, [deviceSn]);
|
||||
|
||||
// 取消返航
|
||||
const handleCancelReturn = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading('cancelReturn');
|
||||
try {
|
||||
const res = await deviceApi.cancelReturnHome(deviceSn);
|
||||
if (res.code === 0) {
|
||||
message.success('取消返航命令已发送');
|
||||
} else {
|
||||
message.error(res.msg || '取消返航命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '取消返航命令发送失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn]);
|
||||
|
||||
// 航线暂停(紧急悬停)
|
||||
const handlePause = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading('pause');
|
||||
try {
|
||||
const res = await deviceApi.pause(deviceSn);
|
||||
if (res.code === 0) {
|
||||
message.success('航线暂停/悬停命令已发送');
|
||||
} else {
|
||||
message.error(res.msg || '暂停命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '暂停命令发送失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn]);
|
||||
|
||||
// 航线恢复
|
||||
const handleResume = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading('resume');
|
||||
try {
|
||||
const res = await deviceApi.resume(deviceSn);
|
||||
if (res.code === 0) {
|
||||
message.success('航线恢复命令已发送');
|
||||
} else {
|
||||
message.error(res.msg || '恢复命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '恢复命令发送失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn]);
|
||||
|
||||
// 切换相机模式
|
||||
const handleSwitchCameraMode = useCallback(async (mode: 'photo' | 'video') => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading('switchMode');
|
||||
try {
|
||||
// camera_mode: 0=拍照模式, 1=录像模式
|
||||
const cameraModeValue = mode === 'photo' ? 0 : 1;
|
||||
const res = await deviceApi.switchCameraMode(deviceSn, cameraModeValue);
|
||||
if (res?.code === 0) {
|
||||
setCameraMode(mode);
|
||||
message.success(`已切换到${mode === 'photo' ? '拍照' : '录像'}模式`);
|
||||
} else {
|
||||
message.error(res?.msg || '切换相机模式失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '切换相机模式失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn]);
|
||||
|
||||
// 拍照
|
||||
const handleTakePhoto = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果当前是录像模式,先切换到拍照模式
|
||||
if (cameraMode !== 'photo') {
|
||||
await handleSwitchCameraMode('photo');
|
||||
}
|
||||
|
||||
setLoading('photo');
|
||||
try {
|
||||
const res = await deviceApi.takePhoto(deviceSn);
|
||||
if (res.code === 0) {
|
||||
message.success('拍照命令已发送');
|
||||
} else {
|
||||
message.error(res.msg || '拍照命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '拍照命令发送失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn, cameraMode, handleSwitchCameraMode]);
|
||||
|
||||
// 开始录像
|
||||
const handleStartRecording = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
// 先切换到录像模式
|
||||
if (cameraMode !== 'video') {
|
||||
await handleSwitchCameraMode('video');
|
||||
}
|
||||
|
||||
setLoading('recording');
|
||||
try {
|
||||
const res = await deviceApi.startRecording(deviceSn);
|
||||
if (res.code === 0) {
|
||||
setIsRecording(true);
|
||||
message.success('开始录像命令已发送');
|
||||
} else {
|
||||
message.error(res.msg || '开始录像命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '开始录像命令发送失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn, cameraMode, handleSwitchCameraMode]);
|
||||
|
||||
// 停止录像
|
||||
const handleStopRecording = useCallback(async () => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading('recording');
|
||||
try {
|
||||
const res = await deviceApi.stopRecording(deviceSn);
|
||||
if (res.code === 0) {
|
||||
setIsRecording(false);
|
||||
message.success('停止录像命令已发送');
|
||||
} else {
|
||||
message.error(res.msg || '停止录像命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '停止录像命令发送失败');
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}, [deviceSn]);
|
||||
|
||||
// DRC杆量控制 (使用WebSocket实时发送,需先进入DRC模式)
|
||||
const handleStickControl = useCallback(async (direction: string) => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!wsConnected) {
|
||||
message.warning('实时控制通道未连接,正在重连...');
|
||||
deviceControlWs.connect(deviceSn);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否已进入DRC模式,如果没有则先进入
|
||||
if (!drcModeRef.current) {
|
||||
const entered = await enterDrcMode();
|
||||
if (!entered) return;
|
||||
}
|
||||
|
||||
const offset = 300;
|
||||
let params = { roll: 1024, pitch: 1024, throttle: 1024, yaw: 1024 };
|
||||
|
||||
switch (direction) {
|
||||
case 'up': params.throttle = 1024 + offset; break;
|
||||
case 'down': params.throttle = 1024 - offset; break;
|
||||
case 'left': params.yaw = 1024 - offset; break;
|
||||
case 'right': params.yaw = 1024 + offset; break;
|
||||
case 'forward': params.pitch = 1024 + offset; break;
|
||||
case 'backward': params.pitch = 1024 - offset; break;
|
||||
case 'moveLeft': params.roll = 1024 - offset; break;
|
||||
case 'moveRight': params.roll = 1024 + offset; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
const success = deviceControlWs.sendStickControl(params);
|
||||
if (!success) {
|
||||
message.error('摇杆指令发送失败');
|
||||
}
|
||||
}, [deviceSn, wsConnected, enterDrcMode]);
|
||||
|
||||
// 云台控制 (使用WebSocket实时发送,需先进入DRC模式)
|
||||
const handleGimbalControl = useCallback(async (direction: string) => {
|
||||
if (!deviceSn) {
|
||||
message.error('未选择设备');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!wsConnected) {
|
||||
message.warning('实时控制通道未连接,正在重连...');
|
||||
deviceControlWs.connect(deviceSn);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否已进入DRC模式,如果没有则先进入
|
||||
if (!drcModeRef.current) {
|
||||
const entered = await enterDrcMode();
|
||||
if (!entered) return;
|
||||
}
|
||||
|
||||
const speed = 20;
|
||||
let params = { pitchSpeed: 0, yawSpeed: 0, locked: false };
|
||||
|
||||
switch (direction) {
|
||||
case 'up': params.pitchSpeed = speed; break;
|
||||
case 'down': params.pitchSpeed = -speed; break;
|
||||
case 'left': params.yawSpeed = -speed; break;
|
||||
case 'right': params.yawSpeed = speed; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
const success = deviceControlWs.sendGimbalControl(params);
|
||||
if (!success) {
|
||||
message.error('云台指令发送失败');
|
||||
}
|
||||
}, [deviceSn, wsConnected, enterDrcMode]);
|
||||
|
||||
return (
|
||||
<div className={`${styles.leftPanel} ${visible ? styles.visible : ''}`}>
|
||||
@@ -76,12 +542,32 @@ const LeftControlPanel: React.FC<LeftControlPanelProps> = ({
|
||||
</div>
|
||||
<div className={styles.hudOverlay}>
|
||||
<div className={styles.crosshair} />
|
||||
{/* HUD左上角 - 飞行模式和状态 */}
|
||||
<div style={{position: 'absolute', top: 10, left: 10, color: '#0f0', fontSize: 12, fontFamily: 'monospace'}}>
|
||||
CAM: Main<br/>ISO: Auto<br/>EXP: 0.0
|
||||
MODE: {flightData.flightMode}<br/>
|
||||
<span style={{color: droneData ? '#0f0' : '#f00'}}>
|
||||
{droneData ? '● 遥测正常' : '○ 等待遥测'}
|
||||
</span><br/>
|
||||
GIMBAL: P{flightData.gimbalPitch}° Y{flightData.gimbalYaw}°
|
||||
</div>
|
||||
{/* HUD左下角 - 无人机GPS坐标和高度 */}
|
||||
<div style={{position: 'absolute', bottom: 30, left: 10, color: '#0ff', fontSize: 11, fontFamily: 'monospace'}}>
|
||||
<AimOutlined style={{marginRight: 4}} />
|
||||
LAT: {flightData.latitude}°<br/>
|
||||
<span style={{marginLeft: 16}}>LNG: {flightData.longitude}°</span><br/>
|
||||
<span style={{marginLeft: 16}}>ALT: {flightData.altitude}m</span>
|
||||
</div>
|
||||
{/* HUD右上角 - 电池信息 */}
|
||||
<div style={{position: 'absolute', top: 10, right: 10, color: '#fff', fontSize: 12, fontFamily: 'monospace', textAlign: 'right'}}>
|
||||
<span style={{color: Number(flightData.batteryPercent) > 30 ? '#0f0' : '#f00'}}>
|
||||
BAT: {flightData.batteryPercent}%
|
||||
</span><br/>
|
||||
{flightData.batteryVoltage}V / {flightData.batteryTemp}°C<br/>
|
||||
<span style={{color: '#0ff'}}>剩余: {flightData.remainFlightTime}min</span>
|
||||
</div>
|
||||
{/* HUD底部状态条 */}
|
||||
<div style={{
|
||||
position: 'absolute', bottom: 10, left: 10, right: 10,
|
||||
position: 'absolute', bottom: 10, left: 10, right: 10,
|
||||
display: 'flex', justifyContent: 'space-between',
|
||||
color: '#fff', fontSize: 12, textShadow: '0 0 2px #000'
|
||||
}}>
|
||||
@@ -89,73 +575,139 @@ const LeftControlPanel: React.FC<LeftControlPanelProps> = ({
|
||||
<span>D: {flightData.distance}m</span>
|
||||
<span>H.S: {flightData.hSpeed}m/s</span>
|
||||
<span>V.S: {flightData.vSpeed}m/s</span>
|
||||
<span>WIND: {flightData.windSpeed}m/s</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
{/* 飞行仪表盘区域 */}
|
||||
<div className={styles.flightDashboard}>
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}><CompassOutlined /> PITCH</div>
|
||||
<div className={styles.dashValue}>{flightData.pitch}°</div>
|
||||
</div>
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}><CompassOutlined /> ROLL</div>
|
||||
<div className={styles.dashValue}>{flightData.roll}°</div>
|
||||
</div>
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}><CompassOutlined /> YAW</div>
|
||||
<div className={styles.dashValue}>{flightData.yaw}°</div>
|
||||
</div>
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}>RTK</div>
|
||||
<div className={styles.dashValue} style={{color: '#00ff88'}}>{flightData.rtkStatus}</div>
|
||||
</div>
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}>SAT</div>
|
||||
<div className={styles.dashValue}>{flightData.satellites}</div>
|
||||
</div>
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}>LINK</div>
|
||||
<div className={styles.dashValue}>{flightData.signal}%</div>
|
||||
</div>
|
||||
<Tooltip title="俯仰角 - 无人机前后倾斜角度">
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}><CompassOutlined /> PITCH</div>
|
||||
<div className={styles.dashValue}>{flightData.pitch}°</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
<Tooltip title="横滚角 - 无人机左右倾斜角度">
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}><CompassOutlined /> ROLL</div>
|
||||
<div className={styles.dashValue}>{flightData.roll}°</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
<Tooltip title="偏航角 - 无人机航向角度">
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}><CompassOutlined /> YAW</div>
|
||||
<div className={styles.dashValue}>{flightData.yaw}°</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
<Tooltip title={`RTK定位状态 (RTK卫星数: ${flightData.rtkSatellites})`}>
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}>RTK</div>
|
||||
<div className={styles.dashValue} style={{
|
||||
color: flightData.rtkStatus === 'FIX' ? '#00ff88' :
|
||||
flightData.rtkStatus === 'FLOAT' ? '#ffaa00' : '#ff4444'
|
||||
}}>
|
||||
{flightData.rtkStatus}
|
||||
</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
<Tooltip title="GPS卫星数量">
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}>SAT</div>
|
||||
<div className={styles.dashValue} style={{
|
||||
color: flightData.satellites >= 10 ? '#00ff88' :
|
||||
flightData.satellites >= 6 ? '#ffaa00' : '#ff4444'
|
||||
}}>
|
||||
{flightData.satellites}
|
||||
</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
<Tooltip title="图传信号质量">
|
||||
<div className={styles.dashItem}>
|
||||
<div className={styles.dashLabel}><WifiOutlined /> LINK</div>
|
||||
<div className={styles.dashValue} style={{
|
||||
color: flightData.signal >= 70 ? '#00ff88' :
|
||||
flightData.signal >= 40 ? '#ffaa00' : '#ff4444'
|
||||
}}>
|
||||
{flightData.signal}%
|
||||
</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
</div>
|
||||
|
||||
<div className={styles.controlsContent}>
|
||||
{/* 主要动作 */}
|
||||
{/* 飞行控制指令 */}
|
||||
<div className={styles.controlGroup}>
|
||||
<div className={styles.groupTitle}>快捷指令 / ACTIONS</div>
|
||||
<div className={styles.groupTitle}>飞行控制 / FLIGHT CONTROL</div>
|
||||
<div className={styles.flightActions}>
|
||||
<button
|
||||
onClick={() => handleControl('一键起飞')}
|
||||
disabled={dockData?.mode_code === 1 || dockData?.drone_in_dock !== 1}
|
||||
>
|
||||
<RocketOutlined /> 一键起飞
|
||||
<button onClick={handleReturnHome} disabled={loading === 'return'}>
|
||||
{loading === 'return' ? <LoadingOutlined /> : <HomeOutlined />} 一键返航
|
||||
</button>
|
||||
<button onClick={() => handleControl('一键返航')}>
|
||||
<HomeOutlined /> 一键返航
|
||||
<button onClick={handleCancelReturn} disabled={loading === 'cancelReturn'}>
|
||||
{loading === 'cancelReturn' ? <LoadingOutlined /> : <StopOutlined />} 取消返航
|
||||
</button>
|
||||
<button className={styles.danger} onClick={() => handleControl('紧急悬停')}>
|
||||
<StopOutlined /> 紧急悬停
|
||||
<button className={styles.danger} onClick={handlePause} disabled={loading === 'pause'}>
|
||||
{loading === 'pause' ? <LoadingOutlined /> : <PauseCircleOutlined />} 航线暂停
|
||||
</button>
|
||||
<button onClick={() => handleControl('拍照')}>
|
||||
<CameraOutlined /> 拍照
|
||||
<button onClick={handleResume} disabled={loading === 'resume'}>
|
||||
{loading === 'resume' ? <LoadingOutlined /> : <CaretRightOutlined />} 航线恢复
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 相机控制 */}
|
||||
<div className={styles.controlGroup}>
|
||||
<div className={styles.groupTitle}>
|
||||
相机控制 / CAMERA
|
||||
<Tag color={cameraMode === 'photo' ? 'blue' : 'red'} style={{ marginLeft: 8 }}>
|
||||
{cameraMode === 'photo' ? '拍照模式' : '录像模式'}
|
||||
</Tag>
|
||||
</div>
|
||||
<div className={styles.flightActions}>
|
||||
<button onClick={handleTakePhoto} disabled={loading === 'photo'}>
|
||||
{loading === 'photo' ? <LoadingOutlined /> : <CameraOutlined />} 拍照
|
||||
</button>
|
||||
{!isRecording ? (
|
||||
<button onClick={handleStartRecording} disabled={loading === 'recording'}>
|
||||
{loading === 'recording' ? <LoadingOutlined /> : <PlayCircleOutlined />} 开始录像
|
||||
</button>
|
||||
) : (
|
||||
<button className={styles.danger} onClick={handleStopRecording} disabled={loading === 'recording'}>
|
||||
{loading === 'recording' ? <LoadingOutlined /> : <StopOutlined />} 停止录像
|
||||
</button>
|
||||
)}
|
||||
<button onClick={() => handleSwitchCameraMode(cameraMode === 'photo' ? 'video' : 'photo')} disabled={loading === 'switchMode'}>
|
||||
{loading === 'switchMode' ? <LoadingOutlined /> : <SwapOutlined />} 切换模式
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 飞行姿态控制 (模拟双摇杆) */}
|
||||
<div className={styles.controlGroup}>
|
||||
<div className={styles.groupTitle}>姿态控制 / ATTITUDE CONTROL</div>
|
||||
<div className={styles.groupTitle}>
|
||||
姿态控制 / ATTITUDE CONTROL
|
||||
<Tooltip title={wsConnected ? '实时控制通道已连接' : '实时控制通道未连接'}>
|
||||
<span style={{ marginLeft: 8, fontSize: 12 }}>
|
||||
{wsConnected ? (
|
||||
<LinkOutlined style={{ color: '#00ff88' }} />
|
||||
) : (
|
||||
<DisconnectOutlined style={{ color: '#ff4444' }} />
|
||||
)}
|
||||
</span>
|
||||
</Tooltip>
|
||||
{drcMode && (
|
||||
<Tag color="green" style={{ marginLeft: 8 }}>DRC模式</Tag>
|
||||
)}
|
||||
</div>
|
||||
<div className={styles.joystickArea}>
|
||||
{/* 左手:升降 + 转向 */}
|
||||
<div className={styles.joystickGroup}>
|
||||
<div className={styles.joystickLabel}>升降 & 偏航 (Left)</div>
|
||||
<div className={styles.dpad}>
|
||||
<button className={styles.up} onClick={() => handleControl('上升')} title="上升"><VerticalAlignTopOutlined /></button>
|
||||
<button className={styles.left} onClick={() => handleControl('左转')} title="左转"><RotateLeftOutlined /></button>
|
||||
<button className={styles.right} onClick={() => handleControl('右转')} title="右转"><RotateRightOutlined /></button>
|
||||
<button className={styles.down} onClick={() => handleControl('下降')} title="下降"><VerticalAlignBottomOutlined /></button>
|
||||
<button className={styles.up} onClick={() => handleStickControl('up')} title="上升"><VerticalAlignTopOutlined /></button>
|
||||
<button className={styles.left} onClick={() => handleStickControl('left')} title="左转"><RotateLeftOutlined /></button>
|
||||
<button className={styles.right} onClick={() => handleStickControl('right')} title="右转"><RotateRightOutlined /></button>
|
||||
<button className={styles.down} onClick={() => handleStickControl('down')} title="下降"><VerticalAlignBottomOutlined /></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -163,10 +715,10 @@ const LeftControlPanel: React.FC<LeftControlPanelProps> = ({
|
||||
<div className={styles.joystickGroup}>
|
||||
<div className={styles.joystickLabel}>水平移动 (Right)</div>
|
||||
<div className={styles.dpad}>
|
||||
<button className={styles.up} onClick={() => handleControl('前进')} title="前进"><ArrowUpOutlined /></button>
|
||||
<button className={styles.left} onClick={() => handleControl('向左飞')} title="向左"><ArrowLeftOutlined /></button>
|
||||
<button className={styles.right} onClick={() => handleControl('向右飞')} title="向右"><ArrowRightOutlined /></button>
|
||||
<button className={styles.down} onClick={() => handleControl('后退')} title="后退"><ArrowDownOutlined /></button>
|
||||
<button className={styles.up} onClick={() => handleStickControl('forward')} title="前进"><ArrowUpOutlined /></button>
|
||||
<button className={styles.left} onClick={() => handleStickControl('moveLeft')} title="向左"><ArrowLeftOutlined /></button>
|
||||
<button className={styles.right} onClick={() => handleStickControl('moveRight')} title="向右"><ArrowRightOutlined /></button>
|
||||
<button className={styles.down} onClick={() => handleStickControl('backward')} title="后退"><ArrowDownOutlined /></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -174,15 +726,20 @@ const LeftControlPanel: React.FC<LeftControlPanelProps> = ({
|
||||
|
||||
{/* 云台控制 */}
|
||||
<div className={styles.controlGroup}>
|
||||
<div className={styles.groupTitle}>云台控制 / GIMBAL</div>
|
||||
<div className={styles.groupTitle}>
|
||||
云台控制 / GIMBAL
|
||||
{drcMode && (
|
||||
<Tag color="green" style={{ marginLeft: 8 }}>DRC模式</Tag>
|
||||
)}
|
||||
</div>
|
||||
<div className={styles.joystickArea}>
|
||||
<div className={styles.joystickGroup} style={{flex: 1}}>
|
||||
<div className={styles.joystickLabel}>云台方向</div>
|
||||
<div className={styles.dpad}>
|
||||
<button className={styles.up} onClick={() => handleControl('云台向上')}><ArrowUpOutlined /></button>
|
||||
<button className={styles.left} onClick={() => handleControl('云台向左')}><ArrowLeftOutlined /></button>
|
||||
<button className={styles.right} onClick={() => handleControl('云台向右')}><ArrowRightOutlined /></button>
|
||||
<button className={styles.down} onClick={() => handleControl('云台向下')}><ArrowDownOutlined /></button>
|
||||
<button className={styles.up} onClick={() => handleGimbalControl('up')} title="云台上仰"><ArrowUpOutlined /></button>
|
||||
<button className={styles.left} onClick={() => handleGimbalControl('left')} title="云台左转"><ArrowLeftOutlined /></button>
|
||||
<button className={styles.right} onClick={() => handleGimbalControl('right')} title="云台右转"><ArrowRightOutlined /></button>
|
||||
<button className={styles.down} onClick={() => handleGimbalControl('down')} title="云台下俯"><ArrowDownOutlined /></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+79
-7
@@ -1,5 +1,5 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Spin, Empty, message, Tooltip, Tabs, Progress } from 'antd';
|
||||
import { Spin, Empty, message, Tooltip, Tabs, Progress, Modal } from 'antd';
|
||||
import {
|
||||
CloseOutlined,
|
||||
EnvironmentOutlined,
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
ThunderboltOutlined,
|
||||
DashboardOutlined,
|
||||
SignalFilled,
|
||||
LoadingOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { deviceApi } from '@/services/device';
|
||||
import { ordersApi } from '@/services/orders';
|
||||
@@ -24,15 +25,16 @@ interface RightPanelProps {
|
||||
onControlClick: (device: any, realtimeData: any) => void;
|
||||
}
|
||||
|
||||
const RightPanel: React.FC<RightPanelProps> = ({
|
||||
visible,
|
||||
tourism,
|
||||
const RightPanel: React.FC<RightPanelProps> = ({
|
||||
visible,
|
||||
tourism,
|
||||
onClose,
|
||||
onControlClick
|
||||
onControlClick
|
||||
}) => {
|
||||
const [deviceList, setDeviceList] = useState<any[]>([]);
|
||||
const [selectedDeviceSn, setSelectedDeviceSn] = useState<string>('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [takeoffLoading, setTakeoffLoading] = useState(false);
|
||||
|
||||
const { realtimeData, sseConnected, connectSSE, disconnectSSE } = useDeviceSSE();
|
||||
|
||||
@@ -85,6 +87,73 @@ const RightPanel: React.FC<RightPanelProps> = ({
|
||||
|
||||
const formatValue = (val: any, unit: string = '') => (val !== undefined && val !== null ? `${val}${unit}` : '-');
|
||||
|
||||
// 一键起飞
|
||||
const handleTakeoff = () => {
|
||||
const dockData = realtimeData?.rawData?.dockData;
|
||||
|
||||
// 检查机库状态
|
||||
if (dockData?.mode_code === 1) {
|
||||
message.warning('机库正在执行任务,无法起飞');
|
||||
return;
|
||||
}
|
||||
|
||||
// 只有明确知道无人机不在库时才阻止(值为0时)
|
||||
if (dockData?.drone_in_dock === 0) {
|
||||
message.warning('无人机不在库,无法起飞');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取机库位置信息
|
||||
const dockLatitude = dockData?.latitude || realtimeData?.gps?.latitude;
|
||||
const dockLongitude = dockData?.longitude || realtimeData?.gps?.longitude;
|
||||
const dockHeight = dockData?.height || realtimeData?.gps?.altitude || 0;
|
||||
|
||||
if (!dockLatitude || !dockLongitude) {
|
||||
message.error('无法获取机库位置信息');
|
||||
return;
|
||||
}
|
||||
|
||||
// 弹窗确认
|
||||
Modal.confirm({
|
||||
title: '确认一键起飞',
|
||||
content: (
|
||||
<div>
|
||||
<p>无人机将自动开机并起飞到指定位置</p>
|
||||
<p style={{ fontSize: 12, color: '#666' }}>
|
||||
目标坐标: {dockLatitude.toFixed(6)}, {dockLongitude.toFixed(6)}<br />
|
||||
目标高度: {(dockHeight + 30).toFixed(1)}m (机库高度+30m)
|
||||
</p>
|
||||
</div>
|
||||
),
|
||||
okText: '确认起飞',
|
||||
cancelText: '取消',
|
||||
onOk: async () => {
|
||||
setTakeoffLoading(true);
|
||||
try {
|
||||
const res = await deviceApi.takeoff({
|
||||
deviceSn: selectedDeviceSn,
|
||||
targetLatitude: dockLatitude,
|
||||
targetLongitude: dockLongitude,
|
||||
targetHeight: dockHeight + 30,
|
||||
securityTakeoffHeight: 50,
|
||||
rthAltitude: 50,
|
||||
maxSpeed: 5,
|
||||
});
|
||||
|
||||
if (res.code === 0) {
|
||||
message.success('起飞命令已发送,无人机将自动开机并起飞');
|
||||
} else {
|
||||
message.error(res.msg || '起飞命令发送失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '起飞命令发送失败');
|
||||
} finally {
|
||||
setTakeoffLoading(false);
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const renderItem = (label: string, value: any, unit: string = '', status: 'normal'|'warning'|'error'|'success' = 'normal') => (
|
||||
<div className={styles.hudItem}>
|
||||
<div className={styles.label}>{label}</div>
|
||||
@@ -190,8 +259,11 @@ const RightPanel: React.FC<RightPanelProps> = ({
|
||||
<button className={styles.primary} onClick={() => onControlClick(selectedDevice, realtimeData)}>
|
||||
<VideoCameraOutlined /> 进入操控台
|
||||
</button>
|
||||
<button disabled={dockData?.mode_code === 1 || dockData?.drone_in_dock !== 1} onClick={() => message.info('一键起飞指令发送')}>
|
||||
<RocketOutlined /> 一键起飞
|
||||
<button
|
||||
disabled={takeoffLoading || dockData?.mode_code === 1 || dockData?.drone_in_dock === 0}
|
||||
onClick={handleTakeoff}
|
||||
>
|
||||
{takeoffLoading ? <LoadingOutlined /> : <RocketOutlined />} 一键起飞
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+57
-1
@@ -14,6 +14,60 @@ export interface DockData {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// 无人机遥测数据接口定义
|
||||
export interface DroneData {
|
||||
// 飞行姿态
|
||||
attitude_pitch?: number; // 俯仰角 (度)
|
||||
attitude_roll?: number; // 横滚角 (度)
|
||||
attitude_head?: number; // 偏航角/航向 (度)
|
||||
|
||||
// 速度
|
||||
horizontal_speed?: number; // 水平速度 (m/s)
|
||||
vertical_speed?: number; // 垂直速度 (m/s)
|
||||
ground_speed?: number; // 地面速度 (m/s)
|
||||
|
||||
// 位置
|
||||
latitude?: number; // 纬度
|
||||
longitude?: number; // 经度
|
||||
height?: number; // 高度 (m)
|
||||
elevation?: number; // 海拔高度 (m)
|
||||
home_distance?: number; // 距Home点距离 (m)
|
||||
|
||||
// 电池
|
||||
battery?: {
|
||||
capacity_percent?: number; // 电量百分比
|
||||
voltage?: number; // 电压 (mV)
|
||||
temperature?: number; // 温度 (°C)
|
||||
remain_flight_time?: number; // 剩余飞行时间 (s)
|
||||
};
|
||||
|
||||
// 飞行模式
|
||||
mode_code?: number; // 飞行模式代码
|
||||
gear?: number; // 档位
|
||||
|
||||
// RTK/GPS
|
||||
position_state?: {
|
||||
rtk_number?: number; // RTK 卫星数
|
||||
gps_number?: number; // GPS 卫星数
|
||||
is_fixed?: number; // 是否固定 (1=FIX)
|
||||
};
|
||||
|
||||
// 云台
|
||||
gimbal_pitch?: number; // 云台俯仰角
|
||||
gimbal_roll?: number; // 云台横滚角
|
||||
gimbal_yaw?: number; // 云台偏航角
|
||||
|
||||
// 其他
|
||||
wind_speed?: number; // 风速 (m/s)
|
||||
wind_direction?: number; // 风向 (度)
|
||||
storage?: {
|
||||
total?: number; // 存储总容量
|
||||
used?: number; // 已使用容量
|
||||
};
|
||||
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// 实时数据
|
||||
export interface RealtimeData {
|
||||
deviceSn: string;
|
||||
@@ -26,7 +80,9 @@ export interface RealtimeData {
|
||||
flightStatus?: string;
|
||||
timestamp?: number;
|
||||
rawData?: {
|
||||
dockData?: any;
|
||||
dockData?: any; // 机库状态数据 (fire_alarm)
|
||||
droneData?: DroneData; // 无人机遥测数据 (msg_alarm)
|
||||
msgAlarmData?: any; // msg_alarm 完整数据
|
||||
[key: string]: any;
|
||||
};
|
||||
}
|
||||
|
||||
+26
-5
@@ -13,7 +13,10 @@ export interface TilesetConfig {
|
||||
position?: { // 模型位置调整
|
||||
longitude?: number; // 经度偏移
|
||||
latitude?: number; // 纬度偏移
|
||||
height?: number; // 高度偏移
|
||||
height?: number; // 高度偏移(通用)
|
||||
cesiumHeight?: number; // Cesium Ion地形对应的高度偏移
|
||||
arcgisHeight?: number; // ArcGIS地形对应的高度偏移
|
||||
terrainType?: string; // 高度标定时使用的地形服务: cesium_ion/arcgis/ellipsoid
|
||||
};
|
||||
transform?: { // 模型变换
|
||||
scale?: number; // 缩放比例
|
||||
@@ -42,7 +45,7 @@ export interface TilesetState {
|
||||
// Hook返回类型
|
||||
export interface UseTilesetLoaderResult {
|
||||
tilesets: Map<string, TilesetState>;
|
||||
loadTileset: (config: TilesetConfig, viewer: any, Cesium: any) => Promise<any>;
|
||||
loadTileset: (config: TilesetConfig, viewer: any, Cesium: any, currentTerrainType?: string) => Promise<any>;
|
||||
unloadTileset: (id: string, viewer: any) => void;
|
||||
unloadAllTilesets: (viewer: any) => void;
|
||||
flyToTileset: (id: string, viewer: any, Cesium: any) => void;
|
||||
@@ -56,11 +59,16 @@ export const useTilesetLoader = (): UseTilesetLoaderResult => {
|
||||
|
||||
/**
|
||||
* 加载3D Tiles模型
|
||||
* @param config 模型配置
|
||||
* @param viewer Cesium Viewer实例
|
||||
* @param Cesium Cesium模块
|
||||
* @param currentTerrainType 当前使用的地形类型,用于选择对应的高度偏移
|
||||
*/
|
||||
const loadTileset = useCallback(async (
|
||||
config: TilesetConfig,
|
||||
viewer: any,
|
||||
Cesium: any
|
||||
Cesium: any,
|
||||
currentTerrainType?: string
|
||||
): Promise<any> => {
|
||||
if (!viewer || !Cesium) {
|
||||
console.error('Viewer或Cesium未初始化');
|
||||
@@ -138,9 +146,22 @@ export const useTilesetLoader = (): UseTilesetLoaderResult => {
|
||||
});
|
||||
});
|
||||
|
||||
// 模型位置调整 - 只有当位置偏移不为0时才调整
|
||||
// 模型位置调整 - 根据当前地形类型选择对应的高度偏移
|
||||
if (config.position) {
|
||||
const { longitude = 0, latitude = 0, height = 0 } = config.position;
|
||||
const { longitude = 0, latitude = 0, cesiumHeight, arcgisHeight } = config.position;
|
||||
|
||||
// 根据当前地形类型选择对应的高度偏移
|
||||
let height = config.position.height || 0; // 默认使用通用高度
|
||||
if (currentTerrainType === 'cesium_ion' && cesiumHeight !== undefined && cesiumHeight !== null) {
|
||||
height = cesiumHeight;
|
||||
console.log(`模型 [${config.name}] 使用 Cesium Ion 高度偏移: ${height}`);
|
||||
} else if (currentTerrainType === 'arcgis' && arcgisHeight !== undefined && arcgisHeight !== null) {
|
||||
height = arcgisHeight;
|
||||
console.log(`模型 [${config.name}] 使用 ArcGIS 高度偏移: ${height}`);
|
||||
} else {
|
||||
console.log(`模型 [${config.name}] 使用通用高度偏移: ${height}`);
|
||||
}
|
||||
|
||||
// 只有当有非零偏移时才应用位置调整
|
||||
if (longitude !== 0 || latitude !== 0 || height !== 0) {
|
||||
const boundingSphere = tileset.boundingSphere;
|
||||
|
||||
+165
-26
@@ -33,8 +33,10 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
|
||||
// 3D Tiles 模型管理
|
||||
const [tilesetPanelVisible, setTilesetPanelVisible] = useState(false);
|
||||
const [currentTourismModels, setCurrentTourismModels] = useState<TilesetConfig[]>([]); // 当前景区的模型
|
||||
const [currentTourismModels, setCurrentTourismModels] = useState<TilesetConfig[]>([]); // 当前景区的所有模型配置
|
||||
const [modelLoading, setModelLoading] = useState(false);
|
||||
const [terrainLoading, setTerrainLoading] = useState(false); // 是否正在加载高程地形
|
||||
const terrainLoadedRef = useRef(false); // 是否已加载高程地形
|
||||
const {
|
||||
tilesets,
|
||||
loadTileset,
|
||||
@@ -79,7 +81,100 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
});
|
||||
}, []);
|
||||
|
||||
// 加载景区绑定的模型,返回是否有模型
|
||||
/**
|
||||
* 动态切换到高程地形
|
||||
* 在进入景区详情、加载3D模型时调用
|
||||
* @param preferredTerrainType 优先使用的地形类型(从模型配置中获取)
|
||||
*/
|
||||
const switchToElevationTerrain = useCallback(async (preferredTerrainType?: string): Promise<string> => {
|
||||
if (!viewerRef.current || !Cesium) {
|
||||
return 'ellipsoid';
|
||||
}
|
||||
|
||||
// 如果已加载且地形类型匹配,跳过
|
||||
if (terrainLoadedRef.current && !preferredTerrainType) {
|
||||
return 'loaded';
|
||||
}
|
||||
|
||||
setTerrainLoading(true);
|
||||
console.log('开始加载高程地形...', preferredTerrainType ? `优先使用: ${preferredTerrainType}` : '');
|
||||
|
||||
let loadedTerrainType = 'ellipsoid';
|
||||
|
||||
// 根据优先地形类型决定加载顺序
|
||||
if (preferredTerrainType === 'arcgis') {
|
||||
// 优先尝试 ArcGIS
|
||||
try {
|
||||
const terrainProvider = await Cesium.ArcGISTiledElevationTerrainProvider.fromUrl(
|
||||
'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer'
|
||||
);
|
||||
console.log('高程地形加载成功: ArcGIS World Elevation (根据模型配置)');
|
||||
viewerRef.current.terrainProvider = terrainProvider;
|
||||
viewerRef.current.scene.globe.depthTestAgainstTerrain = true;
|
||||
terrainLoadedRef.current = true;
|
||||
loadedTerrainType = 'arcgis';
|
||||
} catch (arcgisError) {
|
||||
console.warn('ArcGIS 加载失败,尝试 Cesium Ion:', arcgisError);
|
||||
try {
|
||||
const terrainProvider = await Cesium.CesiumTerrainProvider.fromIonAssetId(1, {
|
||||
requestVertexNormals: true,
|
||||
requestWaterMask: true,
|
||||
});
|
||||
console.log('高程地形加载成功: Cesium World Terrain (备用)');
|
||||
viewerRef.current.terrainProvider = terrainProvider;
|
||||
viewerRef.current.scene.globe.depthTestAgainstTerrain = true;
|
||||
terrainLoadedRef.current = true;
|
||||
loadedTerrainType = 'cesium_ion';
|
||||
message.warning('ArcGIS地形加载失败,已切换到Cesium Ion,模型高度可能有偏差');
|
||||
} catch (cesiumError) {
|
||||
console.warn('Cesium Ion 也加载失败,保持椭球地形:', cesiumError);
|
||||
loadedTerrainType = 'ellipsoid';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 默认优先尝试 Cesium Ion
|
||||
try {
|
||||
const terrainProvider = await Cesium.CesiumTerrainProvider.fromIonAssetId(1, {
|
||||
requestVertexNormals: true,
|
||||
requestWaterMask: true,
|
||||
});
|
||||
console.log('高程地形加载成功: Cesium World Terrain');
|
||||
viewerRef.current.terrainProvider = terrainProvider;
|
||||
viewerRef.current.scene.globe.depthTestAgainstTerrain = true;
|
||||
terrainLoadedRef.current = true;
|
||||
loadedTerrainType = 'cesium_ion';
|
||||
} catch (cesiumError) {
|
||||
console.warn('Cesium World Terrain 加载失败,尝试 ArcGIS:', cesiumError);
|
||||
try {
|
||||
const terrainProvider = await Cesium.ArcGISTiledElevationTerrainProvider.fromUrl(
|
||||
'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer'
|
||||
);
|
||||
console.log('高程地形加载成功: ArcGIS World Elevation');
|
||||
viewerRef.current.terrainProvider = terrainProvider;
|
||||
viewerRef.current.scene.globe.depthTestAgainstTerrain = true;
|
||||
terrainLoadedRef.current = true;
|
||||
loadedTerrainType = 'arcgis';
|
||||
if (preferredTerrainType === 'cesium_ion') {
|
||||
message.warning('Cesium Ion地形加载失败,已切换到ArcGIS,模型高度可能有偏差');
|
||||
}
|
||||
} catch (arcgisError) {
|
||||
console.warn('ArcGIS World Elevation 也加载失败,保持椭球地形:', arcgisError);
|
||||
loadedTerrainType = 'ellipsoid';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setTerrainLoading(false);
|
||||
return loadedTerrainType;
|
||||
}, []);
|
||||
|
||||
/**
|
||||
* 加载景区绑定的模型
|
||||
* 策略:
|
||||
* 1. 默认只加载第一个模型(通常是主模型)
|
||||
* 2. 其他模型存储在 currentTourismModels 中,可通过模型面板手动加载
|
||||
* 3. 如果模型数量较少(<=2个),自动加载所有模型
|
||||
*/
|
||||
const loadTourismModels = useCallback(async (tourismId: number): Promise<boolean> => {
|
||||
if (!viewerRef.current || !Cesium) {
|
||||
console.log('Viewer或Cesium未准备好');
|
||||
@@ -101,39 +196,63 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
if (res.resp_code === 0 && modelList && modelList.length > 0) {
|
||||
const configs = modelList.map((model: any) => convertToTilesetConfig(model));
|
||||
console.log('转换后的模型配置:', configs);
|
||||
|
||||
// 保存所有模型配置(供模型面板使用)
|
||||
setCurrentTourismModels(configs);
|
||||
|
||||
// 【重要】加载模型前,先切换到高程地形
|
||||
// 从第一个模型的配置中获取地形类型(用于保持与高度标定时一致的地形)
|
||||
const firstModelTerrainType = configs[0]?.position?.terrainType;
|
||||
console.log('第一个模型的地形类型配置:', firstModelTerrainType);
|
||||
const loadedTerrainType = await switchToElevationTerrain(firstModelTerrainType);
|
||||
console.log('实际加载的地形类型:', loadedTerrainType);
|
||||
|
||||
// 确定要自动加载的模型数量
|
||||
// 策略:模型数量 <= 2 时全部加载,否则只加载第一个
|
||||
const autoLoadCount = configs.length <= 2 ? configs.length : 1;
|
||||
const configsToLoad = configs.slice(0, autoLoadCount);
|
||||
|
||||
if (configs.length > autoLoadCount) {
|
||||
message.info(`景区共有 ${configs.length} 个模型,已自动加载第 1 个,其他可通过模型面板加载`);
|
||||
}
|
||||
|
||||
let loadedCount = 0;
|
||||
// 加载所有模型
|
||||
for (const config of configs) {
|
||||
let firstTileset: any = null;
|
||||
|
||||
// 加载指定数量的模型,传入实际加载的地形类型
|
||||
for (const config of configsToLoad) {
|
||||
console.log('准备加载模型:', config.name, 'URL:', config.url);
|
||||
if (config.url) {
|
||||
const tileset = await loadTileset(config, viewerRef.current, Cesium);
|
||||
const tileset = await loadTileset(config, viewerRef.current, Cesium, loadedTerrainType);
|
||||
if (tileset) {
|
||||
loadedCount++;
|
||||
message.success(`模型 [${config.name}] 加载成功`);
|
||||
// 加载成功后飞到第一个模型位置
|
||||
if (loadedCount === 1) {
|
||||
// 使用 viewer.flyTo 自动飞到模型
|
||||
try {
|
||||
await viewerRef.current.flyTo(tileset, {
|
||||
duration: 2.0,
|
||||
offset: new Cesium.HeadingPitchRange(
|
||||
Cesium.Math.toRadians(0),
|
||||
Cesium.Math.toRadians(-45),
|
||||
0
|
||||
),
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn('flyTo 失败,尝试 zoomTo:', e);
|
||||
await viewerRef.current.zoomTo(tileset);
|
||||
}
|
||||
firstTileset = tileset;
|
||||
}
|
||||
message.success(`模型 [${config.name}] 加载成功`);
|
||||
}
|
||||
} else {
|
||||
console.warn('模型URL为空,跳过加载:', config.name);
|
||||
}
|
||||
}
|
||||
|
||||
// 飞到第一个加载成功的模型
|
||||
if (firstTileset) {
|
||||
try {
|
||||
await viewerRef.current.flyTo(firstTileset, {
|
||||
duration: 2.0,
|
||||
offset: new Cesium.HeadingPitchRange(
|
||||
Cesium.Math.toRadians(0),
|
||||
Cesium.Math.toRadians(-45),
|
||||
0
|
||||
),
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn('flyTo 失败,尝试 zoomTo:', e);
|
||||
await viewerRef.current.zoomTo(firstTileset);
|
||||
}
|
||||
}
|
||||
|
||||
setModelLoading(false);
|
||||
return loadedCount > 0;
|
||||
} else {
|
||||
@@ -146,7 +265,7 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
setModelLoading(false);
|
||||
return false;
|
||||
}
|
||||
}, [loadTileset, unloadAllTilesets]);
|
||||
}, [loadTileset, unloadAllTilesets, switchToElevationTerrain]);
|
||||
|
||||
// 处理景区点击
|
||||
const handleTourismSelect = useCallback((tourism: any) => {
|
||||
@@ -234,6 +353,13 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
Cesium = cesiumModule;
|
||||
const cesiumBaseUrl = process.env.CESIUM_BASE_URL || '/';
|
||||
Cesium.buildModuleUrl.setBaseUrl(`${cesiumBaseUrl}cesium/`);
|
||||
|
||||
// 设置 Cesium Ion Access Token(用于加载 Cesium World Terrain)
|
||||
const cesiumToken = process.env.REACT_APP_CESIUM_TOKEN;
|
||||
if (cesiumToken) {
|
||||
Cesium.Ion.defaultAccessToken = cesiumToken;
|
||||
}
|
||||
|
||||
if (!document.querySelector('link[href*="cesium"]')) {
|
||||
const link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
@@ -244,8 +370,18 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
|
||||
if (!containerRef.current) return;
|
||||
|
||||
// 【重要】初始化时使用椭球地形(无高程),快速加载
|
||||
// 原因:
|
||||
// 1. 全球地形加载较慢,影响首屏体验
|
||||
// 2. Cesium World Terrain 精度约30m,与大疆建图的厘米级精度差异大
|
||||
// 会导致模型部分贴合、部分悬浮、部分嵌入地面的问题
|
||||
// 3. 大疆建图的模型本身包含了高精度的地形信息,不需要额外的地形数据
|
||||
// 解决方案:使用椭球地形 + 模型高度偏移微调
|
||||
const terrainProvider = new Cesium.EllipsoidTerrainProvider();
|
||||
console.log('地图仪表盘: 使用椭球地形(快速加载,模型自带高程)');
|
||||
|
||||
viewerRef.current = new Cesium.Viewer(containerRef.current, {
|
||||
terrainProvider: new Cesium.EllipsoidTerrainProvider(),
|
||||
terrainProvider: terrainProvider,
|
||||
scene3DOnly: false,
|
||||
shouldAnimate: true,
|
||||
animation: false,
|
||||
@@ -262,6 +398,9 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
maximumRenderTimeChange: Infinity,
|
||||
});
|
||||
|
||||
// 使用椭球地形时不需要深度测试
|
||||
// viewerRef.current.scene.globe.depthTestAgainstTerrain = true;
|
||||
|
||||
viewerRef.current.imageryLayers.remove(viewerRef.current.imageryLayers.get(0));
|
||||
viewerRef.current.scene.globe.baseColor = Cesium.Color.fromCssColorString('#1e4d7b');
|
||||
|
||||
@@ -442,8 +581,8 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 模型加载中提示 */}
|
||||
{modelLoading && (
|
||||
{/* 地形/模型加载中提示 */}
|
||||
{(terrainLoading || modelLoading) && (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
@@ -458,7 +597,7 @@ const CesiumContainer: React.FC<CesiumContainerProps> = (props) => {
|
||||
zIndex: 800,
|
||||
}}
|
||||
>
|
||||
正在加载3D模型...
|
||||
{terrainLoading ? '正在加载高程地形...' : '正在加载3D模型...'}
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user