import React, { useState, useEffect, useRef, useMemo } from 'react'; import { Layout, Card, Row, Col, Statistic, Tag, Button, Table, Typography, Space, Progress, Badge, Timeline, message, Select, Modal, Space as AntSpace } from 'antd'; import { EnvironmentOutlined, RobotOutlined, BranchesOutlined, ThunderboltOutlined, CheckCircleOutlined, PauseOutlined, RocketOutlined, SyncOutlined, WarningOutlined, CloudOutlined, ClockCircleOutlined, ReloadOutlined, AimOutlined, ApiOutlined, SettingOutlined, PlusOutlined, ArrowRightOutlined, EditOutlined, FullscreenOutlined, InfoCircleOutlined, CarOutlined } from '@ant-design/icons'; import DroneLivePlayer from '../VolcRtcPlayer.jsx'; import DroneVideoPlayer from './DroneVideoPlayer'; import AgoraTextTest from '../AgoraRtc.tsx'; import { useWebRTCPlayer } from '../useWebRTCStream.tsx'; import VideoPlayer from '../VideoPlayer.tsx.tsx'; import CesiumMap from '../Map.jsx'; import { getUAVState } from '@/api/device.ts'; const { Content } = Layout; const { Text, Title } = Typography; const taskTimeline = [ { time: '10:24:18', color: 'blue', text: 'UAV-C01 开始执行清洗航线 C-05', status: '任务开始' }, { time: '10:21:35', color: 'orange', text: 'RBT-01 开始执行巡检航线 P-02', status: '任务开始' }, { time: '10:18:02', color: 'green', text: 'GR-02 开始除草作业 G-01', status: '任务开始' }, { time: '10:15:47', color: 'gray', text: 'UAV-C02 任务待命就绪', status: '待命就绪' }, { time: '10:12:33', color: 'blue', text: 'UAV-C01 再次返机补水', status: '返机补给' }, { time: '09:58:10', color: 'green', text: 'RBT-02 完成巡检航线 P-01', status: '任务完成' }, ]; const logs = [ { time: '10:24:35', device: 'UAV-C01', type: '命令', desc: '起飞指令下发,开始作业' }, { time: '10:21:40', device: 'RBT-01', type: '命令', desc: '开始巡检任务 P-02' }, { time: '10:20:11', device: 'UAV-C02', type: '命令', desc: '转入待命状态' }, { time: '10:18:05', device: 'GR-02', type: '命令', desc: '除草作业开始 G-01' }, { time: '10:12:35', device: 'UAV-C01', type: '告警', desc: '返航补水' }, { time: '10:05:22', device: 'RBT-01', type: '事件', desc: '电量较低,建议回充', level: '低电量' }, ]; export default function DeviceOverviewPage({ devices, planedevices, devicesAll, onViewStatus, selectedSn, onDeviceSelect, mqttDroneData }) { const [zoomedVideo, setZoomedVideo] = useState(null); const [mowerView, setMowerView] = useState('front'); const [videoSource, setVideoSource] = useState('drone'); // 恢复 videoSource 状态管理 const [uavState, setUavState] = useState(null); const [persistentDroneData, setPersistentDroneData] = useState(null); // 持久化无人机数据 // 当MQTT数据更新时,更新持久化数据(只在有新数据时更新) useEffect(() => { if (mqttDroneData && mqttDroneData.host) { const { host } = mqttDroneData; setPersistentDroneData({ // 无人机状态数据映射 capacity_percent: host.drone_charge_state?.capacity_percent, environment_temperature: host.environment_temperature, height: host.height, wind_speed: host.wind_speed, rainfall: host.rainfall === 0 ? 'no_rain' : 'rain', position_state: host.position_state, longitude: host.longitude, latitude: host.latitude, heading: host.heading, humidity: host.humidity, drone_in_dock: host.drone_in_dock, cover_state: host.cover_state, temperature: host.temperature, }); } }, [mqttDroneData]); // 优先使用持久化数据,然后是API数据 const currentDroneData = React.useMemo(() => { return persistentDroneData; // 总是返回持久化数据(不会变回null) }, [persistentDroneData]); const selectedDevice = React.useMemo(() => { const sn = selectedSn || (devicesAll?.[0]?.device_sn || devicesAll?.[0]?.serialNumber); return devicesAll?.find(item => (item.device_sn || item.serialNumber) === sn) || null; }, [devicesAll, selectedSn]); const isUAV = !!selectedDevice?.gateway_sn; const { video: robotVideo, error, ready } = useWebRTCPlayer( !isUAV ? selectedDevice?.serialNumber || '' : '', `webrtc://1.95.137.212/live/livestream/${selectedDevice?.serialNumber}`, ); const dynamicTopCards = React.useMemo(() => { const totalDrones = planedevices?.length || 0; const onlineDrones = planedevices?.filter(d => d.onlineStatus === 1).length || 0; const totalRobots = devicesAll?.filter(d => d.productName?.includes('巡检')).length || 0; const activeRobots = devicesAll?.filter(d => d.productName?.includes('巡检') && d.onlineStatus === 1).length || 0; const totalMowers = devicesAll?.filter(d => d.productName?.includes('割草机')).length || 0; const activeMowers = devicesAll?.filter(d => d.productName?.includes('割草机') && d.onlineStatus === 1).length || 0; const washDroneStandby = planedevices?.filter(d => d.productName?.includes('清洗') && d.onlineStatus === 1).length || 0; return [ { title: '无人机机场状态', icon: , value: `${onlineDrones} / ${totalDrones}`, sub: `可调度 ${onlineDrones} 架`, color: '#1890ff' }, { title: '机器人', icon: , value: `${totalMowers} 台`, sub: `作业中 ${activeMowers}`, color: '#13c2c2' }, { title: '无人机', icon: , value: `${totalDrones} 架`, sub: `待命 ${washDroneStandby}`, color: '#eb2f96' }, { title: '设备平均电量', icon: , value: '86%', sub: '', color: '#52c41a' }, { title: '今日任务执行', icon: , value: '12 / 15', sub: '完成率 80%', color: '#2f54eb' }, ]; }, [devicesAll, planedevices]); const dynamicScheduleList = React.useMemo(() => { return devicesAll.map((d, index) => ({ id: d.deviceId || index, name: d.deviceAlias || d.deviceName || d.callsign || '未知设备', type: d.productName || '未知类型', status: d.onlineStatus === 1 ? '在线' : '离线', mode: d.status === 4 ? '待命' : '作业中', battery: d.capacity_percent || 100, position: d.position || '站内', task: d.status === 4 ? '待命' : '作业中', comm: d.rssi ? `${d.rssi}dBm` : '良好' })); }, [devicesAll]); const handleStart = () => message.success('任务已启动'); const handlePause = () => message.info('任务已暂停'); const handleReturn = () => message.info('返航指令已下发'); const handleRecharge = () => message.info('回充指令已下发'); const handleEmergencyStop = () => message.error('已触发急停!'); const fix2 = (val) => { if (val === null || val === undefined || isNaN(val)) return '-'; return Number(val).toFixed(2); }; const getVal = (val) => val == "no_rain" ? "无" : (val || '-'); const handleDeviceChange = (id) => { if (onDeviceSelect) onDeviceSelect(id); }; // 用于视频、状态展示的实时设备数据(优先使用持久化MQTT数据) const realtimeDevice = useMemo(() => { if (!selectedDevice) return null; return { ...selectedDevice, ...uavState, ...currentDroneData, // 持久化数据最后覆盖,确保优先显示 }; }, [selectedDevice, uavState, currentDroneData]); useEffect(() => { const fetchUavState = async () => { if (isUAV && selectedDevice?.gateway_sn && selectedDevice?.device_sn) { try { const res = await getUAVState({ sn: selectedDevice.gateway_sn, droneSn: selectedDevice.device_sn }); if (res.code === 200) { setUavState(res.data); } } catch (err) { console.error('获取无人机状态失败', err); } } else { setUavState(null); } }; fetchUavState(); }, [isUAV, selectedDevice]); return ( {dynamicTopCards.map((card, idx) => (
{card.icon}
{card.title}
{card.value}
{card.sub &&
{card.sub}
}
))}
装备协同态势图} extra={