huang
9 天以前 9a9479a5e34324822b223747b7c88ff060466db0
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<template>
  <div class="group-topology">
    <div class="panel-header">
      <div>
        <h3>设备组拓扑图</h3>
        <p v-if="group">{{ group.groupName }} - 设备执行流程可视化</p>
        <p v-else class="warning">请先选择一个设备组</p>
      </div>
      <div class="action-buttons">
        <el-button :loading="loading" @click="handleRefresh">
          <el-icon><Refresh /></el-icon>
          刷新
        </el-button>
        <el-button @click="toggleLayout">
          <el-icon><Grid /></el-icon>
          {{ layoutMode === 'horizontal' ? '垂直布局' : '水平布局' }}
        </el-button>
      </div>
    </div>
 
    <div v-if="!group" class="empty-state">
      <el-empty description="请选择设备组查看拓扑图" />
    </div>
 
    <div v-else class="topology-container" :class="`layout-${layoutMode}`">
      <template v-for="(device, index) in devices" :key="device.id || device.deviceId">
        <div
          class="topology-node-wrapper"
          :class="`layout-${layoutMode}`"
        >
          <div
            class="topology-node"
            :class="getDeviceTypeClass(device.deviceType)"
            @click="handleNodeClick(device)"
            :title="`点击查看设备详情 | 执行顺序: ${index + 1}`"
          >
            <div class="node-content">
              <div class="node-icon">
                <el-icon :size="24">
                  <component :is="getDeviceIcon(device.deviceType)" />
                </el-icon>
              </div>
              <div class="node-info">
                <div class="node-name">{{ device.deviceName || device.deviceCode }}</div>
                <div class="node-type">{{ getDeviceTypeLabel(device.deviceType) }}</div>
                <div class="node-status">
                  <el-tag :type="getStatusType(getDeviceStatus(device))" size="small">
                    {{ getStatusLabel(getDeviceStatus(device)) }}
                  </el-tag>
                </div>
                <div class="node-actions">
                  <el-button
                    size="small"
                    type="danger"
                    @click.stop="clearPlc(device)"
                    :loading="clearingDeviceId === (device.deviceId || device.id)"
                  >
                    清空 PLC
                  </el-button>
                </div>
              </div>
            </div>
            <!-- 执行顺序标识:右上角的数字圆圈 -->
            <div class="node-order" :title="`执行顺序: 第 ${index + 1} 步`">
              {{ index + 1 }}
            </div>
          </div>
          <!-- 流程方向箭头:表示设备执行顺序和数据流向 -->
          <div
            v-if="index < devices.length - 1"
            class="node-arrow"
            :title="`数据流向: ${device.deviceName || device.deviceCode} → ${devices[index + 1]?.deviceName || devices[index + 1]?.deviceCode}`"
          >
            <el-icon :size="20">
              <ArrowRight v-if="layoutMode === 'horizontal'" />
              <ArrowDown v-else />
            </el-icon>
          </div>
        </div>
      </template>
    </div>
 
    <!-- 设备详情卡片 -->
    <el-card v-if="selectedDevice" class="device-detail-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>设备详情</span>
          <el-button link @click="selectedDevice = null">
            <el-icon><Close /></el-icon>
          </el-button>
        </div>
      </template>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="设备名称">
          {{ selectedDevice.deviceName }}
        </el-descriptions-item>
        <el-descriptions-item label="设备编码">
          {{ selectedDevice.deviceCode }}
        </el-descriptions-item>
        <el-descriptions-item label="设备类型">
          {{ getDeviceTypeLabel(selectedDevice.deviceType) }}
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <div class="status-control">
            <el-tag :type="getStatusType(getDeviceStatus(selectedDevice))">
              {{ getStatusLabel(getDeviceStatus(selectedDevice)) }}
            </el-tag>
            <el-switch
              v-if="isLoadVehicleDevice(selectedDevice)"
              :model-value="Boolean(selectedDevice.onlineState)"
              active-text="联机"
              inactive-text="脱机"
              :loading="togglingDeviceId === (selectedDevice.deviceId || selectedDevice.id)"
              size="small"
              @change="(val) => toggleOnlineState(selectedDevice, val)"
            />
            <el-button
              size="small"
              text
              @click="clearPlc(selectedDevice)"
              :loading="clearingDeviceId === (selectedDevice.deviceId || selectedDevice.id)"
            >
              清空 PLC
            </el-button>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="PLC IP" v-if="selectedDevice.plcIp">
          {{ selectedDevice.plcIp }}
        </el-descriptions-item>
        <el-descriptions-item label="PLC类型" v-if="selectedDevice.plcType">
          {{ selectedDevice.plcType }}
        </el-descriptions-item>
        <el-descriptions-item label="模块名称" v-if="selectedDevice.moduleName">
          {{ selectedDevice.moduleName }}
        </el-descriptions-item>
        <el-descriptions-item label="是否启用">
          <el-tag :type="getEnabledType(selectedDevice.enabled)">
            {{ getEnabledLabel(selectedDevice.enabled) }}
          </el-tag>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>
 
