PhysicalModelTsl.vue
2.85 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
<template>
<BasicModal
title="物模型TSL"
:maskClosable="false"
destroyOnClose
v-bind="$attrs"
width="55rem"
:okButtonProps="{ loading }"
@register="register"
@ok="handleSubmit"
okText="导出物模型"
@cancel="handleCancel"
>
<TslContent :record="record" ref="TslConRef" />
<template #centerFooter>
<Button type="primary" :loading="loading" @click="handleExportAll">导出全部</Button>
</template>
</BasicModal>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import TslContent from './cpns/TslContent.vue';
import { DeviceRecord } from '/@/api/device/model/deviceModel';
import { Button } from 'ant-design-vue';
import { getModelTsl } from '/@/api/device/modelOfMatter';
import { FunctionType } from './cpns/config';
defineEmits(['register']);
const props = defineProps<{
record: DeviceRecord;
}>();
const TslConRef = ref<InstanceType<typeof TslContent>>();
const isUpdate = ref(false);
const loading = ref(false);
const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
setModalProps({ confirmLoading: true });
isUpdate.value = data.isUpdate;
setModalProps({ confirmLoading: false });
// const jsCode = TslConRef?.value.aceEditor.getValue();
// TslConRef?.value.aceEditor.setValue(jsCode);
});
const handleCancel = () => {
TslConRef.value?.resetFormData();
closeModal();
};
const exportJSONFile = (value: Recordable) => {
const blob = new Blob([JSON.stringify(value, null, 2)], { type: 'text/json' });
const objectURL = URL.createObjectURL(blob);
const element = document.createElement('a');
element.href = objectURL;
element.download = `${props.record.name}-model.json`;
element.style.display = 'none';
document.body.appendChild(element);
element.click();
element.remove();
URL.revokeObjectURL(objectURL);
};
const handleSubmit = () => {
const value = TslConRef.value?.getFormData();
if (!value) return;
exportJSONFile(value);
};
const getAllModel = () => {
const { id: deviceProfileId } = props.record;
return Promise.all([
getModelTsl({ deviceProfileId, functionType: FunctionType.EVENTS }),
getModelTsl({ deviceProfileId, functionType: FunctionType.PROPERTIES }),
getModelTsl({ deviceProfileId, functionType: FunctionType.SERVICE }),
]);
};
const handleExportAll = async () => {
loading.value = true;
try {
const [events, properties, services] = await getAllModel();
const value = { properties, services, events };
exportJSONFile(value);
} catch (error) {
throw error;
} finally {
loading.value = false;
}
};
</script>
<style lang="less" scope>
#jsoneditor {
width: 100%;
height: 450px;
}
</style>