Files
web-Iot/src/pages/Devices.tsx

147 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useState, useEffect } from 'react';
import { Layout, Typography, Menu } from 'antd';
import { AimOutlined, AppstoreOutlined, SettingOutlined, BranchesOutlined } from '@ant-design/icons';
import DeviceOverviewPage from '../components/devices/DeviceOverviewPage';
import DeviceStatusPage from '../components/devices/DeviceStatusPage';
import { getSiteDevice, getSiteUAVList } from '../api/device';
import { useSelector } from 'react-redux';
import { RootState } from '../store';
import WayLinePage from '../components/devices/WayLinePage';
import RobotTaskPage from '../components/devices/RobotTaskPage';
const { Header, Content } = Layout;
const { Text } = Typography;
export default function DeviceIndexPage() {
const [activeTab, setActiveTab] = useState('overview');
const [selectedSn, setSelectedSn] = useState(null);
const { stationId } = useSelector((state: RootState) => state.station);
const [devices, setDevices] = useState([]);
const [planedevices, setPlaneDevices] = useState([]);
const [devicesAll, setDevicesAll] = useState([]);
const { userInfo } = useSelector((state: RootState) => state.user);
const tabs = [
{ key: 'overview', label: '装备总览', icon: <AppstoreOutlined /> },
{ key: 'status', label: '设备状态', icon: <SettingOutlined /> },
{ key: 'wayLineTask', label: '航线任务', icon: <AimOutlined /> },
{ key: 'robotTask', label: '机器人任务', icon: <BranchesOutlined /> },
];
// 获取设备列表(用于默认选择第一个)
const fetchAllData = async () => {
if (!stationId) return;
try {
const [deviceRes, planeRes] = await Promise.all([
getSiteDevice({ siteId: stationId, pageSize: 9999, pageNum: 1, orgId: userInfo.orgId }),
getSiteUAVList(stationId),
]);
const devData = deviceRes?.code === 200 ? (deviceRes.rows || []) : [];
let planeData = planeRes?.code === 200 ? (planeRes?.rows || []) : [];
planeData = planeData.map(item => ({
...item,
type: 'plane',
lastRunningStatus: {
longitude: item.longitude || 0,
latitude: item.latitude || 0,
},
deviceAlias: item.drone_callsign || item.device_sn || '无人机',
}));
setDevices(devData);
setPlaneDevices(planeData);
const all = [...devData, ...planeData];
setDevicesAll(all);
// 默认选中第一个设备
if (!selectedSn && all.length > 0) {
setSelectedSn(all[0].device_sn || all[0].serialNumber);
}
} catch (err) {
console.error('数据请求失败', err);
}
};
useEffect(() => {
fetchAllData();
}, [stationId]);
const handleViewStatus = (sn) => {
setSelectedSn(sn);
setActiveTab('status');
};
// 切换tab时,如果是overview或status,确保选择的是第一个设备
const handleTabChange = (key) => {
setActiveTab(key);
console.log('切换到tab', key, '当前选中设备SN', selectedSn);
// 如果没有选择设备,自动选择第一个
if ((key === 'wayLineTask' || key === 'robotTask') && devicesAll.length > 0) {
setSelectedSn(devicesAll[0].device_sn || devicesAll[0].serialNumber);
}
};
const renderContent = () => {
if (activeTab === 'overview') {
return <DeviceOverviewPage
onViewStatus={handleViewStatus}
selectedSn={selectedSn}
onDeviceSelect={(sn) => setSelectedSn(sn)}
/>;
}
if (activeTab === 'status') {
return <DeviceStatusPage
serialNumber={selectedSn}
onDeviceChange={(sn) => setSelectedSn(sn)}
/>;
}
if (activeTab === 'wayLineTask') return <WayLinePage />;
if (activeTab === 'robotTask') return <RobotTaskPage />;
return null;
};
return (
<Layout style={{ minHeight: '100vh', overflow: 'hidden', scrollbarWidth: "thin", scrollbarColor: '#ccc transparent' }}>
<div style={{ display: 'flex', paddingBottom: "4px" }}>
<div style={{ padding: '8px 12px 0', flexShrink: 0 }}>
<Typography.Title level={4} style={{ margin: 0, fontSize: 'clamp(14px, 1.2vw, 18px)' }}>装备控制</Typography.Title>
</div>
<Header style={{
flex: 1, padding: 0, background: 'transparent',
backgroundColor: 'transparent', borderBottom: '1px solid #f0f0f0', height: 40, lineHeight: '40px'
}}>
<Menu
mode="horizontal"
selectedKeys={[activeTab]}
onClick={({ key }) => handleTabChange(key)}
items={tabs}
style={{
border: '0',
height: 40,
lineHeight: '40px',
background: 'transparent',
backgroundColor: 'transparent'
}} />
</Header>
</div>
<Content style={{
maxHeight: 'calc(100vh - 40px)',
overflowY: 'auto',
background: '#f5f7fa',
padding: '8px 0px',
scrollbarWidth: "thin"
}}>
{renderContent()}
</Content>
</Layout>
);
}