| | |
| | | import com.zy.core.News; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.enums.StationCommandType; |
| | | import com.zy.core.enums.WrkIoType; |
| | | import com.zy.core.enums.*; |
| | | import com.zy.core.model.StationObjModel; |
| | | import com.zy.core.model.Task; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.model.protocol.StationProtocol; |
| | | import com.zy.core.plugin.api.MainProcessPluginApi; |
| | | import com.zy.core.task.MainProcessLane; |
| | | import com.zy.core.task.MainProcessTaskSubmitter; |
| | | import com.zy.core.thread.StationThread; |
| | | import com.zy.core.utils.CrnOperateProcessUtils; |
| | | import com.zy.core.utils.DualCrnOperateProcessUtils; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | @Slf4j |
| | | @Component |
| | | public class XiaosongProcess implements MainProcessPluginApi { |
| | | private static final long DISPATCH_INTERVAL_MS = 200L; |
| | | private static final long MAINTENANCE_INTERVAL_MS = 500L; |
| | | |
| | | @Autowired |
| | | private CrnOperateProcessUtils crnOperateUtils; |
| | |
| | | private WmsOperateUtils wmsOperateUtils; |
| | | @Autowired |
| | | private DualCrnOperateProcessUtils dualCrnOperateProcessUtils; |
| | | @Autowired |
| | | private MainProcessTaskSubmitter mainProcessTaskSubmitter; |
| | | |
| | | @Override |
| | | public void run() { |
| | | //检测入库站是否有任务生成,并启动入库 |
| | | checkInStationHasTask(); |
| | | //请求生成入库任务 |
| | | generateStoreWrkFile(); |
| | | // 检测入库站是否有任务生成,并按站点 lane 异步启动入库 |
| | | stationOperateProcessUtils.submitStationEnableInTasks(DISPATCH_INTERVAL_MS); |
| | | // 请求生成入库任务,放入独立 lane,避免拖慢其他主流程方法 |
| | | submitGenerateStoreWrkFileTask(); |
| | | |
| | | //执行堆垛机任务 |
| | | crnOperateUtils.crnIoExecute(); |
| | | //堆垛机任务执行完成-具备仿真能力 |
| | | crnOperateUtils.crnIoExecuteFinish(); |
| | | //执行输送站点入库任务 |
| | | stationOperateProcessUtils.stationInExecute(); |
| | | //执行堆垛机输送站点出库任务 |
| | | stationOperateProcessUtils.crnStationOutExecute(); |
| | | //执行双工位堆垛机输送站点出库任务 |
| | | stationOperateProcessUtils.dualCrnStationOutExecute(); |
| | | //检测输送站点出库任务执行完成 |
| | | stationOperateProcessUtils.stationOutExecuteFinish(); |
| | | //检测输送站点是否运行堵塞 |
| | | stationOperateProcessUtils.checkStationRunBlock(); |
| | | // 执行堆垛机任务 |
| | | crnOperateUtils.submitCrnIoTasks(DISPATCH_INTERVAL_MS); |
| | | // 堆垛机任务执行完成 |
| | | crnOperateUtils.submitCrnIoExecuteFinishTasks(DISPATCH_INTERVAL_MS); |
| | | // 执行输送站点入库任务 |
| | | stationOperateProcessUtils.submitStationInTasks(DISPATCH_INTERVAL_MS); |
| | | // 执行堆垛机输送站点出库任务 |
| | | stationOperateProcessUtils.submitCrnStationOutTasks(DISPATCH_INTERVAL_MS); |
| | | // 执行双工位堆垛机输送站点出库任务 |
| | | stationOperateProcessUtils.submitDualCrnStationOutTasks(DISPATCH_INTERVAL_MS); |
| | | // 检测输送站点出库任务执行完成 |
| | | stationOperateProcessUtils.submitStationOutExecuteFinishTasks(DISPATCH_INTERVAL_MS); |
| | | // 检测任务转完成 |
| | | stationOperateProcessUtils.submitCheckTaskToCompleteTasks(DISPATCH_INTERVAL_MS); |
| | | // 检测输送站点是否运行堵塞 |
| | | stationOperateProcessUtils.submitCheckStationRunBlockTasks(MAINTENANCE_INTERVAL_MS); |
| | | |
| | | // 执行双工位堆垛机任务 |
| | | dualCrnOperateProcessUtils.dualCrnIoExecute(); |
| | | dualCrnOperateProcessUtils.submitDualCrnIoTasks(DISPATCH_INTERVAL_MS); |
| | | // 双工位堆垛机任务执行完成 |
| | | dualCrnOperateProcessUtils.dualCrnIoExecuteFinish(); |
| | | |
| | | dualCrnOperateProcessUtils.submitDualCrnIoExecuteFinishTasks(DISPATCH_INTERVAL_MS); |
| | | } |
| | | |
| | | /** |
| | | * 请求生成入库任务 |
| | | * 入库站,根据条码扫描生成入库工作档 |
| | | */ |
| | | public synchronized void generateStoreWrkFile() { |
| | | try { |
| | | Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key); |
| | | if (systemConfigMapObj == null) { |
| | | return; |
| | | } |
| | | HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj; |
| | | private void submitGenerateStoreWrkFileTask() { |
| | | mainProcessTaskSubmitter.submitSerialTask( |
| | | MainProcessLane.GENERATE_STORE, |
| | | "submitGenerateStoreWrkFileTask", |
| | | DISPATCH_INTERVAL_MS, |
| | | this::submitGenerateStoreWrkFileTasksInternal |
| | | ); |
| | | } |
| | | |
| | | int conveyorStationTaskLimit = 30; |
| | | String conveyorStationTaskLimitStr = systemConfigMap.get("conveyorStationTaskLimit"); |
| | | if (conveyorStationTaskLimitStr != null) { |
| | | conveyorStationTaskLimit = Integer.parseInt(conveyorStationTaskLimitStr); |
| | | } |
| | | int currentStationTaskCount = stationOperateProcessUtils.getCurrentStationTaskCount(); |
| | | if (currentStationTaskCount > conveyorStationTaskLimit) { |
| | | News.error("输送站点任务已达到上限,上限值:{},站点任务数:{}", conveyorStationTaskLimit, currentStationTaskCount); |
| | | return; |
| | | private void submitGenerateStoreWrkFileTasksInternal() { |
| | | List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>()); |
| | | for (final BasDevp basDevp : basDevps) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo()); |
| | | if (stationThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>()); |
| | | for (BasDevp basDevp : basDevps) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo()); |
| | | if (stationThread == null) { |
| | | Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap(); |
| | | if (stationMap == null || stationMap.isEmpty()) { |
| | | continue; |
| | | } |
| | | |
| | | List<StationObjModel> list = basDevp.getBarcodeStationList$(); |
| | | for (final StationObjModel entity : list) { |
| | | Integer stationId = entity == null ? null : entity.getStationId(); |
| | | if (stationId == null || !stationMap.containsKey(stationId)) { |
| | | continue; |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap(); |
| | | |
| | | List<StationObjModel> list = basDevp.getBarcodeStationList$(); |
| | | for (StationObjModel entity : list) { |
| | | Integer stationId = entity.getStationId(); |
| | | if (!stationMap.containsKey(stationId)) { |
| | | continue; |
| | | } |
| | | |
| | | StationProtocol stationProtocol = stationMap.get(stationId); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | if (stationProtocol.isAutoing() |
| | | && stationProtocol.isLoading() |
| | | && stationProtocol.isInEnable() |
| | | && stationProtocol.getTaskNo() > 0) { |
| | | if (Cools.isEmpty(stationProtocol.getBarcode())) { |
| | | continue; |
| | | } |
| | | |
| | | // 检测任务是否生成 |
| | | List<WrkMast> wrkMasts = wrkMastService |
| | | .selectList(new EntityWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode())); |
| | | if (!wrkMasts.isEmpty()) { |
| | | continue; |
| | | } |
| | | |
| | | Object lock = redisUtil.get(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId); |
| | | if (lock != null) { |
| | | continue; |
| | | } |
| | | |
| | | String barcode = stationProtocol.getBarcode(); |
| | | Integer stationIdVal = stationProtocol.getStationId(); |
| | | |
| | | // 1. 首先查询是否有已完成的异步响应 |
| | | String response = wmsOperateUtils.queryAsyncInTaskResponse(barcode, stationIdVal); |
| | | |
| | | if (response != null) { |
| | | // 2. 有响应结果,处理响应 |
| | | if (response.equals("FAILED") || response.startsWith("ERROR:")) { |
| | | // 请求失败,重新发起异步请求 |
| | | News.error("WMS入库请求失败,重新发起请求,barcode={},stationId={},response={}", barcode, |
| | | stationIdVal, response); |
| | | wmsOperateUtils.applyInTaskAsync(barcode, stationIdVal, |
| | | stationProtocol.getPalletHeight()); |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | stationProtocol.setSystemWarning("请求入库失败,WMS返回=" + response); |
| | | continue; |
| | | } |
| | | |
| | | // 解析响应 |
| | | JSONObject jsonObject = JSON.parseObject(response); |
| | | if (jsonObject.getInteger("code").equals(200)) { |
| | | StartupDto dto = jsonObject.getObject("data", StartupDto.class); |
| | | |
| | | CreateInTaskParam taskParam = new CreateInTaskParam(); |
| | | taskParam.setTaskNo(dto.getTaskNo()); |
| | | taskParam.setLocNo(dto.getLocNo()); |
| | | taskParam.setTaskPri(dto.getTaskPri()); |
| | | taskParam.setBarcode(barcode); |
| | | WrkMast wrkMast = commonService.createInTask(taskParam); |
| | | stationProtocol.setSystemWarning(""); |
| | | } else { |
| | | // 接口返回非200,重新发起请求 |
| | | News.error("WMS入库接口返回非200,重新发起请求,barcode={},stationId={},response={}", barcode, |
| | | stationIdVal, response); |
| | | wmsOperateUtils.applyInTaskAsync(barcode, stationIdVal, |
| | | stationProtocol.getPalletHeight()); |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | stationProtocol.setSystemWarning("请求入库失败,WMS返回=" + response); |
| | | } |
| | | } else { |
| | | // 3. 没有响应结果,检查是否有请求正在进行中 |
| | | if (!wmsOperateUtils.isAsyncRequestInProgress(barcode, stationIdVal)) { |
| | | // 没有请求进行中,发起新的异步请求 |
| | | News.info("发起异步WMS入库请求,barcode={},stationId={}", barcode, stationIdVal); |
| | | wmsOperateUtils.applyInTaskAsync(barcode, stationIdVal, |
| | | stationProtocol.getPalletHeight()); |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | stationProtocol.setSystemWarning("请求入库失败,WMS无返回"); |
| | | mainProcessTaskSubmitter.submitKeyedSerialTask( |
| | | MainProcessLane.GENERATE_STORE, |
| | | stationId, |
| | | "generateStoreWrkFile", |
| | | DISPATCH_INTERVAL_MS, |
| | | new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | generateStoreWrkFile(basDevp, entity); |
| | | } |
| | | } |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void generateStoreWrkFile(BasDevp basDevp, StationObjModel entity) { |
| | | if (basDevp == null || entity == null || entity.getStationId() == null) { |
| | | return; |
| | | } |
| | | |
| | | Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key); |
| | | if (systemConfigMapObj == null) { |
| | | return; |
| | | } |
| | | HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj; |
| | | |
| | | int conveyorStationTaskLimit = 30; |
| | | String conveyorStationTaskLimitStr = systemConfigMap.get("conveyorStationTaskLimit"); |
| | | if (conveyorStationTaskLimitStr != null) { |
| | | conveyorStationTaskLimit = Integer.parseInt(conveyorStationTaskLimitStr); |
| | | } |
| | | int currentStationTaskCount = stationOperateProcessUtils.getCurrentStationTaskCount(); |
| | | if (currentStationTaskCount > conveyorStationTaskLimit) { |
| | | News.error("输送站点任务已达到上限,上限值:{},站点任务数:{}", conveyorStationTaskLimit, currentStationTaskCount); |
| | | return; |
| | | } |
| | | |
| | | Integer stationId = entity.getStationId(); |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo()); |
| | | if (stationThread == null) { |
| | | return; |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap(); |
| | | if (stationMap == null || !stationMap.containsKey(stationId)) { |
| | | return; |
| | | } |
| | | |
| | | StationProtocol stationProtocol = stationMap.get(stationId); |
| | | if (stationProtocol == null) { |
| | | return; |
| | | } |
| | | |
| | | if (!(stationProtocol.isAutoing() |
| | | && stationProtocol.isLoading() |
| | | && stationProtocol.isInEnable() |
| | | && stationProtocol.getTaskNo() > 0)) { |
| | | logTraceLimited("generate_wait_condition_" + stationId, 5, |
| | | "[WCS Trace][条码站入库] 条码站暂未满足生成条件。stationId={},autoing={},loading={},inEnable={},enableIn={},taskNo={},targetStaNo={},barcode={},error={},warning={}", |
| | | stationId, stationProtocol.isAutoing(), stationProtocol.isLoading(), |
| | | stationProtocol.isInEnable(), stationProtocol.isEnableIn(), stationProtocol.getTaskNo(), |
| | | stationProtocol.getTargetStaNo(), stationProtocol.getBarcode(), |
| | | stationProtocol.getError(), stationProtocol.getSystemWarning()); |
| | | return; |
| | | } |
| | | if (Cools.isEmpty(stationProtocol.getBarcode())) { |
| | | logTraceLimited("generate_wait_barcode_" + stationId, 5, |
| | | "[WCS Trace][条码站入库] 条码站已到位但还没有条码。stationId={},taskNo={},targetStaNo={},palletHeight={},weight={},error={},warning={}", |
| | | stationId, stationProtocol.getTaskNo(), stationProtocol.getTargetStaNo(), |
| | | stationProtocol.getPalletHeight(), stationProtocol.getWeight(), |
| | | stationProtocol.getError(), stationProtocol.getSystemWarning()); |
| | | return; |
| | | } |
| | | |
| | | if (stationProtocol.getError() > 0) { |
| | | Object backLock = redisUtil.get(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getTaskNo()); |
| | | if (backLock != null) { |
| | | return; |
| | | } |
| | | StationObjModel backStation = entity.getBackStation(); |
| | | StationCommand command = stationThread.getCommand(StationCommandType.MOVE, commonService.getWorkNo(WrkIoType.STATION_BACK.id), stationId, backStation.getStationId(), 0); |
| | | if (command == null) { |
| | | News.taskInfo(stationProtocol.getTaskNo(), "{}工作,获取输送线命令失败", stationProtocol.getTaskNo()); |
| | | return; |
| | | } |
| | | MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command)); |
| | | News.taskInfo(stationProtocol.getTaskNo(), "{}扫码异常,已退回至{}", backStation.getStationId()); |
| | | redisUtil.set(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getTaskNo(), "lock", 10); |
| | | return; |
| | | } |
| | | |
| | | List<WrkMast> wrkMasts = wrkMastService |
| | | .selectList(new EntityWrapper<WrkMast>() |
| | | .eq("barcode", stationProtocol.getBarcode())); |
| | | if (!wrkMasts.isEmpty()) { |
| | | WrkMast firstWrkMast = wrkMasts.get(0); |
| | | News.info("[WCS Debug][条码站入库] 条码已有工作档,跳过生成。stationId={},barcode={},当前站点taskNo={},已存在数量={},首个工作号={},状态={},locNo={}", |
| | | stationId, stationProtocol.getBarcode(), stationProtocol.getTaskNo(), |
| | | wrkMasts.size(), firstWrkMast.getWrkNo(), firstWrkMast.getWrkSts(), |
| | | firstWrkMast.getLocNo()); |
| | | for (WrkMast wrkMast : wrkMasts) { |
| | | if (wrkMast.getIoType().equals(WrkIoType.OUT.id)) { |
| | | if (wrkMast.getWrkSts().equals(WrkStsType.STATION_RUN.sts) || wrkMast.getWrkSts().equals(WrkStsType.STATION_RUN_COMPLETE.sts)) { |
| | | wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts); |
| | | wrkMast.setModiTime(new Date()); |
| | | wrkMastService.updateById(wrkMast); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return; |
| | | } |
| | | |
| | | Object generateLock = redisUtil.get(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId); |
| | | if (generateLock != null) { |
| | | logTraceLimited("generate_wait_lock_" + stationId, 3, |
| | | "[WCS Trace][条码站入库] 条码站生成任务命中限流锁。stationId={},taskNo={},barcode={},warning={}", |
| | | stationId, stationProtocol.getTaskNo(), stationProtocol.getBarcode(), stationProtocol.getSystemWarning()); |
| | | return; |
| | | } |
| | | |
| | | String barcode = stationProtocol.getBarcode(); |
| | | Integer stationIdVal = stationProtocol.getStationId(); |
| | | |
| | | long applyStartMs = System.currentTimeMillis(); |
| | | News.info("[WCS Debug][条码站入库] 准备请求WMS生成入库任务。stationId={},barcode={},stationTaskNo={},palletHeight={},isInEnable={}", |
| | | stationIdVal, barcode, stationProtocol.getTaskNo(), |
| | | stationProtocol.getPalletHeight(), stationProtocol.isInEnable()); |
| | | String response = wmsOperateUtils.applyInTask(barcode, stationIdVal, |
| | | stationProtocol.getPalletHeight()); |
| | | News.info("[WCS Debug][条码站入库] WMS生成入库任务返回。stationId={},barcode={},stationTaskNo={},costMs={},response={}", |
| | | stationIdVal, barcode, stationProtocol.getTaskNo(), |
| | | System.currentTimeMillis() - applyStartMs, response); |
| | | if (response == null) { |
| | | News.error("WMS入库同步请求失败,接口未响应,barcode={},stationId={}", barcode, stationIdVal); |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | stationProtocol.setSystemWarning("请求入库失败,WMS无返回"); |
| | | return; |
| | | } |
| | | |
| | | JSONObject jsonObject = JSON.parseObject(response); |
| | | Integer code = jsonObject.getInteger("code"); |
| | | if (code != null && code.equals(200)) { |
| | | StartupDto dto = jsonObject.getObject("data", StartupDto.class); |
| | | if (dto == null || Cools.isEmpty(dto.getLocNo())) { |
| | | News.warn("[WCS Debug][条码站入库] WMS返回成功但库位为空,等待下一轮生成。stationId={},barcode={},stationTaskNo={},response={}", |
| | | stationIdVal, barcode, stationProtocol.getTaskNo(), response); |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | stationProtocol.setSystemWarning("WMS已返回任务但库位为空"); |
| | | return; |
| | | } |
| | | |
| | | CreateInTaskParam taskParam = new CreateInTaskParam(); |
| | | taskParam.setTaskNo(dto.getTaskNo()); |
| | | taskParam.setLocNo(dto.getLocNo()); |
| | | taskParam.setTaskPri(dto.getTaskPri()); |
| | | taskParam.setBarcode(barcode); |
| | | News.info("[WCS Debug][条码站入库] 准备创建入库工作档。stationId={},barcode={},wmsTaskNo={},locNo={},taskPri={}", |
| | | stationIdVal, barcode, dto.getTaskNo(), dto.getLocNo(), dto.getTaskPri()); |
| | | WrkMast wrkMast = commonService.createInTask(taskParam); |
| | | News.info("[WCS Debug][条码站入库] 入库工作档创建完成。stationId={},barcode={},wmsTaskNo={},wrkNo={},locNo={}", |
| | | stationIdVal, barcode, dto.getTaskNo(), |
| | | wrkMast == null ? null : wrkMast.getWrkNo(), dto.getLocNo()); |
| | | stationProtocol.setSystemWarning(""); |
| | | } else { |
| | | News.error("WMS入库同步请求失败,接口返回Code异常,barcode={},stationId={},response={}", |
| | | barcode, stationIdVal, response); |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | stationProtocol.setSystemWarning("请求入库失败,WMS返回=" + response); |
| | | } |
| | | } |
| | | |
| | | private void logTraceLimited(String lockKey, int seconds, String format, Object... arguments) { |
| | | String redisKey = RedisKeyType.LOG_LIMIT.key + "wcs_trace_" + lockKey; |
| | | try { |
| | | Object lock = redisUtil.get(redisKey); |
| | | if (lock != null) { |
| | | return; |
| | | } |
| | | redisUtil.set(redisKey, "lock", seconds); |
| | | } catch (Exception e) { |
| | | // 诊断日志不能影响主流程。 |
| | | } |
| | | News.info(format, arguments); |
| | | } |
| | | |
| | | //检测入库站是否有任务生成,并启动入库 |