Просмотр исходного кода

refactor(alarm): add type:'product'|'device'

Lind 4 лет назад
Родитель
Сommit
6fd8fa6679

+ 1 - 1
src/components/AlarmEditor/Action/index.ts

@@ -1,6 +1,6 @@
 import type { Field } from '@formily/core';
 import { FormPath, onFieldReact } from '@formily/core';
-import { service } from '@/pages/device/Product';
+import { service } from '@/pages/device/components/Alarm';
 import encodeQuery from '@/utils/encodeQuery';
 import type { Response } from '@/utils/typings';
 import type { DeviceInstance } from '@/pages/device/Instance/typings';

+ 0 - 4
src/pages/device/Instance/Detail/Alarm/index.tsx

@@ -1,4 +0,0 @@
-const Alarm = () => {
-  return <div>告警设置</div>;
-};
-export default Alarm;

+ 35 - 4
src/pages/device/Instance/Detail/Log/index.tsx

@@ -1,15 +1,30 @@
 import type { ProColumns } from '@jetlinks/pro-table';
 import ProTable from '@jetlinks/pro-table';
 import type { LogItem } from '@/pages/device/Instance/Detail/Log/typings';
-import moment from 'moment';
 import { Modal, Tooltip } from 'antd';
 import { EyeOutlined } from '@ant-design/icons';
 import { useIntl } from '@@/plugin-locale/localeExports';
 import { InstanceModel, service } from '@/pages/device/Instance';
