| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import React, { useState, useEffect } from 'react';
- import {
- Button, Table, Form, Input, Select, message, Card, Badge,
- } from 'antd';
- import dayjs from 'dayjs';
- import 'dayjs/locale/zh-cn';
- // 从share/types.ts导入所有类型,包括MapMode
- import type {
- DeviceMonitorData,
- } from '../share/monitorTypes.ts';
- import {
- DeviceStatus, DeviceProtocolType, MetricType, DeviceStatusNameMap, DeviceProtocolTypeNameMap,
- MetricTypeNameMap
- } from '../share/monitorTypes.ts';
- import { getEnumOptions } from './utils.ts';
- import { DeviceInstanceAPI, MonitorAPI } from './api/index.ts';
- // 设备实时监控页面
- export const DeviceMonitorPage = () => {
- const [loading, setLoading] = useState(false);
- const [monitorData, setMonitorData] = useState<DeviceMonitorData[]>([]);
- const [pagination, setPagination] = useState({
- current: 1,
- pageSize: 10,
- total: 0,
- });
- const [deviceOptions, setDeviceOptions] = useState<{label: string, value: number}[]>([]);
- const [formRef] = Form.useForm();
-
- // 监控数据刷新间隔(毫秒)
- const REFRESH_INTERVAL = 30000;
- useEffect(() => {
- fetchDeviceOptions();
- fetchMonitorData();
-
- // 设置定时刷新
- const intervalId = setInterval(() => {
- fetchMonitorData();
- }, REFRESH_INTERVAL);
-
- return () => clearInterval(intervalId);
- }, [pagination.current, pagination.pageSize]);
- const fetchDeviceOptions = async () => {
- try {
- const response = await DeviceInstanceAPI.getDeviceInstances();
- if (response && response.data) {
- const options = response.data.map((device) => ({
- label: device.asset_name || `设备${device.id}`,
- value: device.id
- }));
- setDeviceOptions(options);
- }
- } catch (error) {
- console.error('获取设备列表失败:', error);
- message.error('获取设备列表失败');
- }
- };
- const fetchMonitorData = async () => {
- setLoading(true);
- try {
- const values = formRef.getFieldsValue();
- const params = {
- page: pagination.current,
- pageSize: pagination.pageSize,
- device_id: values.device_id,
- device_name: values.device_name,
- protocol: values.protocol,
- address: values.address,
- metric_type: values.metric_type,
- status: values.status,
- };
-
- const response = await MonitorAPI.getMonitorData(params);
-
- if (response) {
- setMonitorData(response.data || []);
- setPagination({
- ...pagination,
- total: response.total || 0,
- });
- }
- } catch (error) {
- console.error('获取监控数据失败:', error);
- message.error('获取监控数据失败');
- } finally {
- setLoading(false);
- }
- };
- const handleSearch = (values: any) => {
- setPagination({
- ...pagination,
- current: 1,
- });
- fetchMonitorData();
- };
- const handleTableChange = (newPagination: any) => {
- setPagination({
- ...pagination,
- current: newPagination.current,
- pageSize: newPagination.pageSize,
- });
- };
- const metricTypeOptions = getEnumOptions(MetricType, MetricTypeNameMap);
- const statusOptions = getEnumOptions(DeviceStatus, DeviceStatusNameMap);
- const protocolOptions = getEnumOptions(DeviceProtocolType, DeviceProtocolTypeNameMap);
- const getStatusBadge = (status?: DeviceStatus) => {
- switch (status) {
- case DeviceStatus.NORMAL:
- return <Badge status="success" text="正常" />;
- case DeviceStatus.MAINTAIN:
- return <Badge status="processing" text="维护中" />;
- case DeviceStatus.FAULT:
- return <Badge status="error" text="故障" />;
- case DeviceStatus.OFFLINE:
- return <Badge status="default" text="下线" />;
- default:
- return <Badge status="default" text="未知" />;
- }
- };
- const columns = [
- {
- title: '设备ID',
- dataIndex: 'device_id',
- key: 'device_id',
- width: 80,
- },
- {
- title: '设备名称',
- dataIndex: 'device_name',
- key: 'device_name',
- },
- {
- title: '通信协议',
- dataIndex: 'protocol',
- key: 'protocol',
- render: (text: string) => {
- const option = protocolOptions.find(opt => opt.value === text);
- return option ? option.label : text;
- },
- },
- {
- title: '通信地址',
- dataIndex: 'address',
- key: 'address',
- },
- {
- title: '监控指标',
- dataIndex: 'metric_type',
- key: 'metric_type',
- render: (text: string) => {
- const option = metricTypeOptions.find(opt => opt.value === text);
- return option ? option.label : text;
- },
- },
- {
- title: '监控值',
- dataIndex: 'metric_value',
- key: 'metric_value',
- render: (value: number, record: DeviceMonitorData) => {
- return `${value} ${record.unit || ''}`;
- },
- },
- {
- title: '状态',
- dataIndex: 'status',
- key: 'status',
- render: (status: DeviceStatus) => getStatusBadge(status),
- },
- {
- title: '采集时间',
- dataIndex: 'collect_time',
- key: 'collect_time',
- render: (text: Date) => dayjs(text).format('YYYY-MM-DD HH:mm:ss'),
- },
- ];
- return (
- <div>
- <Card title="设备实时监控" style={{ marginBottom: 16 }}>
- <Form
- form={formRef}
- layout="inline"
- onFinish={handleSearch}
- style={{ marginBottom: 16 }}
- >
- <Form.Item name="device_id" label="设备">
- <Select
- placeholder="选择设备"
- style={{ width: 200 }}
- allowClear
- options={deviceOptions}
- />
- </Form.Item>
- <Form.Item name="device_name" label="设备名称">
- <Input placeholder="输入设备名称" style={{ width: 200 }} />
- </Form.Item>
- <Form.Item name="protocol" label="通信协议">
- <Select
- placeholder="选择通信协议"
- style={{ width: 200 }}
- allowClear
- options={protocolOptions}
- />
- </Form.Item>
- <Form.Item name="address" label="通信地址">
- <Input placeholder="输入通信地址" style={{ width: 200 }} />
- </Form.Item>
- <Form.Item name="metric_type" label="监控指标">
- <Select
- placeholder="选择监控指标"
- style={{ width: 200 }}
- allowClear
- options={metricTypeOptions}
- />
- </Form.Item>
- <Form.Item name="status" label="状态">
- <Select
- placeholder="选择状态"
- style={{ width: 120 }}
- allowClear
- options={statusOptions}
- />
- </Form.Item>
- <Form.Item>
- <Button type="primary" htmlType="submit">
- 查询
- </Button>
- </Form.Item>
- </Form>
-
- <Table
- columns={columns}
- dataSource={monitorData}
- rowKey="id"
- pagination={pagination}
- loading={loading}
- onChange={handleTableChange}
- />
- </Card>
- </div>
- );
- };
|