| | |
| | | import com.mes.task.service.MultiDeviceTaskService; |
| | | import com.mes.task.service.TaskExecutionEngine; |
| | | import com.mes.task.service.TaskStatusNotificationService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 多设备任务服务实现 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class MultiDeviceTaskServiceImpl extends ServiceImpl<MultiDeviceTaskMapper, MultiDeviceTask> |
| | | implements MultiDeviceTaskService { |
| | | |
| | |
| | | private final TaskExecutionEngine taskExecutionEngine; |
| | | private final TaskStatusNotificationService notificationService; |
| | | private final ObjectMapper objectMapper; |
| | | |
| | | public MultiDeviceTaskServiceImpl( |
| | | DeviceGroupConfigService deviceGroupConfigService, |
| | | DeviceGroupRelationMapper deviceGroupRelationMapper, |
| | | TaskStepDetailMapper taskStepDetailMapper, |
| | | TaskExecutionEngine taskExecutionEngine, |
| | | TaskStatusNotificationService notificationService, |
| | | ObjectMapper objectMapper) { |
| | | this.deviceGroupConfigService = deviceGroupConfigService; |
| | | this.deviceGroupRelationMapper = deviceGroupRelationMapper; |
| | | this.taskStepDetailMapper = taskStepDetailMapper; |
| | | this.taskExecutionEngine = taskExecutionEngine; |
| | | this.notificationService = notificationService; |
| | | this.objectMapper = objectMapper; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | } |
| | | |
| | | TaskParameters parameters = request.getParameters(); |
| | | if (parameters == null || CollectionUtils.isEmpty(parameters.getGlassIds())) { |
| | | throw new IllegalArgumentException("至少需要配置一条玻璃ID"); |
| | | if (parameters == null) { |
| | | parameters = new TaskParameters(); |
| | | } |
| | | |
| | | // 默认允许卧转立扫码设备在任务执行阶段获取玻璃信息 |
| | | boolean hasGlassIds = !CollectionUtils.isEmpty(parameters.getGlassIds()); |
| | | if (!hasGlassIds) { |
| | | log.info("测试任务未提供玻璃ID,将在设备组流程中由卧转立扫码设备采集玻璃信息: groupId={}", |
| | | groupConfig.getId()); |
| | | } |
| | | |
| | | // 创建任务记录 |
| | |
| | | // 执行任务 |
| | | TaskExecutionResult result = taskExecutionEngine.execute(task, groupConfig, devices, parameters); |
| | | |
| | | // 更新任务结果 |
| | | // 检查任务数据中是否包含持续运行的标记 |
| | | Map<String, Object> resultData = result.getData(); |
| | | boolean isContinuousTask = resultData != null && "任务已启动,定时器在后台运行中".equals(resultData.get("message")); |
| | | |
| | | // 如果是持续运行的任务(定时器模式),保持 RUNNING 状态,不更新为 COMPLETED |
| | | if (isContinuousTask && result.isSuccess()) { |
| | | log.info("任务已启动定时器,保持运行状态: taskId={}, message={}", |
| | | task.getTaskId(), resultData.get("message")); |
| | | task.setResultData(writeJson(resultData)); |
| | | updateById(task); |
| | | // 通知任务状态(保持 RUNNING) |
| | | notificationService.notifyTaskStatus(task); |
| | | return; |
| | | } |
| | | |
| | | // 更新任务结果(非持续运行的任务) |
| | | task.setStatus(result.isSuccess() ? MultiDeviceTask.Status.COMPLETED.name() : MultiDeviceTask.Status.FAILED.name()); |
| | | task.setErrorMessage(result.isSuccess() ? null : result.getMessage()); |
| | | task.setEndTime(new Date()); |
| | | task.setResultData(writeJson(result.getData())); |
| | | task.setResultData(writeJson(resultData)); |
| | | updateById(task); |
| | | |
| | | // 通知任务完成 |
| | |
| | | if (!MultiDeviceTask.Status.RUNNING.name().equals(task.getStatus())) { |
| | | return false; |
| | | } |
| | | taskExecutionEngine.requestTaskCancellation(taskId); |
| | | task.setStatus(MultiDeviceTask.Status.CANCELLED.name()); |
| | | task.setEndTime(new Date()); |
| | | return updateById(task); |
| | | boolean updated = updateById(task); |
| | | if (updated) { |
| | | notificationService.notifyTaskStatus(task); |
| | | } |
| | | return updated; |
| | | } |
| | | |
| | | @Override |