feat:平板页面

This commit is contained in:
2026-04-13 19:17:47 +08:00
parent 90ef9cf146
commit bca4d3ae43
37 changed files with 2987 additions and 652 deletions
+297 -116
View File
@@ -1,13 +1,24 @@
import React, { useEffect } from 'react';
import React, { useEffect, useCallback, useState, useRef, useMemo } from 'react';
import { ConfigProvider, Button, Spin } from 'antd';
import { LogoutOutlined, DisconnectOutlined, ReloadOutlined } from '@ant-design/icons';
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 RightPanel from './components/RightPanel';
import NavigationHUD from './components/NavigationHUD';
import NavigationOverlay from './components/NavigationOverlay';
import EmergencyButton from './components/EmergencyButton';
import ExecLeftPanel from './components/ExecLeftPanel';
import ExecBottomStrip from './components/ExecBottomStrip';
import { useClientAuth } from './hooks/useClientAuth';
import { useClientSSE } from './hooks/useClientSSE';
import { useRouteData } from './hooks/useRouteData';
@@ -15,6 +26,24 @@ import { useNavigationState } from './hooks/useNavigationState';
import { useNavigationData } from './hooks/useNavigationData';
import styles from './index.less';
/** 船 Logo SVG */
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>
);
/** 格式化秒数为 HH:MM:SS */
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 ClientPage: React.FC = () => {
// ── 认证 ──
const { boatData: sseBoatData, sseConnected, loading: sseLoading, deviceOffline, connectSSE, disconnectSSE } = useClientSSE();
@@ -45,16 +74,6 @@ const ClientPage: React.FC = () => {
handlePreviewChange,
} = useNavigationState(deviceSn, selectedRouteId, setSelectedRouteId, routes);
// ── 导航计算 ──
const {
activeWaypoints,
activeRouteInfo,
nextWaypoint,
remainingDistance,
estimatedRemainingTime,
progress,
} = useNavigationData(activeRouteId, routes, routeWaypointsMap, boatData, currentWaypointIndex);
// ── SSE 连接 ──
useEffect(() => {
if (!deviceSn) return;
@@ -62,123 +81,285 @@ const ClientPage: React.FC = () => {
return () => { disconnectSSE(); };
}, [deviceSn]);
// ── 沉浸模式计时器 ──
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((s) => s + 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);
}
// 延迟递增信号,让 selectedRouteId 先生效触发航点加载
setTimeout(() => setPreviewStartSignal((s) => s + 1), 200);
}, [selectedRouteId, setSelectedRouteId]);
// ── 预先计算航线基础信息(不依赖 boatData) ──
const activeWaypointsRaw = useMemo(() => {
if (!activeRouteId) return [];
return routeWaypointsMap[activeRouteId] || [];
}, [activeRouteId, routeWaypointsMap]);
const activeRouteInfoRaw = useMemo(() => {
if (!activeRouteId) return null;
return routes.find((r) => r.routeId === activeRouteId) || null;
}, [activeRouteId, routes]);
// ── 预览模式假数据 ──
const effectiveBoatData = useMemo(() => {
if (!isPreviewing) return boatData;
// 如果有真实 SSE 数据,优先使用
if (boatData?.longitude) return boatData;
// 预览模式下根据航线航点和经过时间生成模拟遥测数据
if (activeWaypointsRaw.length === 0) return null;
const previewSpeed = 2; // m/s,与预览动画速度一致
const totalDist = activeRouteInfoRaw?.totalDistance || 1000;
const traveled = elapsed * previewSpeed;
const fraction = Math.min(traveled / totalDist, 0.99);
const wpIdx = Math.min(
Math.floor(fraction * (activeWaypointsRaw.length - 1)),
activeWaypointsRaw.length - 2,
);
const wp = activeWaypointsRaw[wpIdx];
const nextWp = activeWaypointsRaw[wpIdx + 1] || wp;
// 航段内插值
const segFrac = (fraction * (activeWaypointsRaw.length - 1)) - wpIdx;
const lon = wp.longitude + (nextWp.longitude - wp.longitude) * segFrac;
const lat = wp.latitude + (nextWp.latitude - wp.latitude) * segFrac;
// 计算航向
const dLon = (nextWp.longitude - wp.longitude) * Math.PI / 180;
const lat1 = wp.latitude * Math.PI / 180;
const lat2 = nextWp.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: lon,
latitude: lat,
heading,
speed: previewSpeed,
battery: 85,
signalStrength: 92,
status: 'preview',
recordTime: new Date().toISOString(),
} as import('@/types/boatRoute').BoatTelemetryData;
}, [isPreviewing, boatData, activeWaypointsRaw, activeRouteInfoRaw, elapsed, deviceSn]);
// ── 导航计算(使用 effectiveBoatData 以便预览模式也有数据) ──
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}>
<div className={`${styles.container} ${isImmersive ? styles.sailing : ''}`}>
{/* 设备离线遮罩 */}
{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 ? '正在重新连接...' : '请检查设备是否已开机并接入网络'}
{/* ========== 普通模式 - 顶部导航栏 ========== */}
{!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>
{sseLoading ? (
<Spin size="small" style={{ marginTop: 16 }} />
) : (
<Button
icon={<ReloadOutlined />}
onClick={() => connectSSE(deviceSn)}
style={{ marginTop: 16, borderColor: 'rgba(0,212,255,0.4)', color: '#00d4ff' }}
ghost
>
重试连接
</Button>
)}
<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>
)}
{/* 左侧游览线路 */}
<div className={styles.leftPanel}>
<LeftPanel
routes={routes}
routeWaypointsMap={routeWaypointsMap}
loading={routeLoading}
selectedRouteId={selectedRouteId}
onSelectRoute={setSelectedRouteId}
/>
</div>
{/* 中间三维视图 */}
<div className={styles.centerView}>
<CenterView
deviceSn={deviceSn}
routes={routes}
selectedRouteId={selectedRouteId}
boatData={boatData}
isSailing={isSailing}
sailingLoading={sailingLoading}
previewStopSignal={previewStopSignal}
onStartSailing={handleStartSailing}
onPreviewChange={handlePreviewChange}
/>
{/* ========== 沉浸模式 - 执行导航栏 ========== */}
{isImmersive && (
<div className={styles.execNav}>
<button
className={styles.execBackBtn}
onClick={isPreviewing ? handleStopPreview : handleStopSailing}
>
<LeftOutlined />
</button>
{/* 沉浸模式浮层(航行或预览) */}
{isImmersive && (
<>
{!!activeRouteId && (
<NavigationOverlay
mode={isPreviewing ? 'preview' : 'sailing'}
routeName={activeRouteInfo?.routeName || ''}
waypoints={activeWaypoints.map((wp) => ({
name: wp.pointName || `航点${wp.waypointIndex + 1}`,
waypointIndex: wp.waypointIndex,
}))}
currentWaypointIndex={boatData?.currentWaypointIndex ?? currentWaypointIndex}
totalDistance={activeRouteInfo?.totalDistance ?? 0}
remainingDistance={remainingDistance}
estimatedRemainingTime={estimatedRemainingTime}
progress={progress}
/>
)}
<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 && (
<EmergencyButton onEmergencyStop={handleEmergencyStop} deviceSn={deviceSn} />
<button
className={styles.execPauseBtn}
onClick={isPaused ? handleResumeSailing : handlePauseSailing}
>
{isPaused ? <CaretRightOutlined /> : <PauseOutlined />}
<span>{isPaused ? '继续' : '暂停'}</span>
</button>
)}
<NavigationHUD
mode={isPreviewing ? 'preview' : 'sailing'}
routeName={activeRouteInfo?.routeName || ''}
speed={boatData?.speed ?? 0}
heading={boatData?.heading ?? 0}
battery={boatData?.battery ?? 0}
signalStrength={boatData?.signalStrength}
nextWaypoint={nextWaypoint}
<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={handleStartSailing}
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}
/>
{/* 沉浸模式 - 底部航线信息条 */}
{isImmersive && (
<ExecBottomStrip
progress={progress}
currentWaypointIndex={currentWaypointIndex}
totalWaypoints={activeWaypoints.length}
remainingDistance={remainingDistance}
estimatedRemainingTime={estimatedRemainingTime}
progress={progress}
isPaused={isPaused && isSailing}
onPause={handlePauseSailing}
onResume={handleResumeSailing}
onStop={isPreviewing ? handleStopPreview : handleStopSailing}
/>
</>
)}
</div>
)}
</div>
{/* 右侧设备信息 */}
<div className={styles.rightPanel}>
<RightPanel deviceInfo={deviceInfo} boatData={boatData} sseConnected={sseConnected} />
</div>
{/* 底部状态栏 */}
<div className={styles.statusBar}>
<span className={styles.statusDot} style={{ background: sseConnected ? '#00ff88' : '#ff4d4f' }} />
<span>{sseConnected ? 'SSE 已连接' : 'SSE 未连接'}</span>
<span style={{ marginLeft: 16 }}>设备: {deviceInfo.deviceName || deviceSn}</span>
<Button
type="text"
icon={<LogoutOutlined />}
onClick={handleLogout}
style={{ marginLeft: 'auto', color: 'rgba(255,255,255,0.6)', fontSize: 12 }}
size="small"
>
退出
</Button>
</div>
</div>
</ConfigProvider>