index.vue
4.51 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
<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 }] = useForm({
schemas: [],
showActionButtonGroup: false,
layout: 'vertical',
});
const { genForm } = useGenDynamicForm();
const keys = ref<string[]>([]);
const modBUSForm = ref<any>({});
const isShowModBUS = ref<Boolean>(false);
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[];
}
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);
keys.value = schemas.map((item) => {
return item.identifier!;
});
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({});
if (unref(isShowModBUS)) {
//判断tcp类型 标识符是自定义还是ModBus
const oldValue = getFieldsValue()[unref(formField)];
const newValue = getArray(SingleToHex(oldValue));
modBUSForm.value.registerValues =
unref(modBUSForm).method != '16' ? [newValue[0]] : newValue;
modBUSForm.value.registerNumber = unref(modBUSForm).method != '16' ? 1 : 2;
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>