Commit b746dc51a14231d1c8dbd641504ba798b5131d1e

Authored by xp.Huang
2 parents aef89d0d 3f48b7d4

Merge branch 'ft_local_dev' into 'main'

pref:优化物模型相关代码

See merge request huang/yun-teng-iot-front!389
... ... @@ -17,7 +17,7 @@
17 17 <div>
18 18 <Typography>
19 19 <TypographyParagraph>
20   - <blockquote style="background: #f2f2f2">{{ blockContent }}</blockquote>
  20 + <blockquote style="background: #f2f2f2">{{ useBlockPhysicalContent }}</blockquote>
21 21 </TypographyParagraph>
22 22 </Typography>
23 23 </div>
... ... @@ -50,23 +50,38 @@
50 50 import Service from './cpns/Service.vue';
51 51 import Events from './cpns/Events.vue';
52 52 import { mockData } from '../physical/cpns/components/mock';
  53 + import useCommon from './hook/useCommon';
  54 + import { IAllData } from './types';
53 55
54 56 defineEmits(['register']);
  57 +
55 58 const attrDisable = ref(false);
  59 +
56 60 const serveiceDisable = ref(false);
  61 +
57 62 const eventDisable = ref(false);
58   - const blockContent = `属性一般是设备的运行状态,如当前温度等;服务是设备可被调用的方法,支持定义参数,如执行某项任务;事件则是设备上报的
59   - 通知,如告警,需要被及时处理。`;
  63 +
  64 + const { useBlockPhysicalContent } = useCommon();
  65 +
60 66 const activeKey = ref('1');
  67 +
61 68 const size = ref('small');
  69 +
62 70 const AttrRef = ref<InstanceType<typeof Attribute>>();
  71 +
63 72 const ServiceRef = ref<InstanceType<typeof Service>>();
  73 +
64 74 const EventsRef = ref<InstanceType<typeof Events>>();
  75 +
65 76 const isUpdate = ref(false);
  77 +
66 78 const isViewDetail = ref('');
  79 +
67 80 const isText = ref('');
  81 +
68 82 const dType = ref('');
69   - const allData: any = reactive({
  83 +
  84 + const allData = reactive<IAllData>({
70 85 properties: [],
71 86 events: [],
72 87 services: [],
... ... @@ -75,26 +90,33 @@
75 90 });
76 91
77 92 const setAttrFormData = (data: {}) => AttrRef.value?.setFormData(data);
  93 +
78 94 const setServiceFormData = (data: {}) => ServiceRef.value?.setFormData(data);
  95 +
79 96 const setEventsFormData = (data: {}) => EventsRef.value?.setFormData(data);
  97 +
80 98 const enums = {
81 99 attr: setAttrFormData,
82 100 service: setServiceFormData,
83 101 events: setEventsFormData,
84 102 };
  103 +
85 104 function action(val, data) {
86 105 let F = enums[val];
87 106 F(data);
88 107 }
  108 +
89 109 function dynamicDisable(a, s, e) {
90 110 attrDisable.value = a;
91 111 serveiceDisable.value = s;
92 112 eventDisable.value = e;
93 113 }
  114 +
94 115 function dynamicActive(n, callback) {
95 116 activeKey.value = n;
96 117 callback;
97 118 }
  119 +
98 120 const dynamicData = (t, a, s, e) => {
99 121 switch (t) {
100 122 case 'attr':
... ... @@ -111,6 +133,7 @@
111 133 break;
112 134 }
113 135 };
  136 +
114 137 const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
115 138 setModalProps({ loading: true });
116 139 handleCancel(false);
... ... @@ -134,6 +157,7 @@
134 157 }
135 158 setModalProps({ loading: false });
136 159 });
  160 +
137 161 const handleCancel = (flag) => {
138 162 AttrRef.value?.resetFormData();
139 163 ServiceRef.value?.resetFormData();
... ... @@ -149,6 +173,7 @@
149 173 closeModal();
150 174 }
151 175 };
  176 +
