diff --git a/src/components/WebSocketManager.ts b/src/components/WebSocketManager.ts index 9358164..d7559eb 100644 --- a/src/components/WebSocketManager.ts +++ b/src/components/WebSocketManager.ts @@ -191,7 +191,7 @@ class WebSocketManager { console.log('WebSocket not connected, cannot send message'); return false; } - console.log('WebSocket send message'); + console.log('WebSocket send message', JSON.stringify(data)); this.ws.send(JSON.stringify(data)); return true; } diff --git a/src/components/devices/DeviceOverviewPage.tsx b/src/components/devices/DeviceOverviewPage.tsx index 5bddfdc..f9e2109 100644 --- a/src/components/devices/DeviceOverviewPage.tsx +++ b/src/components/devices/DeviceOverviewPage.tsx @@ -35,6 +35,7 @@ import envConfig from '../../../env'; import utils from '@/lib/utils.ts'; import { useUnitFormatter } from '@/lib/units'; import { useTranslation } from 'react-i18next'; +import { sendEmergencyStop } from './emergencyStopControl'; const { Content } = Layout; const { Text, Title } = Typography; @@ -543,7 +544,11 @@ export default function DeviceOverviewPage() { const handlePause = () => message.info(t('devices.taskPaused')); const handleReturn = () => message.info(t('devices.fixed2ReturnHomeFixed2CommandIssuedFixed2')); const handleRecharge = () => message.info(t('devices.commandIssuedFixed2')); - const handleEmergencyStop = () => message.error(t('devices.s73')); + // 急停:点击时直接下发 button7(绕过控制权限校验),并提示已触发 + const handleEmergencyStop = () => { + sendEmergencyStop(userInfo); + message.error(t('devices.s73')); + }; const fix2 = (val) => { if (val === null || val === undefined || isNaN(val)) return '-'; diff --git a/src/components/devices/DeviceStatusPage.tsx b/src/components/devices/DeviceStatusPage.tsx index 1e7c4ba..2233127 100644 --- a/src/components/devices/DeviceStatusPage.tsx +++ b/src/components/devices/DeviceStatusPage.tsx @@ -671,6 +671,7 @@ export default function DeviceStatusPage() { }, [device, uavState, currentDroneData]); const handleGamepadUpdate = useCallback((state) => { + console.log('handleGamepadUpdate called with state:', isControlled); // 无控制权限时:不发送消息,且只发起一次权限申请(避免手柄/方向盘高频更新刷屏) if (!isControlled) { if (!hasRequestedControlRef.current && !connecting) { diff --git a/src/components/devices/RobotFeatureButtons.tsx b/src/components/devices/RobotFeatureButtons.tsx index 7fc399d..5688f8a 100644 --- a/src/components/devices/RobotFeatureButtons.tsx +++ b/src/components/devices/RobotFeatureButtons.tsx @@ -1,5 +1,8 @@ import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; +import { useSelector } from 'react-redux'; +import { RootState } from '@/store'; +import { sendEmergencyStop } from './emergencyStopControl'; import { ShieldAlert, Zap, @@ -48,6 +51,7 @@ const RobotFeatureButtons: React.FC = ({ className = '', }) => { const { t } = useTranslation(); + const { userInfo } = useSelector((state: RootState) => state.user); const [active, setActive] = useState>(new Set(activeFeatures)); const handleClick = (feature: RobotFeature) => { @@ -56,6 +60,8 @@ const RobotFeatureButtons: React.FC = ({ if (next) nextSet.add(feature); else nextSet.delete(feature); setActive(nextSet); + // 急停:点击时直接下发 button7(绕过控制权限校验) + if (feature === 'emergencyStop') sendEmergencyStop(userInfo); onToggle?.(feature, next); }; diff --git a/src/components/devices/XboxController.tsx b/src/components/devices/XboxController.tsx index 6c8abad..d8981c1 100644 --- a/src/components/devices/XboxController.tsx +++ b/src/components/devices/XboxController.tsx @@ -14,6 +14,9 @@ const XboxController = ({ onGamepadUpdate }) => { }); const requestRef = useRef(null); + // 用 ref 保存最新的回调,避免 RAF 循环闭包捕获旧 props 导致权限判断失效 + const onGamepadUpdateRef = useRef(onGamepadUpdate); + onGamepadUpdateRef.current = onGamepadUpdate; const DEAD_ZONE = 0.08; const normalizeAxis = (v: number) => @@ -46,7 +49,7 @@ const XboxController = ({ onGamepadUpdate }) => { now - lastSendRef.current > SEND_INTERVAL // payload !== lastPayloadRef.current ) { - onGamepadUpdate(state); // ✅ 只有这里才发 + onGamepadUpdateRef.current(state); // ✅ 只有这里才发 lastSendRef.current = now; lastPayloadRef.current = payload; } diff --git a/src/components/devices/emergencyStopControl.ts b/src/components/devices/emergencyStopControl.ts new file mode 100644 index 0000000..db1a002 --- /dev/null +++ b/src/components/devices/emergencyStopControl.ts @@ -0,0 +1,28 @@ +import { wsManager } from '../WebSocketManager'; + +// 急停对应手柄 button7(buttons 数组下标 7) +const EMERGENCY_STOP_BUTTON_INDEX = 7; + +// 构建单帧遥控数据:仅急停位按下/松开,其余按键与摇杆轴保持空闲 +const buildFrame = (pressed: boolean) => { + const buttons = new Array(17).fill(false); + buttons[EMERGENCY_STOP_BUTTON_INDEX] = pressed; + return { buttons, axes: [0, 0, 0, 0], connected: true, ts: Date.now() }; +}; + +/** + * 单击急停:绕过控制权限校验,直接经 WebSocket 下发 button7; + * 约 300ms 后自动补发一帧复位数据,模拟按下再松开。 + */ +export const sendEmergencyStop = (userInfo?: any) => { + const send = (pressed: boolean) => + wsManager.send({ + type: 'remoteControl', + remoteData: buildFrame(pressed), + token: userInfo?.token, + userName: userInfo?.username, + }); + + send(true); + setTimeout(() => send(false), 300); +};