<script setup>
import { computed, ref, watch, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import {
  Refresh,
  Grid,
  ArrowRight,
  ArrowDown,
  Close,
  Files,
  Box,
  Folder
} from '@element-plus/icons-vue'
import { deviceGroupApi, deviceInteractionApi } from '@/api/device/deviceManagement'
 
const props = defineProps({
  group: {
    type: Object,
    default: null
  }
})
 
const loading = ref(false)
const devices = ref([])
const layoutMode = ref('horizontal') // 'horizontal' | 'vertical'
const selectedDevice = ref(null)
const togglingDeviceId = ref(null)
const clearingDeviceId = ref(null)
const refreshIntervalMs = 5000
let refreshTimer = null
 
const fetchDevices = async () => {
  if (!props.group) {
    devices.value = []
    return
  }
  const groupId = props.group.id || props.group.groupId
  if (!groupId) {
    devices.value = []
    return
  }
  try {
    loading.value = true
    const response = await deviceGroupApi.getGroupDevices(groupId)
    const rawList = response?.data
    const deviceList = Array.isArray(rawList)
      ? rawList
      : Array.isArray(rawList?.records)
      ? rawList.records
      : Array.isArray(rawList?.data)
      ? rawList.data
      : []
    // 按执行顺序排序
    devices.value = deviceList
      .map((device) => normalizeDevice(device))
      .sort((a, b) => {
        const orderA = a.executionOrder || a.order || 0
        const orderB = b.executionOrder || b.order || 0
        return orderA - orderB
      })
    syncSelectedDevice()
  } catch (error) {
    ElMessage.error(error?.message || '加载设备列表失败')
    devices.value = []
  } finally {
    loading.value = false
  }
}
 
const handleRefresh = () => {
  fetchDevices()
}
 
const stopAutoRefresh = () => {
  if (refreshTimer) {
    clearInterval(refreshTimer)
    refreshTimer = null
  }
}
 
const startAutoRefresh = () => {
  stopAutoRefresh()
  if (!props.group) return
  refreshTimer = setInterval(() => {
    fetchDevices()
  }, refreshIntervalMs)
}
 
const toggleLayout = () => {
  layoutMode.value = layoutMode.value === 'horizontal' ? 'vertical' : 'horizontal'
}
 
const getDeviceTypeClass = (deviceType) => {
  if (!deviceType) return 'type-unknown'
  const type = deviceType.toUpperCase()
  if (type.includes('VEHICLE') || type.includes('大车')) return 'type-vehicle'
  if (type.includes('GLASS') || type.includes('大理片')) return 'type-glass'
  if (type.includes('STORAGE') || type.includes('存储')) return 'type-storage'
  return 'type-unknown'
}
 
const getDeviceIcon = (deviceType) => {
  if (!deviceType) return Box
  const type = deviceType.toUpperCase()
  if (type.includes('VEHICLE') || type.includes('大车')) return Files
  if (type.includes('GLASS') || type.includes('大理片')) return Box
  if (type.includes('STORAGE') || type.includes('存储')) return Folder
  return Box
}
 
const getDeviceTypeLabel = (deviceType) => {
  if (!deviceType) return '未知设备'
  const type = deviceType.toUpperCase()
  if (type.includes('VEHICLE') || type.includes('大车')) return '上大车设备'
  if (type.includes('GLASS') || type.includes('大理片')) return '大理片设备'
  if (type.includes('STORAGE') || type.includes('存储')) return '玻璃存储设备'
  return deviceType
}
 
const getStatusType = (status) => {
  if (!status) return 'info'
  const s = String(status).toUpperCase()
  if (s === '1' || s === '启用' || s === 'ENABLED' || s === 'ONLINE') return 'success'
  if (s === '0' || s === '停用' || s === 'DISABLED' || s === 'OFFLINE') return 'danger'
  if (s === '2' || s === '维护' || s === 'MAINTENANCE') return 'warning'
  return 'info'
}
 
const getStatusLabel = (status) => {
  if (!status) return '未知'
  const s = String(status).toUpperCase()
  if (s === '1' || s === '启用' || s === 'ENABLED' || s === 'ONLINE') return '在线'
  if (s === '0' || s === '停用' || s === 'DISABLED' || s === 'OFFLINE') return '离线'
  if (s === '2' || s === '维护' || s === 'MAINTENANCE') return '维护中'
  return String(status)
}
 
const getEnabledType = (enabled) => {
  // 支持数字 1/0、布尔值 true/false、字符串 '1'/'0'
  if (enabled === 1 || enabled === true || enabled === '1' || String(enabled).toUpperCase() === 'TRUE') {
    return 'success'
  }
  return 'info'
}
 
const getEnabledLabel = (enabled) => {
  // 支持数字 1/0、布尔值 true/false、字符串 '1'/'0'
  if (enabled === 1 || enabled === true || enabled === '1' || String(enabled).toUpperCase() === 'TRUE') {
    return '启用'
  }
  return '停用'
}
 
watch(
  () => props.group,
  () => {
    fetchDevices()
    selectedDevice.value = null
    startAutoRefresh()
  },
  { immediate: true }
)
 
onMounted(() => {
  startAutoRefresh()
})
 
onUnmounted(() => {
  stopAutoRefresh()
})
 
// 点击节点选择设备
const handleNodeClick = (device) => {
  selectedDevice.value = device
}
 
const syncSelectedDevice = () => {
  if (!selectedDevice.value) return
  const deviceId = selectedDevice.value.deviceId || selectedDevice.value.id
  if (!deviceId) return
  const updated = devices.value.find(
    (item) => (item.deviceId || item.id) === deviceId
  )
  if (updated) {
    selectedDevice.value = updated
  }
}
 
const isLoadVehicleDevice = (device) => {
  if (!device || !device.deviceType) return false
  const type = device.deviceType.toUpperCase()
  return type.includes('VEHICLE') || type.includes('大车')
}
 
const normalizeDevice = (device) => {
  if (!device) return device
  const normalized = { ...device }
  if (normalized.onlineState !== undefined) {
    normalized.onlineState = toBoolean(normalized.onlineState)
  } else if (normalized.isOnline === true || normalized.isOnline === false) {
    normalized.onlineState = normalized.isOnline
  } else if (normalized.status) {
    normalized.onlineState = String(normalized.status).toUpperCase() === 'ONLINE'
  }
  if (isLoadVehicleDevice(normalized) && normalized.onlineState !== undefined) {
    normalized.status = normalized.onlineState ? 'ONLINE' : 'OFFLINE'
  }
  return normalized
}
 
const toBoolean = (value) => {
  if (value === true || value === false) return value
  if (typeof value === 'number') return value !== 0
  const str = String(value).trim().toLowerCase()
  if (str === 'true' || str === '1') return true
  if (str === 'false' || str === '0') return false
  return Boolean(value)
}
 
const getDeviceStatus = (device) => {
  if (!device) return 'UNKNOWN'
  if (isLoadVehicleDevice(device) && device.onlineState !== undefined) {
    return device.onlineState ? 'ONLINE' : 'OFFLINE'
  }
  if (device.isOnline === true || device.isOnline === false) {
    return device.isOnline ? 'ONLINE' : 'OFFLINE'
  }
  if (device.status) return device.status
  if (device.deviceStatus) return device.deviceStatus
  return 'UNKNOWN'
}
 
const toggleOnlineState = async (device, value) => {
  if (!device) return
  const deviceId = device.deviceId || device.id
  if (!deviceId) {
    ElMessage.warning('设备ID不存在,无法设置联机状态')
    return
  }
  try {
    togglingDeviceId.value = deviceId
    await deviceInteractionApi.executeOperation({
      deviceId,
      operation: 'setOnlineState',
      params: {
        onlineState: value
      }
    })
    device.onlineState = value
    device.status = value ? 'ONLINE' : 'OFFLINE'
    if (selectedDevice.value && (selectedDevice.value.deviceId === deviceId || selectedDevice.value.id === deviceId)) {
      selectedDevice.value.onlineState = device.onlineState
      selectedDevice.value.status = device.status
    }
    ElMessage.success(`已将 ${device.deviceName || device.deviceCode} 设置为${value ? '联机' : '脱机'}`)
  } catch (error) {
    ElMessage.error(error?.message || '设置联机状态失败')
  } finally {
    togglingDeviceId.value = null
  }
}
 
const clearPlc = async (device) => {
  if (!device) return
  const deviceId = device.deviceId || device.id
  if (!deviceId) {
    ElMessage.warning('设备ID不存在,无法清空PLC')
    return
  }
  try {
    clearingDeviceId.value = deviceId
    await deviceInteractionApi.executeOperation({
      deviceId,
      operation: 'clearPlc'
    })
    ElMessage.success(`已清空 ${device.deviceName || device.deviceCode} 的PLC数据`)
  } catch (error) {
    ElMessage.error(error?.message || '清空PLC失败')
  } finally {
    clearingDeviceId.value = null
  }
}
 
defineExpose({
  fetchDevices
})
</script>
 
<style scoped>
.group-topology {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(15, 18, 63, 0.08);
}
 
.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
 
.panel-header h3 {
  margin: 0;
}
 
.panel-header p {
  margin: 4px 0 0;
  color: #909399;
  font-size: 13px;
}
 
.panel-header .warning {
  color: #f56c6c;
}
 
.action-buttons {
  display: flex;
  gap: 12px;
}
 
.empty-state {
  padding: 60px 0;
}
 
.topology-container {
  display: flex;
  align-items: center;
  padding: 20px 0;
  min-height: 200px;
  overflow-x: auto;
  /* 平滑滚动 */
  scroll-behavior: smooth;
}
 
.topology-container.layout-horizontal {
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  /* 添加左右内边距,确保第一个和最后一个节点完全可见 */
  padding-left: 20px;
  padding-right: 20px;
}
 
.topology-container.layout-vertical {
  flex-direction: column;
  align-items: center;
}
 
/* 节点包装器:水平布局时横向排列,垂直布局时纵向排列 */
.topology-node-wrapper {
  display: flex;
  align-items: center;
}
 
.topology-node-wrapper.layout-horizontal {
  flex-direction: row;
  align-items: center;
}
 
.topology-node-wrapper.layout-vertical {
  flex-direction: column;
  align-items: center;
}
 
.topology-node {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  transition: transform 0.2s;
}
 
.topology-node:hover {
  transform: translateY(-4px);
}
 
.node-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  min-width: 160px;
  transition: all 0.3s;
}
 