152 177 const handleSubmit = async () => {
153 178 if (activeKey.value == '1') {
154 179 const valueAttr = await AttrRef.value?.getFormData();
... ...
... ... @@ -17,18 +17,21 @@
17 17 </div>
18 18 </template>
19 19 <script lang="ts" setup>
20   - import { ref, unref } from 'vue';
  20 + import { unref } from 'vue';
21 21 import { BasicForm, useForm } from '/@/components/Form';
22 22 import { attrSchemas } from './config';
23 23 import { useModal } from '/@/components/Modal';
24   - import { validateValueStruct } from '../hook/useValidateParital';
25   - import { useChangeTypeGetTypeForm, useGetInOrOutData } from '../hook/useTypeGetForm';
  24 + import useParitalValid from '../hook/useParitalValid';
  25 + import useCommon from '../hook/useCommon';
26 26 import AddParamsModal from './components/AddParamsModal.vue';
27 27 import CommomParam from './components/CommomParam.vue';
  28 + import useDefineVar from '../hook/useDefineVar';
28 29
29   - const inputParamData: any = ref([]);
  30 + const { useChangeTypeGetTypeForm, useGetInOrOutData } = useCommon();
30 31
31   - const CommomParamInParamRef = ref<InstanceType<typeof CommomParam>>();
  32 + const { validateValueStruct } = useParitalValid();
  33 +
  34 + const { inputParamData, CommomParamInParamRef } = useDefineVar();
32 35
33 36 const [registerModal, { openModal }] = useModal();
34 37
... ...
... ... @@ -17,17 +17,18 @@
17 17 </div>
18 18 </template>
19 19 <script lang="ts" setup>
20   - import { ref, unref } from 'vue';
  20 + import { unref } from 'vue';
21 21 import { BasicForm, useForm } from '/@/components/Form';
22 22 import { eventSchemas } from './config';
23 23 import { useModal } from '/@/components/Modal';
24 24 import AddParamsModal from './components/AddParamsModal.vue';
25 25 import CommomParam from './components/CommomParam.vue';
26   - import { useGetInOrOutData } from '../hook/useTypeGetForm';
  26 + import useCommon from '../hook/useCommon';
  27 + import useDefineVar from '../hook/useDefineVar';
27 28
28   - const inputParamData: any = ref([]);
  29 + const { useGetInOrOutData } = useCommon();
29 30
30   - const CommomParamInParamRef = ref<InstanceType<typeof CommomParam>>();
  31 + const { inputParamData, CommomParamInParamRef } = useDefineVar();
31 32
32 33 const [registerModal, { openModal }] = useModal();
33 34
... ...
... ... @@ -28,21 +28,19 @@
28 28 </div>
29 29 </template>
30 30 <script lang="ts" setup>
31   - import { ref, unref } from 'vue';
  31 + import { unref } from 'vue';
32 32 import { BasicForm, useForm } from '/@/components/Form';
33 33 import { serviceSchemas } from './config';
34 34 import { useModal } from '/@/components/Modal';
35 35 import AddParamsModal from './components/AddParamsModal.vue';
36 36 import CommomParam from './components/CommomParam.vue';
37   - import { useGetInOrOutData } from '../hook/useTypeGetForm';
  37 + import useCommon from '../hook/useCommon';
  38 + import useDefineVar from '../hook/useDefineVar';
38 39
39   - const inputParamData: any = ref([]);
  40 + const { inputParamData, outputParamData, CommomParamInParamRef, CommomParamOutParamRef } =
  41 + useDefineVar();
40 42
41   - const outputParamData: any = ref([]);
42   -
43   - const CommomParamInParamRef = ref<InstanceType<typeof CommomParam>>();
44   -
45   - const CommomParamOutParamRef = ref<InstanceType<typeof CommomParam>>();
  43 + const { useGetInOrOutData } = useCommon();
46 44
47 45 const [registerModal, { openModal }] = useModal();
48 46
... ...
... ... @@ -3,7 +3,7 @@
3 3 <div>
4 4 <Typography>
5 5 <TypographyParagraph>
6   - <blockquote style="background: #f2f2f2">{{ blockContent }}</blockquote>
  6 + <blockquote style="background: #f2f2f2">{{ useBlockContent }}</blockquote>
7 7 </TypographyParagraph>
8 8 </Typography>
9 9 </div>
... ... @@ -38,14 +38,18 @@
38 38 import 'jsoneditor/dist/jsoneditor.min.css';
39 39 import { Button, Typography, TypographyParagraph } from 'ant-design-vue';
40 40 import { defaultTslContent } from './config';
  41 + import useCommon from '../hook/useCommon';
41 42
42 43 const { createMessage } = useMessage();
43   - const blockContent = `物模型是对设备在云端的功能描述,包括设备的属性、服务和事件。物联网平台通过定义一种物的描述语言来描述物模型,称之为 TSL(即 Thing
44   -Specification Language),采用 JSON 格式,您可以根据 TSL 组装上报设备的数据。您可以导出完整物模型,用于云端应用开发;您也可以只导出
45   -精简物模型,配合设备端 SDK 实现设备开发。`;
  44 +
  45 + const { useBlockContent } = useCommon();
  46 +
46 47 const jsonValue = ref(defaultTslContent);
  48 +
47 49 const jsonInstance = ref();
  50 +
48 51 const jsoneditorRef = ref();
  52 +
49 53 onMounted(() => {
50 54 nextTick(() => {
51 55 let options = {
... ... @@ -58,7 +62,9 @@ Specification Language),采用 JSON æ ¼å¼ï¼Œæ‚¨å¯ä»¥æ ¹æ® TSL 组装上æŠ
58 62 jsonInstance.value = editor;
59 63 });
60 64 });
  65 +
61 66 const { clipboardRef, copiedRef } = useCopyToClipboard();
  67 +
62 68 const handleCopy = () => {
63 69 try {
64 70 const valueRef = unref(jsonInstance).get();
... ... @@ -81,9 +87,11 @@ Specification Language),采用 JSON æ ¼å¼ï¼Œæ‚¨å¯ä»¥æ ¹æ® TSL 组装上æŠ
81 87 if (!value) return;
82 88 return value;
83 89 };
  90 +
84 91 const resetFormData = () => {
85 92 unref(jsonInstance).set(defaultTslContent);
86 93 };
  94 +
87 95 const handlePremitter = () => {
88 96 const value = unref(jsonInstance).get();
89 97 return unref(jsonInstance).set(value);
... ...
... ... @@ -17,23 +17,20 @@
17 17 </div>
18 18 </template>
19 19 <script lang="ts" setup>
20   - import { ref, unref } from 'vue';
  20 + import { unref } from 'vue';
21 21 import { BasicForm, useForm } from '/@/components/Form';
22 22 import { addParamsSchemas } from '../config';
23 23 import { useModal } from '/@/components/Modal';
24   - import {
25   - useChangeTypeGetTypeForm,
26   - useGetInOrOutData,
27   - useUpdateFormExcludeStruct,
28   - } from '../../hook/useTypeGetForm';
  24 + import useCommon from '../../hook/useCommon';
29 25 import AddParamsModal from './AddParamsModal.vue';
30 26 import CommomParam from './CommomParam.vue';
  27 + import useDefineVar from '../../hook/useDefineVar';
31 28
32 29 defineEmits(['register']);
33 30
34   - const inputParamData: any = ref([]);
  31 + const { inputParamData, CommomParamInParamRef } = useDefineVar();
35 32
36   - const CommomParamInParamRef = ref<InstanceType<typeof CommomParam>>();
  33 + const { useChangeTypeGetTypeForm, useGetInOrOutData, useUpdateFormExcludeStruct } = useCommon();
37 34
38 35 const [registerModal, { openModal }] = useModal();
39 36
... ...
... ... @@ -17,16 +17,24 @@
17 17 import { ref, computed, reactive } from 'vue';
18 18 import { BasicModal, useModalInner } from '/@/components/Modal';
19 19 import AddParamForm from './AddParamForm.vue';
20   - import { validateValueStruct } from '../../hook/useValidateParital';
  20 + import useParitalValid from '../../hook/useParitalValid';
21 21
22 22 const emits = defineEmits(['register', 'data']);
  23 +
  24 + const { validateValueStruct } = useParitalValid();
  25 +
23 26 const setEditData: any = reactive({
24 27 getEditData: {},
25 28 });
  29 +
26 30 const AddParamFormRef = ref<InstanceType<typeof AddParamForm>>();
  31 +
27 32 const isUpdate = ref(false);
  33 +
28 34 const isFlag = ref('');
  35 +
29 36 const excludeStruct = ref(false);
  37 +
30 38 const getTitle = computed(() => (!isUpdate.value ? '编辑参数' : '新增参数'));
31 39
32 40 const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
... ...
... ... @@ -15,6 +15,7 @@
15 15 </template>
16 16 <script lang="ts" setup>
17 17 const emit = defineEmits(['delete', 'edit']);
  18 +
18 19 const props = defineProps({
19 20 index: {
20 21 type: Number,
... ...
1 1 import { buildUUID } from '/@/utils/uuid';
2 2
3   -//模拟阿里云属性服务事件服务端格式
  3 +//模拟的阿里云(属性、服务、事件)服务端格式
4 4 export const mockData = {
5 5 _ppk: {},
6 6 events: [
... ...
src/views/device/profiles/step/cpns/physical/hook/useCommon.ts renamed from src/views/device/profiles/step/cpns/physical/hook/useTypeGetForm.ts
1   -import { validateValueBool, validateValueRangeAndStep } from './useValidateParital';
  1 +import useParitalValid from './useParitalValid';
2 2 import { findDictItemByCode } from '/@/api/system/dict';
3 3 import { buildUUID } from '/@/utils/uuid';
4 4
5   -///根据不同数据类型得到不同表单数据
  5 +///根据不同数据类型得到不同表单数据(模拟的阿里云数据结构)
6 6 type TForm = {
7 7 dataType: string;
8 8 max?: string;
... ... @@ -17,100 +17,120 @@ type TForm = {
17 17 max: string;
18 18 };
19 19 };
20   -export const useChangeTypeGetTypeForm = (type, options: TForm) => {
21   - switch (type) {
22   - //INT和DOUBLE收集表单一样
23   - case 'INT':
24   - validateValueRangeAndStep(
25   - Number(options?.valueRange?.min),
26   - Number(options?.step),
27   - Number(options?.valueRange?.max)
28   - );
29   - return {
30   - dataType: options?.dataType,
31   - max: options?.valueRange?.max,
32   - min: options?.valueRange?.min,
33   - step: options?.step,
34   - unit: options?.unit,
35   - };
36   - case 'DOUBLE':
37   - validateValueRangeAndStep(
38   - Number(options?.valueRange?.min),
39   - Number(options?.step),
40   - Number(options?.valueRange?.max)
41   - );
42   - return {
43   - dataType: options?.dataType,
44   - max: options?.valueRange?.max,
45   - min: options?.valueRange?.min,
46   - step: options?.step,
47   - unit: options?.unit,
48   - };
49   - case 'BOOL':
50   - validateValueBool(Number(options?.boolClose), Number(options?.boolOpen));
51   - return [
52   - {
  20 +
  21 +export default () => {
  22 + const { validateValueRangeAndStep, validateValueBool } = useParitalValid();
  23 +
  24 + const useChangeTypeGetTypeForm = (type, options: TForm) => {
  25 + switch (type) {
  26 + //INT和DOUBLE收集表单一样
  27 + case 'INT':
  28 + validateValueRangeAndStep(
  29 + Number(options?.valueRange?.min),
  30 + Number(options?.step),
  31 + Number(options?.valueRange?.max)
  32 + );
  33 + return {
53 34 dataType: options?.dataType,
54   - name: options?.boolClose,
55   - value: 0,
56   - },
57   - {
  35 + max: options?.valueRange?.max,
  36 + min: options?.valueRange?.min,
  37 + step: options?.step,
  38 + unit: options?.unit,
  39 + };
  40 + case 'DOUBLE':
  41 + validateValueRangeAndStep(
  42 + Number(options?.valueRange?.min),
  43 + Number(options?.step),
  44 + Number(options?.valueRange?.max)
  45 + );
  46 + return {
58 47 dataType: options?.dataType,
59   - name: options?.boolOpen,
60   - value: 1,
61   - },
62   - ];
63   - case 'TEXT':
64   - return {
65   - dataType: options?.dataType,
66   - length: Number(options?.length),
67   - };
68   - }
69   -};
  48 + max: options?.valueRange?.max,
  49 + min: options?.valueRange?.min,
  50 + step: options?.step,
  51 + unit: options?.unit,
  52 + };
  53 + case 'BOOL':
  54 + validateValueBool(Number(options?.boolClose), Number(options?.boolOpen));
  55 + return [
  56 + {
  57 + dataType: options?.dataType,
  58 + name: options?.boolClose,
  59 + value: 0,
  60 + },
  61 + {
  62 + dataType: options?.dataType,
  63 + name: options?.boolOpen,
  64 + value: 1,
  65 + },
  66 + ];
  67 + case 'TEXT':
  68 + return {
  69 + dataType: options?.dataType,
  70 + length: Number(options?.length),
  71 + };
  72 + }
  73 + };
70 74
71   -//是否排除结构体
72   -export const useUpdateFormExcludeStruct = async (F, U) => {
73   - const res: any = await findDictItemByCode({ dictCode: 'data_type' });
74   - const optionTrue = res
75   - .map((m) => {
76   - if (F) {
77   - if (m.itemValue !== 'STRUCT') {
  75 + //是否排除结构体
  76 + const useUpdateFormExcludeStruct = async (F, U) => {
  77 + const res: any = await findDictItemByCode({ dictCode: 'data_type' });
  78 + const optionTrue = res
  79 + .map((m) => {
  80 + if (F) {
  81 + if (m.itemValue !== 'STRUCT') {
  82 + return {
  83 + value: m.itemValue,
  84 + label: m.itemText,
  85 + };
  86 + }
  87 + } else {
78 88 return {
79 89 value: m.itemValue,
80 90 label: m.itemText,
81 91 };
82 92 }
  93 + })
  94 + .filter(Boolean);
  95 + U({
  96 + field: 'dataType',
  97 + componentProps: {
  98 + options: optionTrue,
  99 + },
  100 + });
  101 + };
  102 +
  103 + //获取输入参数或者输出参数弹窗数据
  104 + const useGetInOrOutData = (d, f, inputData, outputData) => {
  105 + if (f == 'input') {
  106 + if (d.id !== null) {
  107 + const findIndex = inputData.findIndex((f) => f.id == d.id);
  108 + if (findIndex !== -1) inputData.splice(findIndex, 1, d);
83 109 } else {
84   - return {
85   - value: m.itemValue,
86   - label: m.itemText,
87   - };
  110 + inputData.push({ ...d, id: buildUUID() });
88 111 }
89   - })
90   - .filter(Boolean);
91   - U({
92   - field: 'dataType',
93   - componentProps: {
94   - options: optionTrue,
95   - },
96   - });
97   -};
98   -
99   -//获取输入参数或者输出参数弹窗数据
100   -export const useGetInOrOutData = (d, f, inputData, outputData) => {
101   - if (f == 'input') {
102   - if (d.id !== null) {
103   - const findIndex = inputData.findIndex((f) => f.id == d.id);
104   - if (findIndex !== -1) inputData.splice(findIndex, 1, d);
105 112 } else {
106   - inputData.push({ ...d, id: buildUUID() });
107   - }
108   - } else {
109   - if (d.id !== null) {
110   - const findIndex = outputData.findIndex((f) => f.id == d.id);
111   - if (findIndex !== -1) outputData.splice(findIndex, 1, d);
112   - } else {
113   - outputData.push({ ...d, id: buildUUID() });
  113 + if (d.id !== null) {
  114 + const findIndex = outputData.findIndex((f) => f.id == d.id);
  115 + if (findIndex !== -1) outputData.splice(findIndex, 1, d);
  116 + } else {
  117 + outputData.push({ ...d, id: buildUUID() });
  118 + }
114 119 }
115   - }
  120 + };
  121 +
  122 + const useBlockContent = `物模型是对设备在云端的功能描述,包括设备的属性、服务和事件。物联网平台通过定义一种物的描述语言来描述物模型,称之为 TSL(即 Thing
  123 + Specification Language),采用 JSON 格式,您可以根据 TSL 组装上报设备的数据。您可以导出完整物模型,用于云端应用开发;您也可以只导出
  124 + 精简物模型,配合设备端 SDK 实现设备开发。`;
  125 +
  126 + const useBlockPhysicalContent = `属性一般是设备的运行状态,如当前温度等;服务是设备可被调用的方法,支持定义参数,如执行某项任务;事件则是设备上报的
  127 + 通知,如告警,需要被及时处理。`;
  128 +
  129 + return {
  130 + useChangeTypeGetTypeForm,
  131 + useUpdateFormExcludeStruct,
  132 + useGetInOrOutData,
  133 + useBlockContent,
  134 + useBlockPhysicalContent,
  135 + };
116 136 };
... ...
  1 +import { ref } from 'vue';
  2 +import CommomParam from './components/CommomParam.vue';
  3 +
  4 +export default () => {
  5 + const inputParamData: any = ref([]);
  6 + const outputParamData: any = ref([]);
  7 + const CommomParamInParamRef = ref<InstanceType<typeof CommomParam>>();
  8 + const CommomParamOutParamRef = ref<InstanceType<typeof CommomParam>>();
  9 +
  10 + return {
  11 + inputParamData,
  12 + outputParamData,
  13 + CommomParamInParamRef,
  14 + CommomParamOutParamRef,
  15 + };
  16 +};
... ...
src/views/device/profiles/step/cpns/physical/hook/useParitalValid.ts renamed from src/views/device/profiles/step/cpns/physical/hook/useValidateParital.ts
... ... @@ -4,27 +4,34 @@
4 4 import { useMessage } from '/@/hooks/web/useMessage';
5 5 const { createMessage } = useMessage();
6 6
7   -export const validateValueRangeAndStep = (min, step, max) => {
8   - if (min > max) {
9   - createMessage.error('最大值必须大于最小值,整数型不能有小数位,单精度有效位为7,双精度为16');
10   - throw '最大值必须大于最小值,整数型不能有小数位,单精度有效位为7,双精度为16';
11   - }
12   - if (step > max - min) {
13   - createMessage.error('步长不能大于取值范围的差值');
14   - throw '步长不能大于取值范围的差值';
15   - }
16   -};
  7 +export default () => {
  8 + const validateValueRangeAndStep = (min, step, max) => {
  9 + if (min > max) {
  10 + createMessage.error('最大值必须大于最小值,整数型不能有小数位,单精度有效位为7,双精度为16');
  11 + throw '最大值必须大于最小值,整数型不能有小数位,单精度有效位为7,双精度为16';
  12 + }
  13 + if (step > max - min) {
  14 + createMessage.error('步长不能大于取值范围的差值');
  15 + throw '步长不能大于取值范围的差值';
  16 + }
  17 + };
17 18
18   -export const validateValueBool = (boolClose, boolOpen) => {
19   - if (boolClose == boolOpen) {
20   - createMessage.error('布尔值不能相同');
21   - throw '布尔值不能相同';
22   - }
23   -};
  19 + const validateValueBool = (boolClose, boolOpen) => {
  20 + if (boolClose == boolOpen) {
  21 + createMessage.error('布尔值不能相同');
  22 + throw '布尔值不能相同';
  23 + }
  24 + };
24 25
25   -export const validateValueStruct = (data: []) => {
26   - if (data.length === 0) {
27   - createMessage.error('struct不能为空');
28   - throw 'struct不能为空';
29   - }
  26 + const validateValueStruct = (data: []) => {
  27 + if (data.length === 0) {
  28 + createMessage.error('struct不能为空');
  29 + throw 'struct不能为空';
  30 + }
  31 + };
  32 + return {
  33 + validateValueRangeAndStep,
  34 + validateValueBool,
  35 + validateValueStruct,
  36 + };
30 37 };
... ...
  1 +export interface IAllData {
  2 + properties: string[];
  3 + events: string[];
  4 + services: string[];
  5 + productKey: '';
  6 + _ppk: {};
  7 +}
... ...