Prechádzať zdrojové kódy

feat:【IoT 物联网】场景联动执行器优化对齐后端

puhui999 10 mesiacov pred
rodič
commit
18758628f1

+ 33 - 18
src/views/iot/rule/scene/components/action/ActionExecutor.vue

@@ -19,19 +19,13 @@
             />
           </el-select>
         </div>
-        <div
-          v-if="actionConfig.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL"
-          class="flex items-center mr-60px"
-        >
+        <div v-if="isDeviceAction" class="flex items-center mr-60px">
           <span class="mr-10px">产品</span>
           <el-button type="primary" @click="handleSelectProduct" size="small" plain>
             {{ product ? product.name : '选择产品' }}
           </el-button>
         </div>
-        <div
-          v-if="actionConfig.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL"
-          class="flex items-center mr-60px"
-        >
+        <div v-if="isDeviceAction" class="flex items-center mr-60px">
           <span class="mr-10px">设备</span>
           <el-button type="primary" @click="handleSelectDevice" size="small" plain>
             {{ isEmpty(deviceList) ? '选择设备' : deviceList.map((d) => d.deviceName).join(',') }}
@@ -47,7 +41,8 @@
 
       <!-- 设备控制执行器 -->
       <DeviceControlAction
-        v-if="actionConfig.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL"
+        v-if="isDeviceAction"
+        :action-type="actionConfig.type"
         :model-value="actionConfig.deviceControl"
         :product-id="product?.id"
         :product-key="product?.productKey"
@@ -56,7 +51,8 @@
 
       <!-- 告警执行器 -->
       <AlertAction
-        v-else-if="actionConfig.type === IotRuleSceneActionTypeEnum.ALERT"
+        v-else-if="isAlertAction"
+        :action-type="actionConfig.type"
         :model-value="actionConfig.alert"
         @update:model-value="(val) => (actionConfig.alert = val)"
       />
@@ -101,28 +97,47 @@ const actionConfig = useVModel(props, 'modelValue', emits) as Ref<ActionConfig>
 
 const message = useMessage()
 
