index.vue
4.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
<template>
<BasicModal
title="命令下发"
:width="650"
@register="registerModal"
@ok="handleOk"
@cancel="handleCancel"
>
<BasicForm @register="registerForm">
<template #serviceCommand="{ field, model }">
<ThingsModelForm
:disabled="deviceDetail?.transportType === 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?.transportType"
/>
</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 { DeviceRecord } from '/@/api/device/model/deviceModel';
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';
defineEmits(['register']);
const thingsModelFormRef = ref<InstanceType<typeof ThingsModelForm>>();
const deviceDetail = ref<DeviceRecord>();
const [registerModal, { setModalProps }] = useModalInner(
(params: ModalParamsType<DeviceRecord>) => {
const { record } = params;
deviceDetail.value = record;
setProps({
schemas: CommandSchemas(record.transportType as TransportTypeEnum, record.deviceProfileId),
});
}
);
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(deviceDetail)!.tbDeviceId);
const [firstItem] = result;
return !!firstItem.value;
};
const handleCommandParams = (
values: CommandDeliveryFormFieldType,
serviceCommand: Recordable
) => {
const { commandType } = values;
const isTcpDevice = unref(deviceDetail)?.transportType === TransportTypeEnum.TCP;
if (commandType === CommandTypeEnum.CUSTOM) {
if (isTcpDevice) {
return values.tcpCommandValue;
}
return parseStringToJSON(values.commandValue!).json;
} else {
if (isTcpDevice) return Reflect.get(serviceCommand, CommandFieldsEnum.SERVICE_COMMAND);
return 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('当前设备不在线');
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(deviceDetail)!.tbDeviceId, rpcCommands);
createMessage.success('命令下发成功');
} finally {
setModalProps({ loading: false, confirmLoading: false });
}
};
</script>
<style scoped lang="less"></style>