diff --git a/src/components/VideoCanvas.tsx b/src/components/VideoCanvas.tsx new file mode 100644 index 0000000..99a44c4 --- /dev/null +++ b/src/components/VideoCanvas.tsx @@ -0,0 +1,166 @@ +import React, { useEffect, useRef } from 'react'; + +interface Props { + video: HTMLVideoElement; + idx: number; + title: string; + className?: string; + noCrop?: boolean; +} + +const VideoCanvas: React.FC = ({ video, idx, title, className, noCrop }) => { + const canvasRef = useRef(null); + const rafRef = useRef(null); + + const stopRender = () => { + if (rafRef.current) { + cancelAnimationFrame(rafRef.current); + rafRef.current = null; + } + clearCanvas(); + }; + const clearCanvas = () => { + const canvas = canvasRef.current; + if (!canvas) return; + const ctx = canvas.getContext('2d'); + if (!ctx) return; + ctx.fillStyle = '#000'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + }; + + // 🔥 绘制汽车影像辅助线 + const drawGuideLines = (ctx: CanvasRenderingContext2D, canvas: HTMLCanvasElement) => { + const w = canvas.width; + const h = canvas.height; + + ctx.save(); + ctx.lineWidth = 2; + ctx.strokeStyle = 'rgba(0, 229, 255, 0.7)'; + ctx.globalAlpha = 0.8; + + // 根据视角画不同辅助线 + if (title.includes('前') || title.includes('front')) { + // 前视角引导线(梯形) + ctx.beginPath(); + ctx.moveTo(w * 0.25, h); + ctx.lineTo(w * 0.45, h * 0.3); + ctx.moveTo(w * 0.75, h); + ctx.lineTo(w * 0.55, h * 0.3); + ctx.stroke(); + + // 中线 + ctx.beginPath(); + ctx.setLineDash([5, 5]); + ctx.moveTo(w / 2, h); + ctx.lineTo(w / 2, h * 0.3); + ctx.stroke(); + ctx.setLineDash([]); + } + + if (title.includes('后') || title.includes('back')) { + // 后视角倒车线(扇形/倒车轨迹) + ctx.beginPath(); + ctx.moveTo(w * 0.3, h * 0.7); + ctx.lineTo(w * 0.15, h * 0.3); + ctx.moveTo(w * 0.7, h * 0.7); + ctx.lineTo(w * 0.85, h * 0.3); + ctx.stroke(); + + // 倒车底线 + ctx.beginPath(); + ctx.moveTo(w * 0.2, h * 0.8); + ctx.lineTo(w * 0.8, h * 0.8); + ctx.stroke(); + } + + if (title.includes('左') || title.includes('left')) { + // 左侧线 + ctx.beginPath(); + ctx.moveTo(w * 0.8, h); + ctx.lineTo(w * 0.8, 0); + ctx.moveTo(w * 0.7, h); + ctx.lineTo(w * 0.7, 0); + ctx.stroke(); + } + + if (title.includes('右') || title.includes('right')) { + // 右侧线 + ctx.beginPath(); + ctx.moveTo(w * 0.2, h); + ctx.lineTo(w * 0.2, 0); + ctx.moveTo(w * 0.3, h); + ctx.lineTo(w * 0.3, 0); + ctx.stroke(); + } + + ctx.restore(); + }; + + const startDraw = () => { + const canvas = canvasRef.current; + if (!canvas) return; + const ctx = canvas.getContext('2d'); + if (!ctx) return; + clearCanvas(); + + const render = () => { + if (video.readyState >= 2) { + const vw = video.videoWidth; + const vh = video.videoHeight; + + let sx = 0; + let sy = 0; + let sw = vw; + let sh = vh; + + if (!noCrop) { + sw = vw / 2; + sh = vh / 2; + switch (idx) { + case 2: sx = sw; break; + case 3: sy = sh; break; + case 4: sx = sw; sy = sh; break; + } + } + + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.drawImage(video, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height); + + // 🔥 绘制辅助线 + //drawGuideLines(ctx, canvas); + } + rafRef.current = requestAnimationFrame(render); + }; + render(); + }; + + useEffect(() => { + stopRender(); + if (!video) return; + startDraw(); + return stopRender; + }, [video, idx, title]); + + return ( +
+ + +
+
+ {title} +
+
+ + LIVE +
+
+
+ ); +}; + +export default VideoCanvas; diff --git a/src/components/VideoPlayer.tsx.tsx b/src/components/VideoPlayer.tsx.tsx new file mode 100644 index 0000000..39850cf --- /dev/null +++ b/src/components/VideoPlayer.tsx.tsx @@ -0,0 +1,50 @@ +import React, { useState, useEffect } from 'react'; +import Empty from './Empty'; +import VideoCanvas from './VideoCanvas'; + +interface VideoPlayerProps { + deviceId: string; + idx: number; + title?: string; + className?: string; + noCrop?: boolean; + + video?: HTMLVideoElement; + error?: boolean; + ready?: boolean; +} +const VideoPlayer: React.FC = ({ + deviceId, + idx, + title, + className = '', + noCrop, + video, error, ready +}) => { + + + const viewTitles = ['前视图', '后视图', '左视图', '右视图']; + const currentTitle = title || viewTitles[idx - 1]; + + if (!deviceId || error) { + return ; + } + + if (!ready || !video) { + return ; + } + + return ( + + ); +}; + +export default VideoPlayer; diff --git a/src/components/devices/DeviceOverviewPage.tsx b/src/components/devices/DeviceOverviewPage.tsx index 1bbdeaa..f9ff706 100644 --- a/src/components/devices/DeviceOverviewPage.tsx +++ b/src/components/devices/DeviceOverviewPage.tsx @@ -45,7 +45,6 @@ const logs = [ // 设备总览组件 export default function DeviceOverviewPage({ devices, planedevices, devicesAll }) { - console.log(devicesAll, "devicesAll") const [selectedDevice, setSelectedDevice] = useState(null); const [planeVideo, setPlaneVideo] = useState(''); @@ -191,7 +190,7 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll } /> - 控制日志 / 告警记录} bordered={false} style={{ borderRadius: 12 }} extra={
}> + 控制日志 / 告警记录} bordered={false} style={{ borderRadius: 12 }} extra={
}> {t} }, @@ -241,10 +240,13 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll } 电量 {fix2(selectedDevice.capacity_percent || 100)}% -
- 温度 - {fix2(selectedDevice.environment_temperature || 25)}℃ -
+ {selectedDevice.environment_temperature && ( +
+ 温度 + {fix2(selectedDevice.environment_temperature || 25)}℃ +
+ )} +
@@ -382,7 +384,7 @@ export default function DeviceOverviewPage({ devices, planedevices, devicesAll } {/* 条件渲染:机器人显示其余信息(此处以任务详情为例) */} {(!selectedDevice?.productName?.includes('无人机') && !selectedDevice?.device_sn) && ( - + 机器人作业状态} bordered={false} style={{ borderRadius: 12, marginBottom: 16 }}>
diff --git a/src/components/devices/WayLinePage.tsx b/src/components/devices/WayLinePage.tsx new file mode 100644 index 0000000..7b3d0cb --- /dev/null +++ b/src/components/devices/WayLinePage.tsx @@ -0,0 +1,356 @@ +import React, { useState } from 'react'; +import { + Row, Col, Card, Button, Space, Table, Tag, Typography, Progress, Badge, + Tabs, Input, Switch, Divider, Tooltip, Timeline +} from 'antd'; +import { + PlusOutlined, ImportOutlined, NodeIndexOutlined, SafetyCertificateOutlined, + SaveOutlined, FullscreenOutlined, EnvironmentOutlined, RobotOutlined, + BranchesOutlined, CloudOutlined, ThunderboltOutlined, CheckCircleOutlined, + InfoCircleOutlined, ClockCircleOutlined, RiseOutlined, FallOutlined, + SettingOutlined, WarningOutlined, ArrowRightOutlined, CloudSyncOutlined +} from '@ant-design/icons'; + +const { Text, Title } = Typography; + +export default function WayLinePage() { + const [activeMainTab, setActiveTab] = useState('wayline'); + + const cardStyle = { borderRadius: 8, border: 'none', boxShadow: '0 2px 8px rgba(0,0,0,0.05)' }; + + // 模拟数据 + const topCards = [ + { title: '今日航线计划', value: '18', unit: '条', trend: '较昨日 +2', color: '#165DFF', icon: }, + { title: '执行中任务', value: '6', unit: '个', desc: '进行中', color: '#165DFF', icon: }, + { title: '待审批任务', value: '4', unit: '个', trend: '较昨日 -1', color: '#FF7D00', icon: }, + { title: '任务完成率', value: '92', unit: '%', trend: '较昨日 +6%', color: '#00B42A', icon: }, + { title: 'AI 优化收益', value: '+7.8', unit: '%', trend: '较昨日 +1.2%', color: '#722ED1', icon: }, + { title: '覆盖装备', value: '4类 / 9台', unit: '', desc: '全部在线', color: '#165DFF', icon: }, + ]; + + return ( +
+ + + {/* 顶部统计卡片 */} + + {topCards.map((item, index) => ( +
+ +
+
+ {item.icon} +
+
+
{item.title}
+
+ {item.value} + {item.unit} +
+
+ {item.trend || item.desc} +
+
+
+
+ + ))} + + + {/* 中间主要内容区 */} + + {/* 左侧:航线编辑与路径规划 */} + + 航线编辑与路径规划} + extra={ + + + + + + +
+ + + + {/* 右侧:任务编排、参数与天气 */} + + 任务编排与 AI 建议} + bordered={false} + bodyStyle={{ padding: 12 }} + style={{ ...cardStyle, marginBottom: 12 }} + > +
{t} }, + { title: '装备', dataIndex: 'device' }, + { title: '类型', dataIndex: 'type', ellipsis: true }, + { title: '优先级', dataIndex: 'level', render: (l) => ▲ {l} }, + { title: '建议时间', dataIndex: 'time' }, + { title: '预计时长', dataIndex: 'duration' }, + { title: '状态', dataIndex: 'status', render: (s) => {s} }, + ]} + /> +
+ AI 建议: + }> + 推荐优先执行 FL-03 航线 + 14:00-16:00 风速最佳 + 区块 B-12 存在高温风险 + +
+ + + + + + + + 航线参数 / 作业参数} + extra={当前航线:FL-03} + bordered={false} + bodyStyle={{ padding: 12 }} + style={{ ...cardStyle, marginBottom: 12 }} + > + + {[ + { label: '飞行高度', value: '35', unit: 'm' }, + { label: '速度', value: '6', unit: 'm/s' }, + { label: '航向重叠', value: '75', unit: '%' }, + { label: '旁向重叠', value: '65', unit: '%' }, + { label: '安全返航电量', value: '25', unit: '%' }, + { label: '清洗喷洒量', value: '120', unit: 'ml/㎡' }, + { label: '除草宽幅', value: '1.2', unit: 'm' }, + { label: '巡检采样频率', value: '2', unit: 'Hz' }, + ].map((item, idx) => ( + +
{item.label}
+
+ + {item.unit} +
+ + ))} + + + + + 避障开关 + + + + 夜航限制 + + + + + + 天气窗口与执行约束} + bordered={false} + bodyStyle={{ padding: 12 }} + style={cardStyle} + > + + +
+
适宜执行
+
+
适宜执行
+
+ + {[ + { label: '风速', value: '2.8 m/s', desc: '适宜', icon: , color: '#165DFF' }, + { label: '温度', value: '28℃', desc: '适宜', icon: < ThunderboltOutlined />, color: '#FF7D00' }, + { label: '降水概率', value: '10%', desc: '极低', icon: , color: '#165DFF' }, + { label: 'GNSS质量', value: '良好', desc: '适宜', icon: , color: '#00B42A' }, + ].map((item, idx) => ( + +
+
{item.icon}
+
+
{item.label}
+
{item.value}
+
{item.desc}
+
+
+ + ))} + +
+ 16:30 后风速升高,建议提前完成任务 + 预计 14:00-16:00 为最佳执行窗口 +
+ + + + + {/* 底部内容区 */} + + + {/* 甘特图/排班视图 */} + + 任务甘特视图 / 排班视图 今日 (2025-05-24)} + extra={} + bordered={false} + bodyStyle={{ padding: 12 }} + style={{ ...cardStyle, height: '100%' }} // 👈 高度 100% + > +
+ {/* 模拟时间轴头部 */} +
+
+
+ 08:0010:0012:0014:0016:0018:00 +
+
+ {/* 模拟甘特条 */} +
+ {[ + { label: '无人机机场 A01 (UAV-A01)', tasks: [{ start: '25%', width: '15%', title: 'FL-03 巡检', color: '#165DFF' }, { start: '70%', width: '10%', title: 'FL-07 复检', color: '#165DFF' }] }, + { label: '巡检机器人 RBT-01', tasks: [{ start: '40%', width: '15%', title: 'RB-02 巡检', color: '#FF7D00' }] }, + { label: '除草机器人 GR-01', tasks: [{ start: '10%', width: '25%', title: 'GR-04 阵列除草', color: '#00B42A' }] }, + { label: '清洗无人机 UAV-C01', tasks: [{ start: '60%', width: '20%', title: 'CL-05 组件清洗', color: '#13C2C2' }] }, + ].map((row, idx) => ( +
+
{row.label}
+
+ {row.tasks.map((task, tidx) => ( +
+
{task.title}
+
10:00-11:20
+
+ ))} +
+
+ ))} +
+ {/* 垂直当前时间红线 */} +
+
+ + + + {/* 任务看板 */} +
+ 工作任务看板} + bordered={false} + bodyStyle={{ padding: 12 }} + style={{ ...cardStyle, height: '100%', display: 'flex', flexDirection: 'column' }} // 👈 高度 100% + > +
{/* 👈 自动撑满 */} + + {[ + { title: '待执行 (3)', bg: '#F7F8FA', list: [{ id: 'FL-07', name: 'AI 复检航线', device: 'UAV-A01', handler: '王工' }, { id: 'CL-05', name: '组件清洗', device: 'UAV-C01', handler: '李工' }] }, + { title: '执行中 (2)', bg: '#E8F3FF', list: [{ id: 'FL-03', name: '无人机热斑巡检', device: 'UAV-A01', handler: '王工', gain: '+8.2%' }, { id: 'GR-04', name: '阵列除草', device: 'GR-01', handler: '赵工' }] }, + { title: '已完成 (4)', bg: '#E8FFEA', list: [{ id: 'FL-01', name: '组件巡检', device: 'UAV-A01', handler: '王工', gain: '+7.1%', time: '08:20' }, { id: 'CL-01', name: '组件清洗', device: 'UAV-C01', handler: '李工', gain: '+5.9%', time: '07:45' }] }, + ].map((col, idx) => ( +
+
+
{col.title}
+
+ {col.list.map((task, tidx) => ( +
+
{task.id} {task.name}
+
装备: {task.device} | 负责人: {task.handler}
+ {task.gain &&
预计收益: {task.gain}
} + {task.time &&
完成时间: {task.time}
} +
+ ))} +
+
+ + ))} + + + +
+ +
+ + + + + ); +} diff --git a/src/components/devices/XboxController.tsx b/src/components/devices/XboxController.tsx new file mode 100644 index 0000000..1c35f03 --- /dev/null +++ b/src/components/devices/XboxController.tsx @@ -0,0 +1,220 @@ + +import React, { useEffect, useState, useRef } from 'react'; + +const XboxController = ({ onGamepadUpdate, handleControlModal, isControlled }) => { + const [gamepadState, setGamepadState] = useState<{ + buttons: boolean[]; + axes: number[]; + connected: boolean; + }>({ + buttons: new Array(17).fill(false), + axes: [0, 0, 0, 0], + connected: false, + }); + + const requestRef = useRef(null); + const DEAD_ZONE = 0.08; + + const normalizeAxis = (v: number) => + Math.abs(v) < DEAD_ZONE ? 0 : Number(v.toFixed(2)); + const lastSendRef = useRef(0); + const SEND_INTERVAL = 1000 / 10; // 20Hz + const lastPayloadRef = useRef(''); + + const updateGamepad = () => { + const gp = navigator.getGamepads()[0]; + + if (gp) { + const state = { + buttons: gp.buttons.map(b => b.pressed), + axes: gp.axes.map(normalizeAxis), + connected: true, + ts: Date.now(), + }; + + setGamepadState(state); + + const payload = JSON.stringify({ + buttons: state.buttons, + axes: state.axes, + }); + + const now = performance.now(); + + if ( + now - lastSendRef.current > SEND_INTERVAL + // payload !== lastPayloadRef.current + ) { + onGamepadUpdate(state); // ✅ 只有这里才发 + lastSendRef.current = now; + lastPayloadRef.current = payload; + } + } + if (!gp) { + // 不要在这里停 RAF + requestRef.current = requestAnimationFrame(updateGamepad); + return; + } + + requestRef.current = requestAnimationFrame(updateGamepad); + }; + useEffect(() => { + requestRef.current = requestAnimationFrame(updateGamepad); + + const onFocus = () => { + // 页面重新获得焦点 → 立刻重拉一次 + requestRef.current = requestAnimationFrame(updateGamepad); + }; + + window.addEventListener('focus', onFocus); + + return () => { + if (requestRef.current) cancelAnimationFrame(requestRef.current); + window.removeEventListener('focus', onFocus); + }; + }, []); // ✅ 空依赖 + + useEffect(() => { + setTimeout(() => { + window.focus(); + navigator.getGamepads(); // 🔥 强制触发一次 + }, 0); + }, [/* 页面切换依赖 */]); + + useEffect(() => { + const handleConnect = (e: GamepadEvent) => { + console.log('🎮 Gamepad connected:', e.gamepad.id); + setGamepadState(prev => ({ ...prev, connected: true })); + }; + + const handleDisconnect = (e: GamepadEvent) => { + console.log('❌ Gamepad disconnected'); + handleControlModal({ control: false }); + setGamepadState(prev => ({ ...prev, connected: false })); + }; + + window.addEventListener('gamepadconnected', handleConnect); + window.addEventListener('gamepaddisconnected', handleDisconnect); + + return () => { + window.removeEventListener('gamepadconnected', handleConnect); + window.removeEventListener('gamepaddisconnected', handleDisconnect); + }; + }, []); + + // UI mapping for Xbox Gamepad + // Buttons: 0:A, 1:B, 2:X, 3:Y, 4:LB, 5:RB, 6:LT, 7:RT, 8:Back, 9:Start, 10:LS, 11:RS, 12:Up, 13:Down, 14:Left, 15:Right + const { buttons, axes, connected } = gamepadState; + + const getAxisStyle = (axisX: number, axisY: number) => ({ + transform: `translate(${axisX * 6}px, ${axisY * 6}px)`, + }); + + return ( +
{ + if (connected && !isControlled) { + // 这里写点击后要执行的逻辑,比如打开权限请求弹窗 + console.log("点击了无控制权限,发起权限请求"); + // 示例:调用你代码中已有的权限请求弹窗逻辑 + handleControlModal({ control: true, type: "switchPermission" }); + } + }}> + {/* Gamepad Status Badge */} +
+ {connected ? isControlled ? '控制中' : '重新选中设备获取控制权限' : '无控制器连接'} +
+ + + {/* Main Body */} + + + {/* Triggers & Bumpers */} + + + + {/* LT / RT Indicators */} + + + + {/* Left Stick Area */} + + + + + + + {/* Right Stick Area */} + + + + + + + + + + {/* D-Pad */} + + + + + + + + + {/* Buttons (ABXY) */} + + {/* A */} + {/* B */} + {/* X */} + {/* Y */} + + + {/* Menu Buttons */} + + + +
+ ); +}; + +export default XboxController; diff --git a/src/components/useWebRTCStream.tsx b/src/components/useWebRTCStream.tsx new file mode 100644 index 0000000..5e139b1 --- /dev/null +++ b/src/components/useWebRTCStream.tsx @@ -0,0 +1,86 @@ +import { useEffect, useRef, useState } from 'react'; + +declare const JSWebrtc: any; + +// 🔥 修复:url 变成必传,deviceId + url 共同作为唯一标识 +export function useWebRTCPlayer(deviceId: string, url: string) { + const videoRef = useRef(null); + const playerRef = useRef(null); + const readyRef = useRef(false); + + const [ready, setReady] = useState(false); + const [error, setError] = useState(false); + + // 统一销毁 + const destroyPlayer = () => { + if (playerRef.current) { + try { playerRef.current.destroy(); } catch { } + playerRef.current = null; + } + if (videoRef.current) { + try { + videoRef.current.pause(); + videoRef.current.srcObject = null; + } catch { } + videoRef.current = null; + } + readyRef.current = false; + setReady(false); + setError(false); + }; + + // 🔥 修复依赖:[deviceId, url] + useEffect(() => { + if (!deviceId || !url) { + destroyPlayer(); + return; + } + + destroyPlayer(); + + // 每个 Hook 创建独立 video + const v = document.createElement('video'); + v.autoplay = true; + v.muted = true; + v.playsInline = true; + v.style.width = '100%'; + v.style.height = '100%'; + v.style.objectFit = 'contain'; + videoRef.current = v; + + const token = localStorage.getItem('videotoken') || JSON.parse(localStorage.getItem('userInfo') || '{}')?.token; + const videoUrl = `${url}?token=${token}`; + + try { + const player = new JSWebrtc.Player(videoUrl, { + video: v, + autoplay: true, + onPlay: () => { + setReady(true); + setError(false); + readyRef.current = true; + }, + onError: (err: any) => { + console.error('播放错误', deviceId, url, err); + setError(true); + setReady(false); + }, + }); + playerRef.current = player; + } catch (e) { + console.error('初始化失败', e); + setError(true); + } + + return () => { + destroyPlayer(); + }; + }, [deviceId, url]); // 🔥 必须加 url + + return { + video: videoRef.current, + ready, + error, + destroyPlayer, + }; +} diff --git a/src/pages/Devices.tsx b/src/pages/Devices.tsx index 871e2d6..96496d9 100644 --- a/src/pages/Devices.tsx +++ b/src/pages/Devices.tsx @@ -1,10 +1,11 @@ import React, { useState, useEffect } from 'react'; import { Layout, Typography, Menu } from 'antd'; -import { AppstoreOutlined, SettingOutlined } from '@ant-design/icons'; +import { AimOutlined, AppstoreOutlined, SettingOutlined } from '@ant-design/icons'; import DeviceOverviewPage from '../components/devices/DeviceOverviewPage'; import { getDeviceListApi, getPlaneDeviceList } from '../api/device'; import { useSelector } from 'react-redux'; import { RootState } from '../store'; +import WayLinePage from '../components/devices/WayLinePage'; const { Header, Content } = Layout; const { Text } = Typography; @@ -25,6 +26,7 @@ export default function DeviceIndexPage() { const tabs = [ { key: 'overview', label: '装备总览', icon: }, { key: 'status', label: '设置状态', icon: }, + { key: 'wayLineTask', label: '航线任务', icon: }, ]; // 👇 核心修复:同时请求 + 同时合并 @@ -67,6 +69,7 @@ export default function DeviceIndexPage() { />; } if (activeTab === 'status') return ; + if (activeTab === 'wayLineTask') return ; return null; };