index.vue
5.19 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
<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 { DataTypeEnum } from '/@/views/device/profiles/step/cpns/physical/cpns/config';
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';
defineEmits(['register']);
const props = defineProps<{ deviceId: string; deviceName: string }>();
const [registerForm, { setProps, getFieldsValue, resetFields, validate }] = useForm({
schemas: [],
showActionButtonGroup: false,
layout: 'vertical',
});
const { genForm } = useGenDynamicForm();
const keys = ref<string[]>([]);
const modBUSForm = ref<any>({});
const isShowModBUS = ref<Boolean>(false); //用于判断标识符类型是否时自定义还是modBUS
const isShowActionType = ref<Boolean>(true); //判断设备属性标识符为modBus时没有填写扩展描述
const formField = ref('');
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 } = deviceDetail || {};
const { registerAddress, actionType } = extensionDesc || {};
formField.value = identifier;
let schemas = [{ dataType: dataType, identifier, functionName: name } as StructJSON];
if (type === DataTypeEnum.IS_STRUCT) {
schemas = dataType?.specs as StructJSON[];
}
keys.value = schemas.map((item) => {
return item.identifier!;
});
isShowActionType.value = actionType ? true : false; //判断modBUS类型时 物模型是否填写扩展描述
//是modBUS类型的就用另外的表单
//判断是否是modBus的下发命令
if (codeType == TaskTypeEnum.MODBUS) {
isShowModBUS.value = true;
modBUSForm.value = {
crc: 'CRC_16_LOWER',
deviceCode: '01',
method: actionType,
registerAddress,
registerNumber: 1,
registerValues: [],
};
setProps({ schemas: formSchemasConfig(schemas[0], actionType) });
} else {
isShowModBUS.value = false;
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 { createMessage } = useMessage();
const loading = ref(false);
const handleSend = async () => {
try {
loading.value = true;
if (!props.deviceId) return;
const sendValue = ref({});
//判断tcp类型 标识符是自定义还是ModBus
if (unref(isShowModBUS)) {
if (!unref(isShowActionType)) {
createMessage.warning('当前物模型扩展描述没有填写');
return;
}
const flag = await validate();
if (!flag) return;
const oldValue = getFieldsValue()[unref(formField)];
modBUSForm.value.registerValues = [oldValue];
modBUSForm.value.registerNumber = 1;
if (unref(modBUSForm).method == '16') {
const newValue = getArray(SingleToHex(oldValue));
modBUSForm.value.registerValues = newValue;
modBUSForm.value.registerNumber = 2;
modBUSForm.value.method = '10';
}
sendValue.value = await genModbusCommand(unref(modBUSForm));
} else {
const _value = getFieldsValue();
sendValue.value = unref(keys).reduce((prev, next) => {
return { ...prev, [next]: _value[next] };
}, {});
}
await sendCommandOneway({
deviceId: props.deviceId,
value: {
persistent: true,
method: 'methodThingskit',
params: unref(isShowModBUS)
? unref(sendValue)
: {
...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>