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

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