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

346 lines
9.5 KiB
React
Raw Normal View History

2026-07-08 11:03:29 +08:00
import React, { useState, useRef, useEffect, useCallback, memo } from 'react';
2026-06-15 09:20:01 +08:00
import VideoPlayer from '../VideoPlayer.tsx';
2026-07-08 11:03:29 +08:00
// 单独抽离视频面板,memo 避免无关重渲染
const VideoPanel = memo(({
target,
idx,
label,
width,
height,
pos,
isDraggingAny,
dragTarget,
onMouseDown,
serialNumber,
robotVideo,
error,
ready
}) => {
// 空值直接不渲染,防止 pos.x 报错
if (!pos) return null;
const isSelfDragging = isDraggingAny && dragTarget === target;
2026-06-15 09:20:01 +08:00
2026-07-08 11:03:29 +08:00
return (
<div
style={{
2026-07-08 10:25:11 +08:00
position: 'absolute',
2026-07-08 11:03:29 +08:00
width,
height,
2026-07-08 10:25:11 +08:00
zIndex: 10,
border: '1px solid #fff3',
borderRadius: 8,
2026-07-08 11:03:29 +08:00
overflow: 'hidden',
backgroundColor: '#000',
// GPU 加速,不触发页面回流
transform: `translate(${pos.x}px, ${pos.y}px)`,
willChange: 'transform',
transition: isSelfDragging ? 'none' : 'transform 0.1s ease-out',
pointerEvents: 'auto'
}}
>
{/* 拖拽手柄 */}
<div
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: 28,
background: 'linear-gradient(to bottom, rgba(0,0,0,0.8), rgba(0,0,0,0.6))',
zIndex: 5,
cursor: isSelfDragging ? 'grabbing' : 'grab',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
userSelect: 'none'
}}
onMouseDown={(e) => onMouseDown(e, target)}
>
<span style={{ color: '#fff', fontSize: 12, fontWeight: 'bold' }}>
⋮⋮ {label}
</span>
2026-06-15 09:20:01 +08:00
</div>
2026-07-08 11:03:29 +08:00
<div style={{ position: 'absolute', top: 28, left: 0, right: 0, bottom: 0 }}>
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:03:29 +08:00
</div>
);
});
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);
// 拖拽临时状态,仅拖动过程使用
const dragInfoRef = useRef({
isDragging: false,
target: null,
startX: 0,
startY: 0,
originX: 0,
originY: 0
});
// 面板尺寸 state(初始化一次)
const [panelSize, setPanelSize] = useState({
leftWidth: 200,
leftHeight: 150,
rightWidth: 200,
rightHeight: 150,
backWidth: 300,
backHeight: 150
});
// 面板位置 state,拖拽只更新对应一项,局部渲染
const [panelPositions, setPanelPositions] = useState({
left: null,
right: null,
back: null
});
// 全局鼠标移动统一处理,只维护一份逻辑
const globalMouseMove = useCallback((e) => {
const drag = dragInfoRef.current;
if (!drag.isDragging || !containerRef.current) return;
const containerRect = containerRef.current.getBoundingClientRect();
const deltaX = e.clientX - drag.startX;
const deltaY = e.clientY - drag.startY;
let newX = drag.originX + deltaX;
let newY = drag.originY + deltaY;
let maxW, maxH;
switch (drag.target) {
case 'left':
maxW = panelSize.leftWidth;
maxH = panelSize.leftHeight;
break;
case 'right':
maxW = panelSize.rightWidth;
maxH = panelSize.rightHeight;
break;
case 'back':
maxW = panelSize.backWidth;
maxH = panelSize.backHeight;
break;
default:
return;
}
// 边界约束
newX = Math.max(0, Math.min(newX, containerRect.width - maxW));
newY = Math.max(0, Math.min(newY, containerRect.height - maxH));
// 仅更新当前拖拽面板位置,不刷新全部组件
setPanelPositions(prev => ({
...prev,
[drag.target]: { x: newX, y: newY }
}));
}, [panelSize]);
const globalMouseUp = useCallback(() => {
dragInfoRef.current.isDragging = false;
dragInfoRef.current.target = null;
}, []);
// 全局一次性挂载鼠标事件,组件销毁解绑
useEffect(() => {
window.addEventListener('mousemove', globalMouseMove);
window.addEventListener('mouseup', globalMouseUp);
return () => {
window.removeEventListener('mousemove', globalMouseMove);
window.removeEventListener('mouseup', globalMouseUp);
};
}, [globalMouseMove, globalMouseUp]);
// 初始化尺寸与位置(只执行一次)
useEffect(() => {
if (!containerRef.current || panelPositions.left) return;
const rect = containerRef.current.getBoundingClientRect();
// 三块面板统一尺寸,消除后视大小不一致问题
const baseW = Math.max(200, Math.floor(rect.width * 0.25));
const baseH = Math.max(150, Math.floor(rect.height * 0.3));
const size = {
leftWidth: baseW,
leftHeight: baseH,
rightWidth: baseW,
rightHeight: baseH,
backWidth: baseW,
backHeight: baseH
};
setPanelSize(size);
// 初始坐标直接赋值,不会为null
setPanelPositions({
left: { x: 8, y: 8 },
right: { x: rect.width - baseW - 8, y: 8 },
back: { x: (rect.width - baseW) / 2, y: rect.height - baseH - 8 }
});
}, [panelPositions.left]);
// 鼠标按下,开启拖拽
const handleDragStart = useCallback((e, target) => {
e.preventDefault();
e.stopPropagation();
const pos = panelPositions[target];
// 二次做空校验,防止初始化未完成拖拽触发
if (!pos) return;
dragInfoRef.current = {
isDragging: true,
target,
startX: e.clientX,
startY: e.clientY,
originX: pos.x,
originY: pos.y
};
}, [panelPositions]);
const dragState = dragInfoRef.current;
return (
<div
ref={containerRef}
style={{
position: 'relative',
width: '100%',
height: '100%',
overflow: 'hidden',
userSelect: dragState.isDragging ? 'none' : 'auto'
}}
>
{/* 底层前视画面,稳定不重渲染 */}
<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}
/>
</div>
2026-07-08 11:03:29 +08:00
{/* 左视 */}
<VideoPanel
target="left"
idx={3}
label="左视"
width={panelSize.leftWidth}
height={panelSize.leftHeight}
pos={panelPositions.left}
isDraggingAny={dragState.isDragging}
dragTarget={dragState.target}
onMouseDown={handleDragStart}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
{/* 右视 */}
<VideoPanel
target="right"
idx={4}
label="右视"
width={panelSize.rightWidth}
height={panelSize.rightHeight}
pos={panelPositions.right}
isDraggingAny={dragState.isDragging}
dragTarget={dragState.target}
onMouseDown={handleDragStart}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
2026-07-08 10:25:11 +08:00
2026-07-08 11:03:29 +08:00
{/* 后视 */}
<VideoPanel
target="back"
idx={2}
label="后视"
width={panelSize.backWidth}
height={panelSize.backHeight}
pos={panelPositions.back}
isDraggingAny={dragState.isDragging}
dragTarget={dragState.target}
onMouseDown={handleDragStart}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
2026-07-08 10:25:11 +08:00
2026-07-08 11:03:29 +08:00
{/* 底部状态面板 */}
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
}