index.vue
8.42 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<script lang="ts" setup>
import { ref } from 'vue';
import { useGenDynamicForm } from './useGenDynamicForm';
import { ModalParamsType } from '/#/utils';
import { DeviceModelOfMatterAttrs } from '/@/api/device/model/deviceModel';
import { StructJSON } from '/@/api/device/model/modelOfMatterModel';
import { BasicForm, useForm } from '/@/components/Form';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { sendCommandOneway } from '/@/api/dataBoard';
import { useMessage } from '/@/hooks/web/useMessage';
import { unref } from 'vue';
import { genModbusCommand } from '/@/api/task';
import { TaskTypeEnum } from '/@/views/task/center/config';
import { SingleToHex, formSchemasConfig } from './config';
import { DataTypeEnum } from '/@/enums/objectModelEnum';
import { TransportTypeEnum } from '/@/enums/deviceEnum';
defineEmits(['register']);
const props = defineProps<{ deviceId: string; deviceName: string }>();
const [registerForm, { setProps, getFieldsValue, resetFields, validate }] = useForm({
schemas: [],
showActionButtonGroup: false,
layout: 'vertical',
});
const { genForm, transformValue } = useGenDynamicForm();
const keys = ref<string[]>([]);
const modBUSForm = ref<any>({});
const isShowModBUS = ref<Boolean>(false); //用于判断标识符类型是否时自定义还是modBUS
const isShowActionType = ref<Boolean>(true); //判断设备属性标识符为modBus时没有填写扩展描述
const formField = ref(''); //存一个表单取值的field
const zoomFactorValue = ref<number>(1); //缩放因子
const isShowMultiply = ref<Boolean>(false); // 只有tcp --> int和double类型才相乘缩放因子
const deviceTransportType = ref<string>();
const objectDataType = ref<DataTypeEnum>();
const [register] = useModalInner(async (params: ModalParamsType<DeviceModelOfMatterAttrs>) => {
const { record } = params;
const { name, detail, identifier, deviceDetail, extensionDesc } = record;
const { dataType } = detail;
const { type } = dataType || {};
const { codeType, deviceProfile, code } = deviceDetail || {};
const { transportType } = deviceProfile || {};
const { registerAddress, actionType, zoomFactor } = extensionDesc || {}; //获取扩展描述内容
formField.value = identifier;
zoomFactorValue.value = zoomFactor ? Number(zoomFactor) : 1;
isShowMultiply.value = type == 'INT' || type == 'DOUBLE' ? true : false;
deviceTransportType.value = transportType;
objectDataType.value = type;
let schemas = [{ dataType: dataType, identifier, functionName: name } as StructJSON];
if (type === DataTypeEnum.STRUCT) {
schemas = dataType?.specs as StructJSON[];
}
keys.value = schemas.map((item) => {
return item.identifier!;
});
isShowActionType.value = actionType ? true : false; //判断modBUS类型时 物模型是否填写扩展描述
//是modBUS类型的就用另外的表单
//判断是否是TCP ==> modBus的下发命令
if (codeType === TaskTypeEnum.MODBUS_RTU && transportType == TransportTypeEnum.TCP) {
isShowModBUS.value = true;
modBUSForm.value = {
crc: 'CRC_16_LOWER',
deviceCode: code,
method: actionType == '16' ? '10' : actionType,
registerAddress,
registerNumber: 1,
registerValues: [],
};
setProps({ schemas: formSchemasConfig(schemas[0], actionType) });
} else {
isShowModBUS.value = false;
if (transportType === TransportTypeEnum.TCP) {
setProps({
schemas: [
{
field: 'command',
label: name,
component: 'Input',
required: true,
rules: [
{
pattern: /^[\s0-9a-fA-F]+$/,
required: true,
message: '请输入ASCII或HEX服务命令(0~9/A~F)',
},
],
componentProps: {
placeholder: `请输入${name}`,
},
},
],
});
} else {
const formSchemas = genForm(schemas);
setProps({ schemas: formSchemas });
}
}
resetFields();
});
const getArray = (values) => {
const str = values.replace(/\s+/g, '');
const array: any = [];
for (let i = 0; i < str.length; i += 4) {
const chunk = parseInt(str.substring(i, i + 4), 16);
array.push(chunk);
}
return array;
};
// 获取小数
const getFloatPart = (number: string | number) => {
const isLessZero = Number(number) < 0;
number = number.toString();
const floatPartStartIndex = number.indexOf('.');
const value = ~floatPartStartIndex
? `${isLessZero ? '-' : ''}0.${number.substring(floatPartStartIndex + 1)}`
: '0';
return Number(value);
};
const { createMessage } = useMessage();
const loading = ref(false);
const handleSend = async () => {
try {
loading.value = true;
if (!props.deviceId) return;
const sendValue = ref({});
//判断tcp类型 标识符是自定义还是ModBus
if (unref(objectDataType) === DataTypeEnum.STRING) {
const flag = await validate();
if (!flag) return;
const value = getFieldsValue()[unref(formField)];
sendValue.value = value;
} else if (unref(isShowModBUS)) {
if (!unref(isShowActionType)) {
createMessage.warning('当前物模型扩展描述没有填写');
return;
}
const flag = await validate();
if (!flag) return;
const oldValue = getFieldsValue()[unref(formField)];
modBUSForm.value.registerNumber = 1;
modBUSForm.value.registerValues = [oldValue];
if (unref(isShowMultiply) && unref(modBUSForm).method == '06') {
const newValue =
Math.trunc(oldValue) * unref(zoomFactorValue) +
getFloatPart(oldValue) * unref(zoomFactorValue);
if (newValue % 1 != 0) {
createMessage.warning(`属性下发类型必须是整数,缩放因子为${unref(zoomFactorValue)}`);
return;
}
if (oldValue * unref(zoomFactorValue) > 65535) {
createMessage.warning(`属性下发值不能超过65535,缩放因子是${unref(zoomFactorValue)}`);
return;
}
//bool类型的就不用去乘缩放因子了
modBUSForm.value.registerValues = [newValue];
}
if (unref(modBUSForm).method == '16' || unref(modBUSForm).method == '10') {
const regex = /^-?\d+(\.\d{0,2})?$/;
const values =
Math.trunc(oldValue) * unref(zoomFactorValue) +
getFloatPart(oldValue) * unref(zoomFactorValue);
if (!regex.test(values as any)) {
createMessage.warning(`属性下发值精确到两位小数,缩放因子是${unref(zoomFactorValue)}`);
return;
}
const newValue =
values == 0 ? [0, 0] : getArray(SingleToHex(unref(isShowMultiply) ? values : oldValue));
modBUSForm.value.registerValues = newValue;
modBUSForm.value.registerNumber = 2;
modBUSForm.value.method = '10';
}
sendValue.value = await genModbusCommand(unref(modBUSForm));
} else {
await validate();
const _value = transformValue(getFieldsValue());
sendValue.value = unref(keys).reduce((prev, next) => {
return { ...prev, [next]: _value[next] };
}, {});
// tcp 设备下发字符串
if (unref(deviceTransportType) === TransportTypeEnum.TCP) {
sendValue.value = Object.values(_value).join('').replaceAll(/\s/g, '');
}
}
await sendCommandOneway({
deviceId: props.deviceId,
value: {
persistent: true,
method: 'methodThingskit',
params: unref(sendValue),
},
});
createMessage.success('属性下发成功');
} catch (error) {
throw error;
} finally {
loading.value = false;
}
};
</script>
<template>
<BasicModal
title="属性下发"
@register="register"
:min-height="60"
ok-text="属性下发"
wrap-class-name="model-of-matter-send-command-modal"
@ok="handleSend"
:ok-button-props="{ loading }"
>
<BasicForm @register="registerForm" class="dynamic-form" />
</BasicModal>
</template>
<style lang="less">
.model-of-matter-send-command-modal {
.dynamic-form {
.ant-input-number {
@apply !w-full;
}
}
}
</style>