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

156 lines
4.8 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, Tabs } from 'antd';
import { AimOutlined, AppstoreOutlined, SettingOutlined } from '@ant-design/icons';
import DeviceOverviewPage from '../components/devices/DeviceOverviewPage';
import DeviceStatusPage from '../components/devices/DeviceStatusPage';
import { getDeviceListApi, getSiteUAVList } from '../api/device';
import { useSelector } from 'react-redux';
import { RootState } from '../store';
import WayLinePage from '../components/devices/WayLinePage';
const { Header, Content } = Layout;
const { Text } = Typography;
const DeviceStatusSettingPage = ({ serialNumber }) => (
<div style={{ padding: 20 }}>
<h2>设置状态页面</h2>
<p>当前查看设备序列号: <span style={{ fontWeight: 'bold', color: '#1890ff' }}>{serialNumber || '未选择设备'}</span></p>
</div>
);
export default function DeviceIndexPage() {
const [activeTab, setActiveTab] = useState('overview');
const [selectedSn, setSelectedSn] = useState(null);
const { userInfo } = useSelector((state: RootState) => state.user);
const { stationId } = useSelector((state: RootState) => state.station);
const [devices, setDevices] = useState([]);
const [planedevices, setPlaneDevices] = useState([]);
const [devicesAll, setDevicesAll] = useState([]);
const tabs = [
{ key: 'overview', label: '装备总览', icon: <AppstoreOutlined /> },
{ key: 'status', label: '设备状态', icon: <SettingOutlined /> },
{ key: 'wayLineTask', label: '航线任务', icon: <AimOutlined /> },
];
// 👇 核心:给无人机补全 lastRunningStatus + type: 'plane'
const fetchAllData = async () => {
if (!userInfo?.username || !stationId) return;
try {
// 1. 同时发请求
const [deviceRes, planeRes] = await Promise.all([
getDeviceListApi({ tenantName: userInfo.username, deptId: 1 }),
getSiteUAVList(stationId),
]);
// 2. 处理普通设备
const devData = deviceRes?.code === 200 ? (deviceRes.data || []) : [];
// 3. 处理无人机 + 自动补全地图需要的字段(关键改动)
let planeData = planeRes?.code === 200 ? (planeRes?.rows || []) : [];
// ====================== ✅ 核心改造 ======================
planeData = planeData.map(item => ({
...item,
// 给无人机统一设置 type: plane
type: 'plane',
// 自动生成 lastRunningStatus(经纬度取无人机自身字段)
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();
}, [userInfo, stationId]);
const handleViewStatus = (sn) => {
setSelectedSn(sn);
setActiveTab('status');
};
const renderContent = () => {
if (activeTab === 'overview') {
return <DeviceOverviewPage
devices={devices}
planedevices={planedevices}
devicesAll={devicesAll}
onViewStatus={handleViewStatus}
selectedSn={selectedSn}
onDeviceSelect={(sn) => setSelectedSn(sn)}
/>;
}
if (activeTab === 'status') {
return <DeviceStatusPage
devicesAll={devicesAll}
serialNumber={selectedSn}
onDeviceChange={(sn) => setSelectedSn(sn)}
/>;
}
if (activeTab === 'wayLineTask') return <WayLinePage planedevices={planedevices} />;
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 }) => setActiveTab(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>
);
}