wwf
12 小时以前 a1d7e81859f554f3a53680cc35f0f49bf1f77098
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!-- 属性选择器组件 -->
<template>
  <div class="flex items-center gap-8px">
    <el-select
      v-model="localValue"
      placeholder="请选择监控项"
      filterable
      clearable
      @change="handleChange"
      class="!w-150px"
      :loading="loading"
    >
      <el-option-group v-for="group in propertyGroups" :key="group.label" :label="group.label">
        <el-option
          v-for="property in group.options"
          :key="property.identifier"
          :label="property.name"
          :value="property.identifier"
        >
          <div class="flex items-center justify-between w-full py-2px">
            <span class="text-14px font-500 text-[var(--el-text-color-primary)] flex-1 truncate">
              {{ property.name }}
            </span>
            <el-tag
              :type="getDataTypeTagType(property.dataType)"
              size="small"
              class="ml-8px flex-shrink-0"
            >
              {{ property.identifier }}
            </el-tag>
          </div>
        </el-option>
      </el-option-group>
    </el-select>
 
    <!-- 属性详情弹出层 -->
    <el-popover
      v-if="selectedProperty"
      placement="right-start"
      :width="350"
      trigger="click"
      :show-arrow="true"
      :offset="8"
      popper-class="property-detail-popover"
    >
      <template #reference>
        <el-button
          type="info"
          :icon="InfoFilled"
          circle
          size="small"
          class="flex-shrink-0"
          title="查看属性详情"
        />
      </template>
 
      <!-- 弹出层内容 -->
      <div class="property-detail-content">
        <div class="flex items-center gap-8px mb-12px">
          <Icon icon="ep:info-filled" class="text-[var(--el-color-info)] text-16px" />
          <span class="text-14px font-500 text-[var(--el-text-color-primary)]">
            {{ selectedProperty.name }}
          </span>
          <el-tag :type="getDataTypeTagType(selectedProperty.dataType)" size="small">
            {{ getDataTypeName(selectedProperty.dataType) }}
          </el-tag>
        </div>
 
        <div class="space-y-8px ml-24px">
          <div class="flex items-start gap-8px">
            <span class="text-12px text-[var(--el-text-color-secondary)] min-w-60px flex-shrink-0">
              标识符:
            </span>
            <span class="text-12px text-[var(--el-text-color-primary)] flex-1">
              {{ selectedProperty.identifier }}
            </span>
          </div>
 
          <div v-if="selectedProperty.description" class="flex items-start gap-8px">
            <span class="text-12px text-[var(--el-text-color-secondary)] min-w-60px flex-shrink-0">
              描述:
            </span>
            <span class="text-12px text-[var(--el-text-color-primary)] flex-1">
              {{ selectedProperty.description }}
            </span>
          </div>
 
          <div v-if="selectedProperty.unit" class="flex items-start gap-8px">
            <span class="text-12px text-[var(--el-text-color-secondary)] min-w-60px flex-shrink-0">
              单位:
            </span>
            <span class="text-12px text-[var(--el-text-color-primary)] flex-1">
              {{ selectedProperty.unit }}
            </span>
          </div>
 
          <div v-if="selectedProperty.range" class="flex items-start gap-8px">
            <span class="text-12px text-[var(--el-text-color-secondary)] min-w-60px flex-shrink-0">
              取值范围:
            </span>
            <span class="text-12px text-[var(--el-text-color-primary)] flex-1">
              {{ selectedProperty.range }}
            </span>
          </div>
 
          <!-- 根据属性类型显示额外信息 -->
          <div
            v-if="
              selectedProperty.type === IoTThingModelTypeEnum.PROPERTY &&
              selectedProperty.accessMode
            "
            class="flex items-start gap-8px"
          >
            <span class="text-12px text-[var(--el-text-color-secondary)] min-w-60px flex-shrink-0">
              访问模式:
            </span>
            <span class="text-12px text-[var(--el-text-color-primary)] flex-1">
              {{ getAccessModeLabel(selectedProperty.accessMode) }}
            </span>
          </div>
 
          <div
            v-if="
              selectedProperty.type === IoTThingModelTypeEnum.EVENT && selectedProperty.eventType
            "
            class="flex items-start gap-8px"
          >
            <span class="text-12px text-[var(--el-text-color-secondary)] min-w-60px flex-shrink-0">
              事件类型:
            </span>
            <span class="text-12px text-[var(--el-text-color-primary)] flex-1">
              {{ getEventTypeLabel(selectedProperty.eventType) }}
            </span>
          </div>
 
          <div
            v-if="
              selectedProperty.type === IoTThingModelTypeEnum.SERVICE && selectedProperty.callType
            "
            class="flex items-start gap-8px"
          >
            <span class="text-12px text-[var(--el-text-color-secondary)] min-w-60px flex-shrink-0">
              调用类型:
            </span>
            <span class="text-12px text-[var(--el-text-color-primary)] flex-1">
              {{ getThingModelServiceCallTypeLabel(selectedProperty.callType) }}
            </span>
          </div>
        </div>
      </div>
    </el-popover>
  </div>
