fix:无人船2

This commit is contained in:
2026-04-08 03:22:34 +08:00
parent a338cf7d02
commit 4b1e695cad
12 changed files with 314 additions and 356 deletions
@@ -125,7 +125,7 @@ const ModelUploadModal: React.FC<ModelUploadModalProps> = ({
const hasTileset = items.some(
(item) =>
item.relativePath.endsWith('tileset.json') &&
!item.relativePath.includes('/Block/'),
!/\/Block[^/]*\//.test(item.relativePath),
);
if (!hasTileset) {
message.warning('未检测到根目录下的tileset.json文件,请确保选择了正确的3D Tiles文件夹');
@@ -134,10 +134,10 @@ const RightPanel: React.FC<RightPanelProps> = ({ deviceInfo, boatData, sseConnec
step={0.5}
value={targetSpeed}
onChange={setTargetSpeed}
tooltip={{ formatter: (v) => `${v} km/h` }}
tooltip={{ formatter: (v) => `${v} m/s` }}
/>
</div>
<span className={styles.speedValue}>{targetSpeed} km/h</span>
<span className={styles.speedValue}>{speed.toFixed(1)} m/s</span>
</div>
{/* 紧急停靠 */}
@@ -160,7 +160,7 @@ const RightPanel: React.FC<RightPanelProps> = ({ deviceInfo, boatData, sseConnec
<div className={styles.dataCardHeader}>船舶状态</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>速度</span>
<span className={styles.dataValue}>{speed.toFixed(1)} km/h</span>
<span className={styles.dataValue}>{speed.toFixed(1)} m/s</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>方向</span>
@@ -11,6 +11,7 @@ export interface UseClientSSEReturn {
boatData: BoatTelemetryData | null;
sseConnected: boolean;
loading: boolean;
deviceOffline: boolean;
connectSSE: (deviceSn: string) => Promise<void>;
disconnectSSE: (deviceSn?: string) => Promise<void>;
}
@@ -19,6 +20,7 @@ export const useClientSSE = (): UseClientSSEReturn => {
const [boatData, setBoatData] = useState<BoatTelemetryData | null>(null);
const [sseConnected, setSSEConnected] = useState(false);
const [loading, setLoading] = useState(false);
const [deviceOffline, setDeviceOffline] = useState(false);
const abortControllerRef = useRef<AbortController | null>(null);
const currentDeviceSnRef = useRef<string>('');
@@ -71,6 +73,7 @@ export const useClientSSE = (): UseClientSSEReturn => {
setSSEConnected(false);
setBoatData(null);
setDeviceOffline(false);
currentDeviceSnRef.current = '';
}, []);
@@ -124,6 +127,7 @@ export const useClientSSE = (): UseClientSSEReturn => {
setSSEConnected(true);
setLoading(false);
setDeviceOffline(false);
reconnectAttemptsRef.current = 0;
const reader = response.body?.getReader();
@@ -149,6 +153,7 @@ export const useClientSSE = (): UseClientSSEReturn => {
const { done, value } = await reader.read();
if (done) {
setSSEConnected(false);
setDeviceOffline(true);
if (shouldReconnectRef.current && currentDeviceSnRef.current === deviceSn) {
scheduleReconnect(deviceSn);
}
@@ -180,6 +185,7 @@ export const useClientSSE = (): UseClientSSEReturn => {
if (error?.name === 'AbortError') return;
console.error('[ClientSSE] 读取流错误:', error);
setSSEConnected(false);
setDeviceOffline(true);
if (shouldReconnectRef.current && currentDeviceSnRef.current === deviceSn) {
scheduleReconnect(deviceSn);
}
@@ -191,6 +197,7 @@ export const useClientSSE = (): UseClientSSEReturn => {
if (error?.name === 'AbortError') return;
console.error('[ClientSSE] 连接失败:', error);
setSSEConnected(false);
setDeviceOffline(true);
setLoading(false);
if (shouldReconnectRef.current && currentDeviceSnRef.current === deviceSn) {
scheduleReconnect(deviceSn);
@@ -224,5 +231,5 @@ export const useClientSSE = (): UseClientSSEReturn => {
};
}, []);
return { boatData, sseConnected, loading, connectSSE, disconnectSSE };
return { boatData, sseConnected, loading, deviceOffline, connectSSE, disconnectSSE };
};
@@ -1,107 +0,0 @@
/**
* 模拟遥测数据生成
* SSE 未连接时自动生成模拟船只遥测数据
*/
import { useEffect, useState, useCallback, useRef } from 'react';
import type { BoatTelemetryData } from '@/types/boatRoute';
/** 生成一帧模拟遥测 */
const createMockTelemetry = (
deviceSn: string,
prevData?: BoatTelemetryData | null,
initLon?: number,
initLat?: number,
): BoatTelemetryData => {
const baseLon = prevData?.longitude ?? initLon ?? 108.366;
const baseLat = prevData?.latitude ?? initLat ?? 22.817;
const baseHeading = prevData?.heading ?? 45;
const headingRad = (baseHeading + (Math.random() - 0.5) * 10) * Math.PI / 180;
const moveDistance = 0.00002 + Math.random() * 0.00003;
const newLon = baseLon + Math.cos(headingRad) * moveDistance;
const newLat = baseLat + Math.sin(headingRad) * moveDistance;
const newHeading = ((baseHeading + (Math.random() - 0.5) * 10) + 360) % 360;
return {
deviceSn,
longitude: newLon,
latitude: newLat,
heading: Number(newHeading.toFixed(1)),
speed: Number((1.5 + Math.random() * 2).toFixed(1)),
battery: Math.max(20, (prevData?.battery ?? 85) - Math.random() * 0.1),
signalStrength: 60 + Math.floor(Math.random() * 30),
status: '巡航中',
recordTime: new Date().toISOString(),
taskId: undefined,
currentWaypointIndex: undefined,
};
};
interface UseMockTelemetryReturn {
/** 合并后的遥测数据(SSE 优先,回退到模拟) */
boatData: BoatTelemetryData | null;
/** 模型中心位���回调,���于重置模拟数据起点 */
handleModelCenter: (lon: number, lat: number) => void;
}
export function useMockTelemetry(
deviceSn: string,
sseBoatData: BoatTelemetryData | null,
): UseMockTelemetryReturn {
const [mockBoatData, setMockBoatData] = useState<BoatTelemetryData | null>(null);
const mockTimerRef = useRef<NodeJS.Timeout | null>(null);
const mockDataRef = useRef<BoatTelemetryData | null>(null);
const modelCenterRef = useRef<{ lon: number; lat: number } | null>(null);
const handleModelCenter = useCallback((lon: number, lat: number) => {
modelCenterRef.current = { lon, lat };
if (mockDataRef.current) {
const newData = {
...mockDataRef.current,
longitude: lon + (Math.random() - 0.5) * 0.001,
latitude: lat + (Math.random() - 0.5) * 0.001,
};
mockDataRef.current = newData;
setMockBoatData(newData);
}
}, []);
useEffect(() => {
// 有真实 SSE 数据时停止模拟
if (sseBoatData) {
if (mockTimerRef.current) {
clearInterval(mockTimerRef.current);
mockTimerRef.current = null;
}
setMockBoatData(null);
mockDataRef.current = null;
return;
}
// 无真实数据且无模拟定时器,启动模拟
if (!mockTimerRef.current && deviceSn) {
const center = modelCenterRef.current;
const firstData = createMockTelemetry(deviceSn, null, center?.lon, center?.lat);
mockDataRef.current = firstData;
setMockBoatData(firstData);
mockTimerRef.current = setInterval(() => {
const center2 = modelCenterRef.current;
const newData = createMockTelemetry(deviceSn, mockDataRef.current, center2?.lon, center2?.lat);
mockDataRef.current = newData;
setMockBoatData(newData);
}, 2000);
}
return () => {
if (mockTimerRef.current) {
clearInterval(mockTimerRef.current);
mockTimerRef.current = null;
}
};
}, [sseBoatData, deviceSn]);
const boatData = sseBoatData || mockBoatData;
return { boatData, handleModelCenter };
}
@@ -144,6 +144,58 @@
50% { opacity: 0.5; }
}
/* ========== 设备离线遮罩 ========== */
.offlineOverlay {
position: absolute;
inset: 0;
z-index: 200;
display: flex;
align-items: center;
justify-content: center;
background: rgba(4, 10, 20, 0.75);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
}
.offlineCard {
display: flex;
flex-direction: column;
align-items: center;
padding: 40px 48px;
background: linear-gradient(135deg, rgba(14, 30, 56, 0.95), rgba(8, 18, 36, 0.98));
border: 1px solid rgba(255, 80, 80, 0.3);
border-radius: 12px;
box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.04);
text-align: center;
max-width: 360px;
}
.offlineIcon {
font-size: 40px;
color: #ff4d4f;
margin-bottom: 16px;
opacity: 0.9;
}
.offlineTitle {
font-size: 20px;
font-weight: 600;
color: #fff;
letter-spacing: 1px;
margin-bottom: 12px;
}
.offlineDesc {
font-size: 13px;
color: rgba(255, 255, 255, 0.5);
line-height: 1.8;
}
.offlineSn {
color: @accent-cyan;
font-family: 'Rajdhani', monospace;
}
/* ========== 滚动条 ========== */
.leftPanel::-webkit-scrollbar,
.rightPanel::-webkit-scrollbar {
+30 -7
View File
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react';
import { ConfigProvider, Button } from 'antd';
import { LogoutOutlined } from '@ant-design/icons';
import { ConfigProvider, Button, Spin } from 'antd';
import { LogoutOutlined, DisconnectOutlined, ReloadOutlined } from '@ant-design/icons';
import { darkTheme } from '@/theme';
import LeftPanel from './components/LeftPanel';
import CenterView from './components/CenterView';
@@ -11,22 +11,20 @@ import EmergencyButton from './components/EmergencyButton';
import { useClientAuth } from './hooks/useClientAuth';
import { useClientSSE } from './hooks/useClientSSE';
import { useRouteData } from './hooks/useRouteData';
import { useMockTelemetry } from './hooks/useMockTelemetry';
import { useNavigationState } from './hooks/useNavigationState';
import { useNavigationData } from './hooks/useNavigationData';
import styles from './index.less';
const ClientPage: React.FC = () => {
// ── 认证 ──
const { boatData: sseBoatData, sseConnected, connectSSE, disconnectSSE } = useClientSSE();
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, handleModelCenter } = useMockTelemetry(deviceSn, sseBoatData);
const boatData = sseBoatData;
// ── 沉浸模式状态机 ──
const {
@@ -70,6 +68,32 @@ const ClientPage: React.FC = () => {
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 ? '正在重新连接...' : '请检查设备是否已开机并接入网络'}
</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>
</div>
)}
{/* 左侧游览线路 */}
<div className={styles.leftPanel}>
<LeftPanel
@@ -88,7 +112,6 @@ const ClientPage: React.FC = () => {
routes={routes}
selectedRouteId={selectedRouteId}
boatData={boatData}
onModelCenter={handleModelCenter}
isSailing={isSailing}
sailingLoading={sailingLoading}
previewStopSignal={previewStopSignal}