huang
2025-11-20 366ba040d2447bacd3455299425e3166f1f992bb
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
package com.mes.task.service;
 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mes.device.entity.DeviceConfig;
import com.mes.device.entity.DeviceGroupConfig;
import com.mes.interaction.DeviceInteraction;
import com.mes.interaction.DeviceInteractionRegistry;
import com.mes.interaction.DeviceLogicHandler;
import com.mes.interaction.DeviceLogicHandlerFactory;
import com.mes.interaction.base.InteractionContext;
import com.mes.interaction.base.InteractionResult;
import com.mes.device.service.DeviceCoordinationService;
import com.mes.device.service.DeviceInteractionService;
import com.mes.task.dto.TaskParameters;
import com.mes.task.entity.MultiDeviceTask;
import com.mes.task.entity.TaskStepDetail;
import com.mes.task.mapper.MultiDeviceTaskMapper;
import com.mes.task.mapper.TaskStepDetailMapper;
import com.mes.task.model.RetryPolicy;
import com.mes.task.model.TaskExecutionContext;
import com.mes.task.model.TaskExecutionResult;
import com.mes.task.service.TaskStatusNotificationService;
import com.mes.device.vo.DevicePlcVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.util.*;
import java.util.concurrent.*;
 
/**
 * 多设备任务执行引擎
 * 支持串行和并行两种执行模式
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TaskExecutionEngine {
 
    private static final Map<String, String> DEFAULT_OPERATIONS = new HashMap<>();
    private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {};
 
    // 执行模式常量
    private static final String EXECUTION_MODE_SERIAL = "SERIAL";
    private static final String EXECUTION_MODE_PARALLEL = "PARALLEL";
 
    static {
        DEFAULT_OPERATIONS.put(DeviceConfig.DeviceType.LOAD_VEHICLE, "feedGlass");
        DEFAULT_OPERATIONS.put(DeviceConfig.DeviceType.LARGE_GLASS, "processGlass");
        DEFAULT_OPERATIONS.put(DeviceConfig.DeviceType.GLASS_STORAGE, "storeGlass");
    }
 
    private final TaskStepDetailMapper taskStepDetailMapper;
    private final MultiDeviceTaskMapper multiDeviceTaskMapper;
    private final DeviceInteractionService deviceInteractionService;
    private final DeviceInteractionRegistry interactionRegistry;
    private final DeviceLogicHandlerFactory handlerFactory;
    private final DeviceCoordinationService deviceCoordinationService;
    private final TaskStatusNotificationService notificationService;
    private final ObjectMapper objectMapper;
 
    // 线程池用于并行执行
    private final ExecutorService executorService = Executors.newCachedThreadPool(r -> {
        Thread t = new Thread(r, "TaskExecutionEngine-Parallel");
        t.setDaemon(true);
        return t;
    });
 
    public TaskExecutionResult execute(MultiDeviceTask task,
                                       DeviceGroupConfig groupConfig,
                                       List<DeviceConfig> devices,
                                       TaskParameters parameters) {
 
        if (CollectionUtils.isEmpty(devices)) {
            return TaskExecutionResult.failure("设备组未配置设备,无法执行任务", Collections.emptyMap());
        }
 
        TaskExecutionContext context = new TaskExecutionContext(parameters);
        
        // 设备协调:检查依赖关系和执行条件
        DeviceCoordinationService.CoordinationResult coordinationResult = 
            deviceCoordinationService.coordinateExecution(groupConfig, devices, context);
        if (!coordinationResult.canExecute()) {
            log.warn("设备协调失败: {}", coordinationResult.getMessage());
            return TaskExecutionResult.failure(coordinationResult.getMessage(), Collections.emptyMap());
        }
        log.info("设备协调成功: {}", coordinationResult.getMessage());
 
        task.setTotalSteps(devices.size());
        task.setStatus(MultiDeviceTask.Status.RUNNING.name());
        multiDeviceTaskMapper.updateById(task);
        
        // 通知任务开始执行
        notificationService.notifyTaskStatus(task);
        
        // 确定执行模式
        String executionMode = determineExecutionMode(groupConfig);
        Integer maxConcurrent = getMaxConcurrentDevices(groupConfig);
 
        log.info("任务执行模式: {}, 最大并发数: {}, 设备数: {}", executionMode, maxConcurrent, devices.size());
 
        List<Map<String, Object>> stepSummaries;
        boolean success;
        String failureMessage;
 
        if (EXECUTION_MODE_PARALLEL.equals(executionMode)) {
            // 并行执行模式
            stepSummaries = new ArrayList<>(Collections.nCopies(devices.size(), null));
            Pair<Boolean, String> result = executeParallel(task, devices, context, stepSummaries, maxConcurrent);
            success = result.getFirst();
            failureMessage = result.getSecond();
        } else {
            // 串行执行模式(默认)
            stepSummaries = new ArrayList<>();
            success = true;
            failureMessage = null;
            for (int i = 0; i < devices.size(); i++) {
                DeviceConfig device = devices.get(i);
                int order = i + 1;
                TaskStepDetail step = createStepRecord(task, device, order);
                StepResult stepResult = executeStep(task, step, device, context);
                stepSummaries.add(stepResult.toSummary());
                if (!stepResult.isSuccess()) {
                    success = false;
                    failureMessage = stepResult.getMessage();
                    break;
                }
            }
        }
 
        Map<String, Object> payload = new HashMap<>();
        payload.put("steps", stepSummaries);
        payload.put("groupId", groupConfig.getId());
        payload.put("deviceCount", devices.size());
        payload.put("executionMode", executionMode);
 
        // 更新任务最终状态
        if (success) {
            task.setStatus(MultiDeviceTask.Status.COMPLETED.name());
        } else {
            task.setStatus(MultiDeviceTask.Status.FAILED.name());
            task.setErrorMessage(failureMessage);
        }
        task.setEndTime(new Date());
        multiDeviceTaskMapper.updateById(task);
        
        // 通知任务完成
        notificationService.notifyTaskStatus(task);
        
        if (success) {
            return TaskExecutionResult.success(payload);
        }
        return TaskExecutionResult.failure(failureMessage != null ? failureMessage : "任务执行失败", payload);
    }
 
    /**
     * 并行执行多个设备操作
     */
    private Pair<Boolean, String> executeParallel(MultiDeviceTask task,
                                                   List<DeviceConfig> devices,
                                                   TaskExecutionContext context,
                                                   List<Map<String, Object>> stepSummaries,
                                                   Integer maxConcurrent) {
        int concurrency = maxConcurrent != null && maxConcurrent > 0 
            ? Math.min(maxConcurrent, devices.size()) 
            : devices.size();
 
        // 创建所有步骤记录
        List<TaskStepDetail> steps = new ArrayList<>();
        for (int i = 0; i < devices.size(); i++) {
            DeviceConfig device = devices.get(i);
            int order = i + 1;
            TaskStepDetail step = createStepRecord(task, device, order);
            steps.add(step);
        }
 
        // 使用信号量控制并发数
        Semaphore semaphore = new Semaphore(concurrency);
        List<CompletableFuture<StepResult>> futures = new ArrayList<>();
 
        for (int i = 0; i < devices.size(); i++) {
            final int index = i;
            final DeviceConfig device = devices.get(index);
            final TaskStepDetail step = steps.get(index);
 
            CompletableFuture<StepResult> future = CompletableFuture.supplyAsync(() -> {
                try {
                    semaphore.acquire();
                    try {
                        return executeStep(task, step, device, context);
                    } finally {
                        semaphore.release();
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    log.error("并行执行被中断, deviceId={}", device.getId(), e);
                    return StepResult.failure(device.getDeviceName(), "执行被中断");
                } catch (Exception e) {
                    log.error("并行执行异常, deviceId={}", device.getId(), e);
                    return StepResult.failure(device.getDeviceName(), e.getMessage());
                }
            }, executorService);
 
            final int finalIndex = index;
            future.whenComplete((result, throwable) -> {
                if (throwable != null) {
                    log.error("并行执行完成时异常, deviceId={}", device.getId(), throwable);
                    stepSummaries.set(finalIndex, StepResult.failure(device.getDeviceName(), 
                        throwable.getMessage()).toSummary());
                } else if (result != null) {
                    stepSummaries.set(finalIndex, result.toSummary());
                }
            });
 
            futures.add(future);
        }
 
        // 等待所有任务完成
        CompletableFuture<Void> allFutures = CompletableFuture.allOf(
            futures.toArray(new CompletableFuture[0])
        );
 
        try {
            allFutures.get(30, TimeUnit.MINUTES); // 最多等待30分钟
        } catch (TimeoutException e) {
            log.error("并行执行超时, taskId={}", task.getTaskId(), e);
            return Pair.of(false, "任务执行超时");
        } catch (Exception e) {
            log.error("等待并行执行完成时异常, taskId={}", task.getTaskId(), e);
            return Pair.of(false, "等待执行完成时发生异常: " + e.getMessage());
        }
 
        // 检查所有步骤的执行结果
        boolean allSuccess = true;
        String firstFailureMessage = null;
        for (int i = 0; i < futures.size(); i++) {
            try {
                StepResult result = futures.get(i).get();
                if (result != null && !result.isSuccess()) {
                    allSuccess = false;
                    if (firstFailureMessage == null) {
                        firstFailureMessage = result.getMessage();
                    }
                }
            } catch (Exception e) {
                log.error("获取步骤执行结果异常, stepIndex={}", i, e);
                allSuccess = false;
                if (firstFailureMessage == null) {
                    firstFailureMessage = "获取执行结果异常: " + e.getMessage();
                }
            }
        }
 
        return Pair.of(allSuccess, firstFailureMessage);
    }
 
    /**
     * 确定执行模式
     */
    private String determineExecutionMode(DeviceGroupConfig groupConfig) {
        if (groupConfig == null) {
            return EXECUTION_MODE_SERIAL; // 默认串行
        }
 
        // 从extraConfig中读取executionMode
        String extraConfig = groupConfig.getExtraConfig();
        if (StringUtils.hasText(extraConfig)) {
            try {
                Map<String, Object> config = objectMapper.readValue(extraConfig, MAP_TYPE);
                Object mode = config.get("executionMode");
                if (mode != null) {
                    String modeStr = String.valueOf(mode).toUpperCase();
                    if (EXECUTION_MODE_PARALLEL.equals(modeStr) || EXECUTION_MODE_SERIAL.equals(modeStr)) {
                        return modeStr;
                    }
                }
            } catch (Exception e) {
                log.warn("解析设备组执行模式失败, groupId={}", groupConfig.getId(), e);
            }
        }
 
        // 如果有maxConcurrentDevices且大于1,默认使用并行模式
        if (groupConfig.getMaxConcurrentDevices() != null && groupConfig.getMaxConcurrentDevices() > 1) {
            return EXECUTION_MODE_PARALLEL;
        }
 
        return EXECUTION_MODE_SERIAL; // 默认串行
    }
 
    /**
     * 获取最大并发设备数
     */
    private Integer getMaxConcurrentDevices(DeviceGroupConfig groupConfig) {
        if (groupConfig == null) {
            return 1;
        }
 
        // 从extraConfig中读取maxConcurrent
        String extraConfig = groupConfig.getExtraConfig();
        if (StringUtils.hasText(extraConfig)) {
            try {
                Map<String, Object> config = objectMapper.readValue(extraConfig, MAP_TYPE);
                Object maxConcurrent = config.get("maxConcurrent");
                if (maxConcurrent instanceof Number) {
                    return ((Number) maxConcurrent).intValue();
                }
            } catch (Exception e) {
                log.warn("解析设备组最大并发数失败, groupId={}", groupConfig.getId(), e);
            }
        }
 
        // 使用实体字段
        return groupConfig.getMaxConcurrentDevices() != null && groupConfig.getMaxConcurrentDevices() > 0
            ? groupConfig.getMaxConcurrentDevices()
            : 1;
    }
 
    /**
     * 简单的Pair类用于返回两个值
     */
    private static class Pair<T, U> {
        private final T first;
        private final U second;
 
        private Pair(T first, U second) {
            this.first = first;
            this.second = second;
        }
 
        public static <T, U> Pair<T, U> of(T first, U second) {
            return new Pair<>(first, second);
        }
 
        public T getFirst() {
            return first;
        }
 
        public U getSecond() {
            return second;
        }
    }
 
    private TaskStepDetail createStepRecord(MultiDeviceTask task, DeviceConfig device, int order) {
        TaskStepDetail step = new TaskStepDetail();
        step.setTaskId(task.getTaskId());
        step.setStepOrder(order);
        step.setDeviceId(String.valueOf(device.getId()));
        step.setStepName(device.getDeviceName());
        step.setStatus(TaskStepDetail.Status.PENDING.name());
        step.setRetryCount(0);
        taskStepDetailMapper.insert(step);
        return step;
    }
 
    private StepResult executeStep(MultiDeviceTask task,
                                   TaskStepDetail step,
                                   DeviceConfig device,
                                   TaskExecutionContext context) {
        return executeStepWithRetry(task, step, device, context, getRetryPolicy(device));
    }
 
    /**
     * 带重试的步骤执行
     */
    private StepResult executeStepWithRetry(MultiDeviceTask task,
                                            TaskStepDetail step,
                                            DeviceConfig device,
                                            TaskExecutionContext context,
                                            RetryPolicy retryPolicy) {
        Date startTime = new Date();
        step.setStartTime(startTime);
        step.setStatus(TaskStepDetail.Status.RUNNING.name());
        step.setRetryCount(0);
 
        DeviceInteraction deviceInteraction = interactionRegistry.getInteraction(device.getDeviceType());
        if (deviceInteraction != null) {
            return executeInteractionStepWithRetry(task, step, device, context, deviceInteraction, retryPolicy);
        }
 
        Map<String, Object> params = buildOperationParams(device, context);
        step.setInputData(toJson(params));
        taskStepDetailMapper.updateById(step);
 
        String operation = determineOperation(device, params);
        DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
        
        int retryAttempt = 0;
        Exception lastException = null;
        
        while (retryAttempt <= retryPolicy.getMaxRetryCount()) {
            try {
                if (retryAttempt > 0) {
                    // 重试前等待
                    long waitTime = retryPolicy.calculateRetryInterval(retryAttempt);
                    log.info("步骤执行重试: deviceId={}, operation={}, retryAttempt={}/{}, waitTime={}ms", 
                        device.getId(), operation, retryAttempt, retryPolicy.getMaxRetryCount(), waitTime);
                    Thread.sleep(waitTime);
                    
                    // 更新步骤状态
                    step.setRetryCount(retryAttempt);
                    step.setStatus(TaskStepDetail.Status.RUNNING.name());
                    step.setStartTime(new Date());
                    taskStepDetailMapper.updateById(step);
                }
 
                DevicePlcVO.OperationResult result;
                if (handler == null) {
                    result = deviceInteractionService.executeOperation(device.getId(), operation, params);
                } else {
                    result = handler.execute(device, operation, params);
                }
 
                boolean opSuccess = Boolean.TRUE.equals(result.getSuccess());
                updateStepAfterOperation(step, result, opSuccess);
                updateTaskProgress(task, step.getStepOrder(), opSuccess);
                
                // 通知步骤更新
                notificationService.notifyStepUpdate(task.getTaskId(), step);
 
                if (opSuccess) {
                    updateContextAfterSuccess(device, context, params);
                    
                    // 同步设备状态
                    deviceCoordinationService.syncDeviceStatus(device, 
                        DeviceCoordinationService.DeviceStatus.COMPLETED, context);
                    
                    return StepResult.success(device.getDeviceName(), result.getMessage());
                } else {
                    // 业务失败,判断是否可重试
                    if (retryAttempt < retryPolicy.getMaxRetryCount() && isRetryableFailure(result)) {
                        retryAttempt++;
                        lastException = new RuntimeException(result.getMessage());
                        log.warn("步骤执行失败,准备重试: deviceId={}, operation={}, retryAttempt={}, message={}", 
                            device.getId(), operation, retryAttempt, result.getMessage());
                        continue;
                    }
                    
                    // 同步失败状态
                    deviceCoordinationService.syncDeviceStatus(device, 
                        DeviceCoordinationService.DeviceStatus.FAILED, context);
                    
                    return StepResult.failure(device.getDeviceName(), result.getMessage());
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.error("步骤执行被中断, deviceId={}, operation={}", device.getId(), operation, e);
                step.setStatus(TaskStepDetail.Status.FAILED.name());
                step.setErrorMessage("执行被中断: " + e.getMessage());
                step.setEndTime(new Date());
                step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
                step.setRetryCount(retryAttempt);
                taskStepDetailMapper.updateById(step);
                updateTaskProgress(task, step.getStepOrder(), false);
                return StepResult.failure(device.getDeviceName(), "执行被中断");
            } catch (Exception e) {
                lastException = e;
                log.error("设备操作异常, deviceId={}, operation={}, retryAttempt={}", 
                    device.getId(), operation, retryAttempt, e);
                
                // 判断是否可重试
                if (retryAttempt < retryPolicy.getMaxRetryCount() && retryPolicy.isRetryable(e)) {
                    retryAttempt++;
                    log.warn("步骤执行异常,准备重试: deviceId={}, operation={}, retryAttempt={}, exception={}", 
                        device.getId(), operation, retryAttempt, e.getClass().getSimpleName());
                    continue;
                }
                
                // 不可重试或达到最大重试次数
                step.setStatus(TaskStepDetail.Status.FAILED.name());
                step.setErrorMessage(e.getMessage());
                step.setEndTime(new Date());
                step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
                step.setRetryCount(retryAttempt);
                taskStepDetailMapper.updateById(step);
                updateTaskProgress(task, step.getStepOrder(), false);
                
                // 通知步骤更新
                notificationService.notifyStepUpdate(task.getTaskId(), step);
                
                // 同步失败状态
                deviceCoordinationService.syncDeviceStatus(device, 
                    DeviceCoordinationService.DeviceStatus.FAILED, context);
                
                String errorMsg = retryAttempt > 0 
                    ? String.format("执行失败(已重试%d次): %s", retryAttempt, e.getMessage())
                    : e.getMessage();
                return StepResult.failure(device.getDeviceName(), errorMsg);
            }
        }
        
        // 达到最大重试次数
        step.setStatus(TaskStepDetail.Status.FAILED.name());
        step.setErrorMessage(lastException != null ? lastException.getMessage() : "执行失败");
        step.setEndTime(new Date());
        step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
        step.setRetryCount(retryAttempt);
        taskStepDetailMapper.updateById(step);
        updateTaskProgress(task, step.getStepOrder(), false);
        
        // 通知步骤更新
        notificationService.notifyStepUpdate(task.getTaskId(), step);
        
        deviceCoordinationService.syncDeviceStatus(device, 
            DeviceCoordinationService.DeviceStatus.FAILED, context);
        
        return StepResult.failure(device.getDeviceName(), 
            String.format("执行失败(已重试%d次)", retryAttempt));
    }
 
    /**
     * 带重试的交互步骤执行
     */
    private StepResult executeInteractionStepWithRetry(MultiDeviceTask task,
                                                      TaskStepDetail step,
                                                      DeviceConfig device,
                                                      TaskExecutionContext context,
                                                      DeviceInteraction deviceInteraction,
                                                      RetryPolicy retryPolicy) {
        int retryAttempt = 0;
        Exception lastException = null;
        
        while (retryAttempt <= retryPolicy.getMaxRetryCount()) {
            try {
                if (retryAttempt > 0) {
                    long waitTime = retryPolicy.calculateRetryInterval(retryAttempt);
                    log.info("交互步骤执行重试: deviceId={}, retryAttempt={}/{}, waitTime={}ms", 
                        device.getId(), retryAttempt, retryPolicy.getMaxRetryCount(), waitTime);
                    Thread.sleep(waitTime);
                    
                    step.setRetryCount(retryAttempt);
                    step.setStatus(TaskStepDetail.Status.RUNNING.name());
                    step.setStartTime(new Date());
                    taskStepDetailMapper.updateById(step);
                }
 
                InteractionContext interactionContext = new InteractionContext(device, context);
                step.setInputData(toJson(context.getParameters()));
                InteractionResult interactionResult = deviceInteraction.execute(interactionContext);
                boolean success = interactionResult != null && interactionResult.isSuccess();
                updateStepAfterInteraction(step, interactionResult);
                updateTaskProgress(task, step.getStepOrder(), success);
 
                if (success) {
                    deviceCoordinationService.syncDeviceStatus(device, 
                        DeviceCoordinationService.DeviceStatus.COMPLETED, context);
                    return StepResult.success(device.getDeviceName(), interactionResult.getMessage());
                } else {
                    if (retryAttempt < retryPolicy.getMaxRetryCount()) {
                        retryAttempt++;
                        continue;
                    }
                    deviceCoordinationService.syncDeviceStatus(device, 
                        DeviceCoordinationService.DeviceStatus.FAILED, context);
                    String message = interactionResult != null ? interactionResult.getMessage() : "交互执行失败";
                    return StepResult.failure(device.getDeviceName(), message);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.error("交互步骤执行被中断, deviceId={}", device.getId(), e);
                step.setStatus(TaskStepDetail.Status.FAILED.name());
                step.setErrorMessage("执行被中断: " + e.getMessage());
                step.setEndTime(new Date());
                step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
                step.setRetryCount(retryAttempt);
                taskStepDetailMapper.updateById(step);
                updateTaskProgress(task, step.getStepOrder(), false);
                return StepResult.failure(device.getDeviceName(), "执行被中断");
            } catch (Exception e) {
                lastException = e;
                log.error("交互执行异常, deviceId={}, retryAttempt={}", device.getId(), retryAttempt, e);
                
                if (retryAttempt < retryPolicy.getMaxRetryCount() && retryPolicy.isRetryable(e)) {
                    retryAttempt++;
                    continue;
                }
                
                step.setStatus(TaskStepDetail.Status.FAILED.name());
                step.setErrorMessage(e.getMessage());
                step.setEndTime(new Date());
                step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
                step.setRetryCount(retryAttempt);
                taskStepDetailMapper.updateById(step);
                updateTaskProgress(task, step.getStepOrder(), false);
                
                // 通知步骤更新
                notificationService.notifyStepUpdate(task.getTaskId(), step);
                
                deviceCoordinationService.syncDeviceStatus(device, 
                    DeviceCoordinationService.DeviceStatus.FAILED, context);
                
                String errorMsg = retryAttempt > 0 
                    ? String.format("执行失败(已重试%d次): %s", retryAttempt, e.getMessage())
                    : e.getMessage();
                return StepResult.failure(device.getDeviceName(), errorMsg);
            }
        }
        
        step.setStatus(TaskStepDetail.Status.FAILED.name());
        step.setErrorMessage(lastException != null ? lastException.getMessage() : "交互执行失败");
        step.setEndTime(new Date());
        step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
        step.setRetryCount(retryAttempt);
        taskStepDetailMapper.updateById(step);
        updateTaskProgress(task, step.getStepOrder(), false);
        
        // 通知步骤更新
        notificationService.notifyStepUpdate(task.getTaskId(), step);
        
        deviceCoordinationService.syncDeviceStatus(device, 
            DeviceCoordinationService.DeviceStatus.FAILED, context);
        
        return StepResult.failure(device.getDeviceName(), 
            String.format("执行失败(已重试%d次)", retryAttempt));
    }
 
    /**
     * 获取重试策略
     */
    private RetryPolicy getRetryPolicy(DeviceConfig device) {
        // 可以从设备配置中读取重试策略
        // 暂时使用默认策略
        return RetryPolicy.defaultPolicy();
    }
 
    /**
     * 判断业务失败是否可重试
     */
    private boolean isRetryableFailure(DevicePlcVO.OperationResult result) {
        if (result == null || result.getMessage() == null) {
            return false;
        }
        String message = result.getMessage().toLowerCase();
        // 网络错误、超时错误可重试
        return message.contains("timeout") || 
               message.contains("connection") ||
               message.contains("网络") ||
               message.contains("超时");
    }
 
 
    private void updateStepAfterOperation(TaskStepDetail step,
                                          DevicePlcVO.OperationResult result,
                                          boolean success) {
        step.setEndTime(new Date());
        if (step.getStartTime() != null) {
            step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
        }
        step.setStatus(success ? TaskStepDetail.Status.COMPLETED.name() : TaskStepDetail.Status.FAILED.name());
        step.setErrorMessage(success ? null : result.getMessage());
        step.setOutputData(toJson(result));
        taskStepDetailMapper.updateById(step);
    }
 
    private void updateStepAfterInteraction(TaskStepDetail step,
                                            InteractionResult result) {
        step.setEndTime(new Date());
        if (step.getStartTime() != null) {
            step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
        }
        boolean success = result != null && result.isSuccess();
        step.setStatus(success ? TaskStepDetail.Status.COMPLETED.name() : TaskStepDetail.Status.FAILED.name());
        step.setErrorMessage(success ? null : (result != null ? result.getMessage() : "交互执行失败"));
        step.setOutputData(result != null ? toJson(result.getData()) : "{}");
        taskStepDetailMapper.updateById(step);
    }
 
    private void updateTaskProgress(MultiDeviceTask task, int currentStep, boolean success) {
        task.setCurrentStep(currentStep);
        if (!success) {
            task.setStatus(MultiDeviceTask.Status.FAILED.name());
        }
        LambdaUpdateWrapper<MultiDeviceTask> update = Wrappers.<MultiDeviceTask>lambdaUpdate()
                .eq(MultiDeviceTask::getId, task.getId())
                .set(MultiDeviceTask::getCurrentStep, currentStep);
        if (!success) {
            update.set(MultiDeviceTask::getStatus, MultiDeviceTask.Status.FAILED.name());
        }
        multiDeviceTaskMapper.update(null, update);
        
        // 通知任务状态更新
        notificationService.notifyTaskStatus(task);
    }
 
    private String determineOperation(DeviceConfig device, Map<String, Object> params) {
        if (params != null && params.containsKey("operation")) {
            Object op = params.get("operation");
            if (op != null) {
                return String.valueOf(op);
            }
        }
        return DEFAULT_OPERATIONS.getOrDefault(device.getDeviceType(), "feedGlass");
    }
 
    private Map<String, Object> buildOperationParams(DeviceConfig device, TaskExecutionContext context) {
        Map<String, Object> params = new HashMap<>();
        TaskParameters taskParams = context.getParameters();
 
        switch (device.getDeviceType()) {
            case DeviceConfig.DeviceType.LOAD_VEHICLE:
                params.put("glassIds", new ArrayList<>(taskParams.getGlassIds()));
                if (StringUtils.hasText(taskParams.getPositionCode())) {
                    params.put("positionCode", taskParams.getPositionCode());
                }
                if (taskParams.getPositionValue() != null) {
                    params.put("positionValue", taskParams.getPositionValue());
                }
                params.put("triggerRequest", true);
                break;
            case DeviceConfig.DeviceType.LARGE_GLASS:
                List<String> source = context.getSafeLoadedGlassIds();
                if (CollectionUtils.isEmpty(source)) {
                    source = taskParams.getGlassIds();
                }
                if (!CollectionUtils.isEmpty(source)) {
                    params.put("glassId", source.get(0));
                    params.put("glassIds", new ArrayList<>(source));
                }
                params.put("processType", taskParams.getProcessType() != null ? taskParams.getProcessType() : 1);
                params.put("triggerRequest", true);
                break;
            case DeviceConfig.DeviceType.GLASS_STORAGE:
                List<String> processed = context.getSafeProcessedGlassIds();
                if (CollectionUtils.isEmpty(processed)) {
                    processed = context.getSafeLoadedGlassIds();
                }
                if (!CollectionUtils.isEmpty(processed)) {
                    params.put("glassId", processed.get(0));
                    params.put("glassIds", new ArrayList<>(processed));
                }
                if (taskParams.getStoragePosition() != null) {
                    params.put("storagePosition", taskParams.getStoragePosition());
                }
                params.put("triggerRequest", true);
                break;
            default:
                if (!CollectionUtils.isEmpty(taskParams.getExtra())) {
                    params.putAll(taskParams.getExtra());
                }
        }
 
        mergeOverrides(device, taskParams, params);
        return params;
    }
 
    private void mergeOverrides(DeviceConfig device, TaskParameters taskParameters, Map<String, Object> params) {
        if (CollectionUtils.isEmpty(taskParameters.getDeviceOverrides())) {
            return;
        }
        Map<String, Object> override = taskParameters.getDeviceOverrides().get(device.getDeviceType());
        if (override == null && StringUtils.hasText(device.getDeviceCode())) {
            override = taskParameters.getDeviceOverrides().get(device.getDeviceCode());
        }
        if (override != null) {
            params.putAll(override);
        }
    }
 
    private void updateContextAfterSuccess(DeviceConfig device,
                                           TaskExecutionContext context,
                                           Map<String, Object> params) {
        List<String> glassIds = extractGlassIds(params);
        
        switch (device.getDeviceType()) {
            case DeviceConfig.DeviceType.LOAD_VEHICLE:
                context.setLoadedGlassIds(glassIds);
                // 数据传递:上大车 -> 下一个设备
                if (!CollectionUtils.isEmpty(glassIds)) {
                    Map<String, Object> transferData = new HashMap<>();
                    transferData.put("glassIds", glassIds);
                    transferData.put("sourceDevice", device.getDeviceCode());
                    // 这里简化处理,实际应该找到下一个设备
                    // 在串行模式下,下一个设备会在循环中自动获取
                }
                break;
            case DeviceConfig.DeviceType.LARGE_GLASS:
                context.setProcessedGlassIds(glassIds);
                // 数据传递:大理片 -> 下一个设备
                if (!CollectionUtils.isEmpty(glassIds)) {
                    Map<String, Object> transferData = new HashMap<>();
                    transferData.put("glassIds", glassIds);
                    transferData.put("sourceDevice", device.getDeviceCode());
                }
                break;
            default:
                break;
        }
    }
 
    private List<String> extractGlassIds(Map<String, Object> params) {
        if (params == null) {
            return Collections.emptyList();
        }
        Object glassIds = params.get("glassIds");
        if (glassIds instanceof List) {
            @SuppressWarnings("unchecked")
            List<String> cast = (List<String>) glassIds;
            return new ArrayList<>(cast);
        }
        Object glassId = params.get("glassId");
        if (glassId != null) {
            return Collections.singletonList(String.valueOf(glassId));
        }
        return Collections.emptyList();
    }
 
    private String toJson(Object value) {
        try {
            return objectMapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            return "{}";
        }
    }
 
    private static class StepResult {
        private final boolean success;
        private final String message;
        private final String deviceName;
 
        private StepResult(boolean success, String message, String deviceName) {
            this.success = success;
            this.message = message;
            this.deviceName = deviceName;
        }
 
        public static StepResult success(String deviceName, String message) {
            return new StepResult(true, message, deviceName);
        }
 
        public static StepResult failure(String deviceName, String message) {
            return new StepResult(false, message, deviceName);
        }
 
        public boolean isSuccess() {
            return success;
        }
 
        public String getMessage() {
            return message;
        }
 
        public Map<String, Object> toSummary() {
            Map<String, Object> summary = new HashMap<>();
            summary.put("deviceName", deviceName);
            summary.put("success", success);
            summary.put("message", message);
            return summary;
        }
    }
}