无人机视频 、机场视频成功

This commit is contained in:
mmc
2026-05-25 10:47:12 +08:00
parent ce49fc25c8
commit 2d6d985544

View File

@@ -12,8 +12,8 @@ interface DroneVideoPlayerProps {
height?: number | string;
showSourceSelect?: boolean;
defaultSource?: 'station' | 'drone';
videoSource?: 'station' | 'drone'; // 新增:支持受控模式
onSourceChange?: (source: 'station' | 'drone') => void; // 新增:支持受控模式
videoSource?: 'station' | 'drone';
onSourceChange?: (source: 'station' | 'drone') => void;
}
const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
@@ -25,36 +25,29 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
onSourceChange
}) => {
const [internalVideoSource, setInternalVideoSource] = useState<'station' | 'drone'>(defaultSource);
// 如果提供了 propsVideoSource,则使用受控值,否则使用内部状态
const videoSource = propsVideoSource !== undefined ? propsVideoSource : internalVideoSource;
const setVideoSource = (source: 'station' | 'drone') => {
if (onSourceChange) {
onSourceChange(source);
} else {
setInternalVideoSource(source);
}
if (onSourceChange) onSourceChange(source);
else setInternalVideoSource(source);
};
const [selectedCamera, setSelectedCamera] = useState<'indoor' | 'outdoor'>('outdoor');
const [selectedCamera, setSelectedCamera] = useState<'indoor' | 'outdoor'>('indoor');
const [selectedLens, setSelectedLens] = useState<'wide' | 'zoom' | 'ir'>('wide');
// 当前选中的摄像头 index
const [selectedDroneCameraIdx, setSelectedDroneCameraIdx] = useState<number>(0);
const [streamData, setStreamData] = useState<any>(null);
const [loading, setLoading] = useState(false);
// 防抖锁:防止重复点击(核心优化)
const switchingLock = useRef(false);
// 加载机场摄像头
const loadStationCamera = async (cameraPosition: 'indoor' | 'outdoor') => {
setSelectedCamera(cameraPosition);
if (!device?.gateway_sn || switchingLock.current) return;
switchingLock.current = true;
setLoading(true);
try {
const cameraIndex = device.gateway_camera_list?.find(
(item: any) => item.camera_position === cameraPosition
)?.camera_index || "";
const cameraIndex = device.gateway_camera_list[0]?.camera_index || ""; // 目前先默认用第一个摄像头
if (!cameraIndex) {
setStreamData(null);
utils.message.warning(`未找到${cameraPosition === 'indoor' ? '室内' : '室外'}摄像头配置`);
@@ -69,11 +62,8 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
videoExpire: 7200,
});
if (res.code === 200) {
setStreamData(res.data);
} else {
utils.message.error(res.msg || '切换机场摄像头失败');
}
if (res.code == 200) setStreamData(res.data);
else utils.message.error(res.msg || '切换机场摄像头失败');
} catch (err) {
console.error('切换机场摄像头失败', err);
} finally {
@@ -82,23 +72,18 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
}
};
// 加载无人机镜头(优化版)
// 加载无人机镜头(使用当前选中的摄像头索引)
const loadDroneLens = async (lensType: 'wide' | 'zoom' | 'ir') => {
setSelectedLens(lensType);
if (!device?.device_sn || switchingLock.current) return;
// 如果已经是当前镜头,不重复请求(超级重要!)
if (selectedLens === lensType && streamData) {
return;
}
switchingLock.current = true;
setLoading(true);
try {
const cameraIndex = device.drone_camera_list?.[1]?.camera_index ||
device.drone_camera_list?.[0]?.camera_index || "";
const cameraList = device.drone_camera_list || [];
const targetCamera = cameraList[selectedDroneCameraIdx];
const cameraIndex = targetCamera?.camera_index || "";
if (!cameraIndex) {
utils.message.warning('未找到无人机相机配置');
@@ -113,11 +98,8 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
videoExpire: 7200,
});
if (res.code === 200) {
setStreamData(res.data);
} else {
utils.message.error(res.msg || '切换无人机镜头失败');
}
if (res.code === 200) setStreamData(res.data);
else utils.message.error(res.msg || '切换无人机镜头失败');
} catch (err) {
console.error('切换无人机镜头失败', err);
} finally {
@@ -126,7 +108,12 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
}
};
// 自动加载默认画面
// 切换无人机摄像头
const switchDroneCamera = (idx: number) => {
setSelectedDroneCameraIdx(idx);
};
// 自动加载
useEffect(() => {
if (device) {
setStreamData(null);
@@ -136,7 +123,13 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
loadDroneLens(selectedLens);
}
}
}, [device, videoSource]);
}, [device, videoSource, selectedDroneCameraIdx]);
// 生成摄像头下拉选项
const droneCameraOptions = device?.drone_camera_list?.map((item: any, idx: number) => ({
label: item.camera_name || `摄像头 ${idx + 1}`,
value: idx,
})) || [];
return (
<div style={{ width: '100%' }}>
@@ -163,7 +156,7 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
overflow: 'hidden',
background: '#000'
}}>
{/* 切换按钮覆盖层 */}
{/* 视频控制按钮 */}
<div style={{
position: 'absolute',
top: '6px',
@@ -171,92 +164,49 @@ const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
display: 'flex',
gap: '4px',
zIndex: 10,
flexWrap: 'wrap',
}}>
{videoSource === 'station' ? (
<>
<Button
disabled={loading}
size="small"
type="text"
style={{
background: selectedCamera === 'indoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff',
fontSize: '12px',
padding: '0 6px',
height: '22px',
borderRadius: '4px',
}}
onClick={() => loadStationCamera('indoor')}
>
室内
</Button>
<Button
disabled={loading}
size="small"
type="text"
style={{
background: selectedCamera === 'outdoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff',
fontSize: '12px',
padding: '0 6px',
height: '22px',
borderRadius: '4px',
}}
onClick={() => loadStationCamera('outdoor')}
>
室外
</Button>
<Button disabled={loading} size="small" type="text" style={{
background: selectedCamera === 'indoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff', fontSize: 12, padding: '0 6px', height: 22, borderRadius: 4
}} onClick={() => loadStationCamera('indoor')}>室内</Button>
<Button disabled={loading} size="small" type="text" style={{
background: selectedCamera === 'outdoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff', fontSize: 12, padding: '0 6px', height: 22, borderRadius: 4
}} onClick={() => loadStationCamera('outdoor')}>室外</Button>
</>
) : (
<>
<Button
disabled={loading}
size="small"
type="text"
style={{
background: selectedLens === 'wide' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff',
fontSize: '12px',
padding: '0 6px',
height: '22px',
borderRadius: '4px',
}}
onClick={() => loadDroneLens('wide')}
>
广角
</Button>
<Button
disabled={loading}
size="small"
type="text"
style={{
background: selectedLens === 'zoom' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff',
fontSize: '12px',
padding: '0 6px',
height: '22px',
borderRadius: '4px',
}}
onClick={() => loadDroneLens('zoom')}
>
变焦
</Button>
<Button
disabled={loading}
size="small"
type="text"
style={{
background: selectedLens === 'ir' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff',
fontSize: '12px',
padding: '0 6px',
height: '22px',
borderRadius: '4px',
}}
onClick={() => loadDroneLens('ir')}
>
红外
</Button>
{/* 动态摄像头下拉框 */}
{droneCameraOptions.length > 0 && (
<Select
disabled={loading}
size="small"
value={selectedDroneCameraIdx}
onChange={switchDroneCamera}
options={droneCameraOptions}
status={loading ? 'warning' : undefined}
/>
)}
<Button disabled={loading} size="small" type="text" style={{
background: selectedLens === 'wide' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff', fontSize: 12, padding: '0 6px', height: 22, borderRadius: 4
}} onClick={() => loadDroneLens('wide')}>广角</Button>
<Button disabled={loading} size="small" type="text" style={{
background: selectedLens === 'zoom' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff', fontSize: 12, padding: '0 6px', height: 22, borderRadius: 4
}} onClick={() => loadDroneLens('zoom')}>变焦</Button>
<Button disabled={loading} size="small" type="text" style={{
background: selectedLens === 'ir' ? '#1890ff' : 'rgba(0,0,0,0.4)',
color: '#fff', fontSize: 12, padding: '0 6px', height: 22, borderRadius: 4
}} onClick={() => loadDroneLens('ir')}>红外</Button>
</>
)}
</div>