| | |
| | | // 只有在任务已开始执行(有任务记录)时才检查MES确认 |
| | | DevicePlcVO.OperationResult mesResult = null; |
| | | try { |
| | | mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap()); |
| | | Map<String, Object> confirmParams = new HashMap<>(); |
| | | confirmParams.put("_taskContext", context); |
| | | mesResult = handler.execute(device, "checkMesConfirm", confirmParams); |
| | | } catch (Exception e) { |
| | | log.warn("进片大车设备检查MES确认状态异常: taskId={}, deviceId={}, error={}", |
| | | task.getTaskId(), device.getId(), e.getMessage()); |
| | |
| | | // 检查是否有已处理的玻璃信息(从大理片笼来的) |
| | | List<String> processedGlassIds = getProcessedGlassIds(context); |
| | | boolean isRunning = TaskStepDetail.Status.RUNNING.name().equals(step.getStatus()); |
| | | boolean isCompleted = TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus()); |
| | | |
| | | // 获取已出片的玻璃ID列表(在方法开始处声明,避免重复定义) |
| | | List<String> outboundGlassIds = getOutboundGlassIds(context); |
| | | |
| | | // 如果步骤已完成,检查是否所有初始玻璃都已出片 |
| | | if (isCompleted) { |
| | | @SuppressWarnings("unchecked") |
| | | List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds"); |
| | | |
| | | // 如果还有未出片的玻璃,重置步骤状态为RUNNING,继续等待 |
| | | if (initialGlassIds != null && !initialGlassIds.isEmpty() |
| | | && (outboundGlassIds == null || !outboundGlassIds.containsAll(initialGlassIds))) { |
| | | log.info("出片大车步骤已完成,但还有未出片的玻璃,重置为RUNNING继续等待: taskId={}, deviceId={}, initialCount={}, outboundCount={}", |
| | | task.getTaskId(), device.getId(), |
| | | initialGlassIds.size(), |
| | | outboundGlassIds != null ? outboundGlassIds.size() : 0); |
| | | step.setStatus(TaskStepDetail.Status.RUNNING.name()); |
| | | step.setEndTime(null); // 清除结束时间 |
| | | step.setSuccessMessage("等待剩余玻璃出片"); |
| | | taskStepDetailMapper.updateById(step); |
| | | notificationService.notifyStepUpdate(task.getTaskId(), step); |
| | | // 继续执行后续逻辑,检查是否有新的已处理玻璃 |
| | | } else { |
| | | // 所有玻璃都已出片,保持完成状态 |
| | | log.debug("出片大车所有玻璃都已出片: taskId={}, deviceId={}, initialCount={}, outboundCount={}", |
| | | task.getTaskId(), device.getId(), |
| | | initialGlassIds != null ? initialGlassIds.size() : 0, |
| | | outboundGlassIds != null ? outboundGlassIds.size() : 0); |
| | | return; |
| | | } |
| | | } |
| | | |
| | | // 如果没有已处理玻璃,则不应主动把步骤拉到RUNNING,只保持已运行状态 |
| | | if (CollectionUtils.isEmpty(processedGlassIds)) { |
| | | if (isRunning) { |
| | | if (isRunning || isCompleted) { |
| | | // 已经在运行的情况下,继续轮询MES任务/确认,避免错过确认 |
| | | DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType()); |
| | | if (handler != null) { |
| | |
| | | DevicePlcVO.OperationResult mesTaskResult = null; |
| | | try { |
| | | mesTaskResult = handler.execute(device, "checkMesTask", Collections.emptyMap()); |
| | | if (mesTaskResult != null && Boolean.TRUE.equals(mesTaskResult.getSuccess())) { |
| | | log.info("出片大车设备已检查MES任务并开始执行: taskId={}, deviceId={}, message={}", |
| | | task.getTaskId(), device.getId(), mesTaskResult.getMessage()); |
| | | if (mesTaskResult != null) { |
| | | if (Boolean.TRUE.equals(mesTaskResult.getSuccess())) { |
| | | log.info("出片大车设备已检查MES任务并开始执行: taskId={}, deviceId={}, message={}", |
| | | task.getTaskId(), device.getId(), mesTaskResult.getMessage()); |
| | | } else { |
| | | log.debug("出片大车设备检查MES任务,等待中: taskId={}, deviceId={}, message={}", |
| | | task.getTaskId(), device.getId(), mesTaskResult.getMessage()); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("出片大车设备检查MES任务异常: taskId={}, deviceId={}, error={}", |
| | |
| | | // 然后检查MES确认状态(只有在任务已开始执行时才检查) |
| | | DevicePlcVO.OperationResult mesResult = null; |
| | | try { |
| | | mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap()); |
| | | Map<String, Object> checkParams = new HashMap<>(); |
| | | checkParams.put("_taskContext", context); |
| | | mesResult = handler.execute(device, "checkMesConfirm", checkParams); |
| | | } catch (Exception e) { |
| | | log.warn("出片大车设备检查MES确认状态异常: taskId={}, deviceId={}, error={}", |
| | | task.getTaskId(), device.getId(), e.getMessage()); |
| | |
| | | log.debug("出片大车设备定时器检测到已处理的玻璃信息: taskId={}, deviceId={}, glassCount={}", |
| | | task.getTaskId(), device.getId(), processedGlassIds.size()); |
| | | |
| | | // 需等待大理片笼完成全部玻璃的处理后再出片 |
| | | @SuppressWarnings("unchecked") |
| | | List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds"); |
| | | if (!CollectionUtils.isEmpty(initialGlassIds) |
| | | && !processedGlassIds.containsAll(initialGlassIds)) { |
| | | // 部分玻璃尚未由大理片笼处理完成,保持等待 |
| | | deviceCoordinationService.syncDeviceStatus(device, |
| | | DeviceCoordinationService.DeviceStatus.WAITING, context); |
| | | if (!TaskStepDetail.Status.PENDING.name().equals(step.getStatus())) { |
| | | step.setStatus(TaskStepDetail.Status.PENDING.name()); |
| | | step.setSuccessMessage("等待大理片笼处理全部玻璃后再出片"); |
| | | taskStepDetailMapper.updateById(step); |
| | | notificationService.notifyStepUpdate(task.getTaskId(), step); |
| | | // 过滤出还未出片的玻璃(支持分批出片) |
| | | // 重新获取已出片的玻璃ID列表(可能在上面的逻辑中已更新) |
| | | outboundGlassIds = getOutboundGlassIds(context); |
| | | List<String> glassIdsToOutbound = new ArrayList<>(); |
| | | for (String glassId : processedGlassIds) { |
| | | if (outboundGlassIds == null || !outboundGlassIds.contains(glassId)) { |
| | | glassIdsToOutbound.add(glassId); |
| | | } |
| | | log.debug("出片大车等待大理片笼完成全部玻璃: taskId={}, deviceId={}, processed={}, initial={}", |
| | | task.getTaskId(), device.getId(), processedGlassIds.size(), initialGlassIds.size()); |
| | | } |
| | | |
| | | // 如果没有需要出片的玻璃(都已经出片过了),继续等待新的已处理玻璃 |
| | | if (glassIdsToOutbound.isEmpty()) { |
| | | log.debug("出片大车已处理的玻璃都已出片,等待新的已处理玻璃: taskId={}, deviceId={}", |
| | | task.getTaskId(), device.getId()); |
| | | return; |
| | | } |
| | | |
| | | log.debug("出片大车准备出片: taskId={}, deviceId={}, 待出片数量={}, 已出片数量={}", |
| | | task.getTaskId(), device.getId(), glassIdsToOutbound.size(), |
| | | outboundGlassIds != null ? outboundGlassIds.size() : 0); |
| | | |
| | | // 执行出片操作 |
| | | Map<String, Object> checkParams = new HashMap<>(); |
| | | checkParams.put("glassIds", new ArrayList<>(processedGlassIds)); |
| | | checkParams.put("glassIds", glassIdsToOutbound); |
| | | checkParams.put("_taskContext", context); |
| | | |
| | | DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType()); |
| | |
| | | notificationService.notifyStepUpdate(task.getTaskId(), step); |
| | | } |
| | | log.debug("出片大车设备定时器执行成功: taskId={}, deviceId={}, glassCount={}", |
| | | task.getTaskId(), device.getId(), processedGlassIds.size()); |
| | | // 清空已处理的玻璃ID列表(已处理) |
| | | clearProcessedGlassIds(context); |
| | | task.getTaskId(), device.getId(), glassIdsToOutbound.size()); |
| | | // 记录已出片的玻璃ID(只记录本次出片的玻璃) |
| | | addOutboundGlassIds(context, glassIdsToOutbound); |
| | | // 从processedGlassIds中移除已出片的玻璃,保留未出片的玻璃 |
| | | processedGlassIds.removeAll(glassIdsToOutbound); |
| | | // 如果还有未出片的玻璃,不清空processedGlassIds;如果全部出片了,清空 |
| | | if (processedGlassIds.isEmpty()) { |
| | | clearProcessedGlassIds(context); |
| | | } else { |
| | | setProcessedGlassIds(context, processedGlassIds); |
| | | } |
| | | |
| | | // feedGlass成功后,先检查MES任务(checkMesTask)来开始执行任务 |
| | | DevicePlcVO.OperationResult mesTaskResult = null; |
| | |
| | | // 只有在任务已开始执行(有任务记录)时才检查MES确认 |
| | | DevicePlcVO.OperationResult mesResult = null; |
| | | try { |
| | | mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap()); |
| | | Map<String, Object> confirmParams = new HashMap<>(); |
| | | confirmParams.put("_taskContext", context); |
| | | mesResult = handler.execute(device, "checkMesConfirm", confirmParams); |
| | | } catch (Exception e) { |
| | | log.warn("出片大车设备检查MES确认状态异常: taskId={}, deviceId={}, error={}", |
| | | task.getTaskId(), device.getId(), e.getMessage()); |
| | | } |
| | | |
| | | // 对于出片大车,需要检查是否所有初始玻璃都已出片 |
| | | // 如果MES返回completed=true,但还有未出片的玻璃,则不应标记为完成 |
| | | if (mesResult != null && mesResult.getData() != null) { |
| | | Object completedFlag = mesResult.getData().get("completed"); |
| | | boolean mesCompleted = false; |
| | | if (completedFlag instanceof Boolean) { |
| | | mesCompleted = (Boolean) completedFlag; |
| | | } else if (completedFlag != null) { |
| | | mesCompleted = "true".equalsIgnoreCase(String.valueOf(completedFlag)); |
| | | } |
| | | |
| | | // 如果MES返回completed=true,检查是否所有初始玻璃都已出片 |
| | | if (mesCompleted) { |
| | | @SuppressWarnings("unchecked") |
| | | List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds"); |
| | | // 重新获取已出片的玻璃ID列表(可能在上面的逻辑中已更新) |
| | | outboundGlassIds = getOutboundGlassIds(context); |
| | | |
| | | // 如果还有未出片的玻璃,修改mesResult,将completed设为false |
| | | if (initialGlassIds != null && !initialGlassIds.isEmpty() |
| | | && (outboundGlassIds == null || !outboundGlassIds.containsAll(initialGlassIds))) { |
| | | log.debug("出片大车MES返回completed=true,但还有未出片的玻璃: taskId={}, deviceId={}, initialCount={}, outboundCount={}", |
| | | task.getTaskId(), device.getId(), |
| | | initialGlassIds.size(), |
| | | outboundGlassIds != null ? outboundGlassIds.size() : 0); |
| | | // 修改mesResult,将completed设为false,保持RUNNING状态 |
| | | Map<String, Object> modifiedData = new HashMap<>(mesResult.getData()); |
| | | modifiedData.put("completed", false); |
| | | DevicePlcVO.OperationResult modifiedResult = new DevicePlcVO.OperationResult(); |
| | | modifiedResult.setSuccess(mesResult.getSuccess()); |
| | | modifiedResult.setMessage(mesResult.getMessage()); |
| | | modifiedResult.setData(modifiedData); |
| | | mesResult = modifiedResult; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 更新步骤状态(大车设备保持RUNNING,直到MES确认完成或任务取消) |
| | |
| | | private void clearProcessedGlassIds(TaskExecutionContext context) { |
| | | if (context != null) { |
| | | context.getSharedData().put("processedGlassIds", new ArrayList<>()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取已出片的玻璃ID列表 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | private List<String> getOutboundGlassIds(TaskExecutionContext context) { |
| | | if (context == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | Object glassIds = context.getSharedData().get("outboundGlassIds"); |
| | | if (glassIds instanceof List) { |
| | | return new ArrayList<>((List<String>) glassIds); |
| | | } |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | /** |
| | | * 添加已出片的玻璃ID列表 |
| | | */ |
| | | private void addOutboundGlassIds(TaskExecutionContext context, List<String> glassIds) { |
| | | if (context != null && glassIds != null && !glassIds.isEmpty()) { |
| | | List<String> existing = getOutboundGlassIds(context); |
| | | Set<String> allOutbound = new HashSet<>(existing); |
| | | allOutbound.addAll(glassIds); |
| | | context.getSharedData().put("outboundGlassIds", new ArrayList<>(allOutbound)); |
| | | } |
| | | } |
| | | |
| | |
| | | // 然后检查MES确认状态 |
| | | DevicePlcVO.OperationResult mesResult = null; |
| | | try { |
| | | mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap()); |
| | | Map<String, Object> checkParams = new HashMap<>(); |
| | | checkParams.put("_taskContext", context); |
| | | mesResult = handler.execute(device, "checkMesConfirm", checkParams); |
| | | } catch (Exception e) { |
| | | log.warn("大车设备检查MES确认状态异常: taskId={}, deviceId={}, error={}", |
| | | task.getTaskId(), device.getId(), e.getMessage()); |