Files
gofly-platform-services/zlt-web/yingyan/src/pages/client/components/RightPanel/index.tsx
T
2026-04-08 03:22:34 +08:00

234 lines
9.6 KiB
TypeScript

import React, { useMemo, useCallback, useRef, useState } from 'react';
import { Button, Slider, message } from 'antd';
import {
WarningOutlined,
ArrowLeftOutlined,
ArrowRightOutlined,
} from '@ant-design/icons';
import type { BoatTelemetryData } from '@/types/boatRoute';
import { sendBoatCommand } from '../../services/clientApi';
import styles from './index.less';
interface RightPanelProps {
deviceInfo: { deviceSn: string; deviceName: string; deviceType: number };
boatData: BoatTelemetryData | null;
sseConnected: boolean;
}
const RightPanel: React.FC<RightPanelProps> = ({ deviceInfo, boatData, sseConnected }) => {
const heading = useMemo(() => boatData?.heading ?? 0, [boatData?.heading]);
const speed = useMemo(() => boatData?.speed ?? 0, [boatData?.speed]);
const battery = useMemo(() => boatData?.battery ?? 0, [boatData?.battery]);
const [targetSpeed, setTargetSpeed] = useState(5);
const activeCommandRef = useRef<string | null>(null);
const handleCommandPress = useCallback((command: string) => {
activeCommandRef.current = command;
sendBoatCommand(deviceInfo.deviceSn, command, 'press').catch(() => {
message.error('指令发送失败');
});
}, [deviceInfo.deviceSn]);
const handleCommandRelease = useCallback((command: string) => {
if (activeCommandRef.current === command) {
activeCommandRef.current = null;
}
sendBoatCommand(deviceInfo.deviceSn, command, 'release').catch(() => {});
}, [deviceInfo.deviceSn]);
const handleEmergencyStop = useCallback(() => {
activeCommandRef.current = null;
sendBoatCommand(deviceInfo.deviceSn, 'stop', 'press').catch(() => {
message.error('紧急停靠指令发送失败');
});
}, [deviceInfo.deviceSn]);
return (
<div className={styles.panel}>
{/* 船体控制 */}
<div className={styles.section}>
<div className={styles.sectionTitle}>船体控制</div>
{/* 罗盘仪表盘 */}
<div className={styles.compassWrapper}>
<div className={styles.compass}>
{/* 外圈刻度 */}
<svg className={styles.compassSvg} viewBox="0 0 200 200">
{/* 外圆 */}
<circle cx="100" cy="100" r="90" fill="none" stroke="rgba(0,150,220,0.3)" strokeWidth="2" />
<circle cx="100" cy="100" r="88" fill="none" stroke="rgba(0,150,220,0.08)" strokeWidth="4" />
{/* 刻度线 */}
{Array.from({ length: 72 }).map((_, i) => {
const angle = i * 5;
const isMajor = angle % 30 === 0;
const r1 = isMajor ? 78 : 83;
const r2 = 88;
const rad = (angle - 90) * Math.PI / 180;
return (
<line
key={i}
x1={100 + r1 * Math.cos(rad)}
y1={100 + r1 * Math.sin(rad)}
x2={100 + r2 * Math.cos(rad)}
y2={100 + r2 * Math.sin(rad)}
stroke={isMajor ? 'rgba(0,180,255,0.6)' : 'rgba(0,180,255,0.2)'}
strokeWidth={isMajor ? 2 : 1}
/>
);
})}
{/* N/S/E/W 标记 */}
<text x="100" y="26" textAnchor="middle" fill="#00d4ff" fontSize="14" fontWeight="bold">N</text>
<text x="100" y="182" textAnchor="middle" fill="rgba(255,255,255,0.5)" fontSize="12">S</text>
<text x="178" y="104" textAnchor="middle" fill="rgba(255,255,255,0.5)" fontSize="12">E</text>
<text x="22" y="104" textAnchor="middle" fill="rgba(255,255,255,0.5)" fontSize="12">W</text>
{/* 方向指针(船头) */}
<g transform={`rotate(${heading}, 100, 100)`}>
<polygon
points="100,32 93,70 100,62 107,70"
fill="#0070d4"
stroke="#00a0ff"
strokeWidth="1"
/>
<line x1="100" y1="62" x2="100" y2="100" stroke="rgba(0,112,212,0.5)" strokeWidth="2" />
</g>
{/* 中心圆点 */}
<circle cx="100" cy="100" r="5" fill="#0a1628" stroke="#00a0ff" strokeWidth="2" />
<circle cx="100" cy="100" r="2" fill="#00d4ff" />
</svg>
</div>
<div className={styles.headingText}>
<span className={styles.headingLabel}>航向</span>
<span className={styles.headingValue}>{heading.toFixed(0)}°</span>
</div>
</div>
{/* 左转 / 右转按钮 */}
<div className={styles.turnRow}>
<Button
className={styles.turnBtn}
onMouseDown={() => handleCommandPress('left')}
onMouseUp={() => handleCommandRelease('left')}
onMouseLeave={() => handleCommandRelease('left')}
onTouchStart={() => handleCommandPress('left')}
onTouchEnd={() => handleCommandRelease('left')}
icon={<ArrowLeftOutlined />}
/>
<Button
className={styles.turnBtn}
onMouseDown={() => handleCommandPress('right')}
onMouseUp={() => handleCommandRelease('right')}
onMouseLeave={() => handleCommandRelease('right')}
onTouchStart={() => handleCommandPress('right')}
onTouchEnd={() => handleCommandRelease('right')}
icon={<ArrowRightOutlined />}
/>
</div>
{/* 速度滑块 */}
<div className={styles.speedRow}>
<span className={styles.speedLabel}>速度</span>
<div className={styles.speedSlider}>
<Slider
min={0}
max={15}
step={0.5}
value={targetSpeed}
onChange={setTargetSpeed}
tooltip={{ formatter: (v) => `${v} m/s` }}
/>
</div>
<span className={styles.speedValue}>{speed.toFixed(1)} m/s</span>
</div>
{/* 紧急停靠 */}
<Button
className={styles.emergencyBtn}
onClick={handleEmergencyStop}
block
>
<WarningOutlined />
<span>紧急停靠</span>
</Button>
</div>
{/* 实时数据 */}
<div className={styles.section}>
<div className={styles.sectionTitle}>实时数据</div>
{/* 船舶状态 */}
<div className={`${styles.dataCard} ${styles.dataCardLevel1}`}>
<div className={styles.dataCardHeader}>船舶状态</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>速度</span>
<span className={styles.dataValue}>{speed.toFixed(1)} m/s</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>方向</span>
<span className={styles.dataValue}>{heading.toFixed(0)}°</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>电量</span>
<span className={styles.dataValue} style={{ color: battery < 20 ? '#ff4d4f' : '#00ff88' }}>
{battery.toFixed(0)}%
</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>模式</span>
<span className={styles.dataValue}>{boatData?.taskId ? '自动巡航' : '待机'}</span>
</div>
</div>
{/* 环境数据 */}
<div className={`${styles.dataCard} ${styles.dataCardLevel2}`}>
<div className={`${styles.dataCardHeader} ${styles.dataCardHeaderGreen}`}>环境数据</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>水质</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}>{boatData?.status || '--'}</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>更新时间</span>
<span className={styles.dataValue} style={{ fontSize: 10 }}>
{boatData?.recordTime ? new Date(boatData.recordTime).toLocaleTimeString('zh-CN') : '--'}
</span>
</div>
</div>
{/* 设备状态 */}
<div className={`${styles.dataCard} ${styles.dataCardLevel3}`}>
<div className={`${styles.dataCardHeader} ${styles.dataCardHeaderCyan}`}>设备状态</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>传感器</span>
<span className={styles.dataValue} style={{ color: '#00ff88' }}>正常</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>连接状态</span>
<span className={styles.dataValue} style={{ color: sseConnected ? '#00ff88' : '#ff4d4f' }}>
{sseConnected ? '已连接' : '未连接'}
</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>设备SN</span>
<span className={styles.dataValue}>{deviceInfo.deviceSn}</span>
</div>
<div className={styles.dataRow}>
<span className={styles.dataLabel}>GPS</span>
<span className={styles.dataValue}>
{boatData?.longitude != null
? `${boatData.longitude.toFixed(4)}, ${boatData.latitude?.toFixed(4)}`
: '--'}
</span>
</div>
</div>
</div>
</div>
);
};
export default RightPanel;