|
|
@@ -1,6 +1,6 @@
|
|
|
import { Col, Input, message, Pagination, Row, Space, Table } from 'antd';
|
|
|
import CheckButton from '@/components/CheckButton';
|
|
|
-import { useEffect, useState } from 'react';
|
|
|
+import { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
import type { PropertyMetadata } from '@/pages/device/Product/typings';
|
|
|
import PropertyCard from './PropertyCard';
|
|
|
import { EditOutlined, SyncOutlined, UnorderedListOutlined } from '@ant-design/icons';
|
|
|
@@ -10,9 +10,9 @@ import { map } from 'rxjs/operators';
|
|
|
import EditProperty from './EditProperty';
|
|
|
import { useParams } from 'umi';
|
|
|
import PropertyLog from '../../MetadataLog/Property';
|
|
|
-import moment from 'moment';
|
|
|
import styles from './index.less';
|
|
|
-import FileComponent from './FileComponent';
|
|
|
+import { throttle } from 'lodash';
|
|
|
+import PropertyTable from './PropertyTable';
|
|
|
|
|
|
interface Props {
|
|
|
data: Partial<PropertyMetadata>[];
|
|
|
@@ -64,17 +64,17 @@ const Property = (props: Props) => {
|
|
|
title: '值',
|
|
|
dataIndex: 'value',
|
|
|
key: 'value',
|
|
|
- render: (text: any, record: any) => (
|
|
|
- <FileComponent type="table" value={propertyValue[record.id]} data={record} />
|
|
|
- ),
|
|
|
+ render: (text: any, record: any) => {
|
|
|
+ return <PropertyTable type="value" value={propertyValue[record.id]} data={record} />;
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: '更新时间',
|
|
|
dataIndex: 'time',
|
|
|
key: 'time',
|
|
|
- render: (text: any, record: any) => (
|
|
|
- <span>{moment(propertyValue[record.id]?.timestamp).format('YYYY-MM-DD HH:mm:ss')}</span>
|
|
|
- ),
|
|
|
+ render: (text: any, record: any) => {
|
|
|
+ return <PropertyTable type="time" value={propertyValue[record.id]} data={record} />;
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: '操作',
|
|
|
@@ -110,6 +110,24 @@ const Property = (props: Props) => {
|
|
|
},
|
|
|
];
|
|
|
|
|
|
+ const list = useRef<any[]>([]);
|
|
|
+
|
|
|
+ const subRef = useRef<any>(null);
|
|
|
+
|
|
|
+ const valueChange = (payload: any) => {
|
|
|
+ (payload || [])
|
|
|
+ .sort((a: any, b: any) => a.timestamp - b.timestamp)
|
|
|
+ .forEach((item: any) => {
|
|
|
+ const { value } = item;
|
|
|
+ propertyValue[value?.property] = { ...item, ...value };
|
|
|
+ });
|
|
|
+ setPropertyValue({ ...propertyValue });
|
|
|
+ list.current = [];
|
|
|
+ };
|
|
|
+
|
|
|
+ // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
+ const throttleFn = useCallback(throttle(valueChange, 500), []);
|
|
|
+
|
|
|
/**
|
|
|
* 订阅属性数据
|
|
|
*/
|
|
|
@@ -118,16 +136,15 @@ const Property = (props: Props) => {
|
|
|
.map((i: PropertyMetadata) => i.id)
|
|
|
.join('-')}`;
|
|
|
const topic = `/dashboard/device/${device.productId}/properties/realTime`;
|
|
|
- subscribeTopic!(id, topic, {
|
|
|
+ subRef.current = subscribeTopic!(id, topic, {
|
|
|
deviceId: device.id,
|
|
|
properties: dataSource.data.map((i: PropertyMetadata) => i.id),
|
|
|
history: 1,
|
|
|
})
|
|
|
?.pipe(map((res) => res.payload))
|
|
|
.subscribe((payload: any) => {
|
|
|
- const { value } = payload;
|
|
|
- propertyValue[value.property] = { ...payload, ...value };
|
|
|
- setPropertyValue({ ...propertyValue });
|
|
|
+ list.current = [...list.current, payload];
|
|
|
+ throttleFn(list.current);
|
|
|
});
|
|
|
};
|
|
|
|
|
|
@@ -161,6 +178,13 @@ const Property = (props: Props) => {
|
|
|
}
|
|
|
}, [dataSource]);
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
+ return () => {
|
|
|
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
|
+ subRef.current && subRef.current?.unsubscribe();
|
|
|
+ };
|
|
|
+ }, []);
|
|
|
+
|
|
|
return (
|
|
|
<div>
|
|
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
|
|
@@ -170,15 +194,15 @@ const Property = (props: Props) => {
|
|
|
placeholder="请输入名称"
|
|
|
onSearch={(value: string) => {
|
|
|
if (!!value) {
|
|
|
- const list = data.filter((item) => {
|
|
|
+ const li = data.filter((item) => {
|
|
|
return (
|
|
|
item.name && item.name.toLowerCase().indexOf(value.toLocaleLowerCase()) !== -1
|
|
|
);
|
|
|
});
|
|
|
- setPropertyList(list);
|
|
|
+ setPropertyList(li);
|
|
|
setDataSource({
|
|
|
- total: list.length,
|
|
|
- data: (list || []).slice(0, 8),
|
|
|
+ total: li.length,
|
|
|
+ data: (li || []).slice(0, 8),
|
|
|
pageSize: 8,
|
|
|
currentPage: 0,
|
|
|
});
|
|
|
@@ -229,6 +253,7 @@ const Property = (props: Props) => {
|
|
|
total={dataSource.total}
|
|
|
showSizeChanger
|
|
|
pageSize={dataSource.pageSize}
|
|
|
+ pageSizeOptions={[8, 16, 32, 48]}
|
|
|
onChange={(page: number, size: number) => {
|
|
|
setDataSource({
|
|
|
total: propertyList.length,
|