|
|
@@ -5,12 +5,18 @@ import { Form, Input, FormItem, Select, Space, ArrayTable } from '@formily/antd'
|
|
|
import { action } from '@formily/reactive';
|
|
|
import type { ISchema } from '@formily/json-schema';
|
|
|
import { service } from '@/pages/device/Command';
|
|
|
-import { Modal } from 'antd';
|
|
|
+import { message, Modal } from 'antd';
|
|
|
import FSelectDevices from '@/components/FSelectDevices';
|
|
|
import { useRef } from 'react';
|
|
|
import type { DeviceMetadata, ProductItem } from '@/pages/device/Product/typings';
|
|
|
|
|
|
-const Create = () => {
|
|
|
+interface Props {
|
|
|
+ close: () => void;
|
|
|
+ visible: boolean;
|
|
|
+}
|
|
|
+
|
|
|
+const Create = (props: Props) => {
|
|
|
+ const { close, visible } = props;
|
|
|
const products = useRef<ProductItem[]>([]);
|
|
|
|
|
|
const metadataRef = useRef<DeviceMetadata>();
|
|
|
@@ -28,7 +34,7 @@ const Create = () => {
|
|
|
|
|
|
const metadata = JSON.parse(product?.metadata as string) as DeviceMetadata;
|
|
|
metadataRef.current = metadata;
|
|
|
- f.setFieldState(field.query('message.properties'), (state) => {
|
|
|
+ f.setFieldState(field.query('message.properties.key'), (state) => {
|
|
|
state.dataSource = metadata?.properties.map((item) => ({
|
|
|
label: item.name,
|
|
|
value: item.id,
|
|
|
@@ -48,6 +54,7 @@ const Create = () => {
|
|
|
state.value = func?.map((item) => ({
|
|
|
key: `${item.name}(${item.id})`,
|
|
|
value: null,
|
|
|
+ name: item.id,
|
|
|
}));
|
|
|
});
|
|
|
});
|
|
|
@@ -65,17 +72,16 @@ const Create = () => {
|
|
|
},
|
|
|
});
|
|
|
|
|
|
- const useAsyncDataSource =
|
|
|
- (services: (arg0: Field<any, any, any, any>) => Promise<any>) => (field: Field) => {
|
|
|
- field.loading = true;
|
|
|
- services(field).then(
|
|
|
- action.bound!((data: any) => {
|
|
|
- products.current = data.result;
|
|
|
- field.dataSource = data.result.map((item: any) => ({ label: item.name, value: item.id }));
|
|
|
- field.loading = false;
|
|
|
- }),
|
|
|
- );
|
|
|
- };
|
|
|
+ const useAsyncDataSource = (services: (arg0: Field) => Promise<any>) => (field: Field) => {
|
|
|
+ field.loading = true;
|
|
|
+ services(field).then(
|
|
|
+ action.bound!((data: any) => {
|
|
|
+ products.current = data.result;
|
|
|
+ field.dataSource = data.result.map((item: any) => ({ label: item.name, value: item.id }));
|
|
|
+ field.loading = false;
|
|
|
+ }),
|
|
|
+ );
|
|
|
+ };
|
|
|
|
|
|
const loadData = async () => service.queryProduct();
|
|
|
|
|
|
@@ -110,36 +116,41 @@ const Create = () => {
|
|
|
],
|
|
|
},
|
|
|
properties: {
|
|
|
- title: '属性',
|
|
|
- 'x-decorator': 'FormItem',
|
|
|
- 'x-component': 'Select',
|
|
|
- enum: [],
|
|
|
- 'x-reactions': {
|
|
|
- dependencies: ['.messageType'],
|
|
|
- fulfill: {
|
|
|
- state: {
|
|
|
- visible: "{{['READ_PROPERTY','WRITE_PROPERTY'].includes($deps[0])}}",
|
|
|
- componentProps: {
|
|
|
- mode: "{{$deps[0]==='READ_PROPERTY'&&'multiple'}}",
|
|
|
+ type: 'object',
|
|
|
+ properties: {
|
|
|
+ key: {
|
|
|
+ title: '属性',
|
|
|
+ 'x-decorator': 'FormItem',
|
|
|
+ 'x-component': 'Select',
|
|
|
+ enum: [],
|
|
|
+ 'x-reactions': {
|
|
|
+ dependencies: ['..messageType'],
|
|
|
+ fulfill: {
|
|
|
+ state: {
|
|
|
+ visible: "{{['READ_PROPERTY','WRITE_PROPERTY'].includes($deps[0])}}",
|
|
|
+ componentProps: {
|
|
|
+ mode: "{{$deps[0]==='READ_PROPERTY'&&'multiple'}}",
|
|
|
+ },
|
|
|
+ },
|
|
|
},
|
|
|
},
|
|
|
+ 'x-visible': false,
|
|
|
},
|
|
|
- },
|
|
|
- 'x-visible': false,
|
|
|
- },
|
|
|
- value: {
|
|
|
- title: '设置值',
|
|
|
- 'x-component': 'Input',
|
|
|
- 'x-decorator': 'FormItem',
|
|
|
- 'x-reactions': {
|
|
|
- dependencies: ['.messageType'],
|
|
|
- fulfill: {
|
|
|
- state: {
|
|
|
- visible: "{{['WRITE_PROPERTY'].includes($deps[0])}}",
|
|
|
+ value: {
|
|
|
+ title: '设置值',
|
|
|
+ 'x-component': 'Input',
|
|
|
+ 'x-decorator': 'FormItem',
|
|
|
+ 'x-reactions': {
|
|
|
+ dependencies: ['..messageType'],
|
|
|
+ fulfill: {
|
|
|
+ state: {
|
|
|
+ visible: "{{['WRITE_PROPERTY'].includes($deps[0])}}",
|
|
|
+ },
|
|
|
+ },
|
|
|
},
|
|
|
+ 'x-visible': false,
|
|
|
},
|
|
|
},
|
|
|
- 'x-visible': false,
|
|
|
},
|
|
|
functionId: {
|
|
|
title: '功能',
|
|
|
@@ -206,8 +217,38 @@ const Create = () => {
|
|
|
},
|
|
|
};
|
|
|
|
|
|
+ const sendCommand = async () => {
|
|
|
+ const values: Record<string, any> = await form.submit();
|
|
|
+ const type = values.message?.messageType;
|
|
|
+ switch (type) {
|
|
|
+ case 'READ_PROPERTY':
|
|
|
+ const property = values.message.properties.key;
|
|
|
+ values.message.properties = property;
|
|
|
+ break;
|
|
|
+ case 'WRITE_PROPERTY':
|
|
|
+ const key = values.message.properties.key;
|
|
|
+ const value = values.message.properties.value;
|
|
|
+ values.message.properties = { [key]: value };
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ const resp = await service.task(values);
|
|
|
+ if (resp.status === 200) {
|
|
|
+ message.success('操作成功');
|
|
|
+ } else {
|
|
|
+ message.error('操作失败');
|
|
|
+ }
|
|
|
+ close();
|
|
|
+ };
|
|
|
return (
|
|
|
- <Modal width="50vw" visible={true} title="下发指令">
|
|
|
+ <Modal
|
|
|
+ onOk={sendCommand}
|
|
|
+ onCancel={() => close()}
|
|
|
+ width="50vw"
|
|
|
+ visible={visible}
|
|
|
+ title="下发指令"
|
|
|
+ >
|
|
|
<Form form={form} labelCol={5} wrapperCol={16}>
|
|
|
<SchemaField schema={schema} scope={{ useAsyncDataSource, loadData }} />
|
|
|
</Form>
|