| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.Future; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.TimeoutException; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class FakeProcess implements MainProcessPluginApi { |
| | | |
| | | private static Map<Integer,Long> stationStayTimeMap = new HashMap<>(); |
| | | private static String enableFake = "N"; |
| | | private static String fakeRealTaskRequestWms = "N"; |
| | | private static String fakeGenerateInTask = "Y"; |
| | | private static String fakeGenerateOutTask = "Y"; |
| | | private static final long METHOD_TIMEOUT_MS = 5000; // 5秒超时 |
| | | private static final ExecutorService timeoutExecutor = Executors.newCachedThreadPool(); |
| | | |
| | | private Thread asyncRunThread = null; |
| | | private static Map<Integer,Long> stationStayTimeMap = new ConcurrentHashMap<>(); |
| | | private static volatile String enableFake = "N"; |
| | | private static volatile String fakeRealTaskRequestWms = "N"; |
| | | private static volatile String fakeGenerateInTask = "Y"; |
| | | private static volatile String fakeGenerateOutTask = "Y"; |
| | | |
| | | private Thread asyncFakeRunThread = null; |
| | | |
| | | @Autowired |
| | |
| | | @Autowired |
| | | private DualCrnOperateProcessUtils dualCrnOperateProcessUtils; |
| | | |
| | | @Override |
| | | public void run() { |
| | | asyncRun(); |
| | | asyncFakeRun(); |
| | | |
| | | //执行堆垛机任务 |
| | | crnOperateUtils.crnIoExecute(); |
| | | //堆垛机任务执行完成-具备仿真能力 |
| | | crnIoExecuteFinish(); |
| | | //执行输送站点入库任务 |
| | | stationOperateProcessUtils.stationInExecute(); |
| | | //执行输送站点出库任务 |
| | | stationOperateProcessUtils.stationOutExecute(); |
| | | //检测输送站点出库任务执行完成 |
| | | stationOperateProcessUtils.stationOutExecuteFinish(); |
| | | |
| | | //执行双工位堆垛机任务 |
| | | dualCrnOperateProcessUtils.dualCrnIoExecute(); |
| | | //双工位堆垛机任务执行完成 |
| | | dualCrnOperateProcessUtils.dualCrnIoExecuteFinish(); |
| | | /** |
| | | * 带超时保护执行方法 |
| | | * @param taskName 任务名称(用于日志) |
| | | * @param task 要执行的任务 |
| | | */ |
| | | private void executeWithTimeout(String taskName, Runnable task) { |
| | | Future<?> future = timeoutExecutor.submit(task); |
| | | try { |
| | | future.get(METHOD_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| | | } catch (TimeoutException e) { |
| | | // 使用 cancel(false) 不发送中断信号,避免 RedisCommandInterruptedException |
| | | // 任务会在后台继续执行直到完成,但主循环不会等待 |
| | | future.cancel(false); |
| | | News.error("[WCS Warning] 方法执行超时,主循环已跳过: {}, 超时时间: {}ms (任务仍在后台运行)", taskName, METHOD_TIMEOUT_MS); |
| | | } catch (Exception e) { |
| | | News.error("[WCS Error] 方法执行异常: {}, 异常: {}", taskName, e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | public void asyncRun() { |
| | | if (asyncRunThread != null) { |
| | | return; |
| | | } |
| | | @Override |
| | | public void run() { |
| | | long startTime = System.currentTimeMillis(); |
| | | asyncFakeRun(); |
| | | |
| | | asyncRunThread = new Thread(() -> { |
| | | while (!Thread.currentThread().isInterrupted()) { |
| | | try { |
| | | // 系统运行状态判断 |
| | | if (!SystemProperties.WCS_RUNNING_STATUS.get()) { |
| | | continue; |
| | | } |
| | | //请求生成入库任务 |
| | | executeWithTimeout("generateStoreWrkFile", this::generateStoreWrkFile); |
| | | |
| | | //请求生成入库任务 |
| | | generateStoreWrkFile(); |
| | | //执行堆垛机任务 |
| | | executeWithTimeout("crnIoExecute", crnOperateUtils::crnIoExecute); |
| | | //堆垛机任务执行完成-具备仿真能力 |
| | | executeWithTimeout("crnIoExecuteFinish", this::crnIoExecuteFinish); |
| | | //执行输送站点入库任务 |
| | | executeWithTimeout("stationInExecute", stationOperateProcessUtils::stationInExecute); |
| | | //执行输送站点出库任务 |
| | | executeWithTimeout("stationOutExecute", stationOperateProcessUtils::stationOutExecute); |
| | | //检测输送站点出库任务执行完成 |
| | | executeWithTimeout("stationOutExecuteFinish", stationOperateProcessUtils::stationOutExecuteFinish); |
| | | |
| | | // 间隔 |
| | | Thread.sleep(50); |
| | | } catch (InterruptedException ie) { |
| | | Thread.currentThread().interrupt(); |
| | | break; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | asyncRunThread.setName("asyncRunProcess"); |
| | | asyncRunThread.setDaemon(true); |
| | | asyncRunThread.start(); |
| | | //执行双工位堆垛机任务 |
| | | executeWithTimeout("dualCrnIoExecute", dualCrnOperateProcessUtils::dualCrnIoExecute); |
| | | //双工位堆垛机任务执行完成 |
| | | executeWithTimeout("dualCrnIoExecuteFinish", dualCrnOperateProcessUtils::dualCrnIoExecuteFinish); |
| | | |
| | | News.info("[WCS Debug] 主线程Run执行完成,耗时:{}ms", System.currentTimeMillis() - startTime); |
| | | } |
| | | |
| | | public void asyncFakeRun() { |
| | |
| | | } |
| | | |
| | | //检测入库站是否有任务生成,并仿真生成模拟入库站点数据 |
| | | private synchronized void checkInStationHasTask() { |
| | | private void checkInStationHasTask() { |
| | | if (!enableFake.equals("Y")) { |
| | | return; |
| | | } |
| | |
| | | } |
| | | |
| | | //生成仿真模拟入库任务 |
| | | private synchronized void generateFakeInTask() { |
| | | private void generateFakeInTask() { |
| | | if (!enableFake.equals("Y")) { |
| | | return; |
| | | } |
| | |
| | | } |
| | | |
| | | //生成仿真模拟出库任务 |
| | | private synchronized void generateFakeOutTask() { |
| | | private void generateFakeOutTask() { |
| | | if (!enableFake.equals("Y")) { |
| | | return; |
| | | } |
| | |
| | | * 入库站,根据条码扫描生成入库工作档 |
| | | */ |
| | | public synchronized void generateStoreWrkFile() { |
| | | if (fakeRealTaskRequestWms.equals("N")) { |
| | | return; |
| | | } |
| | | |
| | | List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>()); |
| | | for (BasDevp basDevp : basDevps) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo()); |
| | | if(stationThread == null){ |
| | | continue; |
| | | try { |
| | | if (fakeRealTaskRequestWms.equals("N")) { |
| | | return; |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap(); |
| | | Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key); |
| | | if (systemConfigMapObj == null) { |
| | | return; |
| | | } |
| | | HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj; |
| | | |
| | | List<StationObjModel> list = basDevp.getBarcodeStationList$(); |
| | | for (StationObjModel entity : list) { |
| | | Integer stationId = entity.getStationId(); |
| | | if(!stationMap.containsKey(stationId)){ |
| | | 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; |
| | | } |
| | | |
| | | List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>()); |
| | | for (BasDevp basDevp : basDevps) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo()); |
| | | if (stationThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | StationProtocol stationProtocol = stationMap.get(stationId); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap(); |
| | | |
| | | //满足自动、有物、有工作号,生成入库数据 |
| | | if (stationProtocol.isAutoing() |
| | | && stationProtocol.isLoading() |
| | | && stationProtocol.getTaskNo() > 0 |
| | | ) { |
| | | if (Cools.isEmpty(stationProtocol.getBarcode())) { |
| | | List<StationObjModel> list = basDevp.getBarcodeStationList$(); |
| | | for (StationObjModel entity : list) { |
| | | Integer stationId = entity.getStationId(); |
| | | if (!stationMap.containsKey(stationId)) { |
| | | continue; |
| | | } |
| | | |
| | | //检测任务是否生成 |
| | | List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode())); |
| | | if (!wrkMasts.isEmpty()) { |
| | | StationProtocol stationProtocol = stationMap.get(stationId); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | Object lock = redisUtil.get(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId); |
| | | if (lock != null) { |
| | | continue; |
| | | } |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | |
| | | String response = wmsOperateUtils.applyInTask(stationProtocol.getBarcode(), stationProtocol.getStationId(), stationProtocol.getPalletHeight()); |
| | | if (response == null) { |
| | | News.error("请求WMS入库接口失败,接口未响应!!!response:{}", 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(stationProtocol.getBarcode()); |
| | | WrkMast wrkMast = commonService.createInTask(taskParam); |
| | | |
| | | StationCommand command = stationThread.getCommand(StationCommandType.WRITE_INFO, wrkMast.getWrkNo(), stationId, stationId, 0); |
| | | if(command == null){ |
| | | News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败"); |
| | | //满足自动、有物、有工作号,生成入库数据 |
| | | if (stationProtocol.isAutoing() |
| | | && stationProtocol.isLoading() |
| | | && stationProtocol.getTaskNo() > 0 |
| | | ) { |
| | | if (Cools.isEmpty(stationProtocol.getBarcode())) { |
| | | continue; |
| | | } |
| | | MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command)); |
| | | |
| | | //检测任务是否生成 |
| | | 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; |
| | | } |
| | | redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2); |
| | | |
| | | String response = wmsOperateUtils.applyInTask(stationProtocol.getBarcode(), stationProtocol.getStationId(), stationProtocol.getPalletHeight()); |
| | | if (response == null) { |
| | | News.error("请求WMS入库接口失败,接口未响应!!!response:{}", 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(stationProtocol.getBarcode()); |
| | | WrkMast wrkMast = commonService.createInTask(taskParam); |
| | | |
| | | StationCommand command = stationThread.getCommand(StationCommandType.WRITE_INFO, wrkMast.getWrkNo(), stationId, stationId, 0); |
| | | if (command == null) { |
| | | News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败"); |
| | | continue; |
| | | } |
| | | MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | //计算所有站点停留时间 |
| | | public synchronized void calcAllStationStayTime() { |
| | | public void calcAllStationStayTime() { |
| | | List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>()); |
| | | for (BasDevp basDevp : basDevps) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo()); |
| | |
| | | } |
| | | |
| | | //检测出库站点停留是否超时 |
| | | public synchronized void checkOutStationStayTimeOut() { |
| | | public void checkOutStationStayTimeOut() { |
| | | List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>()); |
| | | for (BasDevp basDevp : basDevps) { |
| | | List<StationObjModel> outStationList = basDevp.getOutStationList$(); |
| | |
| | | } |
| | | |
| | | //检测入库站点堆垛机是否取走货物 |
| | | public synchronized void checkInStationCrnTake() { |
| | | public void checkInStationCrnTake() { |
| | | List<BasCrnp> basCrnps = basCrnpService.selectList(new EntityWrapper<>()); |
| | | for (BasCrnp basCrnp : basCrnps) { |
| | | List<StationObjModel> inStationList = basCrnp.getInStationList$(); |
| | |
| | | } |
| | | |
| | | //堆垛机任务执行完成 |
| | | public synchronized void crnIoExecuteFinish() { |
| | | public void crnIoExecuteFinish() { |
| | | List<BasCrnp> basCrnps = basCrnpService.selectList(new EntityWrapper<>()); |
| | | for (BasCrnp basCrnp : basCrnps) { |
| | | CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo()); |