远程控制模块的控制状态放在外面
This commit is contained in:
@@ -34,6 +34,9 @@ import {
|
||||
EnvironmentOutlined,
|
||||
DeleteOutlined,
|
||||
SafetyOutlined,
|
||||
LockOutlined,
|
||||
UnlockOutlined,
|
||||
LoadingOutlined,
|
||||
} from '@ant-design/icons';
|
||||
|
||||
import {
|
||||
@@ -87,6 +90,8 @@ export default function DeviceStatusPage() {
|
||||
const [isLoopMode, setIsLoopMode] = useState(true);
|
||||
const controllerRef = useRef<DeviceController | null>(null);
|
||||
const isFirstConnect = useRef(true);
|
||||
// 标记本次会话是否已经发起过控制权限申请,避免手柄/方向盘高频更新时重复申请
|
||||
const hasRequestedControlRef = useRef(false);
|
||||
const [messageApi, contextHolder] = message.useMessage();
|
||||
|
||||
|
||||
@@ -633,18 +638,34 @@ export default function DeviceStatusPage() {
|
||||
}, [device, uavState, currentDroneData]);
|
||||
|
||||
const handleGamepadUpdate = useCallback((state) => {
|
||||
// 无控制权限时:不发送消息,且只发起一次权限申请(避免手柄/方向盘高频更新刷屏)
|
||||
if (!isControlled) {
|
||||
if (!hasRequestedControlRef.current && !connecting) {
|
||||
hasRequestedControlRef.current = true;
|
||||
handleControlModal();
|
||||
}
|
||||
return;
|
||||
}
|
||||
console.log('Gamepad state updated:', state);
|
||||
// 记录方向盘 xy 坐标,用于倒车影像叠加层
|
||||
if (state?.axes) {
|
||||
setSteerXY({ x: state.axes[0] || 0, y: state.axes[1] || 0 });
|
||||
}
|
||||
controllerRef.current?.sendGamepad(state);
|
||||
}, []);
|
||||
}, [isControlled, connecting]);
|
||||
|
||||
const handleControlModal = useCallback(() => {
|
||||
console.log('请求控制权限');
|
||||
controllerRef.current?.requestControl();
|
||||
}, []);
|
||||
|
||||
// 获得控制权限后,重置"已申请"标记,以便后续失去权限时可再次提示
|
||||
useEffect(() => {
|
||||
if (isControlled) {
|
||||
hasRequestedControlRef.current = false;
|
||||
}
|
||||
}, [isControlled]);
|
||||
|
||||
const handleOk = () => {
|
||||
controllerRef.current?.sendPermissionRequest(
|
||||
true,
|
||||
@@ -1423,24 +1444,66 @@ export default function DeviceStatusPage() {
|
||||
{t('devices.remoteControl')}
|
||||
</>
|
||||
}
|
||||
extra={
|
||||
<Space size={8}>
|
||||
{/* 权限状态标签 */}
|
||||
<Tag
|
||||
icon={connecting ? <LoadingOutlined /> : (isControlled ? <UnlockOutlined /> : <LockOutlined />)}
|
||||
color={connecting ? 'gold' : (isControlled ? 'green' : 'default')}
|
||||
style={{ margin: 0, fontSize: 12 }}
|
||||
>
|
||||
{connecting
|
||||
? t('devices.controlRequesting')
|
||||
: isControlled
|
||||
? t('devices.controlGranted')
|
||||
: t('devices.noControlRight')}
|
||||
</Tag>
|
||||
{/* 申请/释放控制按钮 */}
|
||||
{isControlled ? (
|
||||
<Button
|
||||
size="small"
|
||||
danger
|
||||
icon={<LockOutlined />}
|
||||
onClick={() => {
|
||||
controllerRef.current?.release?.();
|
||||
setIsControlled(false);
|
||||
}}
|
||||
>
|
||||
{t('devices.releaseControl')}
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
size="small"
|
||||
type="primary"
|
||||
icon={<UnlockOutlined />}
|
||||
loading={connecting}
|
||||
onClick={handleControlModal}
|
||||
>
|
||||
{t('devices.requestControl')}
|
||||
</Button>
|
||||
)}
|
||||
</Space>
|
||||
}
|
||||
variant="borderless"
|
||||
className="ds-card"
|
||||
style={{ borderRadius: 12, background: '#0f172a', flex: 1, minHeight: 0 }}
|
||||
style={{ borderRadius: 12, background: '#0f172a', flex: 1, minHeight: 0, position: 'relative' }}
|
||||
styles={{
|
||||
header: { color: '#fff' },
|
||||
body: {
|
||||
padding: 12,
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
flexWrap: 'wrap', // ✅ 新增:空间不足自动换行
|
||||
flexWrap: 'wrap',
|
||||
gap: 12,
|
||||
height: 'calc(100% - 57px)',
|
||||
overflow: 'auto', // ✅ 改成 auto,换行后内容不会被截断
|
||||
alignItems: 'center', // 垂直居中,换行后美观
|
||||
overflow: 'auto',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
},
|
||||
}}
|
||||
>
|
||||
|
||||
|
||||
{/* Xbox 手柄 - 左侧,缩小显示 */}
|
||||
<div
|
||||
style={{
|
||||
@@ -1448,17 +1511,13 @@ export default function DeviceStatusPage() {
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
flex: '0 0 auto',
|
||||
minWidth: 320, // ✅ 限制最小宽度,小于这个宽度就会换行
|
||||
minWidth: 320,
|
||||
transform: 'scale(0.82)',
|
||||
transformOrigin: 'left center',
|
||||
}}
|
||||
>
|
||||
<XboxController
|
||||
onGamepadUpdate={handleGamepadUpdate}
|
||||
handleControlModal={handleControlModal}
|
||||
isControlled={isControlled}
|
||||
hasControlRight={hasControlRight}
|
||||
isConnecting={connecting}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1466,7 +1525,6 @@ export default function DeviceStatusPage() {
|
||||
<div
|
||||
style={{
|
||||
flex: '1 1 auto',
|
||||
//minWidth: 300, // ✅ 最小宽度,触发换行阈值,可按需调整
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'center',
|
||||
|
||||
Reference in New Issue
Block a user