452 lines
16 KiB
TypeScript
452 lines
16 KiB
TypeScript
import React, { useEffect, useCallback, useState, useRef, useMemo } from 'react';
|
|
import { App as AntdApp, ConfigProvider, Button, Spin, message, Modal } from 'antd';
|
|
import {
|
|
LogoutOutlined,
|
|
DisconnectOutlined,
|
|
ReloadOutlined,
|
|
WifiOutlined,
|
|
ThunderboltOutlined,
|
|
EnvironmentOutlined,
|
|
HistoryOutlined,
|
|
EyeOutlined,
|
|
LeftOutlined,
|
|
PauseOutlined,
|
|
CaretRightOutlined,
|
|
StopOutlined,
|
|
} from '@ant-design/icons';
|
|
import { darkTheme } from '@/theme';
|
|
import LeftPanel from './components/LeftPanel';
|
|
import CenterView from './components/CenterView';
|
|
import ExecLeftPanel from './components/ExecLeftPanel';
|
|
import ExecBottomStrip from './components/ExecBottomStrip';
|
|
import ExecRightPanel from './components/ExecRightPanel';
|
|
import { useClientAuth } from './hooks/useClientAuth';
|
|
import { useClientSSE } from './hooks/useClientSSE';
|
|
import { useRouteData } from './hooks/useRouteData';
|
|
import { useNavigationState } from './hooks/useNavigationState';
|
|
import { useNavigationData } from './hooks/useNavigationData';
|
|
import { getValidCoordinate } from '@/utils/geo';
|
|
import styles from './index.less';
|
|
|
|
const ShipLogoIcon: React.FC = () => (
|
|
<svg width="20" height="20" viewBox="0 0 36 36" fill="none">
|
|
<path d="M6 27L8.4 21H27.6L30 27C30 27 25.5 30 18 30C10.5 30 6 27 6 27Z" fill="white" fillOpacity="0.9" />
|
|
<rect x="15" y="8" width="6" height="13" rx="1" fill="white" fillOpacity="0.9" />
|
|
<rect x="11" y="14" width="14" height="7" rx="1" fill="white" fillOpacity="0.7" />
|
|
<rect x="16.5" y="4" width="3" height="6" rx="1" fill="white" fillOpacity="0.8" />
|
|
</svg>
|
|
);
|
|
|
|
const formatElapsed = (totalSec: number) => {
|
|
const h = Math.floor(totalSec / 3600).toString().padStart(2, '0');
|
|
const m = Math.floor((totalSec % 3600) / 60).toString().padStart(2, '0');
|
|
const s = (totalSec % 60).toString().padStart(2, '0');
|
|
return `${h}:${m}:${s}`;
|
|
};
|
|
|
|
const calculateDistanceMeters = (
|
|
from: { longitude: number; latitude: number },
|
|
to: { longitude: number; latitude: number },
|
|
) => {
|
|
const earthRadius = 6371000;
|
|
const dLat = (to.latitude - from.latitude) * Math.PI / 180;
|
|
const dLon = (to.longitude - from.longitude) * Math.PI / 180;
|
|
const lat1 = from.latitude * Math.PI / 180;
|
|
const lat2 = to.latitude * Math.PI / 180;
|
|
const a = Math.sin(dLat / 2) ** 2
|
|
+ Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) ** 2;
|
|
return earthRadius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
};
|
|
|
|
const ClientPage: React.FC = () => {
|
|
const [messageApi, messageContextHolder] = message.useMessage();
|
|
const [modalApi, modalContextHolder] = Modal.useModal();
|
|
|
|
const {
|
|
boatData: sseBoatData,
|
|
sseConnected,
|
|
loading: sseLoading,
|
|
deviceOffline,
|
|
connectSSE,
|
|
disconnectSSE,
|
|
} = useClientSSE();
|
|
const { deviceSn, deviceInfo, handleLogout } = useClientAuth(disconnectSSE);
|
|
|
|
const {
|
|
routes,
|
|
routeWaypointsMap,
|
|
selectedRouteId,
|
|
setSelectedRouteId,
|
|
routeLoading,
|
|
} = useRouteData(deviceSn);
|
|
|
|
const boatData = sseBoatData;
|
|
const boatPosition = useMemo(
|
|
() => getValidCoordinate(boatData?.longitude, boatData?.latitude),
|
|
[boatData?.longitude, boatData?.latitude],
|
|
);
|
|
const hasBoatPosition = !!boatPosition;
|
|
const videoStreamUrl = useMemo(() => {
|
|
const rawInfo = deviceInfo as any;
|
|
return rawInfo?.videoStreamUrl
|
|
|| rawInfo?.whepUrl
|
|
|| rawInfo?.streamUrl
|
|
|| rawInfo?.videoUrl
|
|
|| rawInfo?.liveStreamUrl
|
|
|| undefined;
|
|
}, [deviceInfo]);
|
|
|
|
const {
|
|
isPaused,
|
|
currentWaypointIndex,
|
|
previewStopSignal,
|
|
isSailing,
|
|
isPreviewing,
|
|
isImmersive,
|
|
activeRouteId,
|
|
sailingLoading,
|
|
handleStartSailing,
|
|
handlePauseSailing,
|
|
handleResumeSailing,
|
|
handleStopSailing,
|
|
handleStopPreview,
|
|
handlePreviewChange,
|
|
} = useNavigationState(deviceSn, selectedRouteId, setSelectedRouteId, routes, messageApi, modalApi);
|
|
|
|
useEffect(() => {
|
|
if (!deviceSn) return;
|
|
connectSSE(deviceSn);
|
|
return () => {
|
|
disconnectSSE();
|
|
};
|
|
}, [deviceSn, connectSSE, disconnectSSE]);
|
|
|
|
const [elapsed, setElapsed] = useState(0);
|
|
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
const [previewStartSignal, setPreviewStartSignal] = useState(0);
|
|
|
|
useEffect(() => {
|
|
if (isImmersive && !isPaused) {
|
|
timerRef.current = setInterval(() => setElapsed((seconds) => seconds + 1), 1000);
|
|
} else if (timerRef.current) {
|
|
clearInterval(timerRef.current);
|
|
timerRef.current = null;
|
|
}
|
|
|
|
if (!isImmersive) {
|
|
setElapsed(0);
|
|
}
|
|
|
|
return () => {
|
|
if (timerRef.current) clearInterval(timerRef.current);
|
|
};
|
|
}, [isImmersive, isPaused]);
|
|
|
|
const handlePreviewRoute = useCallback((routeId: string) => {
|
|
if (routeId !== selectedRouteId) {
|
|
setSelectedRouteId(routeId);
|
|
}
|
|
setTimeout(() => setPreviewStartSignal((signal) => signal + 1), 200);
|
|
}, [selectedRouteId, setSelectedRouteId]);
|
|
|
|
const handleExecuteRoute = useCallback(async (routeId: string) => {
|
|
const firstWaypoint = routeWaypointsMap[routeId]?.[0];
|
|
if (!firstWaypoint) {
|
|
messageApi.warning('当前航线缺少起始航点,无法执行');
|
|
return;
|
|
}
|
|
if (!hasBoatPosition) {
|
|
messageApi.warning('尚未收到无人船实时位置,暂时不能执行航线');
|
|
return;
|
|
}
|
|
|
|
const distance = calculateDistanceMeters(
|
|
boatPosition!,
|
|
{ longitude: firstWaypoint.longitude, latitude: firstWaypoint.latitude },
|
|
);
|
|
|
|
if (distance > 200) {
|
|
modalApi.warning({
|
|
title: '无法执行航线',
|
|
centered: true,
|
|
width: 500,
|
|
okText: '知道了',
|
|
content: (
|
|
<div style={{ paddingTop: 8, textAlign: 'center', lineHeight: 1.9, fontSize: 16 }}>
|
|
<div>
|
|
无人船距离航线起点约
|
|
<span style={{ color: '#ff7875', fontWeight: 700, margin: '0 6px' }}>
|
|
{Math.round(distance)} 米
|
|
</span>
|
|
</div>
|
|
<div>已超过 200 米,请先将无人船驶近起点后再执行。</div>
|
|
</div>
|
|
),
|
|
});
|
|
return;
|
|
}
|
|
|
|
await handleStartSailing(routeId);
|
|
}, [routeWaypointsMap, boatPosition, hasBoatPosition, handleStartSailing, messageApi, modalApi]);
|
|
|
|
const activeWaypointsRaw = useMemo(() => {
|
|
if (!activeRouteId) return [];
|
|
return routeWaypointsMap[activeRouteId] || [];
|
|
}, [activeRouteId, routeWaypointsMap]);
|
|
|
|
const activeRouteInfoRaw = useMemo(() => {
|
|
if (!activeRouteId) return null;
|
|
return routes.find((route) => route.routeId === activeRouteId) || null;
|
|
}, [activeRouteId, routes]);
|
|
|
|
const effectiveBoatData = useMemo(() => {
|
|
if (!isPreviewing) return boatData;
|
|
if (hasBoatPosition) return boatData;
|
|
if (activeWaypointsRaw.length === 0) return null;
|
|
|
|
const previewSpeed = 2;
|
|
const totalDist = activeRouteInfoRaw?.totalDistance || 1000;
|
|
const traveled = elapsed * previewSpeed;
|
|
const fraction = Math.min(traveled / totalDist, 0.99);
|
|
const waypointIdx = Math.min(
|
|
Math.floor(fraction * (activeWaypointsRaw.length - 1)),
|
|
activeWaypointsRaw.length - 2,
|
|
);
|
|
const waypoint = activeWaypointsRaw[waypointIdx];
|
|
const nextWaypoint = activeWaypointsRaw[waypointIdx + 1] || waypoint;
|
|
|
|
const segFraction = (fraction * (activeWaypointsRaw.length - 1)) - waypointIdx;
|
|
const longitude = waypoint.longitude + (nextWaypoint.longitude - waypoint.longitude) * segFraction;
|
|
const latitude = waypoint.latitude + (nextWaypoint.latitude - waypoint.latitude) * segFraction;
|
|
|
|
const dLon = (nextWaypoint.longitude - waypoint.longitude) * Math.PI / 180;
|
|
const lat1 = waypoint.latitude * Math.PI / 180;
|
|
const lat2 = nextWaypoint.latitude * Math.PI / 180;
|
|
const y = Math.sin(dLon) * Math.cos(lat2);
|
|
const x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
|
|
const heading = ((Math.atan2(y, x) * 180 / Math.PI) + 360) % 360;
|
|
|
|
return {
|
|
deviceSn: deviceSn || '',
|
|
longitude,
|
|
latitude,
|
|
heading,
|
|
speed: previewSpeed,
|
|
battery: 85,
|
|
signalStrength: 92,
|
|
status: 'preview',
|
|
recordTime: new Date().toISOString(),
|
|
} as import('@/types/boatRoute').BoatTelemetryData;
|
|
}, [isPreviewing, boatData, hasBoatPosition, activeWaypointsRaw, activeRouteInfoRaw, elapsed, deviceSn]);
|
|
|
|
const {
|
|
activeWaypoints,
|
|
activeRouteInfo,
|
|
progress,
|
|
remainingDistance,
|
|
estimatedRemainingTime,
|
|
} = useNavigationData(activeRouteId, routes, routeWaypointsMap, effectiveBoatData, currentWaypointIndex);
|
|
|
|
if (!deviceInfo) return null;
|
|
|
|
const modeLabel = isPreviewing ? '预览中' : '执行中';
|
|
const modeTag = isPreviewing ? 'PREVIEW' : 'RUNNING';
|
|
|
|
return (
|
|
<ConfigProvider theme={darkTheme}>
|
|
<AntdApp>
|
|
{messageContextHolder}
|
|
{modalContextHolder}
|
|
<div className={`${styles.container} ${isImmersive ? styles.sailing : ''}`}>
|
|
{!isImmersive && (
|
|
<div className={styles.navBar}>
|
|
<div className={styles.navLogo}>
|
|
<div className={styles.navLogoIcon}><ShipLogoIcon /></div>
|
|
<span className={styles.navLogoText}>WAVE PILOT</span>
|
|
</div>
|
|
|
|
<div className={styles.navSpacer} />
|
|
|
|
<div className={styles.navTabs}>
|
|
<div className={`${styles.navTab} ${styles.navTabActive}`}>
|
|
<EnvironmentOutlined />
|
|
<span>航线管理</span>
|
|
</div>
|
|
<div className={styles.navTab}>
|
|
<EyeOutlined />
|
|
<span>实时监控</span>
|
|
</div>
|
|
<div className={styles.navTab}>
|
|
<HistoryOutlined />
|
|
<span>历史记录</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className={styles.navSpacer} />
|
|
|
|
<div className={styles.navRight}>
|
|
<WifiOutlined className={styles.navIconBtn} />
|
|
<ThunderboltOutlined className={styles.navIconBtn} />
|
|
<div className={styles.devIndicator}>
|
|
<span
|
|
className={styles.devDot}
|
|
style={{ background: sseConnected ? '#10b981' : '#ff4d4f' }}
|
|
/>
|
|
<span className={styles.devSn}>{deviceInfo.deviceName || deviceSn}</span>
|
|
</div>
|
|
<Button
|
|
type="text"
|
|
icon={<LogoutOutlined />}
|
|
onClick={handleLogout}
|
|
className={styles.navLogoutBtn}
|
|
size="small"
|
|
/>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{isImmersive && (
|
|
<div className={styles.execNav}>
|
|
<button
|
|
className={styles.execBackBtn}
|
|
onClick={isPreviewing ? handleStopPreview : handleStopSailing}
|
|
>
|
|
<LeftOutlined />
|
|
</button>
|
|
|
|
<div className={styles.execTitleArea}>
|
|
<div className={styles.execTitle}>
|
|
{activeRouteInfo?.routeName || '航线'} · {modeLabel}
|
|
</div>
|
|
<div className={styles.execSubtitle}>
|
|
{deviceInfo.deviceName || deviceSn} · {isPreviewing ? '预览' : '执行'} · {formatElapsed(elapsed)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className={styles.execBadge}>
|
|
<span className={styles.execBadgeDot} />
|
|
<span>{modeLabel} {modeTag}</span>
|
|
</div>
|
|
|
|
<div className={styles.execSpacer} />
|
|
|
|
<div className={styles.execRight}>
|
|
{isSailing && (
|
|
<button
|
|
className={styles.execPauseBtn}
|
|
onClick={isPaused ? handleResumeSailing : handlePauseSailing}
|
|
>
|
|
{isPaused ? <CaretRightOutlined /> : <PauseOutlined />}
|
|
<span>{isPaused ? '继续' : '暂停'}</span>
|
|
</button>
|
|
)}
|
|
<button
|
|
className={styles.execStopBtn}
|
|
onClick={isPreviewing ? handleStopPreview : handleStopSailing}
|
|
>
|
|
<StopOutlined />
|
|
<span>终止</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div className={styles.mainBody}>
|
|
{deviceOffline && !sseConnected && (
|
|
<div className={styles.offlineOverlay}>
|
|
<div className={styles.offlineCard}>
|
|
<DisconnectOutlined className={styles.offlineIcon} />
|
|
<div className={styles.offlineTitle}>设备不在线</div>
|
|
<div className={styles.offlineDesc}>
|
|
无法订阅设备 <span className={styles.offlineSn}>{deviceInfo.deviceName || deviceSn}</span> 的实时数据
|
|
<br />
|
|
{sseLoading ? '正在重新连接...' : '请检查设备是否已开机并接入网络'}
|
|
</div>
|
|
{sseLoading ? (
|
|
<Spin size="small" style={{ marginTop: 16 }} />
|
|
) : (
|
|
<Button
|
|
icon={<ReloadOutlined />}
|
|
onClick={() => connectSSE(deviceSn)}
|
|
style={{ marginTop: 16, borderColor: 'rgba(34,211,238,0.4)', color: '#22d3ee' }}
|
|
ghost
|
|
>
|
|
重试连接
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{!isImmersive && (
|
|
<div className={styles.leftPanel}>
|
|
<LeftPanel
|
|
routes={routes}
|
|
routeWaypointsMap={routeWaypointsMap}
|
|
loading={routeLoading}
|
|
selectedRouteId={selectedRouteId}
|
|
onSelectRoute={setSelectedRouteId}
|
|
deviceInfo={deviceInfo}
|
|
boatData={boatData}
|
|
sseConnected={sseConnected}
|
|
onStartSailing={handleExecuteRoute}
|
|
onPreviewRoute={handlePreviewRoute}
|
|
sailingLoading={sailingLoading}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{isImmersive && (
|
|
<ExecLeftPanel
|
|
boatData={effectiveBoatData}
|
|
waypoints={activeWaypoints}
|
|
currentWaypointIndex={currentWaypointIndex}
|
|
/>
|
|
)}
|
|
|
|
<div className={styles.centerView}>
|
|
<CenterView
|
|
deviceSn={deviceSn}
|
|
routes={routes}
|
|
selectedRouteId={selectedRouteId}
|
|
boatData={boatData}
|
|
isSailing={isSailing}
|
|
sailingLoading={sailingLoading}
|
|
previewStartSignal={previewStartSignal}
|
|
previewStopSignal={previewStopSignal}
|
|
onStartSailing={handleStartSailing}
|
|
onPreviewChange={handlePreviewChange}
|
|
/>
|
|
|
|
{isSailing && (
|
|
<ExecBottomStrip
|
|
progress={progress}
|
|
currentWaypointIndex={currentWaypointIndex}
|
|
totalWaypoints={activeWaypoints.length}
|
|
remainingDistance={remainingDistance}
|
|
estimatedRemainingTime={estimatedRemainingTime}
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
{isImmersive && (
|
|
<div className={styles.execRightPanel}>
|
|
<ExecRightPanel
|
|
deviceInfo={deviceInfo}
|
|
boatData={effectiveBoatData}
|
|
activeRouteInfo={activeRouteInfo}
|
|
sseConnected={sseConnected}
|
|
videoStreamUrl={videoStreamUrl}
|
|
isPreviewing={isPreviewing}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</AntdApp>
|
|
</ConfigProvider>
|
|
);
|
|
};
|
|
|
|
export default ClientPage;
|