Files
web-Iot/src/components/devices/mowerAllView.jsx

228 lines
6.4 KiB
React
Raw Normal View History

2026-07-08 11:23:03 +08:00
import React, { useState, useRef, useEffect, memo } from 'react';
2026-06-15 09:20:01 +08:00
import VideoPlayer from '../VideoPlayer.tsx';
2026-07-08 11:23:03 +08:00
import DraggablePanel from './DraggablePanel.jsx';
2026-06-15 09:20:01 +08:00
2026-07-08 11:23:03 +08:00
// 视频子面板,使用 memo 避免不必要的重渲染
const VideoSubPanel = memo(({
2026-07-08 11:03:29 +08:00
idx,
label,
2026-07-08 11:23:03 +08:00
x,
y,
2026-07-08 11:03:29 +08:00
width,
height,
2026-07-08 11:23:03 +08:00
bounds,
2026-07-08 11:03:29 +08:00
serialNumber,
robotVideo,
error,
ready
}) => {
2026-07-08 11:23:03 +08:00
const hasVideo = ready && !error;
if (!hasVideo) return null;
return (
<DraggablePanel
initialX={x}
initialY={y}
width={width}
height={height}
bounds={bounds}
handleContent={
<div style={{
width: '60px',
height: '100%',
background: 'rgba(0,0,0,0.5)',
display: 'flex',
alignItems: 'center',
borderRadius: '8px',
}}>
<span style={{ color: '#e9f0eb', fontSize: 16, fontWeight: 'bold' }}>
⋮⋮ {label}
</span>
</div>
2026-07-08 11:03:29 +08:00
2026-06-15 09:20:01 +08:00
2026-07-08 11:23:03 +08:00
}
>
<div style={{
width: '100%',
height: '100%',
2026-07-08 10:25:11 +08:00
border: '1px solid #fff3',
borderRadius: 8,
2026-07-08 11:03:29 +08:00
overflow: 'hidden',
2026-07-08 11:23:03 +08:00
}}>
2026-07-08 10:25:11 +08:00
<VideoPlayer
deviceId={serialNumber}
2026-07-08 11:03:29 +08:00
idx={idx}
2026-07-08 10:25:11 +08:00
className="w-full h-full object-contain"
video={robotVideo}
error={error}
ready={ready}
/>
</div>
2026-07-08 11:23:03 +08:00
</DraggablePanel>
2026-07-08 11:03:29 +08:00
);
});
2026-06-16 14:28:14 +08:00
2026-07-08 11:03:29 +08:00
export function MowerAllView({ serialNumber, robotVideo, error, ready, statusData }) {
const containerRef = useRef(null);
2026-07-08 11:23:03 +08:00
const [panelSize, setPanelSize] = useState(null);
const [initialPositions, setInitialPositions] = useState(null);
const [containerBounds, setContainerBounds] = useState(null);
2026-07-08 11:03:29 +08:00
// 初始化尺寸、窗口缩放重新计算面板尺寸
2026-07-08 11:03:29 +08:00
useEffect(() => {
const calcSize = () => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) return;
const w = rect.width * 0.30;
const h = Math.floor(rect.height * 0.35);
setPanelSize({ width: w, height: h });
setContainerBounds({ width: rect.width, height: rect.height });
setInitialPositions({
left: { x: 8, y: 8 },
right: { x: rect.width - w - 8, y: 8 },
back: { x: (rect.width - w) / 2, y: rect.height - h - 8 }
});
};
calcSize();
const timer = setTimeout(calcSize, 100);
window.addEventListener('resize', calcSize);
return () => {
clearTimeout(timer);
window.removeEventListener('resize', calcSize);
};
}, []);
2026-07-08 11:03:29 +08:00
return (
<div
ref={containerRef}
style={{
position: 'relative',
width: '100%',
height: '100%',
2026-07-08 11:23:03 +08:00
overflow: 'hidden'
2026-07-08 11:03:29 +08:00
}}
>
2026-07-08 11:23:03 +08:00
{/* 前视主画面 - 铺满 */}
2026-07-08 11:03:29 +08:00
<div style={{ position: 'absolute', inset: 0, zIndex: 1 }}>
2026-07-08 10:25:11 +08:00
<VideoPlayer
deviceId={serialNumber}
2026-07-08 11:03:29 +08:00
idx={1}
2026-07-08 10:25:11 +08:00
className="w-full h-full object-contain"
video={robotVideo}
error={error}
ready={ready}
/>
2026-07-08 11:23:03 +08:00
</div>
2026-07-08 10:25:11 +08:00
2026-07-08 11:23:03 +08:00
{/* 三个可拖拽子面板 */}
{panelSize && initialPositions && containerBounds && (
2026-07-08 11:23:03 +08:00
<>
<VideoSubPanel
idx={3}
label="左视"
x={initialPositions.left.x}
y={initialPositions.left.y}
width={panelSize.width}
height={panelSize.height}
bounds={containerBounds}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
<VideoSubPanel
idx={4}
label="右视"
x={initialPositions.right.x}
y={initialPositions.right.y}
width={panelSize.width}
height={panelSize.height}
bounds={containerBounds}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
<VideoSubPanel
idx={2}
label="后视"
x={initialPositions.back.x}
y={initialPositions.back.y}
width={panelSize.width}
height={panelSize.height}
bounds={containerBounds}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
</>
)}
{/* 状态详情面板 */}
2026-07-08 10:25:11 +08:00
{statusData && (
2026-06-15 09:20:01 +08:00
<div style={{
2026-07-08 10:25:11 +08:00
position: 'absolute',
bottom: 8,
left: 8,
background: 'rgba(0,0,0,0.7)',
color: '#fff',
padding: '10px 12px',
2026-06-15 09:20:01 +08:00
borderRadius: 8,
2026-07-08 10:25:11 +08:00
fontSize: 11,
zIndex: 20,
backdropFilter: 'blur(4px)'
2026-06-15 09:20:01 +08:00
}}>
2026-07-08 10:25:11 +08:00
<div style={{ marginBottom: 6, fontWeight: 600, color: '#1890ff' }}>机器人状态</div>
<div style={{ display: 'grid', gridTemplateColumns: 'auto auto', gap: '3px 10px' }}>
<span>电量:</span>
<span style={{ color: (statusData.battery || 0) < 20 ? '#ff4d4f' : '#52c41a' }}>
{statusData.battery !== undefined ? `${statusData.battery}%` : '-'}
</span>
<span>电压:</span>
<span>{statusData.voltage !== undefined ? `${Number(statusData.voltage).toFixed(2)}V` : '-'}</span>
<span>左电机:</span>
<span>{statusData.leftCurrent !== undefined ? `${Number(statusData.leftCurrent).toFixed(2)}A` : '-'}</span>
<span>右电机:</span>
<span>{statusData.rightCurrent !== undefined ? `${Number(statusData.rightCurrent).toFixed(2)}A` : '-'}</span>
<span>左速度:</span>
<span>{statusData.leftMeasureSpeed !== undefined ? `${Number(statusData.leftMeasureSpeed).toFixed(1)}rpm` : '-'}</span>
<span>右速度:</span>
<span>{statusData.rightMeasureSpeed !== undefined ? `${Number(statusData.rightMeasureSpeed).toFixed(1)}rpm` : '-'}</span>
<span>左温度:</span>
<span>{statusData.leftMotorTemp !== undefined ? `${Number(statusData.leftMotorTemp).toFixed(1)}℃` : '-'}</span>
<span>右温度:</span>
<span>{statusData.rightMotorTemp !== undefined ? `${Number(statusData.rightMotorTemp).toFixed(1)}℃` : '-'}</span>
<span>主控温度:</span>
<span>{statusData.chipTemp !== undefined ? `${Number(statusData.chipTemp).toFixed(1)}℃` : '-'}</span>
<span>航向角:</span>
<span>{statusData.yaw !== undefined ? `${Number(statusData.yaw).toFixed(1)}°` : '-'}</span>
<span>俯仰角:</span>
<span>{statusData.pitch !== undefined ? `${Number(statusData.pitch).toFixed(1)}°` : '-'}</span>
<span>定位:</span>
<span>
{statusData.qual == 0 ? '无效' :
statusData.qual == 1 ? 'GPS单点' :
statusData.qual == 2 ? 'DGPS差分' :
statusData.qual == 4 ? 'RTK固定' :
statusData.qual == 5 ? 'RTK浮点' : '未知'}
</span>
<span>卫星:</span>
<span>{statusData.satelliteCnt || 0}颗</span>
<span>模式:</span>
<span>
{statusData.controlMode == 1 ? '本地' :
statusData.controlMode == 3 ? '远程' : '未知'}
</span>
</div>
2026-06-15 09:20:01 +08:00
</div>
2026-07-08 10:25:11 +08:00
)}
2026-06-15 09:20:01 +08:00
</div>
2026-07-08 11:03:29 +08:00
);
2026-06-15 09:20:01 +08:00
}