+/** 计算属性:判断是否为设备相关执行类型 */
+const isDeviceAction = computed(() => {
+  return [
+    IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET,
+    IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE
+  ].includes(actionConfig.value.type as any)
+})
+
+/** 计算属性:判断是否为告警相关执行类型 */
+const isAlertAction = computed(() => {
+  return [
+    IotRuleSceneActionTypeEnum.ALERT_TRIGGER,
+    IotRuleSceneActionTypeEnum.ALERT_RECOVER
+  ].includes(actionConfig.value.type as any)
+})
+
 /** 初始化执行器结构 */
 const initActionConfig = () => {
   if (!actionConfig.value) {
-    actionConfig.value = { type: IotRuleSceneActionTypeEnum.DEVICE_CONTROL } as ActionConfig
+    actionConfig.value = { type: IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET } as ActionConfig
   }
 
   // 设备控制执行器初始化
-  if (
-    actionConfig.value.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL &&
-    !actionConfig.value.deviceControl
-  ) {
+  if (isDeviceAction.value && !actionConfig.value.deviceControl) {
     actionConfig.value.deviceControl = {
       productKey: '',
       deviceNames: [],
-      type: IotDeviceMessageTypeEnum.PROPERTY,
-      identifier: IotDeviceMessageIdentifierEnum.PROPERTY_SET,
+      type:
+        actionConfig.value.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET
+          ? IotDeviceMessageTypeEnum.PROPERTY
+          : IotDeviceMessageTypeEnum.SERVICE,
+      identifier:
+        actionConfig.value.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET
+          ? IotDeviceMessageIdentifierEnum.PROPERTY_SET
+          : IotDeviceMessageIdentifierEnum.SERVICE_INVOKE,
       data: {}
     } as ActionDeviceControl
   }
 
   // 告警执行器初始化
-  if (actionConfig.value.type === IotRuleSceneActionTypeEnum.ALERT && !actionConfig.value.alert) {
+  if (isAlertAction.value && !actionConfig.value.alert) {
     actionConfig.value.alert = {} as ActionAlert
   }
 }

+ 19 - 2
src/views/iot/rule/scene/components/action/AlertAction.vue

@@ -1,5 +1,15 @@
 <template>
   <div class="bg-[#dbe5f6] p-10px">
+    <!-- 告警类型说明 -->
+    <div class="flex items-center mb-10px" v-if="actionType">
+      <el-icon class="mr-5px text-orange-500"><Icon icon="ep:warning-filled" /></el-icon>
+      <span class="text-gray-600">
+        {{
+          actionType === IotRuleSceneActionTypeEnum.ALERT_TRIGGER ? '触发告警通知' : '告警恢复通知'
+        }}
+      </span>
+    </div>
+
     <div class="flex items-center mb-10px">
       <span class="mr-10px w-80px">接收方式</span>
       <el-select
@@ -63,12 +73,19 @@
 
 <script setup lang="ts">
 import { useVModel } from '@vueuse/core'
-import { ActionAlert, IotAlertConfigReceiveTypeEnum } from '@/api/iot/rule/scene/scene.types'
+import {
+  ActionAlert,
+  IotAlertConfigReceiveTypeEnum,
+  IotRuleSceneActionTypeEnum
+} from '@/api/iot/rule/scene/scene.types'
 
 /** 告警执行器组件 */
 defineOptions({ name: 'AlertAction' })
 
-const props = defineProps<{ modelValue: any }>()
+const props = defineProps<{
+  modelValue: any
+  actionType?: number
+}>()
 const emits = defineEmits(['update:modelValue'])
 const alertConfig = useVModel(props, 'modelValue', emits) as Ref<ActionAlert>
 

+ 38 - 8
src/views/iot/rule/scene/components/action/DeviceControlAction.vue

@@ -1,7 +1,13 @@
 <template>
   <div class="bg-[#dbe5f6] flex p-10px">
     <div class="flex flex-col items-center justify-center mr-10px h-a">
-      <el-select v-model="deviceControlConfig.type" class="!w-160px" clearable placeholder="">
+      <el-select
+        v-model="deviceControlConfig.type"
+        disabled
+        class="!w-160px"
+        clearable
+        placeholder=""
+      >
         <el-option label="属性" :value="IotDeviceMessageTypeEnum.PROPERTY" />
         <el-option label="服务" :value="IotDeviceMessageTypeEnum.SERVICE" />
       </el-select>
@@ -74,7 +80,8 @@ import { ThingModelApi } from '@/api/iot/thingmodel'
 import {
   ActionDeviceControl,
   IotDeviceMessageIdentifierEnum,
-  IotDeviceMessageTypeEnum
+  IotDeviceMessageTypeEnum,
+  IotRuleSceneActionTypeEnum
 } from '@/api/iot/rule/scene/scene.types'
 import ThingModelParamInput from '../ThingModelParamInput.vue'
 
@@ -83,6 +90,7 @@ defineOptions({ name: 'DeviceControlAction' })
 
 const props = defineProps<{
   modelValue: any
+  actionType: number
   productId?: number
   productKey?: string
 }>()
@@ -98,10 +106,6 @@ const addParameter = () => {
     message.warning('请先选择一个产品')
     return
   }
-  if (parameters.value.length >= thingModels.value().length) {
-    message.warning(`该产品只有${thingModels.value().length}个物模型!!!`)
-    return
-  }
   parameters.value.push({ identifier: '', value: undefined })
 }
 const removeParameter = (index: number) => {
@@ -140,8 +144,14 @@ const initDeviceControlConfig = () => {
     deviceControlConfig.value = {
       productKey: '',
       deviceNames: [],
-      type: IotDeviceMessageTypeEnum.PROPERTY,
-      identifier: IotDeviceMessageIdentifierEnum.PROPERTY_SET,
+      type:
+        props.actionType === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET
+          ? IotDeviceMessageTypeEnum.PROPERTY
+          : IotDeviceMessageTypeEnum.SERVICE,
+      identifier:
+        props.actionType === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET
+          ? IotDeviceMessageIdentifierEnum.PROPERTY_SET
+          : IotDeviceMessageIdentifierEnum.SERVICE_INVOKE,
       data: {}
     } as ActionDeviceControl
   } else {
@@ -208,6 +218,26 @@ watch(
   }
 )
 
+/** 监听执行类型变化 */
+watch(
+  () => props.actionType,
+  (val: any) => {
+    if (!val) {
+      return
+    }
+    // 切换执行类型时清空参数
+    deviceControlConfig.value.data = {}
+    parameters.value = []
+    if (val === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET) {
+      deviceControlConfig.value.type = IotDeviceMessageTypeEnum.PROPERTY
+      deviceControlConfig.value.identifier = IotDeviceMessageIdentifierEnum.PROPERTY_SET
+    } else if (val === IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE) {
+      deviceControlConfig.value.type = IotDeviceMessageTypeEnum.SERVICE
+      deviceControlConfig.value.identifier = IotDeviceMessageIdentifierEnum.SERVICE_INVOKE
+    }
+  }
+)
+
 /** 监听消息类型变化 */
 watch(
   () => deviceControlConfig.value.type,