From 368ae2d0c8ac1617fd2078f24f97ad6b28ccf0d6 Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Tue, 2 Jun 2026 10:48:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A7=E5=88=B6=E9=80=BB=E8=BE=91=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E5=90=8E=E5=8F=B0=E4=BF=9D=E6=B4=BB=20=E4=BB=A5?= =?UTF-8?q?=E9=98=B2=E5=93=AA=E4=B8=AA=E6=B5=8F=E8=A7=88=E5=99=A8=E4=B8=94?= =?UTF-8?q?=E5=90=8E=E5=8F=B0websocket=E6=96=AD=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/devices/RobotTaskPage.tsx | 17 ++- src/components/devices/deviceController.js | 160 ++++++++------------- 2 files changed, 69 insertions(+), 108 deletions(-) diff --git a/src/components/devices/RobotTaskPage.tsx b/src/components/devices/RobotTaskPage.tsx index 58a5f9a..1783f36 100644 --- a/src/components/devices/RobotTaskPage.tsx +++ b/src/components/devices/RobotTaskPage.tsx @@ -90,7 +90,6 @@ export default function RobotTaskPage() { const [isTaskModalOpen, setIsTaskModalOpen] = useState(false); const [selectedPathWork, setSelectedPathWork] = useState(null); - // 获取任务列表数据 // 获取任务列表数据 const fetchTaskList = async () => { try { @@ -151,7 +150,7 @@ export default function RobotTaskPage() { const fetchRouteList = async () => { if (!stationId) return; getRobotRouteList({ siteId: stationId }).then(res => { - if (res.code === 200 && res.data) { + if (res.code == 200 && res.data) { setRouteList(res.data); } else { setRouteList([]); @@ -599,13 +598,13 @@ export default function RobotTaskPage() { >
{routeList - .filter(item => { - if (!routeSearch) return true; - const name = (item.workName || item.name || '').toLowerCase(); - const id = String(item.id || '').toLowerCase(); - const key = routeSearch.toLowerCase(); - return name.includes(key) || id.includes(key); - }) + //.filter(item => { + // if (!routeSearch) return true; + // const name = (item.workName || item.name || '').toLowerCase(); + // const id = String(item.id || '').toLowerCase(); + // const key = routeSearch.toLowerCase(); + // return name.includes(key) || id.includes(key); + //}) .map(route => (
{ + this.connectDevice(this.currentDevice); + }, 500); + } + } + + // ========== 【集成】后台保活 ========== + startKeepAlive() { + this.keepAliveInterval = setInterval(() => { + if ( + this.connectedDeviceSn && + this.wsStatus !== 'connected' && + this.currentDevice + ) { + console.log('🔄 后台保活:尝试重连 WebSocket'); + this.connectDevice(this.currentDevice); + } + }, 8000); } destroy() { this.clearControlTimeout(); + // 清理保活 + if (this.keepAliveInterval) { + clearInterval(this.keepAliveInterval); + this.keepAliveInterval = null; + } + + // 移除监听 + document.removeEventListener('visibilitychange', this.handleVisibilityChange); + this.unSubMessage?.(); this.unSubStatus?.(); @@ -70,27 +103,25 @@ export default class DeviceController { handleWsStatus(status) { this.wsStatus = status; - this.onWsStatusChange?.(status); if (status === 'disconnected') { this.isControlled = false; - this.onControlStatusChange?.(false); } } async connectDevice(device) { if (!device) return; - const sn = - device.serialNumber || device.device_sn; + + // 保存当前设备,用于保活/重连 + this.currentDevice = device; + + const sn = device.serialNumber || device.device_sn; this.connectedDeviceSn = null; - this.wsDeviceInfo = {}; - this.isControlled = false; - this.onControlStatusChange?.(false); wsManager.close(); @@ -102,12 +133,8 @@ export default class DeviceController { token: this.userInfo.token, }); - this.hasControlRight = res?.code === 200; - - this.onHasControlRightChange?.( - this.hasControlRight, - ); + this.onHasControlRightChange?.(this.hasControlRight); if (!this.hasControlRight) { this.messageApi.warning('其他端正在控制'); @@ -115,13 +142,11 @@ export default class DeviceController { } wsManager.connect(sn, this.wsUrl); - this.connectedDeviceSn = sn; await this.getControl(sn); } catch (err) { console.error(err); - this.messageApi.error('权限校验失败'); } } @@ -133,35 +158,23 @@ export default class DeviceController { deviceId: serialnum, }); - - this.isControlled = - res?.code === 200 && - res?.data?.remoteControl === true; - - this.onControlStatusChange?.( - this.isControlled, - ); + this.isControlled = res?.code === 200 && res?.data?.remoteControl === true; + this.onControlStatusChange?.(this.isControlled); } catch (err) { console.error(err); - this.isControlled = false; - this.onControlStatusChange?.(false); } } requestControl() { - if (this.isRequestControl) { - return; - } - + if (this.isRequestControl) return; if (!this.hasControlRight) { this.messageApi.warning('无控制权限'); return; } this.isRequestControl = true; - this.onRequestControlStatusChange?.(true); this.messageApi.loading({ @@ -170,22 +183,14 @@ export default class DeviceController { duration: 0, }); - this.sendPermissionRequest( - true, - 'switchPermission', - ); - + this.sendPermissionRequest(true, 'switchPermission'); this.clearControlTimeout(); this.controlTimeout = setTimeout(() => { if (!this.isRequestControl) return; - this.isRequestControl = false; - this.onRequestControlStatusChange?.(false); - this.messageApi.destroy('control'); - this.messageApi.error('控制请求超时'); }, 10000); } @@ -202,10 +207,7 @@ export default class DeviceController { type, switchResult: data, token: this.userInfo.token, - deviceId: - type === 'switchPermission' - ? this.connectedDeviceSn - : this.requestDeviceId, + deviceId: type === 'switchPermission' ? this.connectedDeviceSn : this.requestDeviceId, userName: this.userInfo.username, }); } @@ -214,15 +216,11 @@ export default class DeviceController { switch (msg.type) { case 'deviceInfo': { const data = {}; - msg.data.forEach((item) => { data[item.name] = item.value; }); - this.wsDeviceInfo = data; - this.onDeviceInfoUpdate?.(data); - break; } @@ -230,64 +228,36 @@ export default class DeviceController { if (msg.isAuth) { this.getControl(this.connectedDeviceSn); } - break; } case 'switchResult': { - console.log(msg, "msg2222222222") - this.clearControlTimeout(); - this.isRequestControl = false; - this.onRequestControlStatusChange?.(false); - if ( - msg.success === false || - msg.switchResult === false - ) { + if (msg.success === false || msg.switchResult === false) { this.isControlled = false; - this.onControlStatusChange?.(false); - - this.messageApi.error({ - key: 'control', - content: msg.message || '控制失败', - }); - + this.messageApi.error({ key: 'control', content: msg.message || '控制失败' }); return; } this.isControlled = true; - this.onControlStatusChange?.(true); - - this.messageApi.success({ - key: 'control', - content: '控制成功', - }); - + this.messageApi.success({ key: 'control', content: '控制成功' }); break; } case 'switchPermission': { - console.log(msg, "msg111111111111111") - const platform = ( - msg.platform || '' - ).toLowerCase(); - - if (!platform || platform === 'web' || platform == "null") { - return; - } + const platform = (msg.platform || '').toLowerCase(); + if (!platform || platform === 'web' || platform === 'null') return; this.requestDeviceId = msg.deviceId; - this.onPermissionRequest?.({ title: '权限请求', content: `${msg.platform} 请求控制设备`, }); - break; } @@ -311,20 +281,12 @@ export default class DeviceController { } sendGamepad(state) { - if ( - !this.isControlled || - !this.connectedDeviceSn - ) { - return; - } - console.log(state, "state") - + if (!this.isControlled || !this.connectedDeviceSn) return; wsManager.send({ type: 'remoteControl', remoteData: state, token: this.userInfo.token, userName: this.userInfo.username, - }); } }