501 lines
15 KiB
TypeScript
501 lines
15 KiB
TypeScript
import React, {
|
||
useState,
|
||
useEffect,
|
||
useMemo,
|
||
useRef,
|
||
useCallback,
|
||
} from 'react';
|
||
|
||
import {
|
||
Row,
|
||
Col,
|
||
Card,
|
||
Tag,
|
||
Typography,
|
||
Space,
|
||
Empty,
|
||
Descriptions,
|
||
Badge,
|
||
Progress,
|
||
Button,
|
||
Select,
|
||
message,
|
||
Modal,
|
||
} from 'antd';
|
||
|
||
import {
|
||
DashboardOutlined,
|
||
GlobalOutlined,
|
||
VideoCameraOutlined,
|
||
ControlOutlined,
|
||
CameraOutlined,
|
||
ThunderboltOutlined,
|
||
EnvironmentOutlined,
|
||
} from '@ant-design/icons';
|
||
|
||
import XboxController from './XboxController';
|
||
import DroneVideoPlayer from './DroneVideoPlayer';
|
||
import CesiumMap from '../Map.jsx';
|
||
import { useSelector } from 'react-redux';
|
||
import { RootState } from '@/src/store/index.ts';
|
||
import { useWebRTCPlayer } from '../useWebRTCStream.tsx';
|
||
import VideoPlayer from '../VideoPlayer.tsx';
|
||
import DeviceController from './DeviceController';
|
||
import { DatePicker } from 'antd';
|
||
import dayjs from 'dayjs';
|
||
const { RangePicker } = DatePicker;
|
||
|
||
const { Text } = Typography;
|
||
|
||
export default function DeviceStatusPage({
|
||
devicesAll,
|
||
serialNumber,
|
||
onDeviceChange,
|
||
}) {
|
||
const { userInfo } = useSelector((state: RootState) => state.user);
|
||
|
||
const [messageApi, contextHolder] = message.useMessage();
|
||
|
||
const controllerRef = useRef<DeviceController | null>(null);
|
||
|
||
const [isControlled, setIsControlled] = useState(false);
|
||
const [hasControlRight, setHasControlRight] = useState(false);
|
||
const [connecting, setConnecting] = useState(false);
|
||
const [wsStatus, setWsStatus] = useState('disconnected');
|
||
const [wsDeviceInfo, setWsDeviceInfo] = useState({});
|
||
const [selectedDrone, setSelectedDrone] = useState(null);
|
||
const [taskList, setTaskList] = useState([]);
|
||
|
||
// 👇 加上这两个
|
||
const [queryTimeRange, setQueryTimeRange] = useState([]); // 时间范围
|
||
const [startTimestamp, setStartTimestamp] = useState(''); // 开始时间戳
|
||
const [endTimestamp, setEndTimestamp] = useState(''); // 结束时间戳
|
||
|
||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||
const [modalContent, setModalContent] = useState({
|
||
title: '',
|
||
content: '',
|
||
});
|
||
|
||
const [mowerView, setMowerView] = useState('front');
|
||
|
||
const device = useMemo(() => {
|
||
const sn =
|
||
serialNumber ||
|
||
devicesAll?.[0]?.device_sn ||
|
||
devicesAll?.[0]?.serialNumber;
|
||
|
||
if (!sn) return null;
|
||
|
||
return devicesAll?.find(
|
||
(d) => (d.device_sn || d.serialNumber) === sn,
|
||
);
|
||
}, [devicesAll, serialNumber]);
|
||
|
||
const isUAV = useMemo(() => {
|
||
return !!device?.device_sn;
|
||
}, [device]);
|
||
|
||
const runningStatus = useMemo(() => {
|
||
const base = device?.lastRunningStatus || {};
|
||
|
||
return isUAV
|
||
? base
|
||
: {
|
||
...base,
|
||
...wsDeviceInfo,
|
||
};
|
||
}, [device, wsDeviceInfo, isUAV]);
|
||
|
||
const { video: robotVideo, error, ready } = useWebRTCPlayer(
|
||
serialNumber || '',
|
||
`webrtc://1.95.137.212/live/livestream/${serialNumber}`,
|
||
);
|
||
const getVal = (val) => val === "no_rain" ? "无" : (val || '-');
|
||
|
||
|
||
useEffect(() => {
|
||
const controller = new DeviceController(userInfo, messageApi);
|
||
|
||
controller.onDeviceInfoUpdate = setWsDeviceInfo;
|
||
controller.onControlStatusChange = setIsControlled;
|
||
controller.onHasControlRightChange = setHasControlRight;
|
||
controller.onRequestControlStatusChange = setConnecting;
|
||
controller.onWsStatusChange = setWsStatus;
|
||
|
||
controller.onPermissionRequest = (content) => {
|
||
setModalContent(content);
|
||
setIsModalOpen(true);
|
||
};
|
||
|
||
controller.init();
|
||
|
||
controllerRef.current = controller;
|
||
|
||
return () => {
|
||
controller.destroy();
|
||
controllerRef.current = null;
|
||
};
|
||
}, [userInfo, messageApi]);
|
||
|
||
useEffect(() => {
|
||
if (!device || !controllerRef.current) return;
|
||
|
||
controllerRef.current.connectDevice(device);
|
||
}, [device]);
|
||
|
||
const mapDevices = useMemo(() => {
|
||
if (!device) return [];
|
||
|
||
return {
|
||
...device,
|
||
lastRunningStatus: {
|
||
...device.lastRunningStatus,
|
||
longitude:
|
||
runningStatus.longitude || device.longitude,
|
||
latitude:
|
||
runningStatus.latitude || device.latitude,
|
||
},
|
||
name:
|
||
device.deviceAlias ||
|
||
device.callsign ||
|
||
device.deviceName ||
|
||
'设备',
|
||
type: isUAV
|
||
? 'drone'
|
||
: device.productName?.includes('割草机')
|
||
? 'mower'
|
||
: 'robot',
|
||
};
|
||
}, [device, isUAV, runningStatus]);
|
||
|
||
const handleGamepadUpdate = useCallback((state) => {
|
||
controllerRef.current?.sendGamepad(state);
|
||
}, []);
|
||
|
||
const handleControlModal = useCallback(() => {
|
||
controllerRef.current?.requestControl();
|
||
}, []);
|
||
|
||
const handleOk = () => {
|
||
controllerRef.current?.sendPermissionRequest(
|
||
true,
|
||
'switchResult',
|
||
);
|
||
controllerRef.current.setIsControlled(false);
|
||
|
||
setIsModalOpen(false);
|
||
};
|
||
|
||
const handleCancel = () => {
|
||
|
||
controllerRef.current?.sendPermissionRequest(
|
||
false,
|
||
'switchResult',
|
||
);
|
||
controllerRef.current.setIsControlled(true);
|
||
|
||
setIsModalOpen(false);
|
||
};
|
||
|
||
const fix2 = (val) => {
|
||
if (val === null || val === undefined || isNaN(val)) {
|
||
return '-';
|
||
}
|
||
|
||
return Number(val).toFixed(2);
|
||
};
|
||
|
||
if (!device) {
|
||
return (
|
||
<Empty
|
||
description="请选择设备"
|
||
style={{ marginTop: 100 }}
|
||
/>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<div style={{ padding: 0 }}>
|
||
{contextHolder}
|
||
|
||
<div
|
||
style={{
|
||
marginBottom: 16,
|
||
display: 'flex',
|
||
alignItems: 'center',
|
||
gap: 12,
|
||
}}
|
||
>
|
||
<Text strong>选择设备:</Text>
|
||
|
||
<Select
|
||
style={{ width: 280 }}
|
||
value={device?.device_sn || device?.serialNumber}
|
||
onChange={(val) => onDeviceChange?.(val)}
|
||
showSearch
|
||
optionFilterProp="label"
|
||
options={[
|
||
{
|
||
label: <span>机器人</span>,
|
||
title: 'manager',
|
||
options: devicesAll.filter(item => item.serialNumber).map(item => ({
|
||
value: item.device_sn || item.serialNumber,
|
||
label: item.deviceAlias || item.deviceName || '未知设备'
|
||
})),
|
||
},
|
||
{
|
||
label: <span>无人机</span>,
|
||
title: 'engineer',
|
||
options: devicesAll.filter(item => item.gateway_sn).map(item => ({
|
||
value: item.device_sn,
|
||
label: item.drone_callsign || item.callsign || '未知设备'
|
||
})),
|
||
},
|
||
]}
|
||
/>
|
||
|
||
<Tag color="blue">
|
||
{isUAV ? '无人机' : '机器人'}
|
||
</Tag>
|
||
|
||
|
||
</div>
|
||
|
||
<Row gutter={[16, 16]}>
|
||
<Col xs={24} lg={16}>
|
||
<Card
|
||
title={
|
||
<>
|
||
<VideoCameraOutlined />
|
||
实时视频监控
|
||
</>
|
||
}
|
||
bordered={false}
|
||
style={{ borderRadius: 12 }}
|
||
bodyStyle={{ padding: 12 }}
|
||
>
|
||
{!isUAV && (
|
||
<Space style={{ marginBottom: 12 }}>
|
||
<Button type={mowerView === 'front' ? 'primary' : 'default'} icon={<CameraOutlined />} onClick={() => setMowerView('front')}>前视</Button>
|
||
<Button type={mowerView === 'back' ? 'primary' : 'default'} onClick={() => setMowerView('back')}>后视</Button>
|
||
<Button type={mowerView === 'left' ? 'primary' : 'default'} onClick={() => setMowerView('left')}>左视</Button>
|
||
<Button type={mowerView === 'right' ? 'primary' : 'default'} onClick={() => setMowerView('right')}>右视</Button>
|
||
{/*<Button type={mowerView === 'top' ? 'primary' : 'default'} onClick={() => setMowerView('top')}>顶视</Button>*/}
|
||
<Button type={mowerView === 'all' ? 'primary' : 'default'} onClick={() => setMowerView('all')}>全景</Button>
|
||
</Space>
|
||
)}
|
||
|
||
<div
|
||
style={{
|
||
//height: 450,
|
||
borderRadius: 8,
|
||
overflow: 'hidden',
|
||
display: 'flex',
|
||
flexWrap: 'wrap', // 允许换行
|
||
}}
|
||
>
|
||
{isUAV ? (
|
||
<DroneVideoPlayer
|
||
device={device}
|
||
height={450}
|
||
showSourceSelect={true}
|
||
defaultSource="drone"
|
||
/>
|
||
) : robotVideo ? (
|
||
// 全景模式 = 4 分屏
|
||
mowerView === 'all' ? (
|
||
<div style={{
|
||
display: 'grid',
|
||
gridTemplateColumns: '1fr 1fr',
|
||
width: '100%',
|
||
height: '100%'
|
||
}}>
|
||
{/* 前 */}
|
||
<div style={{ position: 'relative', border: '1px solid #222' }}>
|
||
<VideoPlayer
|
||
deviceId={serialNumber}
|
||
idx={1}
|
||
className="w-full h-full object-contain"
|
||
video={robotVideo}
|
||
error={error}
|
||
ready={ready}
|
||
/>
|
||
<div style={{ position: 'absolute', top: 4, left: 8, color: '#fff', fontSize: 12, background: '#0006', padding: '2px 6px', borderRadius: 4 }}>前视</div>
|
||
</div>
|
||
{/* 后 */}
|
||
<div style={{ position: 'relative', border: '1px solid #222' }}>
|
||
<VideoPlayer
|
||
deviceId={serialNumber}
|
||
idx={2}
|
||
className="w-full h-full object-contain"
|
||
video={robotVideo}
|
||
error={error}
|
||
ready={ready}
|
||
/>
|
||
<div style={{ position: 'absolute', top: 4, left: 8, color: '#fff', fontSize: 12, background: '#0006', padding: '2px 6px', borderRadius: 4 }}>后视</div>
|
||
</div>
|
||
{/* 左 */}
|
||
<div style={{ position: 'relative', border: '1px solid #222' }}>
|
||
<VideoPlayer
|
||
deviceId={serialNumber}
|
||
idx={3}
|
||
className="w-full h-full object-contain"
|
||
video={robotVideo}
|
||
error={error}
|
||
ready={ready}
|
||
/>
|
||
<div style={{ position: 'absolute', top: 4, left: 8, color: '#fff', fontSize: 12, background: '#0006', padding: '2px 6px', borderRadius: 4 }}>左视</div>
|
||
</div>
|
||
{/* 右 */}
|
||
<div style={{ position: 'relative', border: '1px solid #222' }}>
|
||
<VideoPlayer
|
||
deviceId={serialNumber}
|
||
idx={4}
|
||
className="w-full h-full object-contain"
|
||
video={robotVideo}
|
||
error={error}
|
||
ready={ready}
|
||
/>
|
||
<div style={{ position: 'absolute', top: 4, left: 8, color: '#fff', fontSize: 12, background: '#0006', padding: '2px 6px', borderRadius: 4 }}>右视</div>
|
||
</div>
|
||
</div>
|
||
) : (
|
||
// 单画面模式
|
||
<VideoPlayer
|
||
deviceId={serialNumber}
|
||
idx={
|
||
mowerView === 'front' ? 1
|
||
: mowerView === 'back' ? 2
|
||
: mowerView === 'left' ? 3
|
||
: 4
|
||
}
|
||
className="w-full h-full object-contain"
|
||
video={robotVideo}
|
||
error={error}
|
||
ready={ready}
|
||
/>
|
||
)
|
||
) : (
|
||
<div style={{ color: '#ccc', margin: 'auto' }}>
|
||
视频加载中...
|
||
</div>
|
||
)}
|
||
</div>
|
||
</Card>
|
||
|
||
<Card
|
||
title={
|
||
<>
|
||
<GlobalOutlined />
|
||
位置态势图
|
||
</>
|
||
}
|
||
bordered={false}
|
||
style={{ borderRadius: 12 }}
|
||
bodyStyle={{
|
||
padding: 12,
|
||
height: 400,
|
||
}}
|
||
>
|
||
<CesiumMap devices={[mapDevices]} />
|
||
</Card>
|
||
</Col>
|
||
|
||
<Col xs={24} lg={8}>
|
||
<Card
|
||
title={<><DashboardOutlined /> {isUAV ? '无人机状态' : '机器人状态'}详情</>}
|
||
bordered={false}
|
||
style={{ borderRadius: 12, marginBottom: 16 }}
|
||
extra={<Tag color={device.onlineStatus === 1 ? 'success' : 'default'}>{device.onlineStatus === 1 ? '在线' : '离线'}</Tag>}
|
||
>
|
||
{isUAV ? (
|
||
<Descriptions column={2} size="small" layout="vertical">
|
||
<Descriptions.Item label={<><ThunderboltOutlined /> 电池电量</>}>
|
||
<Progress percent={device.capacity_percent || 0} size="small" status={(device.capacity_percent || 0) < 20 ? 'exception' : 'active'} />
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label={<><EnvironmentOutlined /> 飞行高度</>}>
|
||
<Text strong style={{ fontSize: 16 }}>{fix2(device.height)} m</Text>
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="环境风速"><Text>{fix2(device.wind_speed)} m/s</Text></Descriptions.Item>
|
||
<Descriptions.Item label="环境温度"><Text>{fix2(device.environment_temperature)} ℃</Text></Descriptions.Item>
|
||
<Descriptions.Item label="降雨状态">
|
||
<Tag color={device.rainfall === 'no_rain' ? 'green' : 'red'}>{getVal(device.rainfall)}</Tag>
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="返航距离"><Text>{fix2(device.home_distance)} m</Text></Descriptions.Item>
|
||
<Descriptions.Item label="GPS 卫星数">
|
||
<Badge count={device.position_state?.gps_number || 0} style={{ backgroundColor: '#1890ff' }} />
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="RTK 卫星数">
|
||
<Badge count={device.position_state?.rtk_number || 0} style={{ backgroundColor: '#52c41a' }} />
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="RTK 状态" span={2}>
|
||
<Tag color={device.position_state?.is_fixed === 'fixing_successful' ? 'success' : 'warning'}>
|
||
{device.position_state?.is_fixed === 'fixing_successful' ? 'RTK 已固定' : '未固定'}
|
||
</Tag>
|
||
</Descriptions.Item>
|
||
</Descriptions>
|
||
) : (
|
||
<Descriptions column={2} size="small" layout="vertical">
|
||
<Descriptions.Item label={<><ThunderboltOutlined /> 电池电量</>}>
|
||
<Progress percent={runningStatus.battery || 0} size="small" status={(runningStatus.battery || 0) < 20 ? 'exception' : 'active'} />
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label={<><DashboardOutlined /> 系统电压</>}>
|
||
<Text strong style={{ fontSize: 16 }}>{fix2(runningStatus.voltage)} V</Text>
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="左电机电流"><Text>{fix2(runningStatus?.leftCurrent)} A</Text></Descriptions.Item>
|
||
<Descriptions.Item label="右电机电流"><Text>{fix2(runningStatus?.rightCurrent)} A</Text></Descriptions.Item>
|
||
<Descriptions.Item label="左轮速度"><Text>{fix2(runningStatus?.leftMeasureSpeed)} m/s</Text></Descriptions.Item>
|
||
<Descriptions.Item label="右轮速度"><Text>{fix2(runningStatus?.rightMeasureSpeed)} m/s</Text></Descriptions.Item>
|
||
<Descriptions.Item label="左电机温度"><Text>{fix2(runningStatus?.leftMotorTemp)} ℃</Text></Descriptions.Item>
|
||
<Descriptions.Item label="右电机温度"><Text>{fix2(runningStatus?.rightMotorTemp)} ℃</Text></Descriptions.Item>
|
||
<Descriptions.Item label="主控温度"><Text>{fix2(runningStatus?.chipTemp)} ℃</Text></Descriptions.Item>
|
||
<Descriptions.Item label="卫星数量">
|
||
<Badge showZero count={runningStatus?.satelliteCnt || 0} style={{ backgroundColor: '#52c41a' }} />
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="偏航角 (Yaw)"><Text>{fix2(runningStatus?.yaw)}°</Text></Descriptions.Item>
|
||
<Descriptions.Item label="俯仰角 (Pitch)"><Text>{fix2(runningStatus?.pitch)}°</Text></Descriptions.Item>
|
||
</Descriptions>
|
||
)}
|
||
</Card>
|
||
{!isUAV && (
|
||
<Card
|
||
title={
|
||
<>
|
||
<ControlOutlined />
|
||
远程控制
|
||
</>
|
||
}
|
||
bordered={false}
|
||
style={{
|
||
borderRadius: 12,
|
||
background: '#0f172a',
|
||
}}
|
||
headStyle={{ color: '#fff' }}
|
||
>
|
||
<XboxController
|
||
onGamepadUpdate={handleGamepadUpdate}
|
||
handleControlModal={handleControlModal}
|
||
isControlled={isControlled}
|
||
hasControlRight={hasControlRight}
|
||
isConnecting={connecting}
|
||
/>
|
||
</Card>
|
||
)}
|
||
</Col>
|
||
</Row>
|
||
|
||
<Modal
|
||
open={isModalOpen}
|
||
onOk={handleOk}
|
||
onCancel={handleCancel}
|
||
title={modalContent.title}
|
||
okText="确定"
|
||
cancelText="取消"
|
||
>
|
||
<p>{modalContent.content}</p>
|
||
</Modal>
|
||
</div>
|
||
);
|
||
}
|