</template>
 
<script setup lang="ts">
import { useVModel } from '@vueuse/core'
import { InfoFilled } from '@element-plus/icons-vue'
import {
  IotRuleSceneTriggerTypeEnum,
  IoTThingModelTypeEnum,
  getAccessModeLabel,
  getEventTypeLabel,
  getThingModelServiceCallTypeLabel,
  getDataTypeName,
  getDataTypeTagType,
  THING_MODEL_GROUP_LABELS
} from '@/views/iot/utils/constants'
import type {
  IotThingModelTSLResp,
  ThingModelEvent,
  ThingModelParam,
  ThingModelProperty,
  ThingModelService
} from '@/api/iot/thingmodel'
import { ThingModelApi } from '@/api/iot/thingmodel'
 
/** 属性选择器组件 */
defineOptions({ name: 'PropertySelector' })
 
/** 属性选择器内部使用的统一数据结构 */
interface PropertySelectorItem {
  identifier: string
  name: string
  description?: string
  dataType: string
  type: number // IoTThingModelTypeEnum
  accessMode?: string
  required?: boolean
  unit?: string
  range?: string
  eventType?: string
  callType?: string
  inputParams?: ThingModelParam[]
  outputParams?: ThingModelParam[]
  property?: ThingModelProperty
  event?: ThingModelEvent
  service?: ThingModelService
}
 
const props = defineProps<{
  modelValue?: string
  triggerType: number
  productId?: number
  deviceId?: number
}>()
 
const emit = defineEmits<{
  (e: 'update:modelValue', value: string): void
  (e: 'change', value: { type: string; config: any }): void
}>()
 
const localValue = useVModel(props, 'modelValue', emit)
 
const loading = ref(false) // 加载状态
const propertyList = ref<PropertySelectorItem[]>([]) // 属性列表
const thingModelTSL = ref<IotThingModelTSLResp | null>(null) // 物模型TSL数据
 
// 计算属性:属性分组
const propertyGroups = computed(() => {
  const groups: { label: string; options: any[] }[] = []
 
  if (props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST) {
    groups.push({
      label: THING_MODEL_GROUP_LABELS.PROPERTY,
      options: propertyList.value.filter((p) => p.type === IoTThingModelTypeEnum.PROPERTY)
    })
  }
 
  if (props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST) {
    groups.push({
      label: THING_MODEL_GROUP_LABELS.EVENT,
      options: propertyList.value.filter((p) => p.type === IoTThingModelTypeEnum.EVENT)
    })
  }
 
  if (props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE) {
    groups.push({
      label: THING_MODEL_GROUP_LABELS.SERVICE,
      options: propertyList.value.filter((p) => p.type === IoTThingModelTypeEnum.SERVICE)
    })
  }
 
  return groups.filter((group) => group.options.length > 0)
})
 
// 计算属性:当前选中的属性
const selectedProperty = computed(() => {
  return propertyList.value.find((p) => p.identifier === localValue.value)
})
 
/**
 * 处理选择变化事件
 * @param value 选中的属性标识符
 */
const handleChange = (value: string) => {
  const property = propertyList.value.find((p) => p.identifier === value)
  if (property) {
    emit('change', {
      type: property.dataType,
      config: property
    })
  }
}
 
