index.vue
4.7 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
<template>
<BasicModal
:title="t('business.commandDeliveryText')"
:width="650"
@register="registerModal"
@ok="handleOk"
@cancel="handleCancel"
>
<BasicForm @register="registerForm">
<template #serviceCommand="{ field, model }">
<ThingsModelForm
:disabled="
deviceDetail?.deviceData?.transportConfiguration?.type === TransportTypeEnum.TCP
"
ref="thingsModelFormRef"
v-model:value="model[field]"
:key="model[CommandFieldsEnum.SERVICE_OBJECT_MODEL]?.identifier"
:inputData="model[CommandFieldsEnum.SERVICE_OBJECT_MODEL]?.functionJson?.inputData"
:transportType="deviceDetail?.deviceData?.transportConfiguration?.type"
/>
</template>
</BasicForm>
</BasicModal>
</template>
<script lang="ts" setup>
import { nextTick, ref, unref } from 'vue';
import { BasicForm, ThingsModelForm, useForm } from '/@/components/Form';
import { CommandDeliveryFormFieldType, CommandFieldsEnum, CommandSchemas } from './config';
import { useMessage } from '/@/hooks/web/useMessage';
import { CommandDeliveryWayEnum } from '/@/enums/deviceEnum';
import { CommandTypeEnum, RPCCommandMethodEnum, TransportTypeEnum } from '/@/enums/deviceEnum';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { getDeviceActiveTime } from '/@/api/alarm/position';
import { RpcCommandType } from '/@/api/device/model/deviceConfigModel';
import { parseStringToJSON } from '/@/components/CodeEditor';
import { commandIssuanceApi } from '/@/api/device/deviceManager';
import { EdgeDeviceItemType } from '/@/api/edgeManage/model/edgeInstance';
import { useI18n } from '/@/hooks/web/useI18n';
defineEmits(['register']);
const { t } = useI18n();
const thingsModelFormRef = ref<InstanceType<typeof ThingsModelForm>>();
const props = defineProps<{
deviceDetail: EdgeDeviceItemType;
}>();
const [registerModal, { setModalProps }] = useModalInner(
(params: ModalParamsType<EdgeDeviceItemType>) => {
const { record } = params;
setProps({
schemas: CommandSchemas(record),
});
}
);
const { createMessage } = useMessage();
const [registerForm, { setProps, getFieldsValue, validate, resetFields, clearValidate }] =
useForm({
labelWidth: 120,
baseColProps: { span: 20 },
labelAlign: 'right',
showSubmitButton: false,
showResetButton: false,
});
const handleCancel = async () => {
await resetFields();
await nextTick();
await clearValidate();
};
const handleValidate = async () => {
await validate();
await unref(thingsModelFormRef)?.validate?.();
};
const handleValidateDeviceActive = async (): Promise<boolean> => {
const result = await getDeviceActiveTime(unref(props.deviceDetail)!.id?.id);
const [firstItem] = result;
return !!firstItem.value;
};
const handleCommandParams = (
values: CommandDeliveryFormFieldType,
serviceCommand: Recordable
) => {
const { commandType, service } = values;
const isTcpDevice =
unref(props.deviceDetail)?.deviceData?.transportConfiguration?.type === TransportTypeEnum.TCP;
if (commandType === CommandTypeEnum.CUSTOM) {
if (isTcpDevice) {
const value = values.tcpCommandValue;
return value?.replaceAll(/\s/g, '');
}
return parseStringToJSON(values.commandValue!).json;
} else {
if (isTcpDevice) return Reflect.get(serviceCommand, CommandFieldsEnum.SERVICE_COMMAND);
return {
[service!]: serviceCommand,
};
}
};
const handleOk = async () => {
await handleValidate();
try {
setModalProps({ loading: true, confirmLoading: true });
const values = getFieldsValue() as CommandDeliveryFormFieldType;
const { callType, commandType } = values;
const serviceCommand = unref(thingsModelFormRef)?.getFieldsValue() || {};
if (callType === CommandDeliveryWayEnum.TWO_WAY && !(await handleValidateDeviceActive())) {
createMessage.warn(t('visual.board.notDeviceText'));
return;
}
const rpcCommands: RpcCommandType = {
additionalInfo: {
cmdType:
commandType === CommandTypeEnum.CUSTOM
? CommandTypeEnum.CUSTOM
: CommandTypeEnum.SERVICE,
},
method: RPCCommandMethodEnum.THINGSKIT,
persistent: true,
params: handleCommandParams(values, serviceCommand),
};
await commandIssuanceApi(callType, unref(props.deviceDetail)!.id?.id, rpcCommands);
createMessage.success(t('visual.board.commandOkText'));
} finally {
setModalProps({ loading: false, confirmLoading: false });
}
};
</script>
<style scoped lang="less"></style>