ActionExecutor.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div class="m-10px">
  3. <!-- TODO puhui999: 产品设备回显 -->
  4. <div class="relative bg-[#eff3f7] h-50px flex items-center px-10px">
  5. <div class="flex items-center mr-60px">
  6. <span class="mr-10px">执行动作</span>
  7. <el-select
  8. v-model="actionConfig.type"
  9. class="!w-240px"
  10. clearable
  11. placeholder="请选择执行类型"
  12. >
  13. <el-option
  14. v-for="dict in getIntDictOptions(DICT_TYPE.IOT_RULE_SCENE_ACTION_TYPE_ENUM)"
  15. :key="dict.value"
  16. :label="dict.label"
  17. :value="dict.value"
  18. />
  19. </el-select>
  20. </div>
  21. <div
  22. v-if="actionConfig.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL"
  23. class="flex items-center mr-60px"
  24. >
  25. <span class="mr-10px">产品</span>
  26. <el-button type="primary" @click="handleSelectProduct" size="small" plain>
  27. {{ product ? product.name : '选择产品' }}
  28. </el-button>
  29. </div>
  30. <div
  31. v-if="actionConfig.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL"
  32. class="flex items-center mr-60px"
  33. >
  34. <span class="mr-10px">设备</span>
  35. <el-button type="primary" @click="handleSelectDevice" size="small" plain>
  36. {{ isEmpty(deviceList) ? '选择设备' : deviceList.map((d) => d.deviceName).join(',') }}
  37. </el-button>
  38. </div>
  39. <!-- 删除执行器 -->
  40. <div class="absolute top-auto right-16px bottom-auto">
  41. <el-tooltip content="删除执行器" placement="top">
  42. <slot></slot>
  43. </el-tooltip>
  44. </div>
  45. </div>
  46. <!-- 设备控制执行器 -->
  47. <DeviceControlAction
  48. v-if="actionConfig.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL"
  49. :model-value="actionConfig.deviceControl"
  50. :product-id="product?.id"
  51. @update:model-value="(val) => (actionConfig.deviceControl = val)"
  52. />
  53. <!-- 告警执行器 -->
  54. <AlertAction
  55. v-else-if="actionConfig.type === IotRuleSceneActionTypeEnum.ALERT"
  56. :model-value="actionConfig.alert"
  57. @update:model-value="(val) => (actionConfig.alert = val)"
  58. />
  59. <!-- 数据桥接执行器 -->
  60. <DataBridgeAction
  61. v-else-if="actionConfig.type === IotRuleSceneActionTypeEnum.DATA_BRIDGE"
  62. :model-value="actionConfig.dataBridgeId"
  63. @update:model-value="(val) => (actionConfig.dataBridgeId = val)"
  64. />
  65. </div>
  66. <!-- 产品、设备的选择 -->
  67. <ProductTableSelect ref="productTableSelectRef" @success="handleProductSelect" />
  68. <DeviceTableSelect
  69. ref="deviceTableSelectRef"
  70. multiple
  71. :product-id="product?.id"
  72. @success="handleDeviceSelect"
  73. />
  74. </template>
  75. <script setup lang="ts">
  76. import { useVModel } from '@vueuse/core'
  77. import { isEmpty } from '@/utils/is'
  78. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  79. import ProductTableSelect from '@/views/iot/product/product/components/ProductTableSelect.vue'
  80. import DeviceTableSelect from '@/views/iot/device/device/components/DeviceTableSelect.vue'
  81. import DeviceControlAction from './DeviceControlAction.vue'
  82. import AlertAction from './AlertAction.vue'
  83. import DataBridgeAction from './DataBridgeAction.vue'
  84. import { ProductVO } from '@/api/iot/product/product'
  85. import { DeviceVO } from '@/api/iot/device/device'
  86. import {
  87. ActionAlert,
  88. ActionConfig,
  89. ActionDeviceControl,
  90. IotDeviceMessageIdentifierEnum,
  91. IotDeviceMessageTypeEnum,
  92. IotRuleSceneActionTypeEnum
  93. } from '@/api/iot/rule/scene/scene.types'
  94. /** 场景联动之执行器组件 */
  95. defineOptions({ name: 'ActionExecutor' })
  96. const props = defineProps<{ modelValue: any }>()
  97. const emits = defineEmits(['update:modelValue'])
  98. const actionConfig = useVModel(props, 'modelValue', emits) as Ref<ActionConfig>
  99. const message = useMessage()
  100. // 初始化执行器结构
  101. const initActionConfig = () => {
  102. if (!actionConfig.value) {
  103. actionConfig.value = { type: IotRuleSceneActionTypeEnum.DEVICE_CONTROL } as ActionConfig
  104. }
  105. // 设备控制执行器初始化
  106. if (
  107. actionConfig.value.type === IotRuleSceneActionTypeEnum.DEVICE_CONTROL &&
  108. !actionConfig.value.deviceControl
  109. ) {
  110. actionConfig.value.deviceControl = {
  111. productKey: '',
  112. deviceNames: [],
  113. type: IotDeviceMessageTypeEnum.PROPERTY,
  114. identifier: IotDeviceMessageIdentifierEnum.PROPERTY_SET,
  115. data: {}
  116. } as ActionDeviceControl
  117. }
  118. // 告警执行器初始化
  119. if (actionConfig.value.type === IotRuleSceneActionTypeEnum.ALERT && !actionConfig.value.alert) {
  120. actionConfig.value.alert = {} as ActionAlert
  121. }
  122. // 数据桥接执行器初始化
  123. if (
  124. actionConfig.value.type === IotRuleSceneActionTypeEnum.DATA_BRIDGE &&
  125. !actionConfig.value.dataBridgeId
  126. ) {
  127. actionConfig.value.dataBridgeId = undefined
  128. }
  129. }
  130. // 产品和设备选择
  131. const productTableSelectRef = ref<InstanceType<typeof ProductTableSelect>>()
  132. const deviceTableSelectRef = ref<InstanceType<typeof DeviceTableSelect>>()
  133. const product = ref<ProductVO>()
  134. const deviceList = ref<DeviceVO[]>([])
  135. /** 处理选择产品 */
  136. const handleSelectProduct = () => {
  137. productTableSelectRef.value?.open()
  138. }
  139. /** 处理选择设备 */
  140. const handleSelectDevice = () => {
  141. if (!product.value) {
  142. message.warning('请先选择一个产品')
  143. return
  144. }
  145. deviceTableSelectRef.value?.open()
  146. }
  147. /** 处理产品选择成功 */
  148. const handleProductSelect = (val: ProductVO) => {
  149. product.value = val
  150. if (actionConfig.value.deviceControl) {
  151. actionConfig.value.deviceControl.productKey = val.productKey
  152. }
  153. // 重置设备选择
  154. deviceList.value = []
  155. if (actionConfig.value.deviceControl) {
  156. actionConfig.value.deviceControl.deviceNames = []
  157. }
  158. }
  159. /** 处理设备选择成功 */
  160. const handleDeviceSelect = (val: DeviceVO[]) => {
  161. deviceList.value = val
  162. if (actionConfig.value.deviceControl) {
  163. actionConfig.value.deviceControl.deviceNames = val.map((item) => item.deviceName)
  164. }
  165. }
  166. // 监听执行类型变化,初始化对应配置
  167. watch(
  168. () => actionConfig.value.type,
  169. () => {
  170. initActionConfig()
  171. },
  172. { immediate: true }
  173. )
  174. /** 初始化 */
  175. onMounted(() => {
  176. initActionConfig()
  177. })
  178. </script>