/**
 * 获取物模型TSL数据
 */
const getThingModelTSL = async () => {
  if (!props.productId) {
    thingModelTSL.value = null
    propertyList.value = []
    return
  }
 
  loading.value = true
  try {
    const tslData = await ThingModelApi.getThingModelTSLByProductId(props.productId)
 
    if (tslData) {
      thingModelTSL.value = tslData
      parseThingModelData()
    } else {
      console.error('获取物模型TSL失败: 返回数据为空')
      propertyList.value = []
    }
  } catch (error) {
    console.error('获取物模型TSL失败:', error)
    propertyList.value = []
  } finally {
    loading.value = false
  }
}
 
/** 解析物模型 TSL 数据 */
const parseThingModelData = () => {
  const tsl = thingModelTSL.value
  const properties: PropertySelectorItem[] = []
 
  if (!tsl) {
    propertyList.value = properties
    return
  }
  // 解析属性
  if (tsl.properties && Array.isArray(tsl.properties)) {
    tsl.properties.forEach((prop) => {
      properties.push({
        identifier: prop.identifier,
        name: prop.name,
        description: prop.description,
        dataType: prop.dataType,
        type: IoTThingModelTypeEnum.PROPERTY,
        accessMode: prop.accessMode,
        required: prop.required,
        unit: getPropertyUnit(prop),
        range: getPropertyRange(prop),
        property: prop
      })
    })
  }
 
  // 解析事件
  if (tsl.events && Array.isArray(tsl.events)) {
    tsl.events.forEach((event) => {
      properties.push({
        identifier: event.identifier,
        name: event.name,
        description: event.description,
        dataType: 'struct',
        type: IoTThingModelTypeEnum.EVENT,
        eventType: event.type,
        required: event.required,
        outputParams: event.outputParams,
        event: event
      })
    })
  }
 
  // 解析服务
  if (tsl.services && Array.isArray(tsl.services)) {
    tsl.services.forEach((service) => {
      properties.push({
        identifier: service.identifier,
        name: service.name,
        description: service.description,
        dataType: 'struct',
        type: IoTThingModelTypeEnum.SERVICE,
        callType: service.callType,
        required: service.required,
        inputParams: service.inputParams,
        outputParams: service.outputParams,
        service: service
      })
    })
  }
  propertyList.value = properties
}
 
/**
 * 获取属性单位
 * @param property 属性对象
 * @returns 属性单位
 */
const getPropertyUnit = (property: any) => {
  if (!property) return undefined
 
  // 数值型数据的单位
  if (property.dataSpecs && property.dataSpecs.unit) {
    return property.dataSpecs.unit
  }
 
  return undefined
}
 
/**
 * 获取属性范围描述
 * @param property 属性对象
 * @returns 属性范围描述
 */
const getPropertyRange = (property: any) => {
  if (!property) return undefined
 
  // 数值型数据的范围
  if (property.dataSpecs) {
    const specs = property.dataSpecs
    if (specs.min !== undefined && specs.max !== undefined) {
      return `${specs.min}~${specs.max}`
    }
  }
 
  // 枚举型和布尔型数据的选项
  if (property.dataSpecsList && Array.isArray(property.dataSpecsList)) {
    return property.dataSpecsList.map((item: any) => `${item.name}(${item.value})`).join(', ')
  }
 
  return undefined
}
 
/** 监听产品变化 */
watch(
  () => props.productId,
  () => {
    getThingModelTSL()
  },
  { immediate: true }
)
 
/** 监听触发类型变化 */
watch(
  () => props.triggerType,
  () => {
    localValue.value = ''
  }
)
</script>
 
<style scoped>
/* 下拉选项样式 */
:deep(.el-select-dropdown__item) {
  height: auto;
  padding: 6px 20px;
}
 
/* 弹出层内容样式 */
.property-detail-content {
  padding: 4px 0;
}
 
/* 弹出层自定义样式 */
:global(.property-detail-popover) {
  /* 可以在这里添加全局弹出层样式 */
  max-width: 400px !important;
}
 
:global(.property-detail-popover .el-popover__content) {
  padding: 16px !important;
}
</style>