303 lines
12 KiB
TypeScript
303 lines
12 KiB
TypeScript
import React, { useEffect, useMemo, useState } from 'react';
|
|
import {
|
|
VideoCameraOutlined,
|
|
RadarChartOutlined,
|
|
EnvironmentOutlined,
|
|
ThunderboltOutlined,
|
|
WifiOutlined,
|
|
} from '@ant-design/icons';
|
|
import WhepPlayer from '@/pages/map3d/components/MapContainer/3d/BottomPanel/WhepPlayer';
|
|
import type { BoatRouteListVO, BoatTelemetryData } from '@/types/boatRoute';
|
|
import { getValidCoordinate } from '@/utils/geo';
|
|
import styles from './index.less';
|
|
|
|
interface ExecRightPanelProps {
|
|
deviceInfo: any;
|
|
boatData: BoatTelemetryData | null;
|
|
activeRouteInfo: BoatRouteListVO | null;
|
|
sseConnected: boolean;
|
|
videoStreamUrl?: string;
|
|
isPreviewing?: boolean;
|
|
}
|
|
|
|
interface LidarSample {
|
|
angle: number;
|
|
distance: number;
|
|
}
|
|
|
|
const clampNumber = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));
|
|
|
|
const extractLidarSamples = (deviceInfo: any, boatData: BoatTelemetryData | null): LidarSample[] => {
|
|
const raw =
|
|
(boatData as any)?.lidarSamples ||
|
|
(boatData as any)?.lidarPoints ||
|
|
(boatData as any)?.lidarDistances ||
|
|
(deviceInfo as any)?.lidarSamples ||
|
|
(deviceInfo as any)?.lidarPoints ||
|
|
(deviceInfo as any)?.lidarDistances ||
|
|
[];
|
|
|
|
if (!Array.isArray(raw) || raw.length === 0) return [];
|
|
|
|
if (typeof raw[0] === 'number') {
|
|
return raw.map((distance: number, index: number) => ({
|
|
angle: (index / raw.length) * 360,
|
|
distance: Number(distance) || 0,
|
|
}));
|
|
}
|
|
|
|
return raw
|
|
.map((item: any, index: number) => ({
|
|
angle: Number(item?.angle ?? item?.deg ?? item?.theta ?? (index / raw.length) * 360),
|
|
distance: Number(item?.distance ?? item?.range ?? item?.value ?? 0),
|
|
}))
|
|
.filter((item) => Number.isFinite(item.angle) && Number.isFinite(item.distance));
|
|
};
|
|
|
|
const createMockLidarSamples = (phase: number): LidarSample[] => (
|
|
Array.from({ length: 72 }, (_, index) => {
|
|
const angle = index * 5;
|
|
const wave = Math.sin((angle + phase * 28) * Math.PI / 180);
|
|
const ripple = Math.cos((angle * 2 + phase * 36) * Math.PI / 180);
|
|
const bump = (index % 9 === 0 ? 4.5 : 0) + (index % 13 === 0 ? 2.6 : 0);
|
|
const distance = clampNumber(16 + wave * 6 + ripple * 3 + bump, 3.6, 31);
|
|
return { angle, distance };
|
|
})
|
|
);
|
|
|
|
const ExecRightPanel: React.FC<ExecRightPanelProps> = ({
|
|
deviceInfo,
|
|
boatData,
|
|
activeRouteInfo,
|
|
sseConnected,
|
|
videoStreamUrl,
|
|
isPreviewing = false,
|
|
}) => {
|
|
const [mockPhase, setMockPhase] = useState(0);
|
|
|
|
useEffect(() => {
|
|
if (!isPreviewing) return undefined;
|
|
const timer = window.setInterval(() => {
|
|
setMockPhase((value) => (value + 1) % 360);
|
|
}, 900);
|
|
return () => window.clearInterval(timer);
|
|
}, [isPreviewing]);
|
|
|
|
const realLidarSamples = useMemo(() => extractLidarSamples(deviceInfo, boatData), [deviceInfo, boatData]);
|
|
const lidarSamples = useMemo(
|
|
() => (isPreviewing ? createMockLidarSamples(mockPhase) : realLidarSamples),
|
|
[isPreviewing, mockPhase, realLidarSamples],
|
|
);
|
|
const boatPosition = getValidCoordinate(boatData?.longitude, boatData?.latitude);
|
|
const hasBoatPosition = !!boatPosition;
|
|
|
|
const lidarMetrics = useMemo(() => {
|
|
if (!lidarSamples.length) {
|
|
return {
|
|
minDistance: null,
|
|
frontDistance: null,
|
|
leftDistance: null,
|
|
rightDistance: null,
|
|
};
|
|
}
|
|
|
|
const pickClosest = (targetAngle: number) => {
|
|
const matched = lidarSamples.reduce((best, current) => {
|
|
const delta = Math.abs((((current.angle - targetAngle) % 360) + 540) % 360 - 180);
|
|
return delta < best.delta ? { delta, distance: current.distance } : best;
|
|
}, { delta: Number.POSITIVE_INFINITY, distance: 0 });
|
|
return matched.distance;
|
|
};
|
|
|
|
return {
|
|
minDistance: Math.min(...lidarSamples.map((item) => item.distance)),
|
|
frontDistance: pickClosest(0),
|
|
leftDistance: pickClosest(270),
|
|
rightDistance: pickClosest(90),
|
|
};
|
|
}, [lidarSamples]);
|
|
|
|
const lidarPoints = useMemo(() => {
|
|
if (!lidarSamples.length) return [];
|
|
const maxDistance = Math.max(10, ...lidarSamples.map((item) => item.distance));
|
|
const radius = 88;
|
|
|
|
return lidarSamples.slice(0, 72).map((sample) => {
|
|
const normalized = clampNumber(sample.distance / maxDistance, 0.08, 1);
|
|
const actualRadius = normalized * radius;
|
|
const angleRad = (sample.angle - 90) * Math.PI / 180;
|
|
return {
|
|
x: 110 + Math.cos(angleRad) * actualRadius,
|
|
y: 110 + Math.sin(angleRad) * actualRadius,
|
|
};
|
|
});
|
|
}, [lidarSamples]);
|
|
|
|
const statusItems = [
|
|
{
|
|
label: '设备状态',
|
|
value: isPreviewing ? '模拟预览' : (sseConnected ? '在线' : '离线'),
|
|
icon: <WifiOutlined />,
|
|
accent: isPreviewing ? '#22d3ee' : (sseConnected ? '#10b981' : '#ef4444'),
|
|
},
|
|
{
|
|
label: '剩余电量',
|
|
value: `${boatData?.battery ?? (isPreviewing ? 86 : 0)}%`,
|
|
icon: <ThunderboltOutlined />,
|
|
accent: (boatData?.battery ?? 86) > 20 ? '#22d3ee' : '#f59e0b',
|
|
},
|
|
{
|
|
label: '当前位置',
|
|
value: hasBoatPosition ? `${boatData!.longitude.toFixed(4)}, ${boatData!.latitude.toFixed(4)}` : '--',
|
|
icon: <EnvironmentOutlined />,
|
|
accent: '#94a3b8',
|
|
},
|
|
];
|
|
|
|
const formatMeters = (distance: number | null) => (distance == null ? '--' : `${distance.toFixed(distance >= 10 ? 0 : 1)} m`);
|
|
|
|
return (
|
|
<div className={styles.panel}>
|
|
<div className={styles.section}>
|
|
<div className={styles.sectionHeader}>
|
|
<span className={styles.sectionTitle}>
|
|
<VideoCameraOutlined />
|
|
视频回传
|
|
</span>
|
|
<span className={styles.sectionTag}>
|
|
{isPreviewing ? '模拟流' : (videoStreamUrl ? '实时流' : '未接入')}
|
|
</span>
|
|
</div>
|
|
<div className={styles.videoCard}>
|
|
{isPreviewing ? (
|
|
<div className={styles.mockVideoCard}>
|
|
<div className={styles.mockVideoGlow} />
|
|
<div className={styles.mockVideoScanline} />
|
|
<div className={styles.mockVideoHud}>
|
|
<span className={styles.mockVideoBadge}>SIM CAM</span>
|
|
<span className={styles.mockVideoMeta}>1080P / 25 FPS</span>
|
|
</div>
|
|
<div className={styles.mockVideoScene}>
|
|
<div className={styles.mockHorizon} />
|
|
<div className={styles.mockWave} />
|
|
<div className={styles.mockBoatMarker}>
|
|
<span className={styles.mockBoatDot} />
|
|
<span>航线预览目标</span>
|
|
</div>
|
|
</div>
|
|
<div className={styles.mockVideoFooter}>
|
|
<span>模拟海况:2级</span>
|
|
<span>光照:良好</span>
|
|
<span>延迟:42ms</span>
|
|
</div>
|
|
</div>
|
|
) : videoStreamUrl ? (
|
|
<WhepPlayer url={videoStreamUrl} autoPlay={true} muted={true} videoOnly={true} />
|
|
) : (
|
|
<div className={styles.videoPlaceholder}>
|
|
<VideoCameraOutlined className={styles.placeholderIcon} />
|
|
<div className={styles.placeholderTitle}>暂无视频流</div>
|
|
<div className={styles.placeholderDesc}>设备未配置 `videoStreamUrl` 或 `whepUrl`</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className={styles.section}>
|
|
<div className={styles.sectionHeader}>
|
|
<span className={styles.sectionTitle}>
|
|
<RadarChartOutlined />
|
|
激光雷达
|
|
</span>
|
|
<span className={styles.sectionTag}>
|
|
{isPreviewing ? `模拟 ${lidarSamples.length} 点` : (lidarSamples.length > 0 ? `${lidarSamples.length} 点` : '未接入')}
|
|
</span>
|
|
</div>
|
|
|
|
<div className={styles.lidarCard}>
|
|
<div className={styles.lidarCanvas}>
|
|
<svg viewBox="0 0 220 220" className={styles.lidarSvg}>
|
|
<circle cx="110" cy="110" r="88" className={styles.lidarRing} />
|
|
<circle cx="110" cy="110" r="58" className={styles.lidarRing} />
|
|
<circle cx="110" cy="110" r="28" className={styles.lidarRing} />
|
|
<line x1="110" y1="14" x2="110" y2="206" className={styles.lidarAxis} />
|
|
<line x1="14" y1="110" x2="206" y2="110" className={styles.lidarAxis} />
|
|
<line
|
|
x1="110"
|
|
y1="110"
|
|
x2={110 + Math.cos((mockPhase - 90) * Math.PI / 180) * 96}
|
|
y2={110 + Math.sin((mockPhase - 90) * Math.PI / 180) * 96}
|
|
className={styles.lidarSweep}
|
|
/>
|
|
{lidarPoints.map((point, index) => (
|
|
<circle key={`${point.x}-${point.y}-${index}`} cx={point.x} cy={point.y} r="2.6" className={styles.lidarPoint} />
|
|
))}
|
|
</svg>
|
|
{lidarPoints.length === 0 && (
|
|
<div className={styles.lidarEmpty}>
|
|
<RadarChartOutlined className={styles.placeholderIcon} />
|
|
<span>暂无激光雷达数据</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className={styles.lidarMetrics}>
|
|
<div className={styles.metricItem}>
|
|
<span className={styles.metricLabel}>最近障碍距离</span>
|
|
<span className={styles.metricValue}>{formatMeters(lidarMetrics.minDistance)}</span>
|
|
</div>
|
|
<div className={styles.metricRow}>
|
|
<div className={styles.metricMini}>
|
|
<span className={styles.metricLabel}>前方</span>
|
|
<span className={styles.metricValue}>{formatMeters(lidarMetrics.frontDistance)}</span>
|
|
</div>
|
|
<div className={styles.metricMini}>
|
|
<span className={styles.metricLabel}>左侧</span>
|
|
<span className={styles.metricValue}>{formatMeters(lidarMetrics.leftDistance)}</span>
|
|
</div>
|
|
<div className={styles.metricMini}>
|
|
<span className={styles.metricLabel}>右侧</span>
|
|
<span className={styles.metricValue}>{formatMeters(lidarMetrics.rightDistance)}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className={styles.section}>
|
|
<div className={styles.sectionHeader}>
|
|
<span className={styles.sectionTitle}>执行摘要</span>
|
|
<span className={styles.sectionTag}>{activeRouteInfo?.routeName || '未选择航线'}</span>
|
|
</div>
|
|
<div className={styles.summaryGrid}>
|
|
{statusItems.map((item) => (
|
|
<div key={item.label} className={styles.summaryCard}>
|
|
<div className={styles.summaryIcon} style={{ color: item.accent }}>{item.icon}</div>
|
|
<div className={styles.summaryLabel}>{item.label}</div>
|
|
<div className={styles.summaryValue}>{item.value}</div>
|
|
</div>
|
|
))}
|
|
<div className={styles.summaryCard}>
|
|
<div className={styles.summaryLabel}>规划总长</div>
|
|
<div className={styles.summaryValue}>
|
|
{activeRouteInfo ? `${(activeRouteInfo.totalDistance / 1000).toFixed(2)} km` : '--'}
|
|
</div>
|
|
</div>
|
|
<div className={styles.summaryCard}>
|
|
<div className={styles.summaryLabel}>预计时长</div>
|
|
<div className={styles.summaryValue}>{activeRouteInfo?.totalDurationStr || '--'}</div>
|
|
</div>
|
|
<div className={styles.summaryCard}>
|
|
<div className={styles.summaryLabel}>航向 / 速度</div>
|
|
<div className={styles.summaryValue}>
|
|
{boatData ? `${Math.round(boatData.heading || 0)}° / ${boatData.speed?.toFixed(1) || '0.0'} m/s` : '--'}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default ExecRightPanel;
|