feat:【IoT 物联网】简化部分 thingmodel 维护的代码

This commit is contained in:
YunaiV
2025-06-29 15:02:39 +08:00
parent 677b0d61ca
commit 4af1875001
7 changed files with 41 additions and 58 deletions

View File

@ -6,15 +6,12 @@
prop="event.type"
>
<el-radio-group v-model="thingModelEvent.type">
<!-- TODO @AI使用枚举 -->
<el-radio :value="IoTThingModelEventTypeEnum.INFO.value">
{{ IoTThingModelEventTypeEnum.INFO.label }}
</el-radio>
<el-radio :value="IoTThingModelEventTypeEnum.ALERT.value">
{{ IoTThingModelEventTypeEnum.ALERT.label }}
</el-radio>
<el-radio :value="IoTThingModelEventTypeEnum.ERROR.value">
{{ IoTThingModelEventTypeEnum.ERROR.label }}
<el-radio
v-for="eventType in Object.values(IoTThingModelEventTypeEnum)"
:key="eventType.value"
:value="eventType.value"
>
{{ eventType.label }}
</el-radio>
</el-radio-group>
</el-form-item>

View File

@ -160,9 +160,8 @@ const submitForm = async () => {
}
}
/** 填写额外的属性 */
/** 填写额外的属性(处理不同类型的情况) */
const fillExtraAttributes = (data: any) => {
// 处理不同类型的情况
// 属性
if (data.type === IoTThingModelTypeEnum.PROPERTY) {
removeDataSpecs(data.property)
@ -191,6 +190,7 @@ const fillExtraAttributes = (data: any) => {
delete data.service
}
}
/** 处理 dataSpecs 为空的情况 */
const removeDataSpecs = (val: any) => {
if (isEmpty(val.dataSpecs)) {

View File

@ -15,7 +15,7 @@
<el-button link type="primary" @click="openParamForm(null)">+新增参数</el-button>
<!-- param 表单 -->
<Dialog v-model="dialogVisible" :title="dialogTitle" append-to-body>
<Dialog v-model="dialogVisible" title="新增参数" append-to-body>
<el-form
ref="paramFormRef"
v-loading="formLoading"
@ -32,7 +32,6 @@
<!-- 属性配置 -->
<ThingModelProperty v-model="formData.property" is-params />
</el-form>
<template #footer>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
@ -54,7 +53,6 @@ const props = defineProps<{ modelValue: any; direction: string }>()
const emits = defineEmits(['update:modelValue'])
const thingModelParams = useVModel(props, 'modelValue', emits) as Ref<any[]>
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('新增参数') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中1修改时的数据加载2提交的按钮禁用
const paramFormRef = ref() // 表单 ref
const formData = ref<any>({
@ -101,8 +99,8 @@ const submitForm = async () => {
// 校验参数
await paramFormRef.value.validate()
try {
const data = unref(formData)
// 构建数据对象
const data = unref(formData)
const item = {
identifier: data.identifier,
name: data.name,
@ -117,19 +115,16 @@ const submitForm = async () => {
dataSpecsList: isEmpty(data.property.dataSpecsList) ? undefined : data.property.dataSpecsList
}
// 查找是否已有相同 identifier 的
// 新增或修改同 identifier 的参数
const existingIndex = thingModelParams.value.findIndex(
(spec) => spec.identifier === data.identifier
)
if (existingIndex > -1) {
// 更新已有项
thingModelParams.value[existingIndex] = item
} else {
// 添加新项
thingModelParams.value.push(item)
}
} finally {
// 隐藏对话框
dialogVisible.value = false
}
}

View File

@ -83,13 +83,13 @@
v-model="property.dataSpecsList"
/>
<el-form-item v-if="!isStructDataSpecs && !isParams" label="读写类型" prop="property.accessMode">
<!-- TODO @AI枚举 -->
<el-radio-group v-model="property.accessMode">
<el-radio :label="IoTThingModelAccessModeEnum.READ_WRITE.value">
{{ IoTThingModelAccessModeEnum.READ_WRITE.label }}
</el-radio>
<el-radio :label="IoTThingModelAccessModeEnum.READ_ONLY.value">
{{ IoTThingModelAccessModeEnum.READ_ONLY.label }}
<el-radio
v-for="accessMode in Object.values(IoTThingModelAccessModeEnum)"
:key="accessMode.value"
:label="accessMode.value"
>
{{ accessMode.label }}
</el-radio>
</el-radio-group>
</el-form-item>
@ -118,14 +118,11 @@ const props = defineProps<{ modelValue: any; isStructDataSpecs?: boolean; isPara
const emits = defineEmits(['update:modelValue'])
const property = useVModel(props, 'modelValue', emits) as Ref<ThingModelProperty>
const getDataTypeOptions2 = computed(() => {
return !props.isStructDataSpecs
? getDataTypeOptions()
: getDataTypeOptions().filter(
(item: any) =>
!([IoTDataSpecsDataTypeEnum.STRUCT, IoTDataSpecsDataTypeEnum.ARRAY] as any[]).includes(
item.value
)
)
if (!props.isStructDataSpecs) {
return getDataTypeOptions()
}
const excludedTypes = [IoTDataSpecsDataTypeEnum.STRUCT, IoTDataSpecsDataTypeEnum.ARRAY]
return getDataTypeOptions().filter((item: any) => !excludedTypes.includes(item.value))
}) // 获得数据类型列表
/** 属性值的数据类型切换时初始化相关数据 */
@ -158,7 +155,7 @@ const handleChange = (dataType: any) => {
}
}
// 默认选中读写
/** 默认选中读写 */
watch(
() => property.value.accessMode,
(val: string) => {

View File

@ -6,12 +6,12 @@
prop="service.callType"
>
<el-radio-group v-model="service.callType">
<!-- TODO @AI使用 IoTThingModelServiceCallTypeEnum 处理下 -->
<el-radio :value="IoTThingModelServiceCallTypeEnum.ASYNC.value">
{{ IoTThingModelServiceCallTypeEnum.ASYNC.label }}
</el-radio>
<el-radio :value="IoTThingModelServiceCallTypeEnum.SYNC.value">
{{ IoTThingModelServiceCallTypeEnum.SYNC.label }}
<el-radio
v-for="callType in Object.values(IoTThingModelServiceCallTypeEnum)"
:key="callType.value"
:value="callType.value"
>
{{ callType.label }}
</el-radio>
</el-radio-group>
</el-form-item>
@ -46,7 +46,7 @@ const props = defineProps<{ modelValue: any; isStructDataSpecs?: boolean }>()
const emits = defineEmits(['update:modelValue'])
const service = useVModel(props, 'modelValue', emits) as Ref<ThingModelService>
// 默认选中ASYNC 异步
/** 默认选中ASYNC 异步 */
watch(
() => service.value.callType,
(val: string) =>

View File

@ -38,7 +38,6 @@
<!-- 属性配置 -->
<ThingModelProperty v-model="formData.property" is-struct-data-specs />
</el-form>
<template #footer>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
@ -117,19 +116,16 @@ const submitForm = async () => {
dataSpecsList: isEmpty(data.property.dataSpecsList) ? undefined : data.property.dataSpecsList
}
// 查找是否已有相同 identifier 的
// 新增或修改同 identifier 的参数
const existingIndex = dataSpecsList.value.findIndex(
(spec) => spec.identifier === data.identifier
)
if (existingIndex > -1) {
// 更新已有项
dataSpecsList.value[existingIndex] = item
} else {
// 添加新项
dataSpecsList.value.push(item)
}
} finally {
// 隐藏对话框
dialogVisible.value = false
}
}

View File

@ -1,20 +1,18 @@
import { isEmpty } from '@/utils/is'
/** iot 依赖注入 KEY */
/** IoT 依赖注入 KEY */
export const IOT_PROVIDE_KEY = {
PRODUCT: 'IOT_PRODUCT'
}
// IOT 产品物模型类型枚举类
/** IoT 产品物模型类型枚举类 */
export const IoTThingModelTypeEnum = {
PROPERTY: 1, // 属性
SERVICE: 2, // 服务
EVENT: 3 // 事件
} as const
/**
* IoT 设备消息的方法枚举
*/
/** IoT 设备消息的方法枚举 */
export const IotDeviceMessageMethodEnum = {
// ========== 设备状态 ==========
STATE_UPDATE: {
@ -57,28 +55,28 @@ export const IotDeviceMessageMethodEnum = {
}
}
// IOT 产品物模型类型枚举类
// IoT 产品物模型类型枚举类
export const IotThingModelTypeEnum = {
PROPERTY: 1, // 属性
SERVICE: 2, // 服务
EVENT: 3 // 事件
}
// IOT 产品物模型服务调用方式枚举
// IoT 产品物模型服务调用方式枚举
export const IoTThingModelServiceCallTypeEnum = {
ASYNC: {
label: '异步调用',
label: '异步',
value: 'async'
},
SYNC: {
label: '同步调用',
label: '同步',
value: 'sync'
}
} as const
export const getThingModelServiceCallTypeLabel = (value: string): string | undefined =>
Object.values(IoTThingModelServiceCallTypeEnum).find((type) => type.value === value)?.label
// IOT 产品物模型事件类型枚举
// IoT 产品物模型事件类型枚举
export const IoTThingModelEventTypeEnum = {
INFO: {
label: '信息',
@ -96,13 +94,13 @@ export const IoTThingModelEventTypeEnum = {
export const getEventTypeLabel = (value: string): string | undefined =>
Object.values(IoTThingModelEventTypeEnum).find((type) => type.value === value)?.label
// IOT 产品物模型参数是输入参数还是输出参数
// IoT 产品物模型参数是输入参数还是输出参数
export const IoTThingModelParamDirectionEnum = {
INPUT: 'input', // 输入参数
OUTPUT: 'output' // 输出参数
} as const
// IOT 产品物模型访问模式枚举类
// IoT 产品物模型访问模式枚举类
export const IoTThingModelAccessModeEnum = {
READ_WRITE: {
label: '读写',