Skip to content

Commit 5391720

Browse files
author
puhui999
committed
【功能完善】IOT: ThingModel 服务和事件
1 parent d7c33b4 commit 5391720

File tree

8 files changed

+331
-86
lines changed

8 files changed

+331
-86
lines changed

src/api/iot/thingmodel/index.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,59 +38,6 @@ export interface ThingModelService {
3838
[key: string]: any
3939
}
4040

41-
// IOT 产品物模型类型枚举类
42-
export const ThingModelType = {
43-
PROPERTY: 1, // 属性
44-
SERVICE: 2, // 服务
45-
EVENT: 3 // 事件
46-
} as const
47-
48-
// IOT 产品物模型访问模式枚举类
49-
export const ThingModelAccessMode = {
50-
READ_WRITE: {
51-
label: '读写',
52-
value: 'rw'
53-
},
54-
READ_ONLY: {
55-
label: '只读',
56-
value: 'r'
57-
}
58-
} as const
59-
60-
// IOT 产品物模型服务调用方式枚举
61-
export const ThingModelServiceCallType = {
62-
ASYNC: {
63-
label: '异步调用',
64-
value: 'async'
65-
},
66-
SYNC: {
67-
label: '同步调用',
68-
value: 'sync'
69-
}
70-
} as const
71-
72-
// IOT 产品物模型事件类型枚举
73-
export const ThingModelServiceEventType = {
74-
INFO: {
75-
label: '信息',
76-
value: 'info'
77-
},
78-
ALERT: {
79-
label: '告警',
80-
value: 'alert'
81-
},
82-
ERROR: {
83-
label: '故障',
84-
value: 'error'
85-
}
86-
} as const
87-
88-
// IOT 产品物模型参数是输入参数还是输出参数
89-
export const ThingModelParamDirection = {
90-
INPUT: 'input', // 输入参数
91-
OUTPUT: 'output' // 输出参数
92-
} as const
93-
9441
// IoT 产品物模型 API
9542
export const ThingModelApi = {
9643
// 查询产品物模型分页

src/views/iot/thingmodel/ThingModelEvent.vue

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@
22
<el-form-item
33
:rules="[{ required: true, message: '请选择事件类型', trigger: 'change' }]"
44
label="事件类型"
5-
prop="thingModelEvent.type"
5+
prop="event.type"
66
>
77
<el-radio-group v-model="thingModelEvent.type">
8-
<el-radio :value="ThingModelServiceEventType.INFO.value">
9-
{{ ThingModelServiceEventType.INFO.label }}
8+
<el-radio :value="ThingModelEventType.INFO.value">
9+
{{ ThingModelEventType.INFO.label }}
1010
</el-radio>
11-
<el-radio :value="ThingModelServiceEventType.ALERT.value">
12-
{{ ThingModelServiceEventType.ALERT.label }}
11+
<el-radio :value="ThingModelEventType.ALERT.value">
12+
{{ ThingModelEventType.ALERT.label }}
1313
</el-radio>
14-
<el-radio :value="ThingModelServiceEventType.ERROR.value">
15-
{{ ThingModelServiceEventType.ERROR.label }}
14+
<el-radio :value="ThingModelEventType.ERROR.value">
15+
{{ ThingModelEventType.ERROR.label }}
1616
</el-radio>
1717
</el-radio-group>
1818
</el-form-item>
19+
<el-form-item label="输出参数">
20+
<ThingModelInputOutputParam
21+
v-model="thingModelEvent.outputParams"
22+
:direction="ThingModelParamDirection.OUTPUT"
23+
/>
24+
</el-form-item>
1925
</template>
2026

2127
<script lang="ts" setup>
28+
import ThingModelInputOutputParam from './ThingModelInputOutputParam.vue'
2229
import { useVModel } from '@vueuse/core'
23-
import { ThingModelEvent, ThingModelServiceEventType } from '@/api/iot/thingmodel'
30+
import { ThingModelEvent } from '@/api/iot/thingmodel'
31+
import { ThingModelParamDirection, ThingModelEventType } from './config'
2432
2533
/** IoT 物模型事件 */
2634
defineOptions({ name: 'ThingModelEvent' })

src/views/iot/thingmodel/ThingModelForm.vue

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
/>
3737
<!-- 事件配置 -->
3838
<ThingModelEvent v-if="formData.type === ThingModelType.EVENT" v-model="formData.event" />
39+
<el-form-item label="描述" prop="description">
40+
<el-input
41+
v-model="formData.description"
42+
:maxlength="200"
43+
:rows="3"
44+
placeholder="请输入属性描述"
45+
type="textarea"
46+
/>
47+
</el-form-item>
3948
</el-form>
4049

4150
<template #footer>
@@ -50,9 +59,9 @@ import { ProductVO } from '@/api/iot/product/product'
5059
import ThingModelProperty from './ThingModelProperty.vue'
5160
import ThingModelService from './ThingModelService.vue'
5261
import ThingModelEvent from './ThingModelEvent.vue'
53-
import { ThingModelApi, ThingModelData, ThingModelType } from '@/api/iot/thingmodel'
62+
import { ThingModelApi, ThingModelData } from '@/api/iot/thingmodel'
5463
import { IOT_PROVIDE_KEY } from '@/views/iot/utils/constants'
55-
import { DataSpecsDataType, ThingModelFormRules } from './config'
64+
import { DataSpecsDataType, ThingModelFormRules, ThingModelType } from './config'
5665
import { cloneDeep } from 'lodash-es'
5766
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
5867
import { isEmpty } from '@/utils/is'
@@ -111,7 +120,6 @@ const submitForm = async () => {
111120
// 信息补全
112121
data.productId = product!.value.id
113122
data.productKey = product!.value.productKey
114-
data.description = data.property.description
115123
data.dataType = data.property.dataType
116124
data.property.identifier = data.identifier
117125
data.property.name = data.name
@@ -164,7 +172,9 @@ const resetForm = () => {
164172
dataSpecs: {
165173
dataType: DataSpecsDataType.INT
166174
}
167-
}
175+
},
176+
service: {},
177+
event: {}
168178
}
169179
formRef.value?.resetFields()
170180
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<template>
2+
<div
3+
v-for="(item, index) in thingModelParams"
4+
:key="index"
5+
class="w-1/1 param-item flex justify-between px-10px mb-10px"
6+
>
7+
<span>参数名称:{{ item.name }}</span>
8+
<div class="btn">
9+
<el-button link type="primary" @click="openParamForm(item)">编辑</el-button>
10+
<el-divider direction="vertical" />
11+
<el-button link type="danger" @click="deleteParamItem(index)">删除</el-button>
12+
</div>
13+
</div>
14+
<el-button link type="primary" @click="openParamForm(null)">+新增参数</el-button>
15+
16+
<!-- param 表单 -->
17+
<Dialog v-model="dialogVisible" :title="dialogTitle" append-to-body>
18+
<el-form
19+
ref="paramFormRef"
20+
v-loading="formLoading"
21+
:model="formData"
22+
:rules="ThingModelFormRules"
23+
label-width="100px"
24+
>
25+
<el-form-item label="参数名称" prop="name">
26+
<el-input v-model="formData.name" placeholder="请输入功能名称" />
27+
</el-form-item>
28+
<el-form-item label="标识符" prop="identifier">
29+
<el-input v-model="formData.identifier" placeholder="请输入标识符" />
30+
</el-form-item>
31+
<!-- 属性配置 -->
32+
<ThingModelProperty v-model="formData.property" is-params />
33+
</el-form>
34+
35+
<template #footer>
36+
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
37+
<el-button @click="dialogVisible = false">取 消</el-button>
38+
</template>
39+
</Dialog>
40+
</template>
41+
42+
<script lang="ts" setup>
43+
import { useVModel } from '@vueuse/core'
44+
import ThingModelProperty from './ThingModelProperty.vue'
45+
import { DataSpecsDataType, ThingModelFormRules } from './config'
46+
import { isEmpty } from '@/utils/is'
47+
48+
/** 输入输出参数配置组件 */
49+
defineOptions({ name: 'ThingModelInputOutputParam' })
50+
51+
const props = defineProps<{ modelValue: any; direction: string }>()
52+
const emits = defineEmits(['update:modelValue'])
53+
const thingModelParams = useVModel(props, 'modelValue', emits) as Ref<any[]>
54+
const dialogVisible = ref(false) // 弹窗的是否展示
55+
const dialogTitle = ref('新增参数') // 弹窗的标题
56+
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
57+
const paramFormRef = ref() // 表单 ref
58+
const formData = ref<any>({
59+
dataType: DataSpecsDataType.INT,
60+
property: {
61+
dataType: DataSpecsDataType.INT,
62+
dataSpecs: {
63+
dataType: DataSpecsDataType.INT
64+
}
65+
}
66+
})
67+
68+
/** 打开 param 表单 */
69+
const openParamForm = (val: any) => {
70+
dialogVisible.value = true
71+
resetForm()
72+
if (isEmpty(val)) {
73+
return
74+
}
75+
// 编辑时回显数据
76+
formData.value = {
77+
identifier: val.identifier,
78+
name: val.name,
79+
description: val.description,
80+
property: {
81+
dataType: val.dataType,
82+
dataSpecs: val.dataSpecs,
83+
dataSpecsList: val.dataSpecsList
84+
}
85+
}
86+
}
87+
/** 删除 param 项 */
88+
const deleteParamItem = (index: number) => {
89+
thingModelParams.value.splice(index, 1)
90+
}
91+
92+
/** 添加参数 */
93+
const submitForm = async () => {
94+
// 初始化参数列表
95+
if (isEmpty(thingModelParams.value)) {
96+
thingModelParams.value = []
97+
}
98+
// 校验参数
99+
await paramFormRef.value.validate()
100+
try {
101+
const data = unref(formData)
102+
// 构建数据对象
103+
const item = {
104+
identifier: data.identifier,
105+
name: data.name,
106+
description: data.description,
107+
dataType: data.property.dataType,
108+
paraOrder: 0, // TODO @puhui999: 先写死默认看看后续
109+
direction: props.direction,
110+
dataSpecs:
111+
!!data.property.dataSpecs && Object.keys(data.property.dataSpecs).length > 1
112+
? data.property.dataSpecs
113+
: undefined,
114+
dataSpecsList: isEmpty(data.property.dataSpecsList) ? undefined : data.property.dataSpecsList
115+
}
116+
117+
// 查找是否已有相同 identifier 的项
118+
const existingIndex = thingModelParams.value.findIndex(
119+
(spec) => spec.identifier === data.identifier
120+
)
121+
if (existingIndex > -1) {
122+
// 更新已有项
123+
thingModelParams.value[existingIndex] = item
124+
} else {
125+
// 添加新项
126+
thingModelParams.value.push(item)
127+
}
128+
} finally {
129+
// 隐藏对话框
130+
dialogVisible.value = false
131+
}
132+
}
133+
134+
/** 重置表单 */
135+
const resetForm = () => {
136+
formData.value = {
137+
dataType: DataSpecsDataType.INT,
138+
property: {
139+
dataType: DataSpecsDataType.INT,
140+
dataSpecs: {
141+
dataType: DataSpecsDataType.INT
142+
}
143+
}
144+
}
145+
paramFormRef.value?.resetFields()
146+
}
147+
</script>
148+
149+
<style lang="scss" scoped>
150+
.param-item {
151+
background-color: #e4f2fd;
152+
}
153+
</style>

