fix:无人船
This commit is contained in:
@@ -19,12 +19,13 @@ interface CenterViewProps {
|
||||
boatData: BoatTelemetryData | null;
|
||||
onModelCenter?: (lon: number, lat: number) => void;
|
||||
isSailing?: boolean;
|
||||
sailingLoading?: boolean;
|
||||
previewStopSignal?: number;
|
||||
onStartSailing?: (routeId: string) => void;
|
||||
onStartSailing?: (routeId: string) => Promise<void>;
|
||||
onPreviewChange?: (previewing: boolean) => void;
|
||||
}
|
||||
|
||||
const CenterView: React.FC<CenterViewProps> = ({ deviceSn, routes, selectedRouteId, boatData, onModelCenter, isSailing, previewStopSignal, onStartSailing, onPreviewChange }) => {
|
||||
const CenterView: React.FC<CenterViewProps> = ({ deviceSn, routes, selectedRouteId, boatData, onModelCenter, isSailing, sailingLoading, previewStopSignal, onStartSailing, onPreviewChange }) => {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const viewerRef = useRef<any>(null);
|
||||
const [viewerReady, setViewerReady] = useState(false);
|
||||
@@ -447,10 +448,12 @@ const CenterView: React.FC<CenterViewProps> = ({ deviceSn, routes, selectedRoute
|
||||
<Button
|
||||
icon={<PlayCircleOutlined />}
|
||||
onClick={handleExecuteRoute}
|
||||
loading={sailingLoading}
|
||||
disabled={sailingLoading}
|
||||
size="small"
|
||||
style={{ borderColor: '#00ff88', color: '#00ff88' }}
|
||||
style={{ borderColor: '#00ff88', color: sailingLoading ? undefined : '#00ff88' }}
|
||||
>
|
||||
开始航行
|
||||
{sailingLoading ? '启动中...' : '开始航行'}
|
||||
</Button>
|
||||
</Space>
|
||||
</div>
|
||||
|
||||
@@ -174,7 +174,7 @@ const RightPanel: React.FC<RightPanelProps> = ({ deviceInfo, boatData, sseConnec
|
||||
</div>
|
||||
<div className={styles.dataRow}>
|
||||
<span className={styles.dataLabel}>模式</span>
|
||||
<span className={styles.dataValue}>{boatData?.taskId ? '自动' : '手动'}</span>
|
||||
<span className={styles.dataValue}>{boatData?.taskId ? '自动巡航' : '待机'}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -183,15 +183,19 @@ const RightPanel: React.FC<RightPanelProps> = ({ deviceInfo, boatData, sseConnec
|
||||
<div className={`${styles.dataCardHeader} ${styles.dataCardHeaderGreen}`}>环境数据</div>
|
||||
<div className={styles.dataRow}>
|
||||
<span className={styles.dataLabel}>水质</span>
|
||||
<span className={styles.dataValue} style={{ color: '#00d4aa' }}>优</span>
|
||||
<span className={styles.dataValue} style={{ color: '#00d4aa' }}>
|
||||
{boatData?.status ? '正常' : '--'}
|
||||
</span>
|
||||
</div>
|
||||
<div className={styles.dataRow}>
|
||||
<span className={styles.dataLabel}>风速</span>
|
||||
<span className={styles.dataValue}>{(1.5 + (boatData?.speed ?? 0) * 0.3).toFixed(1)} m/s</span>
|
||||
<span className={styles.dataLabel}>状态</span>
|
||||
<span className={styles.dataValue}>{boatData?.status || '--'}</span>
|
||||
</div>
|
||||
<div className={styles.dataRow}>
|
||||
<span className={styles.dataLabel}>温度</span>
|
||||
<span className={styles.dataValue}>{(18 + Math.round((boatData?.heading ?? 0) % 5)).toFixed(0)}°C</span>
|
||||
<span className={styles.dataLabel}>更新时间</span>
|
||||
<span className={styles.dataValue} style={{ fontSize: 10 }}>
|
||||
{boatData?.recordTime ? new Date(boatData.recordTime).toLocaleTimeString('zh-CN') : '--'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
/**
|
||||
* 沉浸模式状态机
|
||||
* 管理 sailing / preview / idle 三态切换及相关操作回调
|
||||
* 开始航行:创建无人船任务(saveMission,missionType=3)后自动下发执行
|
||||
* 暂停/恢复/结束:调用真实无人船控制API,携带missionCode
|
||||
*/
|
||||
import { useState, useCallback } from 'react';
|
||||
import { useState, useCallback, useRef } from 'react';
|
||||
import { Modal, message } from 'antd';
|
||||
import { sendBoatCommand } from '../services/clientApi';
|
||||
import {
|
||||
createBoatMission,
|
||||
pauseBoatMission,
|
||||
resumeBoatMission,
|
||||
undoBoatMission,
|
||||
} from '../services/clientApi';
|
||||
import type { BoatRouteListVO } from '@/types/boatRoute';
|
||||
|
||||
type ActiveRouteMode = 'sailing' | 'preview' | null;
|
||||
|
||||
@@ -22,7 +30,9 @@ interface UseNavigationStateReturn {
|
||||
isPreviewing: boolean;
|
||||
isImmersive: boolean;
|
||||
activeRouteId: string | null;
|
||||
handleStartSailing: (routeId: string) => void;
|
||||
missionCode: string | null;
|
||||
sailingLoading: boolean;
|
||||
handleStartSailing: (routeId: string) => Promise<void>;
|
||||
handlePauseSailing: () => void;
|
||||
handleResumeSailing: () => void;
|
||||
handleStopSailing: () => void;
|
||||
@@ -35,54 +45,119 @@ export function useNavigationState(
|
||||
deviceSn: string,
|
||||
selectedRouteId: string | null,
|
||||
setSelectedRouteId: (id: string | null) => void,
|
||||
routes: BoatRouteListVO[],
|
||||
): UseNavigationStateReturn {
|
||||
const [activeRoute, setActiveRoute] = useState<ActiveRouteState>({ mode: null, routeId: null });
|
||||
const [isPaused, setIsPaused] = useState(false);
|
||||
const [currentWaypointIndex, setCurrentWaypointIndex] = useState(0);
|
||||
const [previewStopSignal, setPreviewStopSignal] = useState(0);
|
||||
const [missionCode, setMissionCode] = useState<string | null>(null);
|
||||
const [sailingLoading, setSailingLoading] = useState(false);
|
||||
|
||||
// 用 ref 保持最新 missionCode,供回调闭包使用
|
||||
const missionCodeRef = useRef<string | null>(null);
|
||||
|
||||
const isSailing = activeRoute.mode === 'sailing';
|
||||
const isPreviewing = activeRoute.mode === 'preview';
|
||||
const isImmersive = activeRoute.mode !== null;
|
||||
const activeRouteId = activeRoute.routeId;
|
||||
|
||||
const handleStartSailing = useCallback((routeId: string) => {
|
||||
if (activeRoute.mode === 'preview') {
|
||||
setPreviewStopSignal((prev) => prev + 1);
|
||||
const handleStartSailing = useCallback(async (routeId: string) => {
|
||||
if (!deviceSn) {
|
||||
message.error('设备SN缺失,无法开始航行');
|
||||
return;
|
||||
}
|
||||
setActiveRoute({ mode: 'sailing', routeId });
|
||||
setSelectedRouteId(routeId);
|
||||
setIsPaused(false);
|
||||
setCurrentWaypointIndex(0);
|
||||
}, [activeRoute.mode, setSelectedRouteId]);
|
||||
const route = routes.find((r) => r.routeId === routeId);
|
||||
if (!route) {
|
||||
message.error('航线信息不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
setSailingLoading(true);
|
||||
try {
|
||||
// 停止当前预览
|
||||
if (activeRoute.mode === 'preview') {
|
||||
setPreviewStopSignal((prev) => prev + 1);
|
||||
}
|
||||
|
||||
// 创建无人船任务(后端 missionType=3 会自动执行)
|
||||
const res = await createBoatMission({
|
||||
deviceSn,
|
||||
boatRouteId: route.routeId,
|
||||
boatRouteName: route.routeName,
|
||||
});
|
||||
|
||||
if (res.resp_code !== 0) {
|
||||
message.error(res.resp_msg || '创建任务失败');
|
||||
return;
|
||||
}
|
||||
|
||||
const code = res.data?.missionCode;
|
||||
if (!code) {
|
||||
message.error('任务创建成功但未返回任务编码');
|
||||
return;
|
||||
}
|
||||
|
||||
missionCodeRef.current = code;
|
||||
setMissionCode(code);
|
||||
setActiveRoute({ mode: 'sailing', routeId });
|
||||
setSelectedRouteId(routeId);
|
||||
setIsPaused(false);
|
||||
setCurrentWaypointIndex(0);
|
||||
message.success('航行任务已启动');
|
||||
} catch (e: any) {
|
||||
message.error(e?.message || '启动航行失败,请重试');
|
||||
} finally {
|
||||
setSailingLoading(false);
|
||||
}
|
||||
}, [deviceSn, routes, activeRoute.mode, setSelectedRouteId]);
|
||||
|
||||
const handlePauseSailing = useCallback(() => {
|
||||
if (!isSailing) return;
|
||||
const code = missionCodeRef.current;
|
||||
if (!code) {
|
||||
message.warning('任务编码缺失,无法暂停');
|
||||
return;
|
||||
}
|
||||
setIsPaused(true);
|
||||
sendBoatCommand(deviceSn, 'pause', 'press').catch(() => {
|
||||
pauseBoatMission(deviceSn, code).catch(() => {
|
||||
message.error('暂停指令发送失败');
|
||||
setIsPaused(false);
|
||||
});
|
||||
}, [deviceSn, isSailing]);
|
||||
|
||||
const handleResumeSailing = useCallback(() => {
|
||||
if (!isSailing) return;
|
||||
const code = missionCodeRef.current;
|
||||
if (!code) {
|
||||
message.warning('任务编码缺失,无法恢复');
|
||||
return;
|
||||
}
|
||||
setIsPaused(false);
|
||||
sendBoatCommand(deviceSn, 'resume', 'press').catch(() => {
|
||||
message.error('继续指令发送失败');
|
||||
resumeBoatMission(deviceSn, code).catch(() => {
|
||||
message.error('恢复航行指令发送失败');
|
||||
setIsPaused(true);
|
||||
});
|
||||
}, [deviceSn, isSailing]);
|
||||
|
||||
const handleStopSailing = useCallback(() => {
|
||||
const code = missionCodeRef.current;
|
||||
Modal.confirm({
|
||||
title: '确认结束航行',
|
||||
content: '结束后将退��导航模式,返回线路选择',
|
||||
content: '结束后将退出导航模式,返回线路选择',
|
||||
okText: '结束航行',
|
||||
cancelText: '取消',
|
||||
onOk: () => {
|
||||
setActiveRoute({ mode: null, routeId: null });
|
||||
setIsPaused(false);
|
||||
setCurrentWaypointIndex(0);
|
||||
sendBoatCommand(deviceSn, 'stop', 'press').catch(() => {});
|
||||
missionCodeRef.current = null;
|
||||
setMissionCode(null);
|
||||
if (code) {
|
||||
undoBoatMission(deviceSn, code).catch(() => {
|
||||
message.warning('终止指令发送失败,但已退出导航模式');
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
}, [deviceSn]);
|
||||
@@ -92,12 +167,17 @@ export function useNavigationState(
|
||||
}, []);
|
||||
|
||||
const handleEmergencyStop = useCallback(() => {
|
||||
const code = missionCodeRef.current;
|
||||
setActiveRoute({ mode: null, routeId: null });
|
||||
setIsPaused(false);
|
||||
setCurrentWaypointIndex(0);
|
||||
sendBoatCommand(deviceSn, 'stop', 'press').catch(() => {
|
||||
message.error('紧急停靠指令发送失败');
|
||||
});
|
||||
missionCodeRef.current = null;
|
||||
setMissionCode(null);
|
||||
if (code) {
|
||||
undoBoatMission(deviceSn, code).catch(() => {
|
||||
message.warning('紧急停靠指令发送失败');
|
||||
});
|
||||
}
|
||||
message.warning('已执行紧急停靠');
|
||||
}, [deviceSn]);
|
||||
|
||||
@@ -125,6 +205,8 @@ export function useNavigationState(
|
||||
isPreviewing,
|
||||
isImmersive,
|
||||
activeRouteId,
|
||||
missionCode,
|
||||
sailingLoading,
|
||||
handleStartSailing,
|
||||
handlePauseSailing,
|
||||
handleResumeSailing,
|
||||
|
||||
@@ -37,6 +37,7 @@ const ClientPage: React.FC = () => {
|
||||
isPreviewing,
|
||||
isImmersive,
|
||||
activeRouteId,
|
||||
sailingLoading,
|
||||
handleStartSailing,
|
||||
handlePauseSailing,
|
||||
handleResumeSailing,
|
||||
@@ -44,7 +45,7 @@ const ClientPage: React.FC = () => {
|
||||
handleStopPreview,
|
||||
handleEmergencyStop,
|
||||
handlePreviewChange,
|
||||
} = useNavigationState(deviceSn, selectedRouteId, setSelectedRouteId);
|
||||
} = useNavigationState(deviceSn, selectedRouteId, setSelectedRouteId, routes);
|
||||
|
||||
// ── 导航计算 ──
|
||||
const {
|
||||
@@ -89,6 +90,7 @@ const ClientPage: React.FC = () => {
|
||||
boatData={boatData}
|
||||
onModelCenter={handleModelCenter}
|
||||
isSailing={isSailing}
|
||||
sailingLoading={sailingLoading}
|
||||
previewStopSignal={previewStopSignal}
|
||||
onStartSailing={handleStartSailing}
|
||||
onPreviewChange={handlePreviewChange}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { request } from 'umi';
|
||||
|
||||
const PREFIX = '/api-device/client';
|
||||
const ORDER_PREFIX = '/api-order';
|
||||
const DEVICE_PREFIX = '/api-device';
|
||||
|
||||
/**
|
||||
* 校验设备SN
|
||||
@@ -44,3 +46,61 @@ export async function sendBoatCommand(deviceSn: string, command: string, action:
|
||||
{ method: 'POST', data: { deviceSn, command, action } },
|
||||
);
|
||||
}
|
||||
|
||||
// ========== 任务管理 ==========
|
||||
|
||||
/**
|
||||
* 创建无人船航线任务
|
||||
*/
|
||||
export async function createBoatMission(params: {
|
||||
deviceSn: string;
|
||||
boatRouteId: string;
|
||||
boatRouteName: string;
|
||||
missionName?: string;
|
||||
}) {
|
||||
return request<API.Result<{ missionCode: string; [key: string]: any }>>(
|
||||
`${ORDER_PREFIX}/mission/saveMission`,
|
||||
{
|
||||
method: 'POST',
|
||||
data: {
|
||||
missionType: 3, // BOAT_ROUTE
|
||||
missionName: params.missionName || `${params.boatRouteName}-${new Date().toLocaleString('zh-CN')}`,
|
||||
deviceSn: params.deviceSn,
|
||||
boatRouteId: params.boatRouteId,
|
||||
boatRouteName: params.boatRouteName,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// ========== 无人船控制指令(需要missionCode)==========
|
||||
|
||||
/**
|
||||
* 暂停航行任务
|
||||
*/
|
||||
export async function pauseBoatMission(deviceSn: string, missionCode: string) {
|
||||
return request<API.Result<void>>(
|
||||
`${DEVICE_PREFIX}/device/boat/command/pause`,
|
||||
{ method: 'POST', params: { deviceSn, missionCode } },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复航行任务
|
||||
*/
|
||||
export async function resumeBoatMission(deviceSn: string, missionCode: string) {
|
||||
return request<API.Result<void>>(
|
||||
`${DEVICE_PREFIX}/device/boat/command/resume`,
|
||||
{ method: 'POST', params: { deviceSn, missionCode } },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 终止航行任务
|
||||
*/
|
||||
export async function undoBoatMission(deviceSn: string, missionCode: string) {
|
||||
return request<API.Result<void>>(
|
||||
`${DEVICE_PREFIX}/device/boat/command/undo`,
|
||||
{ method: 'POST', params: { deviceSn, missionCode } },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user