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

pull/789/MERGE
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" prop="event.type"
> >
<el-radio-group v-model="thingModelEvent.type"> <el-radio-group v-model="thingModelEvent.type">
<!-- TODO @AI使用枚举 --> <el-radio
<el-radio :value="IoTThingModelEventTypeEnum.INFO.value"> v-for="eventType in Object.values(IoTThingModelEventTypeEnum)"
{{ IoTThingModelEventTypeEnum.INFO.label }} :key="eventType.value"
</el-radio> :value="eventType.value"
<el-radio :value="IoTThingModelEventTypeEnum.ALERT.value"> >
{{ IoTThingModelEventTypeEnum.ALERT.label }} {{ eventType.label }}
</el-radio>
<el-radio :value="IoTThingModelEventTypeEnum.ERROR.value">
{{ IoTThingModelEventTypeEnum.ERROR.label }}
</el-radio> </el-radio>
</el-radio-group> </el-radio-group>
</el-form-item> </el-form-item>

View File

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

View File

@ -15,7 +15,7 @@
<el-button link type="primary" @click="openParamForm(null)">+</el-button> <el-button link type="primary" @click="openParamForm(null)">+</el-button>
<!-- param 表单 --> <!-- param 表单 -->
<Dialog v-model="dialogVisible" :title="dialogTitle" append-to-body> <Dialog v-model="dialogVisible" title="新增参数" append-to-body>
<el-form <el-form
ref="paramFormRef" ref="paramFormRef"
v-loading="formLoading" v-loading="formLoading"
@ -32,7 +32,6 @@
<!-- 属性配置 --> <!-- 属性配置 -->
<ThingModelProperty v-model="formData.property" is-params /> <ThingModelProperty v-model="formData.property" is-params />
</el-form> </el-form>
<template #footer> <template #footer>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button> <el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </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 emits = defineEmits(['update:modelValue'])
const thingModelParams = useVModel(props, 'modelValue', emits) as Ref<any[]> const thingModelParams = useVModel(props, 'modelValue', emits) as Ref<any[]>
const dialogVisible = ref(false) // const dialogVisible = ref(false) //
const dialogTitle = ref('新增参数') //
const formLoading = ref(false) // 12 const formLoading = ref(false) // 12
const paramFormRef = ref() // ref const paramFormRef = ref() // ref
const formData = ref<any>({ const formData = ref<any>({
@ -101,8 +99,8 @@ const submitForm = async () => {
// //
await paramFormRef.value.validate() await paramFormRef.value.validate()
try { try {
const data = unref(formData)
// //
const data = unref(formData)
const item = { const item = {
identifier: data.identifier, identifier: data.identifier,
name: data.name, name: data.name,
@ -117,19 +115,16 @@ const submitForm = async () => {
dataSpecsList: isEmpty(data.property.dataSpecsList) ? undefined : data.property.dataSpecsList dataSpecsList: isEmpty(data.property.dataSpecsList) ? undefined : data.property.dataSpecsList
} }
// identifier // identifier
const existingIndex = thingModelParams.value.findIndex( const existingIndex = thingModelParams.value.findIndex(
(spec) => spec.identifier === data.identifier (spec) => spec.identifier === data.identifier
) )
if (existingIndex > -1) { if (existingIndex > -1) {
//
thingModelParams.value[existingIndex] = item thingModelParams.value[existingIndex] = item
} else { } else {
//
thingModelParams.value.push(item) thingModelParams.value.push(item)
} }
} finally { } finally {
//
dialogVisible.value = false dialogVisible.value = false
} }
} }

View File

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

View File

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

View File

@ -38,7 +38,6 @@
<!-- 属性配置 --> <!-- 属性配置 -->
<ThingModelProperty v-model="formData.property" is-struct-data-specs /> <ThingModelProperty v-model="formData.property" is-struct-data-specs />
</el-form> </el-form>
<template #footer> <template #footer>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button> <el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </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 dataSpecsList: isEmpty(data.property.dataSpecsList) ? undefined : data.property.dataSpecsList
} }
// identifier // identifier
const existingIndex = dataSpecsList.value.findIndex( const existingIndex = dataSpecsList.value.findIndex(
(spec) => spec.identifier === data.identifier (spec) => spec.identifier === data.identifier
) )
if (existingIndex > -1) { if (existingIndex > -1) {
//
dataSpecsList.value[existingIndex] = item dataSpecsList.value[existingIndex] = item
} else { } else {
//
dataSpecsList.value.push(item) dataSpecsList.value.push(item)
} }
} finally { } finally {
//
dialogVisible.value = false dialogVisible.value = false
} }
} }

View File

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