config.ts
6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { ModelOfMatterParams } from '/@/api/device/model/modelOfMatterModel';
import { getModelServices } from '/@/api/device/modelOfMatter';
import { FormProps, FormSchema, useComponentRegister } from '/@/components/Form';
import { validateTCPCustomCommand } from '/@/components/Form/src/components/ThingsModelForm';
import { JSONEditor, JSONEditorValidator } from '/@/components/CodeEditor';
import {
TransportTypeEnum,
CommandTypeEnum,
ServiceCallTypeEnum,
CommandDeliveryWayEnum,
TCPProtocolTypeEnum,
} from '/@/enums/deviceEnum';
import { DeviceTypeEnum } from '/@/api/device/model/deviceModel';
import { EdgeDeviceItemType } from '/@/api/edgeManage/model/edgeInstance';
import { useI18n } from '/@/hooks/web/useI18n';
export interface CommandDeliveryFormFieldType {
[CommandFieldsEnum.COMMAND_TYPE]: CommandTypeEnum;
[CommandFieldsEnum.TCP_COMMAND_VALUE]?: string;
[CommandFieldsEnum.COMAND_VALUE]?: string;
[CommandFieldsEnum.SERVICE]?: string;
[CommandFieldsEnum.MODEL_INPUT]?: ModelOfMatterParams;
[CommandFieldsEnum.CALL_TYPE]: CommandDeliveryWayEnum;
}
export enum CommandFieldsEnum {
COMMAND_TYPE = 'commandType',
TCP_COMMAND_VALUE = 'tcpCommandValue',
COMAND_VALUE = 'commandValue',
SERVICE = 'service',
MODEL_INPUT = 'modelInput',
CALL_TYPE = 'callType',
SERVICE_COMMAND = 'serviceCommand',
SERVICE_OBJECT_MODEL = 'serviceObjectModel',
}
useComponentRegister('JSONEditor', JSONEditor);
export const CommandSchemas = (deviceRecord: EdgeDeviceItemType): FormSchema[] => {
const { deviceData, deviceProfileId, deviceType } = deviceRecord;
const { t } = useI18n();
const isTCPTransport = deviceData.transportConfiguration.type === TransportTypeEnum.TCP;
const isTCPModbus =
isTCPTransport &&
deviceRecord.deviceProfile?.profileData?.transportConfiguration?.protocol ===
TCPProtocolTypeEnum.MODBUS_RTU;
return [
{
field: CommandFieldsEnum.COMMAND_TYPE,
component: 'RadioGroup',
label: t('rule.linkedge.index.commandIssuanceType'),
defaultValue: CommandTypeEnum.CUSTOM,
required: true,
componentProps: ({ formActionType }) => {
const { setFieldsValue } = formActionType;
const getOptions = () => {
const options = [{ label: t('enum.commandType.custom'), value: CommandTypeEnum.CUSTOM }];
if (isTCPModbus || (isTCPTransport && deviceType === DeviceTypeEnum.SENSOR))
return options;
options.push({ label: t('enum.commandType.service'), value: CommandTypeEnum.SERVICE });
return options;
};
return {
options: getOptions(),
onChange() {
setFieldsValue({
[CommandFieldsEnum.SERVICE]: null,
[CommandFieldsEnum.MODEL_INPUT]: null,
[CommandFieldsEnum.COMAND_VALUE]: null,
[CommandFieldsEnum.TCP_COMMAND_VALUE]: null,
});
},
};
},
},
{
field: CommandFieldsEnum.CALL_TYPE,
component: 'RadioGroup',
label: t('deviceManagement.device.callTypeText'),
required: true,
defaultValue: CommandDeliveryWayEnum.ONE_WAY,
ifShow: ({ model }) => model[CommandFieldsEnum.COMMAND_TYPE] === CommandTypeEnum.CUSTOM,
componentProps: {
options: Object.values(CommandDeliveryWayEnum).map((value) => ({
label: t(`enum.commandWay.${value}`),
value,
})),
},
},
{
field: CommandFieldsEnum.TCP_COMMAND_VALUE,
label: t('deviceManagement.device.commandText'),
required: true,
ifShow: ({ model }) =>
deviceData.transportConfiguration.type === TransportTypeEnum.TCP &&
model[CommandFieldsEnum.COMMAND_TYPE] === CommandTypeEnum.CUSTOM,
component: 'Input',
rules: [{ validator: validateTCPCustomCommand }],
componentProps: {
placeholder: t('common.inputText') + t('deviceManagement.device.commandText'),
},
},
{
field: CommandFieldsEnum.COMAND_VALUE,
label: t('deviceManagement.device.commandText'),
component: 'JSONEditor',
colProps: { span: 20 },
changeEvent: 'update:value',
valueField: 'value',
required: true,
rules: JSONEditorValidator(),
ifShow: ({ model }) =>
deviceData.transportConfiguration.type !== TransportTypeEnum.TCP &&
model[CommandFieldsEnum.COMMAND_TYPE] === CommandTypeEnum.CUSTOM,
componentProps: {
height: 250,
},
},
{
field: CommandFieldsEnum.SERVICE,
label: t('business.serviceText'),
component: 'ApiSelect',
required: true,
ifShow: ({ model }) => model[CommandFieldsEnum.COMMAND_TYPE] !== CommandTypeEnum.CUSTOM,
rules: [{ required: true }],
componentProps: ({ formActionType }) => {
const { setFieldsValue } = formActionType;
return {
api: getModelServices,
params: {
deviceProfileId: deviceProfileId.id,
},
valueField: 'identifier',
labelField: 'functionName',
getPopupContainer: () => document.body,
placeholder: t('common.chooseText') + t('business.serviceText'),
onChange(
value: string,
options: ModelOfMatterParams & Record<'label' | 'value', string>
) {
if (!value) return;
setFieldsValue({
[CommandFieldsEnum.CALL_TYPE]:
options.callType === ServiceCallTypeEnum.ASYNC
? CommandDeliveryWayEnum.ONE_WAY
: CommandDeliveryWayEnum.TWO_WAY,
[CommandFieldsEnum.MODEL_INPUT]: null,
[CommandFieldsEnum.SERVICE_OBJECT_MODEL]: Object.assign(options, {
functionName: options.label,
identifier: options.value,
}),
});
},
};
},
},
{
field: CommandFieldsEnum.SERVICE_OBJECT_MODEL,
label: 'Server thingsModel',
component: 'Input',
ifShow: false,
},
{
field: CommandFieldsEnum.MODEL_INPUT,
component: 'Input',
label: t('deviceManagement.product.inputDatText'),
changeEvent: 'update:value',
valueField: 'value',
ifShow: ({ model }) =>
model[CommandFieldsEnum.SERVICE] &&
model[CommandFieldsEnum.SERVICE_OBJECT_MODEL] &&
model[CommandFieldsEnum.COMMAND_TYPE] !== CommandTypeEnum.CUSTOM,
componentProps: {
formProps: {
wrapperCol: { span: 24 },
} as FormProps,
},
slot: 'serviceCommand',
},
];
};