package com.mes.step; import com.mes.core.StationContext; /** * 扫描工件码步骤 * 等待用户扫描工件码,然后自动进入下一步 */ public class ScanProductStep extends AbstractStep { public ScanProductStep() { super("scan_product", "扫描工件码"); this.async = false; // 扫码完成后同步进入下一步 this.requiresUserInteraction = true; // 需要用户扫码 } @Override public boolean execute(StationContext context) { String productSn = context.getProductSn(); if (productSn == null || productSn.trim().isEmpty()) { log.warn("[{}] 工件码为空,等待用户扫码", context.getStationCode()); context.setStatusMessage("请扫描工件码", 1); return false; } // 处理36位码(取前20位) String processedSn = productSn.trim(); if (processedSn.length() == 36) { processedSn = processedSn.substring(0, 20); log.info("[{}] 36位码处理: {} -> {}", context.getStationCode(), productSn, processedSn); } // 保存处理后的工件码 context.setProductSn(processedSn); context.setState(StationContext.WorkflowState.SCANNING); context.setStatusMessage("工件码: " + processedSn, 0); log.info("[{}] 扫描工件码完成: {}", context.getStationCode(), processedSn); return true; } @Override public void onSuccess(StationContext context) { super.onSuccess(context); // 扫码成功,自动进入质量检查 } @Override public void onFailure(StationContext context, String error) { super.onFailure(context, error); context.setProductSn(null); } @Override public boolean canExecute(StationContext context) { // 只有在空闲状态才能扫码 return context.getState() == StationContext.WorkflowState.IDLE || context.getState() == StationContext.WorkflowState.ERROR; } // ========== 流程监控扩展 ========== @Override public java.util.List getConditions(StationContext context) { java.util.List conditions = new java.util.ArrayList<>(); boolean stateOk = context.getState() == StationContext.WorkflowState.IDLE || context.getState() == StationContext.WorkflowState.ERROR; conditions.add(condition("state", "工位状态: " + context.getState() + " (需要 IDLE 或 ERROR)", stateOk)); boolean hasSn = context.getProductSn() != null && !context.getProductSn().isEmpty(); conditions.add(condition("productSn", "工件码: " + (hasSn ? context.getProductSn() : "(未扫描)"), hasSn)); return conditions; } @Override public void simulateSuccess(StationContext context) { log.info("[{}] 模拟扫码成功", context.getStationCode()); // 如果没有工件码,设置一个模拟的 if (context.getProductSn() == null || context.getProductSn().isEmpty()) { String mockSn = "MOCK_" + System.currentTimeMillis(); context.setProductSn(mockSn); log.info("[{}] 设置模拟工件码: {}", context.getStationCode(), mockSn); } context.setState(StationContext.WorkflowState.SCANNING); onSuccess(context); } }