huang
2025-11-25 19f59c243e8df97c8b9fd9dba4e758be8235d68b
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
<template>
  <div class="task-orchestration">
    <div class="panel-header">
      <div>
        <h3>多设备测试编排</h3>
        <p v-if="group">当前设备组:{{ group.groupName }}({{ group.deviceCount || '-' }} 台设备)</p>
        <p v-else class="warning">请先在左侧选择一个设备组</p>
        <p v-if="group && loadDeviceName" class="sub-info">当前设备:{{ loadDeviceName }}</p>
      </div>
      <div class="action-buttons">
        <el-button
          type="danger"
          plain
          :disabled="!group || !loadDeviceId || loadDeviceLoading"
          :loading="clearLoading"
          @click="handleClearPlc"
        >
          <el-icon><Delete /></el-icon>
          清空PLC
        </el-button>
        <el-button type="primary" :disabled="!group" :loading="loading" @click="handleSubmit">
          <el-icon><Promotion /></el-icon>
          启动测试
        </el-button>
      </div>
    </div>
 
    <el-form :model="form" label-width="120px" :rules="rules" ref="formRef">
      <el-form-item label="玻璃ID列表" prop="glassIds" required>
        <el-input
          v-model="glassIdsInput"
          type="textarea"
          :rows="4"
          placeholder="请输入玻璃条码,支持多行或逗号分隔,每行一个或逗号分隔"
          show-word-limit
          :maxlength="5000"
        />
        <div class="form-tip">
          已输入 {{ glassIds.length }} 个玻璃ID
        </div>
      </el-form-item>
      
      <el-divider content-position="left">设备特定配置</el-divider>
      
      <el-form-item label="位置编码">
        <el-input
          v-model="form.positionCode"
          placeholder="例如:POS1"
          clearable
        />
        <div class="form-tip">上大车设备的位置编码</div>
      </el-form-item>
      
      <el-form-item label="位置值">
        <el-input-number
          v-model="form.positionValue"
          :min="0"
          :max="9999"
          placeholder="位置数值"
        />
        <div class="form-tip">上大车设备的位置数值</div>
      </el-form-item>
      
      <el-form-item label="存储位置">
        <el-input-number
          v-model="form.storagePosition"
          :min="1"
          :max="200"
          placeholder="存储位置编号"
        />
        <div class="form-tip">玻璃存储设备的存储位置</div>
      </el-form-item>
      
      <el-form-item label="处理类型">
        <el-select v-model="form.processType" placeholder="选择处理类型" clearable>
          <el-option label="标准处理" :value="1" />
          <el-option label="快速处理" :value="2" />
          <el-option label="慢速处理" :value="3" />
        </el-select>
        <div class="form-tip">大理片设备的处理类型</div>
      </el-form-item>
      
      <el-divider content-position="left">执行配置</el-divider>
      
      <el-form-item label="执行间隔 (ms)">
        <el-input-number
          v-model="form.executionInterval"
          :min="100"
          :max="10000"
          :step="100"
          placeholder="设备操作间隔时间"
        />
        <div class="form-tip">每个设备操作之间的间隔时间(毫秒)</div>
      </el-form-item>
      
      <el-form-item label="超时时间 (分钟)">
        <el-input-number
          v-model="form.timeoutMinutes"
          :min="1"
          :max="60"
          :step="1"
          placeholder="任务超时时间"
        />
        <div class="form-tip">任务执行的最大超时时间</div>
      </el-form-item>
      
      <el-form-item label="重试次数">
        <el-input-number
          v-model="form.retryCount"
          :min="0"
          :max="10"
          :step="1"
          placeholder="失败重试次数"
        />
        <div class="form-tip">设备操作失败时的最大重试次数</div>
      </el-form-item>
    </el-form>
  </div>
</template>
 
<script setup>
import { computed, reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Delete, Promotion } from '@element-plus/icons-vue'
import { multiDeviceTaskApi } from '@/api/device/multiDeviceTask'
import { deviceGroupApi, deviceInteractionApi } from '@/api/device/deviceManagement'
 
const props = defineProps({
  group: {
    type: Object,
    default: null
  }
})
 
const emit = defineEmits(['task-started'])
 
const form = reactive({
  positionCode: '',
  positionValue: null,
  storagePosition: null,
  processType: null,
  executionInterval: 1000,
  timeoutMinutes: 30,
  retryCount: 3
})
 
const formRef = ref(null)
 
const rules = {
  glassIds: [
    {
      validator: (rule, value, callback) => {
        if (glassIds.value.length === 0) {
          callback(new Error('请至少输入一个玻璃ID'))
        } else if (glassIds.value.length > 100) {
          callback(new Error('玻璃ID数量不能超过100个'))
        } else {
          // 验证玻璃ID格式
          const invalidIds = glassIds.value.filter(id => {
            // 简单的格式验证:不能为空,长度在1-50之间
            return !id || id.length === 0 || id.length > 50
          })
          if (invalidIds.length > 0) {
            callback(new Error(`存在无效的玻璃ID格式,请检查`))
          } else {
            callback()
          }
        }
      },
      trigger: 'blur'
    }
  ]
}
 
