diff --git a/src/components/Map.jsx b/src/components/Map.jsx index a57863d..6815049 100644 --- a/src/components/Map.jsx +++ b/src/components/Map.jsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback } from 'react'; +import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback } from 'react'; import carImg from '../assets/images/carplane.png'; import inspectImg from "../assets/images/inspectCar.png" import planeImg from '../assets/images/planeicon.png'; @@ -2554,7 +2554,28 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, // 暴露 ref 方法 useImperativeHandle(ref, () => ({ - captureScreenshot + captureScreenshot, + // 寻车/定位:直接飞到指定经纬度,绕过飞行优先级系统,确保手动触发一定生效 + flyToPosition: (position, options = {}) => { + const viewer = viewerRef.current; + if (!viewer || !position) return; + const { lon, lat } = position; + if (lon == null || lat == null || isNaN(Number(lon)) || isNaN(Number(lat))) return; + const Cesium = window.Cesium; + const height = options.height || 300; + const duration = options.duration ?? 1.5; + try { viewer.camera.cancelFlight(); } catch (e) { /* 忽略 */ } + lastFlightPriorityRef.current = -1; + viewer.camera.flyTo({ + destination: Cesium.Cartesian3.fromDegrees(Number(lon), Number(lat), height), + orientation: { + heading: Cesium.Math.toRadians(0.0), + pitch: Cesium.Math.toRadians(-90.0), + roll: 0.0, + }, + duration, + }); + }, })); return ( diff --git a/src/components/devices/CarReverseGuideLine.tsx b/src/components/devices/CarReverseGuideLine.tsx new file mode 100644 index 0000000..645ff1d --- /dev/null +++ b/src/components/devices/CarReverseGuideLine.tsx @@ -0,0 +1,132 @@ +import React, { useEffect, useRef } from 'react'; +type GuideLineProps = { + /** 方向 forward前进 reverse倒车 */ + direction: 'forward' | 'reverse'; + /** 方向盘转角,单位°:左转负数,右转正数,范围建议 -45 ~ 45 */ + steer: number; + /** 画布宽度 */ + width?: number; + /** 画布高度 */ + height?: number; + /** 引导线底部(车尾端)两条线间距 */ + bottomGap?: number; + /** 引导线顶部(远端)两条线间距 */ + topGap?: number; +}; +const CarReverseGuideLine: React.FC = ({ + direction, + steer, + width = 640, + height = 480, + bottomGap = 100, + topGap = 160, +}) => { + const canvasRef = useRef(null); + useEffect(() => { + const canvas = canvasRef.current; + if (!canvas) return; + const ctx = canvas.getContext('2d'); + if (!ctx) return; + ctx.clearRect(0, 0, width, height); + // 车尾中心点(画布底部) + const baseX = width / 2; + const baseY = height - 20; + const totalDist = direction === 'reverse' ? height * 0.82 : height * 0.68; + const maxWheelAngle = 45; + // 限制车轮转角范围 + const wheelAngleRad = Math.max(-maxWheelAngle, Math.min(maxWheelAngle, steer)) * Math.PI / 180; + const step = 2; // 步长,越小曲线越平滑 + + // 迭代计算轨迹点函数 + const getPathPoints = (side: 'left' | 'right'): [number, number][] => { + const points: [number, number][] = []; + let x = baseX; + let y = baseY; + let angle = -Math.PI / 2; // 初始朝向:向上(画布Y减小方向) + const stepCount = Math.floor(totalDist / step); + points.push([x, y]); + for (let i = 0; i < stepCount; i++) { + // 当前进度 0~1,0=底部起点,1=最远顶部 + const progress = i / stepCount; + // 线性插值:从底部gap过渡到顶部gap + const currentGap = bottomGap * (1 - progress) + topGap * progress; + const sideOffset = side === 'left' ? -currentGap / 2 : currentGap / 2; + + if (Math.abs(wheelAngleRad) < 0.001) { + // 回正:直线向上 + y -= step; + } else { + // 有转向:改变朝向,再前进一小步 + const turnStepRad = step * Math.tan(wheelAngleRad) / 240; + // 分母240:控制转弯曲率,调大转弯更平缓;调小弯更急 + angle += turnStepRad; + x += Math.cos(angle) * step; + y += Math.sin(angle) * step; + } + // 叠加当前侧边偏移 + const px = x + sideOffset; + const py = y; + points.push([px, py]); + // 超出画布提前停止绘制 + if (py < 0 || px < 0 || px > width) break; + } + return points; + }; + + // 绘制左右两条轨迹 + ctx.lineWidth = 3; + ctx.strokeStyle = '#0f0'; + ctx.setLineDash([]); + const leftPath = getPathPoints('left'); + const rightPath = getPathPoints('right'); + const drawPath = (pts: [number, number][]) => { + ctx.beginPath(); + pts.forEach(([px, py], idx) => { + if (idx === 0) ctx.moveTo(px, py); + else ctx.lineTo(px, py); + }); + ctx.stroke(); + ctx.beginPath(); // 强制清空当前路径缓存!重点! + }; + drawPath(leftPath); + drawPath(rightPath); + + // ========== 横向距离标记虚线 ========== + ctx.strokeStyle = '#ff0'; + ctx.lineWidth = 2; + ctx.setLineDash([8, 6]); + const markCount = 3; + for (let m = 1; m <= markCount; m++) { + const ratio = m / (markCount + 1); + const idx = Math.floor(leftPath.length * ratio); + if (idx >= leftPath.length) continue; + const [lx, ly] = leftPath[idx]; + const [rx, ry] = rightPath[idx]; + ctx.beginPath(); + ctx.moveTo(lx, ly); + ctx.lineTo(rx, ry); + ctx.stroke(); + } + + // ========== 车身底部框 ========== + //ctx.setLineDash([]); + //ctx.strokeStyle = '#0ff'; + //ctx.lineWidth = 2; + //ctx.beginPath(); + //ctx.rect(baseX - bottomGap / 2, baseY - 30, bottomGap, 30); + //ctx.stroke(); + }, [direction, steer, width, height, bottomGap, topGap]); + + return ( +
+ {/* 背景倒车画面,可以替换 video / img */} + +
+ ); +}; +export default CarReverseGuideLine; diff --git a/src/components/devices/DeviceStatusPage.tsx b/src/components/devices/DeviceStatusPage.tsx index 5fae939..1e7c4ba 100644 --- a/src/components/devices/DeviceStatusPage.tsx +++ b/src/components/devices/DeviceStatusPage.tsx @@ -71,6 +71,8 @@ import { dataUrlToFile } from '@/lib/utils.ts'; import { useUnitFormatter } from '@/lib/units'; import { PauseOutlined, PlayCircleOutlined } from '@ant-design/icons'; import { useTranslation } from 'react-i18next'; + +import CarReverseGuideLine from './CarReverseGuideLine'; const { RangePicker } = DatePicker; const { Text } = Typography; @@ -94,6 +96,9 @@ export default function DeviceStatusPage() { const hasRequestedControlRef = useRef(false); const [messageApi, contextHolder] = message.useMessage(); + const [direction, setDirection] = useState<'forward' | 'reverse'>('reverse'); + const [steer, setSteer] = useState(0); + // 视频播放开关 const [isVideoPlaying, setIsVideoPlaying] = useState(false); @@ -463,25 +468,53 @@ export default function DeviceStatusPage() { heading: currentDroneData.heading || 0, }; } + // 无人机没有实时数据时,用 lastRunningStatus 兜底 + const baseLon = device?.lastRunningStatus?.longitude || device?.longitude; + const baseLat = device?.lastRunningStatus?.latitude || device?.latitude; + if (baseLon && baseLat) { + return { + lon: Number(baseLon), + lat: Number(baseLat), + heading: device?.lastRunningStatus?.heading || 0, + }; + } return null; } - // 2. 机器人:从 wsDeviceInfo / runningStatus 取实时经纬度 - const robotLon = runningStatus?.longitude || wsDeviceInfo?.longitude; - const robotLat = runningStatus?.latitude || wsDeviceInfo?.latitude; + // 2. 机器人:优先从 wsDeviceInfo / runningStatus / locationInfo 取实时经纬度 + let robotLon = runningStatus?.longitude || wsDeviceInfo?.longitude || locationInfo?.longitude; + let robotLat = runningStatus?.latitude || wsDeviceInfo?.latitude || locationInfo?.latitude; + + // 3. 没有实时经纬度时,从 lastRunningStatus 兜底 + if ((!robotLon || !robotLat) && (device?.lastRunningStatus || device?.locationMessage)) { + robotLon = robotLon || device?.lastRunningStatus?.longitude || device?.locationMessage?.longitude; + robotLat = robotLat || device?.lastRunningStatus?.latitude || device?.locationMessage?.latitude; + } + // 4. 最终兜底:设备静态经纬度 + if ((!robotLon || !robotLat) && device) { + robotLon = robotLon || device.longitude; + robotLat = robotLat || device.latitude; + } if (robotLon && robotLat) { return { lon: robotLon, lat: robotLat, - //heading: -98 - heading: runningStatus?.yaw || wsDeviceInfo?.yaw || 0, + heading: runningStatus?.yaw || wsDeviceInfo?.yaw || locationInfo?.yaw || device?.lastRunningStatus?.heading || 0, }; } return null; - }, [isUAV, currentDroneData, runningStatus, wsDeviceInfo]); + }, [isUAV, currentDroneData, runningStatus, wsDeviceInfo, locationInfo, device]); + // 寻车:地图定位到当前设备位置 + const handleFindCar = () => { + if (!realtimePosition) { + messageApi.warning(t('devices.locationUnknown')); + return; + } + mapRef.current?.flyToPosition?.(realtimePosition, { height: 300 }); + }; useEffect(() => { const controller = new DeviceController(userInfo, messageApi); @@ -927,6 +960,8 @@ export default function DeviceStatusPage() { } + + return (