.topology-node:hover .node-content {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
 
.node-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  color: #fff;
}
 
.type-vehicle .node-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
 
.type-glass .node-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}
 
.type-storage .node-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}
 
.type-unknown .node-icon {
  background: linear-gradient(135deg, #909399 0%, #b1b3b8 100%);
}
 
.node-info {
  text-align: center;
  width: 100%;
}
 
.node-name {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
  word-break: break-all;
}
 
.node-type {
  font-size: 12px;
  color: #909399;
  margin-bottom: 8px;
}
 
.node-status {
  display: flex;
  justify-content: center;
}
 
.node-actions {
  margin-top: 6px;
  display: flex;
  justify-content: center;
}
 
.node-order {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #409eff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.4);
}
 
.node-arrow {
  color: #c0c4cc;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
 
.status-control {
  display: flex;
  align-items: center;
  gap: 12px;
}
 
/* 水平布局:箭头在节点右侧 */
.topology-node-wrapper.layout-horizontal .node-arrow {
  margin-left: 20px;
  margin-right: 20px;
}
 
/* 垂直布局:箭头在节点下方 */
.topology-node-wrapper.layout-vertical .node-arrow {
  margin-top: 15px;
  margin-bottom: 15px;
}
 
.device-detail-card {
  margin-top: 20px;
}
 
.device-detail-card .card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
 
@media (max-width: 768px) {
  .panel-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
 
  .action-buttons {
    width: 100%;
    flex-wrap: wrap;
  }
 
  .topology-container.layout-horizontal {
    flex-direction: column;
    gap: 30px;
  }
 
  .node-arrow {
    transform: rotate(90deg);
  }
}
</style>