perf: 【IoT 物联网】场景联动执行器和触发器的参数值类型都调整为了字符串类型

pull/806/head
puhui999 2025-08-05 21:26:01 +08:00
parent 9917683f0a
commit d81c544ad9
6 changed files with 281 additions and 113 deletions

View File

@ -175,7 +175,7 @@ interface Action {
productId?: number // 产品编号
deviceId?: number // 设备编号
identifier?: string // 物模型标识符(服务调用时使用)
params?: Record<string, any> // 请求参数
params?: string // 请求参数
alertConfigId?: number // 告警配置编号
}

View File

@ -82,8 +82,12 @@ import { useVModel } from '@vueuse/core'
import ProductSelector from '../selectors/ProductSelector.vue'
import DeviceSelector from '../selectors/DeviceSelector.vue'
import JsonParamsInput from '../inputs/JsonParamsInput.vue'
import { Action, ThingModelService } from '@/api/iot/rule/scene/scene.types'
import { IotRuleSceneActionTypeEnum } from '@/views/iot/utils/constants'
import { Action, ThingModelProperty, ThingModelService } from '@/api/iot/rule/scene/scene.types'
import {
IotRuleSceneActionTypeEnum,
IoTThingModelAccessModeEnum
} from '@/views/iot/utils/constants'
import { ThingModelApi } from '@/api/iot/thingmodel'
/** 设备控制配置组件 */
defineOptions({ name: 'DeviceControlConfig' })
@ -99,7 +103,7 @@ const emit = defineEmits<{
const action = useVModel(props, 'modelValue', emit)
//
const thingModelProperties = ref<any[]>([]) //
const thingModelProperties = ref<ThingModelProperty[]>([]) //
const loadingThingModel = ref(false) //
const selectedService = ref<ThingModelService | null>(null) //
const serviceList = ref<ThingModelService[]>([]) //
@ -108,21 +112,16 @@ const loadingServices = ref(false) // 服务加载状态
//
const paramsValue = computed({
get: () => {
// params JSON
if (action.value.params && typeof action.value.params === 'object') {
return JSON.stringify(action.value.params, null, 2)
}
return ''
// params
return action.value.params || ''
},
set: (value: string) => {
try {
if (value.trim()) {
action.value.params = JSON.parse(value)
} else {
action.value.params = {}
}
} catch (error) {
console.error('JSON解析错误:', error)
}
// JSON
action.value.params = value.trim() || ''
}
})
@ -151,7 +150,7 @@ const handleProductChange = (productId?: number) => {
if (action.value.productId !== productId) {
action.value.deviceId = undefined
action.value.identifier = undefined //
action.value.params = {}
action.value.params = '' //
selectedService.value = null //
serviceList.value = [] //
}
@ -173,7 +172,7 @@ const handleProductChange = (productId?: number) => {
const handleDeviceChange = (deviceId?: number) => {
//
if (action.value.deviceId !== deviceId) {
action.value.params = {}
action.value.params = '' //
}
}
@ -187,7 +186,7 @@ const handleServiceChange = (serviceIdentifier?: string) => {
selectedService.value = service
//
action.value.params = {}
action.value.params = ''
//
if (service && service.inputParams && service.inputParams.length > 0) {
@ -195,12 +194,29 @@ const handleServiceChange = (serviceIdentifier?: string) => {
service.inputParams.forEach((param) => {
defaultParams[param.identifier] = getDefaultValueForParam(param)
})
action.value.params = defaultParams
// JSON
action.value.params = JSON.stringify(defaultParams, null, 2)
}
}
/**
* 加载物模型属性
* 获取物模型TSL数据
* @param productId 产品ID
* @returns 物模型TSL数据
*/
const getThingModelTSL = async (productId: number) => {
if (!productId) return null
try {
return await ThingModelApi.getThingModelTSLByProductId(productId)
} catch (error) {
console.error('获取物模型TSL数据失败:', error)
return null
}
}
/**
* 加载物模型属性可写属性
* @param productId 产品ID
*/
const loadThingModelProperties = async (productId: number) => {
@ -211,39 +227,22 @@ const loadThingModelProperties = async (productId: number) => {
try {
loadingThingModel.value = true
// TODO: API
// const response = await ProductApi.getThingModel(productId)
// 使
thingModelProperties.value = [
{
identifier: 'BatteryLevel',
name: '电池电量',
dataType: 'int',
description: '设备电池电量百分比'
},
{
identifier: 'WaterLeachState',
name: '漏水状态',
dataType: 'bool',
description: '设备漏水检测状态'
},
{
identifier: 'Temperature',
name: '温度',
dataType: 'float',
description: '环境温度值'
},
{
identifier: 'Humidity',
name: '湿度',
dataType: 'float',
description: '环境湿度值'
}
]
const tslData = await getThingModelTSL(productId)
//
if (!tslData?.properties) {
thingModelProperties.value = []
return
}
// accessMode 'w'
thingModelProperties.value = tslData.properties.filter(
(property: ThingModelProperty) =>
property.accessMode &&
(property.accessMode === IoTThingModelAccessModeEnum.READ_WRITE.value ||
property.accessMode === IoTThingModelAccessModeEnum.WRITE_ONLY.value)
)
} catch (error) {
console.error('加载物模型失败:', error)
console.error('加载物模型属性失败:', error)
thingModelProperties.value = []
} finally {
loadingThingModel.value = false
@ -260,11 +259,16 @@ const loadServiceList = async (productId: number) => {
return
}
loadingServices.value = true
try {
const { ThingModelApi } = await import('@/api/iot/thingmodel')
const tslData = await ThingModelApi.getThingModelTSLByProductId(productId)
serviceList.value = tslData?.services || []
loadingServices.value = true
const tslData = await getThingModelTSL(productId)
if (!tslData?.services) {
serviceList.value = []
return
}
serviceList.value = tslData.services
} catch (error) {
console.error('加载服务列表失败:', error)
serviceList.value = []
@ -316,43 +320,67 @@ const getDefaultValueForParam = (param: any) => {
}
}
//
const isInitialized = ref(false)
/**
* 初始化组件数据
*/
const initializeComponent = async () => {
if (isInitialized.value) return
const currentAction = action.value
if (!currentAction) return
//
if (currentAction.productId && isPropertySetAction.value) {
await loadThingModelProperties(currentAction.productId)
}
//
if (currentAction.productId && isServiceInvokeAction.value && currentAction.identifier) {
// TSL
await loadServiceFromTSL(currentAction.productId, currentAction.identifier)
}
isInitialized.value = true
}
/**
* 组件初始化
*/
onMounted(() => {
//
if (action.value.productId && isPropertySetAction.value) {
loadThingModelProperties(action.value.productId)
}
//
if (action.value.productId && isServiceInvokeAction.value && action.value.identifier) {
// TSL
loadServiceFromTSL(action.value.productId, action.value.identifier)
}
initializeComponent()
})
// action.value
//
watch(
() => action.value,
async (newAction) => {
if (newAction) {
//
if (isServiceInvokeAction.value && newAction.productId) {
if (newAction.identifier) {
//
await loadServiceFromTSL(newAction.productId, newAction.identifier)
} else {
//
await loadServiceList(newAction.productId)
}
} else if (isServiceInvokeAction.value) {
//
selectedService.value = null
serviceList.value = []
() => [action.value.productId, action.value.type, action.value.identifier],
async ([newProductId, , newIdentifier], [oldProductId, , oldIdentifier]) => {
//
if (!isInitialized.value) return
//
if (newProductId !== oldProductId) {
if (newProductId && isPropertySetAction.value) {
await loadThingModelProperties(newProductId as number)
} else if (newProductId && isServiceInvokeAction.value) {
await loadServiceList(newProductId as number)
}
}
},
{ deep: true, immediate: true }
//
if (
newIdentifier !== oldIdentifier &&
newProductId &&
isServiceInvokeAction.value &&
newIdentifier
) {
const service = serviceList.value.find((s: any) => s.identifier === newIdentifier)
if (service) {
selectedService.value = service
}
}
}
)
</script>

View File

@ -59,8 +59,14 @@
</el-form-item>
</el-col>
<!-- 操作符选择 - 服务调用不需要操作符 -->
<el-col v-if="triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE" :span="6">
<!-- 操作符选择 - 服务调用和事件上报不需要操作符 -->
<el-col
v-if="
triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE &&
triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST
"
:span="6"
>
<el-form-item label="操作符" required>
<OperatorSelector
:model-value="condition.operator"
@ -72,7 +78,14 @@
</el-col>
<!-- 值输入 -->
<el-col :span="triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE ? 18 : 12">
<el-col
:span="
triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE ||
triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST
? 18
: 12
"
>
<el-form-item
:label="
triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE
@ -84,20 +97,18 @@
<!-- 服务调用参数配置 -->
<JsonParamsInput
v-if="triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE"
:model-value="condition.value"
@update:model-value="(value) => updateConditionField('value', value)"
v-model="conditionValueAsString"
type="service"
:config="{ service: propertyConfig }"
:config="serviceConfig"
placeholder="请输入JSON格式的服务参数"
@validate="handleValueValidate"
/>
<!-- 事件上报参数配置 -->
<JsonParamsInput
v-else-if="triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST"
:model-value="condition.value"
@update:model-value="(value) => updateConditionField('value', value)"
v-model="conditionValueAsString"
type="event"
:config="{ event: propertyConfig }"
:config="eventConfig"
placeholder="请输入JSON格式的事件参数"
@validate="handleValueValidate"
/>
@ -217,6 +228,52 @@ const isDeviceStatusTrigger = computed(() => {
return props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_STATE_UPDATE
})
// - JsonParamsInput
const serviceConfig = computed(() => {
if (
propertyConfig.value &&
props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE
) {
return {
service: {
name: propertyConfig.value.name || '服务',
inputParams: propertyConfig.value.inputParams || []
}
}
}
return undefined
})
// - JsonParamsInput
const eventConfig = computed(() => {
if (propertyConfig.value && props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST) {
return {
event: {
name: propertyConfig.value.name || '事件',
outputParams: propertyConfig.value.outputParams || []
}
}
}
return undefined
})
// JsonParamsInput
const conditionValueAsString = computed({
get: () => {
const value = condition.value.value
if (value === null || value === undefined) {
return ''
}
if (typeof value === 'object') {
return JSON.stringify(value, null, 2)
}
return String(value)
},
set: (newValue: string) => {
condition.value.value = newValue
}
})
//
// TODO @puhui999
const getTriggerTypeText = (type: number) => {
@ -264,6 +321,14 @@ const handlePropertyChange = (propertyInfo: any) => {
if (propertyInfo) {
propertyType.value = propertyInfo.type
propertyConfig.value = propertyInfo.config
// '='
if (
props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST ||
props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE
) {
condition.value.operator = '='
}
}
updateValidationResult()
}
@ -306,9 +371,10 @@ const updateValidationResult = () => {
return
}
//
//
if (
props.triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE &&
props.triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST &&
!condition.value.operator
) {
isValid.value = false
@ -336,8 +402,9 @@ watch(
condition.value.productId,
condition.value.deviceId,
condition.value.identifier,
//
props.triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE
//
props.triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE &&
props.triggerType !== IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST
? condition.value.operator
: null,
condition.value.value

View File

@ -169,7 +169,6 @@ interface Props {
interface Emits {
(e: 'update:modelValue', value: string): void
(e: 'validate', result: { valid: boolean; message: string }): void
}
@ -441,37 +440,90 @@ const generateExampleJson = () => {
return JSON.stringify(example, null, 2)
}
//
onMounted(() => {
//
const isInitialized = ref(false)
//
const initializeData = () => {
if (isInitialized.value) return
if (localValue.value) {
try {
paramsJson.value = localValue.value
// modelValue 使
if (localValue.value.trim()) {
try {
// JSON
const parsed = JSON.parse(localValue.value)
paramsJson.value = JSON.stringify(parsed, null, 2)
} catch {
// JSON使
paramsJson.value = localValue.value
}
} else {
paramsJson.value = ''
}
jsonError.value = ''
} catch (error) {
console.error('初始化参数失败:', error)
jsonError.value = '初始参数格式错误'
}
}
isInitialized.value = true
}
//
onMounted(() => {
initializeData()
})
//
//
watch(
() => localValue.value,
(newValue) => {
if (newValue !== paramsJson.value) {
paramsJson.value = newValue || ''
(newValue, oldValue) => {
//
if (newValue === oldValue) return
try {
let newJsonString = ''
if (newValue && newValue.trim()) {
try {
// JSON
const parsed = JSON.parse(newValue)
newJsonString = JSON.stringify(parsed, null, 2)
} catch {
// JSON使
newJsonString = newValue
}
}
// JSON
if (newJsonString !== paramsJson.value) {
paramsJson.value = newJsonString
jsonError.value = ''
}
} catch (error) {
console.error('数据回显失败:', error)
jsonError.value = '数据格式错误'
}
}
},
{ immediate: true }
)
//
watch(
() => props.config,
() => {
//
paramsJson.value = ''
localValue.value = ''
jsonError.value = ''
(newConfig, oldConfig) => {
//
if (JSON.stringify(newConfig) !== JSON.stringify(oldConfig)) {
//
if (!localValue.value) {
paramsJson.value = ''
jsonError.value = ''
}
}
}
)
</script>

View File

@ -288,7 +288,9 @@ const handleChange = (value: string) => {
}
}
// TSL
/**
* 获取物模型TSL数据
*/
const getThingModelTSL = async () => {
if (!props.productId) {
thingModelTSL.value = null
@ -298,8 +300,15 @@ const getThingModelTSL = async () => {
loading.value = true
try {
thingModelTSL.value = await ThingModelApi.getThingModelTSLByProductId(props.productId)
parseThingModelData()
const tslData = await ThingModelApi.getThingModelTSLByProductId(props.productId)
if (tslData) {
thingModelTSL.value = tslData
parseThingModelData()
} else {
// TSL
await getThingModelList()
}
} catch (error) {
console.error('获取物模型TSL失败:', error)
// TSL
@ -309,7 +318,9 @@ const getThingModelTSL = async () => {
}
}
//
/**
* 获取物模型列表备用方案
*/
const getThingModelList = async () => {
if (!props.productId) {
propertyList.value = []

View File

@ -109,9 +109,19 @@ export const IoTThingModelAccessModeEnum = {
READ_ONLY: {
label: '只读',
value: 'r'
},
WRITE_ONLY: {
label: '只写',
value: 'w'
}
} as const
/** 获取访问模式标签 */
export const getAccessModeLabel = (value: string): string => {
const mode = Object.values(IoTThingModelAccessModeEnum).find((mode) => mode.value === value)
return mode?.label || value
}
/** 属性值的数据类型 */
export const IoTDataSpecsDataTypeEnum = {
INT: 'int',