diff --git a/src/views/iot/rule/data/rule/DataRuleForm.vue b/src/views/iot/rule/data/rule/DataRuleForm.vue index c6beb5135..3adc171aa 100644 --- a/src/views/iot/rule/data/rule/DataRuleForm.vue +++ b/src/views/iot/rule/data/rule/DataRuleForm.vue @@ -1,5 +1,5 @@ - + - + @@ -105,12 +108,12 @@ import { ProductApi } from '@/api/iot/product/product' import { DeviceApi } from '@/api/iot/device/device' import { ThingModelApi } from '@/api/iot/thingmodel' -import { IotDeviceMessageMethodEnum } from '@/views/iot/utils/constants' +import { IotDeviceMessageMethodEnum, IotThingModelTypeEnum } from '@/views/iot/utils/constants' const formData = ref([]) const productList = ref([]) // 产品列表 const deviceList = ref([]) // 设备列表 -const thingModelMap = ref>(new Map()) // 缓存物模型数据,key 为 productId +const thingModelCache = ref>(new Map()) // 缓存物模型数据,key 为 productId const formRules = reactive({ productId: [{ required: true, message: '产品不能为空', trigger: 'change' }], @@ -124,7 +127,7 @@ const upstreamMethods = computed(() => { return Object.values(IotDeviceMessageMethodEnum).filter((item) => item.upstream) }) -/** 根据产品ID过滤设备 */ +/** 根据产品 ID 过滤设备 */ const getFilteredDevices = (productId: number) => { if (!productId) return [] return deviceList.value.filter((device: any) => device.productId === productId) @@ -132,27 +135,24 @@ const getFilteredDevices = (productId: number) => { /** 判断是否需要显示标识符选择器 */ const shouldShowIdentifierSelect = (row: any) => { - return ( - row.method === IotDeviceMessageMethodEnum.EVENT_POST.method || - row.method === IotDeviceMessageMethodEnum.PROPERTY_POST.method - ) + return [ + IotDeviceMessageMethodEnum.EVENT_POST.method, + IotDeviceMessageMethodEnum.PROPERTY_POST.method + ].includes(row.method) } /** 获取物模型选项 */ const getThingModelOptions = (row: any) => { - if (!row.productId || !shouldShowIdentifierSelect(row)) return [] - - const thingModels: any[] = thingModelMap.value.get(row.productId) || [] - let filteredModels: any[] = [] - - if (row.method === IotDeviceMessageMethodEnum.EVENT_POST.method) { - // 事件类型,type = 3 - filteredModels = thingModels.filter((item: any) => item.type === 3) - } else if (row.method === IotDeviceMessageMethodEnum.PROPERTY_POST.method) { - // 属性类型,type = 1 - filteredModels = thingModels.filter((item: any) => item.type === 1) + if (!row.productId || !shouldShowIdentifierSelect(row)) { + return [] + } + const thingModels: any[] = thingModelCache.value.get(row.productId) || [] + let filteredModels: any[] = [] + if (row.method === IotDeviceMessageMethodEnum.EVENT_POST.method) { + filteredModels = thingModels.filter((item: any) => item.type === IotThingModelTypeEnum.EVENT) + } else if (row.method === IotDeviceMessageMethodEnum.PROPERTY_POST.method) { + filteredModels = thingModels.filter((item: any) => item.type === IotThingModelTypeEnum.PROPERTY) } - return filteredModels.map((item: any) => ({ label: `${item.name} (${item.identifier})`, value: item.identifier @@ -179,39 +179,30 @@ const loadDeviceList = async () => { /** 加载物模型数据 */ const loadThingModel = async (productId: number) => { - if (thingModelMap.value.has(productId)) { - return // 已缓存,无需重复加载 + // 已缓存,无需重复加载 + if (thingModelCache.value.has(productId)) { + return } - try { const thingModels = await ThingModelApi.getThingModelList({ productId }) - thingModelMap.value.set(productId, thingModels) + thingModelCache.value.set(productId, thingModels) } catch (error) { console.error('加载物模型失败:', error) - thingModelMap.value.set(productId, []) } } /** 产品变化时处理 */ const handleProductChange = async (row: any, _index: number) => { - // 清空其他字段 - row.deviceId = undefined + row.deviceId = 0 row.method = undefined row.identifier = undefined - - // 根据产品ID过滤设备列表不需要额外处理,计算属性会自动过滤 -} - -/** 设备变化时处理 */ -const handleDeviceChange = (row: any, _index: number) => { - // 设备变化时可以做一些额外处理 + row.identifierLoading = false } /** 消息方法变化时处理 */ const handleMethodChange = async (row: any, _index: number) => { // 清空标识符 row.identifier = undefined - // 如果需要加载物模型数据 if (shouldShowIdentifierSelect(row) && row.productId) { row.identifierLoading = true @@ -254,7 +245,6 @@ const setData = (data: any[]) => { ...item, identifierLoading: false })) - // 为已有数据预加载物模型 data?.forEach(async (item) => { if (item.productId && shouldShowIdentifierSelect(item)) { diff --git a/src/views/iot/rule/data/rule/index.vue b/src/views/iot/rule/data/rule/index.vue index 07810767e..04c088123 100644 --- a/src/views/iot/rule/data/rule/index.vue +++ b/src/views/iot/rule/data/rule/index.vue @@ -76,8 +76,12 @@ - - + + + + + + { diff --git a/src/views/iot/rule/data/sink/index.vue b/src/views/iot/rule/data/sink/index.vue index 4bc4cf8d1..723abed24 100644 --- a/src/views/iot/rule/data/sink/index.vue +++ b/src/views/iot/rule/data/sink/index.vue @@ -198,7 +198,7 @@ const handleDelete = async (id: number) => { // 删除的二次确认 await message.delConfirm() // 发起删除 - await DataSinkApi.deleteDataBridge(id) + await DataSinkApi.deleteDataSink(id) message.success(t('common.delSuccess')) // 刷新列表 await getList() diff --git a/src/views/iot/rule/scene/components/action/DataBridgeAction.vue b/src/views/iot/rule/scene/components/action/DataBridgeAction.vue index b1577e7f1..540c7b7a7 100644 --- a/src/views/iot/rule/scene/components/action/DataBridgeAction.vue +++ b/src/views/iot/rule/scene/components/action/DataBridgeAction.vue @@ -4,7 +4,7 @@ 数据流转目的 () const emits = defineEmits(['update:modelValue']) const dataBridgeId = useVModel(props, 'modelValue', emits) -const dataBridgeList = ref([]) // 数据流转目的列表 +const dataSinkList = ref([]) // 数据流转目的列表 -/** 获取数据流转目的列表 */ -const getDataBridgeList = async () => { - dataBridgeList.value = await DataSinkApi.getDataSinkSimpleList() -} - -onMounted(() => { - getDataBridgeList() +onMounted(async () => { + // 获取数据流转目的列表 + dataSinkList.value = await DataSinkApi.getDataSinkSimpleList() }) diff --git a/src/views/iot/thingmodel/config.ts b/src/views/iot/thingmodel/config.ts index 7c9699a19..c5ff1e339 100644 --- a/src/views/iot/thingmodel/config.ts +++ b/src/views/iot/thingmodel/config.ts @@ -55,6 +55,7 @@ export const getDataTypeOptionsLabel = (value: string) => { return dataType && `${dataType.value}(${dataType.label})` } +// TODO @puhui999:使用 ThingModelTypeEnum 替换 // IOT 产品物模型类型枚举类 export const ThingModelType = { PROPERTY: 1, // 属性 diff --git a/src/views/iot/utils/constants.ts b/src/views/iot/utils/constants.ts index af450ea17..fee3bc8a0 100644 --- a/src/views/iot/utils/constants.ts +++ b/src/views/iot/utils/constants.ts @@ -52,3 +52,10 @@ export const IotDeviceMessageMethodEnum = { upstream: false } } + +// IOT 产品物模型类型枚举类 +export const IotThingModelTypeEnum = { + PROPERTY: 1, // 属性 + SERVICE: 2, // 服务 + EVENT: 3 // 事件 +}