const glassIdsInput = ref('')
const loading = ref(false)
const clearLoading = ref(false)
const loadDeviceId = ref(null)
const loadDeviceName = ref('')
const loadDeviceLoading = ref(false)
 
watch(
  () => props.group,
  () => {
    glassIdsInput.value = ''
    fetchLoadDevice()
  }
)
 
const glassIds = computed(() => {
  if (!glassIdsInput.value) return []
  return glassIdsInput.value
    .split(/[\n,,]/)
    .map((item) => item.trim())
    .filter((item) => item.length > 0)
})
 
const fetchLoadDevice = async () => {
  loadDeviceId.value = null
  loadDeviceName.value = ''
  if (!props.group) {
    return
  }
  const groupId = props.group.id || props.group.groupId
  if (!groupId) {
    return
  }
  loadDeviceLoading.value = true
  try {
    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
      : []
    const targetDevice =
      deviceList.find((item) => (item.deviceType || '').toUpperCase() === 'LOAD_VEHICLE') ||
      deviceList[0]
    if (targetDevice && targetDevice.id) {
      loadDeviceId.value = targetDevice.id
      loadDeviceName.value = targetDevice.deviceName || targetDevice.deviceCode || `ID: ${targetDevice.id}`
    }
  } catch (error) {
    console.error('加载设备信息失败:', error)
    ElMessage.error(error?.message || '获取设备信息失败')
  } finally {
    loadDeviceLoading.value = false
  }
}
 
const handleSubmit = async () => {
  if (!props.group) {
    ElMessage.warning('请先选择设备组')
    return
  }
  
  // 表单验证
  if (!formRef.value) return
  try {
    await formRef.value.validate()
  } catch (error) {
    ElMessage.warning('请检查表单输入')
    return
  }
  
  if (glassIds.value.length === 0) {
    ElMessage.warning('请至少输入一个玻璃ID')
    return
  }
  
  try {
    loading.value = true
    
    // 构建任务参数
    const parameters = {
      glassIds: glassIds.value,
      executionInterval: form.executionInterval || 1000
    }
    
    // 添加可选参数
    if (form.positionCode) {
      parameters.positionCode = form.positionCode
    }
    if (form.positionValue !== null) {
      parameters.positionValue = form.positionValue
    }
    if (form.storagePosition !== null) {
      parameters.storagePosition = form.storagePosition
    }
    if (form.processType !== null) {
      parameters.processType = form.processType
    }
    if (form.timeoutMinutes) {
      parameters.timeoutMinutes = form.timeoutMinutes
    }
    if (form.retryCount !== null) {
      parameters.retryCount = form.retryCount
    }
    
    // 异步启动任务,立即返回,不阻塞
    const response = await multiDeviceTaskApi.startTask({
      groupId: props.group.id || props.group.groupId,
      parameters
    })
    
    const task = response?.data
    if (task && task.taskId) {
      ElMessage.success(`任务已启动(异步执行): ${task.taskId}`)
      emit('task-started', task)
      
      // 立即刷新监控列表,显示新启动的任务
      setTimeout(() => {
        emit('task-started')
      }, 500)
      
      // 重置表单(保留部分配置),方便继续启动其他设备组
      glassIdsInput.value = ''
      form.positionCode = ''
      form.positionValue = null
      form.storagePosition = null
      form.processType = null
      
      // 提示用户可以继续启动其他设备组
      ElMessage.info('可以继续选择其他设备组启动测试,多个设备组将并行执行')
    } else {
      ElMessage.warning('任务启动响应异常')
    }
  } catch (error) {
    ElMessage.error(error?.message || '任务启动失败')
  } finally {
    loading.value = false
  }
}
 
const handleClearPlc = async () => {
  if (!props.group) {
    ElMessage.warning('请先选择设备组')
    return
  }
  if (!loadDeviceId.value) {
    ElMessage.warning('未找到上大车设备,无法清空PLC')
    return
  }
  try {
    clearLoading.value = true
    const response = await deviceInteractionApi.executeOperation({
      deviceId: loadDeviceId.value,
      operation: 'clearGlass',
      params: {
        positionCode: form.positionCode || null
      }
    })
    if (response?.code !== 200) {
      throw new Error(response?.message || 'PLC清空失败')
    }
    const result = response?.data
    if (result?.success) {
      ElMessage.success(result?.message || 'PLC已清空')
      glassIdsInput.value = ''
    } else {
      throw new Error(result?.message || 'PLC清空失败')
    }
  } catch (error) {
    console.error('清空PLC失败:', error)
    ElMessage.error(error?.message || 'PLC清空失败')
  } finally {
    clearLoading.value = false
  }
}
</script>
 
<style scoped>
.task-orchestration {
  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: 16px;
}
 
.panel-header h3 {
  margin: 0;
}
 
.panel-header p {
  margin: 4px 0 0;
  color: #909399;
  font-size: 13px;
}
 
.panel-header .warning {
  color: #f56c6c;
}
 
.panel-header .sub-info {
  margin-top: 4px;
  color: #606266;
  font-size: 12px;
}
 
.action-buttons {
  display: flex;
  gap: 12px;
  align-items: center;
}
 
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
  line-height: 1.4;
}
</style>