src/views/iot/thingmodel/ThingModelProperty.vue

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
v-if="property.dataType === DataSpecsDataType.STRUCT"
7676
v-model="property.dataSpecsList"
7777
/>
78-
<el-form-item v-if="!isStructDataSpecs" label="读写类型" prop="property.accessMode">
78+
<el-form-item v-if="!isStructDataSpecs && !isParams" label="读写类型" prop="property.accessMode">
7979
<el-radio-group v-model="property.accessMode">
8080
<el-radio :label="ThingModelAccessMode.READ_WRITE.value">
8181
{{ ThingModelAccessMode.READ_WRITE.label }}
@@ -85,32 +85,28 @@
8585
</el-radio>
8686
</el-radio-group>
8787
</el-form-item>
88-
<el-form-item label="属性描述" prop="description">
89-
<el-input
90-
v-model="property.description"
91-
:maxlength="200"
92-
:rows="3"
93-
placeholder="请输入属性描述"
94-
type="textarea"
95-
/>
96-
</el-form-item>
9788
</template>
9889

9990
<script lang="ts" setup>
10091
import { useVModel } from '@vueuse/core'
101-
import { DataSpecsDataType, dataTypeOptions, validateBoolName } from './config'
92+
import {
93+
DataSpecsDataType,
94+
dataTypeOptions,
95+
ThingModelAccessMode,
96+
validateBoolName
97+
} from './config'
10298
import {
10399
ThingModelArrayDataSpecs,
104100
ThingModelEnumDataSpecs,
105101
ThingModelNumberDataSpecs,
106102
ThingModelStructDataSpecs
107103
} from './dataSpecs'
108-
import { ThingModelAccessMode, ThingModelProperty } from '@/api/iot/thingmodel'
104+
import { ThingModelProperty } from '@/api/iot/thingmodel'
109105
110-
/** IoT 物模型数据 */
106+
/** IoT 物模型属性 */
111107
defineOptions({ name: 'ThingModelProperty' })
112108
113-
const props = defineProps<{ modelValue: any; isStructDataSpecs?: boolean }>()
109+
const props = defineProps<{ modelValue: any; isStructDataSpecs?: boolean; isParams?: boolean }>()
114110
const emits = defineEmits(['update:modelValue'])
115111
const property = useVModel(props, 'modelValue', emits) as Ref<ThingModelProperty>
116112
const getDataTypeOptions = computed(() => {

src/views/iot/thingmodel/ThingModelService.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,25 @@
1313
</el-radio>
1414
</el-radio-group>
1515
</el-form-item>
16+
<el-form-item label="输入参数">
17+
<ThingModelInputOutputParam
18+
v-model="service.inputParams"
19+
:direction="ThingModelParamDirection.INPUT"
20+
/>
21+
</el-form-item>
22+
<el-form-item label="输出参数">
23+
<ThingModelInputOutputParam
24+
v-model="service.outputParams"
25+
:direction="ThingModelParamDirection.OUTPUT"
26+
/>
27+
</el-form-item>
1628
</template>
1729

1830
<script lang="ts" setup>
31+
import ThingModelInputOutputParam from './ThingModelInputOutputParam.vue'
1932
import { useVModel } from '@vueuse/core'
20-
import { ThingModelService, ThingModelServiceCallType } from '@/api/iot/thingmodel'
33+
import { ThingModelService } from '@/api/iot/thingmodel'
34+
import { ThingModelParamDirection, ThingModelServiceCallType } from './config'
2135
2236
/** IoT 物模型服务 */
2337
defineOptions({ name: 'ThingModelService' })

0 commit comments

Comments
 (0)