hear 2 лет назад
Родитель
Сommit
7ce8b163bc

+ 45 - 3
src/pages/DataCollect/Collector/components/Point/Save/modbus.tsx

@@ -1,5 +1,5 @@
 import { Button, Modal } from 'antd';
-import { createForm, Field, registerValidateRules } from '@formily/core';
+import { createForm, Field, onFieldValueChange, registerValidateRules } from '@formily/core';
 import { createSchemaField } from '@formily/react';
 import React, { useEffect, useState } from 'react';
 import * as ICONS from '@ant-design/icons';
@@ -54,6 +54,48 @@ export default (props: Props) => {
   const form = createForm({
     validateFirst: true,
     initialValues: data || {},
+    effects: () => {
+      onFieldValueChange('configuration.function', (field, f) => {
+        const value = (field as Field).value;
+        if (value === 'InputRegisters') {
+          f.setFieldState('configuration.codec.provider', (state) => {
+            state.dataSource = state.dataSource?.filter((item) => item.value !== 'bool');
+          });
+          f.setFieldState('accessModes', (state) => {
+            state.componentProps = {
+              placeholder: '请选择访问类型',
+              model: 'multiple',
+              itemStyle: {
+                display: 'flex',
+                flexDirection: 'column',
+                justifyContent: 'space-around',
+                minWidth: '130px',
+                height: '50px',
+              },
+              options: [{ label: '读', value: 'read' }],
+            };
+          });
+        } else {
+          f.setFieldState('accessModes', (state) => {
+            state.componentProps = {
+              placeholder: '请选择访问类型',
+              model: 'multiple',
+              itemStyle: {
+                display: 'flex',
+                flexDirection: 'column',
+                justifyContent: 'space-around',
+                minWidth: '130px',
+                height: '50px',
+              },
+              options: [
+                { label: '读', value: 'read' },
+                { label: '写', value: 'write' },
+              ],
+            };
+          });
+        }
+      });
+    },
   });
 
   const SchemaField = createSchemaField({
@@ -174,7 +216,7 @@ export default (props: Props) => {
             enum: [
               { label: '01线圈寄存器', value: 'Coils' },
               { label: '03保存寄存器', value: 'HoldingRegisters' },
-              { label: '04输入寄存器', value: 'DiscreteInputs' },
+              { label: '04输入寄存器', value: 'InputRegisters' },
             ],
             'x-validator': [
               {
@@ -290,7 +332,7 @@ export default (props: Props) => {
                 dependencies: ['..function', 'configuration.parameter.quantity'],
                 fulfill: {
                   state: {
-                    visible: '{{$deps[0] === "HoldingRegisters"}}',
+                    visible: '{{$deps[0] === "HoldingRegisters" || $deps[0] === "InputRegisters"}}',
                     selfErrors:
                       '{{$deps[1] && $self.value && {"int8:": 1, "int16": 2, "int32": 4, "int64": 8, "ieee754_float": 4, "ieee754_double": 8, "hex": 1}[$self.value] > $deps[1] * 2 ? "数据类型长度需 <= 寄存器数量 * 2" : ""}}',
                   },

+ 138 - 85
src/pages/device/Instance/Detail/EdgeMap/mapTable/index.tsx

@@ -1,6 +1,11 @@
 import PermissionButton from '@/components/PermissionButton';
 import TitleComponent from '@/components/TitleComponent';
-import { DisconnectOutlined, QuestionCircleOutlined } from '@ant-design/icons';
+import {
+  DisconnectOutlined,
+  PlayCircleOutlined,
+  QuestionCircleOutlined,
+  StopOutlined,
+} from '@ant-design/icons';
 import { FormItem, ArrayTable, Editable, Select, NumberPicker } from '@formily/antd';
 import { createForm, Field, FormPath, onFieldReact } from '@formily/core';
 import type { Response } from '@/utils/typings';
@@ -51,35 +56,142 @@ const MapTable = (props: Props) => {
     return <>{text?.metadataName}</>;
   };
   const StatusRender = (propsRender: any) => {
+    // console.log(propsRender.value,'---------')
+    const record = ArrayTable.useRecord?.();
+    const index = ArrayTable.useIndex?.();
+    const state = record(index)?.state;
     if (propsRender.value) {
-      return <Badge status="success" text={'已绑定'} />;
+      if (state.value === 'enabled') {
+        return <Badge status="success" text={'在线'} />;
+      } else {
+        return <Badge status="warning" text={'离线'} />;
+      }
     } else {
       return <Badge status="error" text={'未绑定'} />;
     }
   };
+  const save = async (item: any) => {
+    const res = await service.saveMap(edgeId, item);
+    if (res.status === 200) {
+      onlyMessage('保存成功');
+      if (props.formRef) {
+        props.close();
+      } else {
+        reload('save');
+      }
+    }
+  };
+  const form = createForm({
+    values: {
+      requestList: metaData,
+    },
+    effects: () => {
+      onFieldReact('requestList.*.channelId', async (field, f) => {
+        const value = (field as Field).value;
+        // console.log(field, 'provider')
+        if (value) {
+          const param = channelList.find((item: any) => item.value === value);
+          const providerPath = FormPath.transform(
+            field.path,
+            /\d+/,
+            (index) => `requestList.${parseInt(index)}.provider`,
+          );
+          f.setFieldState(providerPath, (state) => {
+            state.value = param?.provider;
+          });
+        }
+        const path = FormPath.transform(
+          field.path,
+          /\d+/,
+          (index) => `requestList.${parseInt(index)}.collectorId`,
+        );
+        const path1 = FormPath.transform(
+          field.path,
+          /\d+/,
+          (index) => `requestList.${parseInt(index)}.pointId`,
+        );
+        f.setFieldState(path, (state) => {
+          if (value) {
+            state.required = true;
+            form.validate();
+          } else {
+            state.required = false;
+            form.validate();
+          }
+        });
+        f.setFieldState(path1, (state) => {
+          if (value) {
+            state.required = true;
+            form.validate();
+          } else {
+            state.required = false;
+            form.validate();
+          }
+        });
+      });
+    },
+  });
   const ActionButton = () => {
     const record = ArrayTable.useRecord?.();
     const index = ArrayTable.useIndex?.();
     return (
-      <PermissionButton
-        isPermission={permission.update}
-        style={{ padding: 0 }}
-        disabled={!record(index)?.id}
-        tooltip={{
-          title: '解绑',
-        }}
-        popConfirm={{
-          title: '确认解绑',
-          disabled: !record(index)?.id,
-          onConfirm: async () => {
-            remove(record(index)?.id);
-          },
-        }}
-        key="unbind"
-        type="link"
-      >
-        <DisconnectOutlined />
-      </PermissionButton>
+      <>
+        <PermissionButton
+          isPermission={permission.update}
+          style={{ padding: 0 }}
+          disabled={!record(index)?.id}
+          tooltip={{
+            title: '解绑',
+          }}
+          popConfirm={{
+            title: '确认解绑',
+            disabled: !record(index)?.id,
+            onConfirm: async () => {
+              remove(record(index)?.id);
+            },
+          }}
+          key="unbind"
+          type="link"
+        >
+          <DisconnectOutlined />
+        </PermissionButton>
+        {record(index).id && (
+          <PermissionButton
+            isPermission={permission.update}
+            style={{ padding: 0, marginLeft: 10 }}
+            // disabled={!record(index)?.id}
+            tooltip={{
+              title: record(index).state.value === 'enabled' ? '禁用' : '启用',
+            }}
+            popConfirm={{
+              title: record(index).state.value === 'enabled' ? '确认禁用' : '确认启用',
+              disabled: !record(index)?.id,
+              onConfirm: async () => {
+                const value: any = await form.submit();
+                const array = value.requestList.filter((item: any) => item.channelId);
+                const findArray = array.find((item: any) => item.id === record(index)?.id);
+                const arr = {
+                  ...findArray,
+                  state: record(index).state.value === 'enabled' ? 'disabled' : 'enabled',
+                };
+                const filterArray = array.filter((item: any) => item.id !== record(index)?.id);
+                console.log('array----', array, findArray);
+                const submitData = {
+                  deviceId: deviceId,
+                  provider: array[0]?.provider,
+                  requestList: [...filterArray, arr],
+                };
+                console.log('submitData', submitData);
+                save(submitData);
+              },
+            }}
+            key="unbind"
+            type="link"
+          >
+            {record(index).state.value === 'enabled' ? <StopOutlined /> : <PlayCircleOutlined />}
+          </PermissionButton>
+        )}
+      </>
     );
   };
 
@@ -152,68 +264,6 @@ const MapTable = (props: Props) => {
     },
   });
 
-  const save = async (item: any) => {
-    const res = await service.saveMap(edgeId, item);
-    if (res.status === 200) {
-      onlyMessage('保存成功');
-      if (props.formRef) {
-        props.close();
-      } else {
-        reload('save');
-      }
-    }
-  };
-
-  const form = createForm({
-    values: {
-      requestList: metaData,
-    },
-    effects: () => {
-      onFieldReact('requestList.*.channelId', async (field, f) => {
-        const value = (field as Field).value;
-        // console.log(field, 'provider')
-        if (value) {
-          const param = channelList.find((item: any) => item.value === value);
-          const providerPath = FormPath.transform(
-            field.path,
-            /\d+/,
-            (index) => `requestList.${parseInt(index)}.provider`,
-          );
-          f.setFieldState(providerPath, (state) => {
-            state.value = param?.provider;
-          });
-        }
-        const path = FormPath.transform(
-          field.path,
-          /\d+/,
-          (index) => `requestList.${parseInt(index)}.collectorId`,
-        );
-        const path1 = FormPath.transform(
-          field.path,
-          /\d+/,
-          (index) => `requestList.${parseInt(index)}.pointId`,
-        );
-        f.setFieldState(path, (state) => {
-          if (value) {
-            state.required = true;
-            form.validate();
-          } else {
-            state.required = false;
-            form.validate();
-          }
-        });
-        f.setFieldState(path1, (state) => {
-          if (value) {
-            state.required = true;
-            form.validate();
-          } else {
-            state.required = false;
-            form.validate();
-          }
-        });
-      });
-    },
-  });
   const add = async () => {
     const value = await props.formRef.validateFields();
     const mapValue: any = await form.submit();
@@ -457,13 +507,16 @@ const MapTable = (props: Props) => {
               const submitData = {
                 deviceId: deviceId,
                 provider: array[0]?.provider,
-                requestList: array,
+                requestList: array.map((item: any) => ({
+                  ...item,
+                  state: 'enabled',
+                })),
               };
               save(submitData);
             }
           }}
         >
-          保存
+          保存并启用
         </PermissionButton>
       </div>
       <div>