feat: 物联网卡管理
This commit is contained in:
@@ -1,181 +1,62 @@
|
||||
import React, { useEffect, useState, useMemo, useCallback, useRef } from 'react';
|
||||
import { history } from 'umi';
|
||||
import { ConfigProvider, message, Button, Modal } from 'antd';
|
||||
import React, { useEffect } from 'react';
|
||||
import { ConfigProvider, Button } from 'antd';
|
||||
import { LogoutOutlined } 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 { useClientAuth } from './hooks/useClientAuth';
|
||||
import { useClientSSE } from './hooks/useClientSSE';
|
||||
import { getClientRoutes } from './services/clientApi';
|
||||
import { getBoatRouteDetail } from '@/services/boatRoute';
|
||||
import type { BoatRouteListVO, BoatTelemetryData, BoatWaypointVO } from '@/types/boatRoute';
|
||||
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 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; // ~2-5m
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
||||
const ClientPage: React.FC = () => {
|
||||
const [deviceInfo, setDeviceInfo] = useState<any>(null);
|
||||
const [routes, setRoutes] = useState<BoatRouteListVO[]>([]);
|
||||
const [routeWaypointsMap, setRouteWaypointsMap] = useState<Record<string, BoatWaypointVO[]>>({});
|
||||
const [selectedRouteId, setSelectedRouteId] = useState<string | null>(null);
|
||||
const [routeLoading, setRouteLoading] = useState(false);
|
||||
|
||||
// ── 认证 ──
|
||||
const { boatData: sseBoatData, sseConnected, connectSSE, disconnectSSE } = useClientSSE();
|
||||
const { deviceSn, deviceInfo, handleLogout } = useClientAuth(disconnectSSE);
|
||||
|
||||
const deviceSn = useMemo(() => sessionStorage.getItem('client_device_sn') || '', []);
|
||||
// ── 航线数据 ──
|
||||
const { routes, routeWaypointsMap, selectedRouteId, setSelectedRouteId, routeLoading } =
|
||||
useRouteData(deviceSn);
|
||||
|
||||
// 模拟数据:SSE未连接时自动生成模拟遥测数据
|
||||
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 { boatData, handleModelCenter } = useMockTelemetry(deviceSn, sseBoatData);
|
||||
|
||||
// 模型中心位置回调:当模型加载完成后,重置模拟数据起始位置到模型附近
|
||||
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);
|
||||
}
|
||||
}, []);
|
||||
// ── 沉浸模式状态机 ──
|
||||
const {
|
||||
isPaused,
|
||||
currentWaypointIndex,
|
||||
previewStopSignal,
|
||||
isSailing,
|
||||
isPreviewing,
|
||||
isImmersive,
|
||||
activeRouteId,
|
||||
handleStartSailing,
|
||||
handlePauseSailing,
|
||||
handleResumeSailing,
|
||||
handleStopSailing,
|
||||
handleStopPreview,
|
||||
handleEmergencyStop,
|
||||
handlePreviewChange,
|
||||
} = useNavigationState(deviceSn, selectedRouteId, setSelectedRouteId);
|
||||
|
||||
// 启动/停止模拟数据:当真实SSE数据到达时停止模拟,否则持续模拟
|
||||
useEffect(() => {
|
||||
// 有真实SSE数据,停止模拟
|
||||
if (sseBoatData) {
|
||||
if (mockTimerRef.current) {
|
||||
clearInterval(mockTimerRef.current);
|
||||
mockTimerRef.current = null;
|
||||
}
|
||||
setMockBoatData(null);
|
||||
mockDataRef.current = null;
|
||||
return;
|
||||
}
|
||||
// ── 导航计算 ──
|
||||
const {
|
||||
activeWaypoints,
|
||||
activeRouteInfo,
|
||||
nextWaypoint,
|
||||
remainingDistance,
|
||||
estimatedRemainingTime,
|
||||
progress,
|
||||
} = useNavigationData(activeRouteId, routes, routeWaypointsMap, boatData, currentWaypointIndex);
|
||||
|
||||
// 无真实数据且无模拟定时器,启动模拟
|
||||
if (!mockTimerRef.current && deviceSn) {
|
||||
console.log('[Mock] 启动模拟数据');
|
||||
// 立即生成第一帧
|
||||
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]);
|
||||
|
||||
// 优先使用真实SSE数据,回退到模拟数据
|
||||
const boatData = sseBoatData || mockBoatData;
|
||||
|
||||
// 退出登录
|
||||
const handleLogout = useCallback(() => {
|
||||
Modal.confirm({
|
||||
title: '确认退出',
|
||||
content: '退出后需要重新输入设备SN登录',
|
||||
okText: '退出',
|
||||
cancelText: '取消',
|
||||
onOk: () => {
|
||||
disconnectSSE();
|
||||
sessionStorage.removeItem('client_device_sn');
|
||||
sessionStorage.removeItem('client_device_info');
|
||||
history.push('/client/login');
|
||||
},
|
||||
});
|
||||
}, [disconnectSSE]);
|
||||
|
||||
// 校验登录状态
|
||||
useEffect(() => {
|
||||
const sn = sessionStorage.getItem('client_device_sn');
|
||||
const info = sessionStorage.getItem('client_device_info');
|
||||
if (!sn || !info) {
|
||||
history.push('/client/login');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setDeviceInfo(JSON.parse(info));
|
||||
} catch {
|
||||
history.push('/client/login');
|
||||
}
|
||||
}, []);
|
||||
|
||||
// 加载游览线路列表 + 预加载航点详情
|
||||
useEffect(() => {
|
||||
if (!deviceSn) return;
|
||||
setRouteLoading(true);
|
||||
getClientRoutes(deviceSn)
|
||||
.then(async (res) => {
|
||||
if (res.resp_code === 0 && res.data) {
|
||||
setRoutes(res.data);
|
||||
// 并行预加载所有线路的航点详情
|
||||
const detailResults = await Promise.allSettled(
|
||||
res.data.map((r) => getBoatRouteDetail(r.routeId)),
|
||||
);
|
||||
const wpMap: Record<string, BoatWaypointVO[]> = {};
|
||||
detailResults.forEach((result, idx) => {
|
||||
if (result.status === 'fulfilled' && result.value.resp_code === 0 && result.value.data?.waypoints) {
|
||||
wpMap[res.data[idx].routeId] = result.value.data.waypoints;
|
||||
}
|
||||
});
|
||||
setRouteWaypointsMap(wpMap);
|
||||
}
|
||||
})
|
||||
.catch(() => message.error('线路加载失败'))
|
||||
.finally(() => setRouteLoading(false));
|
||||
}, [deviceSn]);
|
||||
|
||||
// 连接SSE
|
||||
// ── SSE 连接 ──
|
||||
useEffect(() => {
|
||||
if (!deviceSn) return;
|
||||
connectSSE(deviceSn);
|
||||
@@ -184,9 +65,10 @@ const ClientPage: React.FC = () => {
|
||||
|
||||
if (!deviceInfo) return null;
|
||||
|
||||
// ── 渲染 ──
|
||||
return (
|
||||
<ConfigProvider theme={darkTheme}>
|
||||
<div className={styles.container}>
|
||||
<div className={`${styles.container} ${isImmersive ? styles.sailing : ''}`}>
|
||||
{/* 左侧游览线路 */}
|
||||
<div className={styles.leftPanel}>
|
||||
<LeftPanel
|
||||
@@ -206,16 +88,56 @@ const ClientPage: React.FC = () => {
|
||||
selectedRouteId={selectedRouteId}
|
||||
boatData={boatData}
|
||||
onModelCenter={handleModelCenter}
|
||||
isSailing={isSailing}
|
||||
previewStopSignal={previewStopSignal}
|
||||
onStartSailing={handleStartSailing}
|
||||
onPreviewChange={handlePreviewChange}
|
||||
/>
|
||||
|
||||
{/* 沉浸模式浮层(航行或预览) */}
|
||||
{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}
|
||||
/>
|
||||
)}
|
||||
{isSailing && (
|
||||
<EmergencyButton onEmergencyStop={handleEmergencyStop} deviceSn={deviceSn} />
|
||||
)}
|
||||
<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}
|
||||
remainingDistance={remainingDistance}
|
||||
estimatedRemainingTime={estimatedRemainingTime}
|
||||
progress={progress}
|
||||
isPaused={isPaused && isSailing}
|
||||
onPause={handlePauseSailing}
|
||||
onResume={handleResumeSailing}
|
||||
onStop={isPreviewing ? handleStopPreview : handleStopSailing}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 右侧设备信息 */}
|
||||
<div className={styles.rightPanel}>
|
||||
<RightPanel
|
||||
deviceInfo={deviceInfo}
|
||||
boatData={boatData}
|
||||
sseConnected={sseConnected}
|
||||
/>
|
||||
<RightPanel deviceInfo={deviceInfo} boatData={boatData} sseConnected={sseConnected} />
|
||||
</div>
|
||||
|
||||
{/* 底部状态栏 */}
|
||||
|
||||
Reference in New Issue
Block a user