设备状态界面 无人机布局根据韩 建议优化

This commit is contained in:
mmc
2026-07-08 09:45:51 +08:00
parent 8dafe58541
commit a73ebc1bf4
2 changed files with 339 additions and 240 deletions

View File

@@ -16,17 +16,24 @@ interface DroneVideoPlayerProps {
defaultSource?: 'station' | 'drone';
videoSource?: 'station' | 'drone';
onSourceChange?: (source: 'station' | 'drone') => void;
// 新增:状态数据
statusData?: {
[key: string]: any;
};
// 新增:显示类型(用于区分显示机场状态还是无人机状态)
displayType?: 'station' | 'drone';
}
const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
device,
height = 160,
showSourceSelect = true,
dualVideo = false,
defaultSource = 'drone',
videoSource: propsVideoSource,
onSourceChange
onSourceChange,
statusData,
displayType
}) => {
const [internalVideoSource, setInternalVideoSource] = useState<'station' | 'drone'>(defaultSource);
const videoSource = propsVideoSource !== undefined ? propsVideoSource : internalVideoSource;
@@ -125,6 +132,15 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
const prevRef = useRef({
selectedCamera: '',
selectedLens: '',
deviceSn: '',
gatewaySn: '',
videoSource: ''
});
// 记住是否已经加载过,避免重复加载
const hasLoadedRef = useRef({
station: false,
drone: false
});
useEffect(() => {
@@ -133,37 +149,69 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
const prev = prevRef.current;
const currentCamera = selectedCamera;
const currentLens = selectedLens;
const currentDeviceSn = device?.device_sn || '';
const currentGatewaySn = device?.gateway_sn || '';
const currentVideoSource = videoSource;
// 双画面:只刷新【真正变化】的那一路,另一路不动
if (dualVideo) {
// 只有【机场摄像头】变化了,才刷新机场
if (currentCamera !== prev.selectedCamera) {
// 只有【机场摄像头】或设备变化了,才刷新机场
const stationChanged = currentCamera !== prev.selectedCamera || currentGatewaySn !== prev.gatewaySn;
if (stationChanged || !hasLoadedRef.current.station) {
setStationStreamData(null);
loadStationCamera(currentCamera);
hasLoadedRef.current.station = true;
}
// 只有【无人机镜头】变化了,才刷新无人机
if (currentLens !== prev.selectedLens) {
// 只有【无人机镜头】或设备变化了,才刷新无人机
const droneChanged = currentLens !== prev.selectedLens || currentDeviceSn !== prev.deviceSn;
if (droneChanged || !hasLoadedRef.current.drone) {
setDroneStreamData(null);
loadDroneLens(currentLens);
hasLoadedRef.current.drone = true;
}
// 保存最新值,供下一次对比
prevRef.current = {
selectedCamera: currentCamera,
selectedLens: currentLens,
deviceSn: currentDeviceSn,
gatewaySn: currentGatewaySn,
videoSource: currentVideoSource
};
return;
}
// 单画面:保持原有逻辑
// 单画面:只在真正变化时才重新加载
const sourceChanged = currentVideoSource !== prev.videoSource;
const deviceChanged = currentDeviceSn !== prev.deviceSn || currentGatewaySn !== prev.gatewaySn;
const lensChanged = currentLens !== prev.selectedLens;
const cameraChanged = currentCamera !== prev.selectedCamera;
if (videoSource === 'station') {
setStationStreamData(null);
loadStationCamera(currentCamera);
if ((sourceChanged || cameraChanged || deviceChanged) || !hasLoadedRef.current.station) {
setStationStreamData(null);
loadStationCamera(currentCamera);
hasLoadedRef.current.station = true;
hasLoadedRef.current.drone = false; // 重置另一边的加载状态
}
} else {
setDroneStreamData(null);
loadDroneLens(currentLens);
if ((sourceChanged || lensChanged || deviceChanged) || !hasLoadedRef.current.drone) {
setDroneStreamData(null);
loadDroneLens(currentLens);
hasLoadedRef.current.drone = true;
hasLoadedRef.current.station = false; // 重置另一边的加载状态
}
}
// 保存最新值,供下一次对比
prevRef.current = {
selectedCamera: currentCamera,
selectedLens: currentLens,
deviceSn: currentDeviceSn,
gatewaySn: currentGatewaySn,
videoSource: currentVideoSource
};
}, [
device,
videoSource,
@@ -179,8 +227,17 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
value: idx,
})) || [];
// 格式化数字
const formatNumber = (num: any, decimals = 2) => {
if (num === null || num === undefined || isNaN(num)) return '-';
return Number(num).toFixed(decimals);
};
const renderVideo = (type: 'drone' | 'station', streamData: any, loading: boolean) => {
const isDrone = type === 'drone';
const currentDisplayType = displayType || type;
const data = statusData;
return (
<div style={{
flex: 1,
@@ -291,9 +348,62 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
</div>
)}
{/* 状态详情叠加层 - 左上角 */}
{data && (
<div style={{
position: 'absolute',
top: 6,
left: 6,
background: 'rgba(0,0,0,0.7)',
color: '#fff',
padding: '8px 10px',
borderRadius: 6,
fontSize: 11,
zIndex: 5,
maxWidth: '40%',
backdropFilter: 'blur(4px)'
}}>
{currentDisplayType === 'station' ? (
<>
<div style={{ marginBottom: 4, fontWeight: 600, color: '#1890ff' }}>机场状态</div>
<div style={{ display: 'grid', gridTemplateColumns: 'auto auto', gap: '2px 8px' }}>
<span>温度:</span><span>{formatNumber(data.temperature || data.environment_temperature)}°C</span>
<span>湿度:</span><span>{data.humidity !== undefined ? `${data.humidity}%` : '-'}</span>
<span>风速:</span><span>{formatNumber(data.wind_speed)}m/s</span>
<span>降雨:</span><span style={{ color: data.rainfall === 'no_rain' || data.rainfall === 0 ? '#52c41a' : '#ff4d4f' }}>
{data.rainfall === 'no_rain' || data.rainfall === 0 ? '无' : '有'}
</span>
<span>机库:</span><span style={{ color: data.cover_state === 1 || data.cover_state === 'open' ? '#52c41a' : '#faad14' }}>
{data.cover_state === 1 || data.cover_state === 'open' ? '已开' : '已关'}
</span>
<span>无人机:</span><span style={{ color: data.drone_in_dock === 1 || data.drone_in_dock === true ? '#52c41a' : '#1890ff' }}>
{data.drone_in_dock === 1 || data.drone_in_dock === true ? '在库' : '外出'}
</span>
</div>
</>
) : (
<>
<div style={{ marginBottom: 4, fontWeight: 600, color: '#52c41a' }}>无人机状态</div>
<div style={{ display: 'grid', gridTemplateColumns: 'auto auto', gap: '2px 8px' }}>
<span>电量:</span><span style={{ color: (data.capacity_percent || 0) < 20 ? '#ff4d4f' : '#52c41a' }}>
{data.capacity_percent !== undefined ? `${data.capacity_percent}%` : '-'}
</span>
<span>高度:</span><span>{formatNumber(data.height)}m</span>
<span>水平速度:</span><span>{formatNumber(data.horizontal_speed)}m/s</span>
<span>垂直速度:</span><span>{formatNumber(data.vertical_speed)}m/s</span>
<span>航向角:</span><span>{formatNumber(data.heading)}°</span>
<span>GPS:</span><span>{data.position_state?.gps_number || 0}颗</span>
<span>RTK:</span><span style={{ color: data.position_state?.is_fixed === 2 || data.position_state?.is_fixed === 'fixing_successful' ? '#52c41a' : '#faad14' }}>
{data.position_state?.is_fixed === 2 || data.position_state?.is_fixed === 'fixing_successful' ? '已固定' : '未固定'}
</span>
</div>
</>
)}
</div>
)}
{/* 底部信息叠加 */}
{isDrone && device && (
{/* 底部信息叠加 - 显示经纬度 */}
{data && data.latitude !== undefined && data.longitude !== undefined && (
<div style={{
position: 'absolute',
bottom: 0,
@@ -302,12 +412,11 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
background: 'rgba(0,0,0,0.6)',
color: '#fff',
display: 'flex',
justifyContent: 'space-between',
justifyContent: 'center',
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>位置: {formatNumber(data.latitude, 6)}, {formatNumber(data.longitude, 6)}</span>
</div>
)}
</div>