+import { useEffect, useState } from 'react';
 
 const Log = () => {
   const intl = useIntl();
 
+  const [type, setType] = useState<any>({});
+  useEffect(() => {
+    service.getLogType().then((resp) => {
+      if (resp.status === 200) {
+        const list = (resp.result as { text: string; value: string }[]).reduce(
+          (previousValue, currentValue) => {
+            previousValue[currentValue.value] = currentValue;
+            return previousValue;
+          },
+          {},
+        );
+        setType(list);
+      }
+    });
+  }, []);
   const columns: ProColumns<LogItem>[] = [
     {
       dataIndex: 'index',
@@ -19,14 +34,29 @@ const Log = () => {
     {
       title: '类型',
       dataIndex: 'type',
-      renderText: (type) => type.text,
-      valueEnum: [], //TODO
+      renderText: (text) => text.text,
+      valueEnum: type,
     },
     {
       title: '时间',
       dataIndex: 'timestamp',
       defaultSortOrder: 'descend',
-      renderText: (text: string) => moment(text).format('YYYY-MM-DD HH:mm:ss'),
+      valueType: 'dateTime',
+      sorter: true,
+      hideInSearch: true,
+    },
+    {
+      title: '时间',
+      dataIndex: 'timestamp',
+      defaultSortOrder: 'descend',
+      valueType: 'dateTimeRange',
+      sorter: true,
+      hideInTable: true,
+      search: {
+        transform: (value) => ({
+          timestamp$BTW: value.toString(),
+        }),
+      },
     },
     {
       title: intl.formatMessage({
@@ -63,6 +93,7 @@ const Log = () => {
       defaultParams={{
         deviceId: InstanceModel.detail.id,
       }}
+      rowKey="id"
       pagination={{
         pageSize: 10,
       }}

+ 7 - 11
src/pages/device/Instance/Detail/index.tsx

@@ -7,7 +7,7 @@ import { statusMap } from '@/pages/device/Product';
 import { observer } from '@formily/react';
 import Config from '@/pages/device/Instance/Detail/Config';
 import Log from '@/pages/device/Instance/Detail/Log';
-import Alarm from '@/pages/device/Instance/Detail/Alarm';
+import Alarm from '@/pages/device/components/Alarm';
 import Info from '@/pages/device/Instance/Detail/Info';
 import Functions from '@/pages/device/Instance/Detail/Functions';
 import Running from '@/pages/device/Instance/Detail/Running';
@@ -109,7 +109,11 @@ const InstanceDetail = observer(() => {
         id: 'pages.device.instanceDetail.alarm',
         defaultMessage: '告警设置',
       }),
-      component: <Alarm />,
+      component: (
+        <Card>
+          <Alarm type="device" />
+        </Card>
+      ),
     },
     {
       key: 'visualization',
@@ -117,15 +121,7 @@ const InstanceDetail = observer(() => {
         id: 'pages.device.instanceDetail.visualization',
         defaultMessage: '可视化',
       }),
-      component: null,
-    },
-    {
-      key: 'shadow',
-      tab: intl.formatMessage({
-        id: 'pages.device.instanceDetail.shadow',
-        defaultMessage: '设备影子',
-      }),
-      component: null,
+      component: <div>开发中...</div>,
     },
   ];
 

+ 3 - 3
src/pages/device/Product/Detail/index.tsx

@@ -7,7 +7,7 @@ import { productModel, service } from '@/pages/device/Product';
 import { useCallback, useEffect, useState } from 'react';
 import { useIntl } from '@@/plugin-locale/localeExports';
 import Metadata from '@/pages/device/components/Metadata';
-import Alarm from '@/pages/device/Product/Detail/Alarm';
+import Alarm from '@/pages/device/components/Alarm';
 import type { DeviceMetadata } from '@/pages/device/Product/typings';
 import { Link } from 'umi';
 import { Store } from 'jetlinks-store';
@@ -199,7 +199,7 @@ const ProductDetail = observer(() => {
             })}
             key="metadata"
           >
-            <Metadata />
+            <Metadata type="product" />
           </Tabs.TabPane>
           <Tabs.TabPane
             tab={intl.formatMessage({
@@ -208,7 +208,7 @@ const ProductDetail = observer(() => {
             })}
             key="alarm"
           >
-            <Alarm />
+            <Alarm type="product" />
           </Tabs.TabPane>
         </Tabs>
       </Card>

+ 0 - 28
src/pages/device/Product/service.ts

@@ -108,34 +108,6 @@ class Service extends BaseService<ProductItem> {
       method: 'GET',
     });
 
-  public notifier = {
-    types: () =>
-      request(`/${SystemConst.API_BASE}/notifier/config/types`, {
-        method: 'GET',
-      }),
-    config: (params: Record<string, unknown>) =>
-      request(`/${SystemConst.API_BASE}/notifier/config/_query/no-paging`, {
-        method: 'GET',
-        params,
-      }),
-    template: (params: Record<string, unknown>) =>
-      request(`/${SystemConst.API_BASE}/notifier/template/_query/no-paging`, {
-        method: 'GET',
-        params,
-      }),
-  };
-
-  public deviceDetail = (id: string) =>
-    request(`/${SystemConst.API_BASE}/device/instance/${id}/detail`, {
-      method: 'GET',
-    });
-
-  public saveAlarm = (id: string, data: Record<string, unknown>) =>
-    request(`/${SystemConst.API_BASE}/device/alarm/product/${id}`, {
-      method: 'PATCH',
-      data,
-    });
-
   public category = () =>
     request(`/${SystemConst.API_BASE}/device/category/_tree?paging=false`, {
       method: 'GET',

src/pages/device/Product/Detail/Alarm/Edit/index.less → src/pages/device/components/Alarm/Edit/index.less


+ 10 - 7
src/pages/device/Product/Detail/Alarm/Edit/index.tsx

@@ -23,20 +23,24 @@ import { useIntl } from '@@/plugin-locale/localeExports';
 import './index.less';
 import FAutoComplete from '@/components/FAutoComplete';
 import { action } from '@formily/reactive';
-import { productModel, service } from '@/pages/device/Product';
+import { productModel } from '@/pages/device/Product';
 import FSelectDevice from '@/components/FSelectDevice';
 import Trigger from '@/components/AlarmEditor/Trigger';
 import Action from '@/components/AlarmEditor/Action';
+import { service } from '@/pages/device/components/Alarm';
+import { useParams } from 'umi';
 
 interface Props {
   visible: boolean;
   close: () => void;
   data: any;
+  type: 'product' | 'device';
 }
 
 const EditAlarm = (props: Props) => {
-  const { visible, close } = props;
+  const { visible, close, type } = props;
 
+  const params = useParams<{ id: string }>();
   const intl = useIntl();
   const form = createForm({
     initialValues: props.data?.alarmRule,
@@ -286,23 +290,22 @@ const EditAlarm = (props: Props) => {
 
   const handleSubmit = async () => {
     const alarmRule = (await form.submit()) as any;
-    const id = productModel.current?.id;
+    const id = params?.id;
     if (id) {
       const data = {
         name: alarmRule?.name,
-        target: 'product',
+        target: type,
         targetId: id,
+        id: props.data.id,
         alarmRule: {
           ...alarmRule,
           productId: productModel.current?.id,
           productName: productModel.current?.name,
         },
       };
-      await service.saveAlarm(id, data);
+      await service.saveAlarm(type, id, data);
       message.success('保存成功');
       props.close();
-    } else {
-      message.error('刷新页面重试');
     }
   };
   return (

+ 24 - 2
src/pages/device/Product/Detail/Alarm/Record/index.tsx

@@ -2,8 +2,15 @@ import type { AlarmRecord } from '@/pages/device/Product/typings';
 import type { ProColumns } from '@jetlinks/pro-table';
 import ProTable from '@jetlinks/pro-table';
 import { useIntl } from '@@/plugin-locale/localeExports';
+import { service } from '@/pages/device/components/Alarm';
+import { useParams } from 'umi';
 
-const Record = () => {
+interface Props {
+  type: 'device' | 'product';
+}
+
+const Record = (props: Props) => {
+  const { type } = props;
   const intl = useIntl();
   const columns: ProColumns<AlarmRecord>[] = [
     {
@@ -30,6 +37,7 @@ const Record = () => {
         id: 'pages.device.alarm.time',
         defaultMessage: '告警时间',
       }),
+      valueType: 'dateTime',
       dataIndex: 'alarmTime',
       defaultSortOrder: 'descend',
       sorter: true,
@@ -43,6 +51,20 @@ const Record = () => {
     },
   ];
 
-  return <ProTable columns={columns} rowKey="id" search={false} />;
+  const params = useParams<{ id: string }>();
+  return (
+    <ProTable
+      columns={columns}
+      rowKey="id"
+      request={(param) => service.record(param)}
+      pagination={{
+        pageSize: 10,
+      }}
+      defaultParams={{
+        [`${type}Id`]: params.id,
+      }}
+      search={false}
+    />
+  );
 };
 export default Record;

+ 10 - 3
src/pages/device/Product/Detail/Alarm/Setting/index.tsx

@@ -3,7 +3,6 @@ import ProTable from '@jetlinks/pro-table';
 import type { AlarmSetting } from '@/pages/device/Product/typings';
 import { Button, Space, Tooltip } from 'antd';
 import { useIntl } from '@@/plugin-locale/localeExports';
-import { service } from '@/pages/device/Product';
 import { useParams } from 'umi';
 import {
   EditOutlined,
@@ -14,8 +13,14 @@ import {
 } from '@ant-design/icons';
 import Edit from '../Edit';
 import { useState } from 'react';
+import { service } from '@/pages/device/components/Alarm';
 
-const Setting = () => {
+interface Props {
+  type: 'product' | 'device';
+}
+
+const Setting = (props: Props) => {
+  const { type } = props;
   const intl = useIntl();
   const param = useParams<{ id: string }>();
   const [edit, setEdit] = useState<boolean>(false);
@@ -45,6 +50,7 @@ const Setting = () => {
         id: 'pages.table.createTime',
         defaultMessage: '创建时间',
       }),
+      valueType: 'dateTime',
       dataIndex: 'createTime',
     },
     {
@@ -130,7 +136,7 @@ const Setting = () => {
         ]}
         pagination={false}
         request={async () => {
-          const response = await service.productAlarm(param.id);
+          const response = await service.alarmList(type, param.id);
           return {
             result: { data: response.result },
             success: true,
@@ -142,6 +148,7 @@ const Setting = () => {
         search={false}
       />
       <Edit
+        type={type}
         data={data}
         visible={edit}
         close={() => {

+ 12 - 5
src/pages/device/Product/Detail/Alarm/index.tsx

@@ -1,9 +1,16 @@
 import { Tabs } from 'antd';
-import Setting from '@/pages/device/Product/Detail/Alarm/Setting';
-import Record from '@/pages/device/Product/Detail/Alarm/Record';
+import Setting from './Setting';
+import Record from './Record';
 import { useIntl } from '@@/plugin-locale/localeExports';
+import Service from './service';
 
-const Alarm = () => {
+interface Props {
+  type: 'product' | 'device';
+}
+
+export const service = new Service();
+
+const Alarm = (props: Props) => {
   const intl = useIntl();
   return (
     <Tabs>
@@ -14,7 +21,7 @@ const Alarm = () => {
           defaultMessage: '告警设置',
         })}
       >
-        <Setting />
+        <Setting type={props.type} />
       </Tabs.TabPane>
       <Tabs.TabPane
         key="record"
@@ -23,7 +30,7 @@ const Alarm = () => {
           defaultMessage: '告警记录',
         })}
       >
-        <Record />
+        <Record type={props.type} />
       </Tabs.TabPane>
     </Tabs>
   );

+ 47 - 0
src/pages/device/components/Alarm/service.ts

@@ -0,0 +1,47 @@
+import BaseService from '@/utils/BaseService';
+import { request } from '@@/plugin-request/request';
+import SystemConst from '@/utils/const';
+
+class Service extends BaseService<any> {
+  public alarmList = (type: 'product' | 'device', id: string) =>
+    request(`/${SystemConst.API_BASE}/device/alarm/${type}/${id}`, {
+      method: 'GET',
+    });
+
+  public notifier = {
+    types: () =>
+      request(`/${SystemConst.API_BASE}/notifier/config/types`, {
+        method: 'GET',
+      }),
+    config: (params: Record<string, unknown>) =>
+      request(`/${SystemConst.API_BASE}/notifier/config/_query/no-paging`, {
+        method: 'GET',
+        params,
+      }),
+    template: (params: Record<string, unknown>) =>
+      request(`/${SystemConst.API_BASE}/notifier/template/_query/no-paging`, {
+        method: 'GET',
+        params,
+      }),
+  };
+
+  // 查询产品告警记录
+  public record = (params: Record<string, unknown>) =>
+    request(`/${SystemConst.API_BASE}/device/alarm/history/_query`, {
+      method: 'GET',
+      params,
+    });
+
+  public saveAlarm = (type: 'product' | 'device', id: string, data: Record<string, unknown>) =>
+    request(`/${SystemConst.API_BASE}/device/alarm/${type}/${id}`, {
+      method: 'PATCH',
+      data,
+    });
+
+  public deviceDetail = (id: string) =>
+    request(`/${SystemConst.API_BASE}/device/instance/${id}/detail`, {
+      method: 'GET',
+    });
+}
+
+export default Service;