From 3e28500e76ffd71b7da90f40d1f3126c00a0e5f0 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 13 四月 2026 14:49:41 +0800
Subject: [PATCH] #站点运行优化

---
 src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java |  180 +++++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 137 insertions(+), 43 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java b/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
index 2cfd566..f3fbe1d 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
@@ -30,7 +30,10 @@
 import com.zy.core.network.DeviceConnectPool;
 import com.zy.core.network.ZyStationConnectDriver;
 import com.zy.core.network.entity.ZyStationStatusEntity;
-import com.zy.core.utils.DeviceLogRedisKeyBuilder;
+import com.zy.core.task.DeviceAsyncLogPublisher;
+import com.zy.core.thread.impl.v5.StationMoveSegmentExecutor;
+import com.zy.core.thread.support.RecentStationArrivalTracker;
+import com.zy.core.thread.support.StationErrLogSupport;
 import com.zy.system.entity.Config;
 import com.zy.system.service.ConfigService;
 import lombok.Data;
@@ -48,17 +51,24 @@
     private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d;
 
     private List<StationProtocol> statusList = new ArrayList<>();
+    private volatile List<Integer> taskNoList = new ArrayList<>();
     private DeviceConfig deviceConfig;
     private RedisUtil redisUtil;
     private ZyStationConnectDriver zyStationConnectDriver;
+    private StationMoveSegmentExecutor segmentExecutor;
     private int deviceLogCollectTime = 200;
     private boolean initStatus = false;
     private long deviceDataLogTime = System.currentTimeMillis();
     private ExecutorService executor = Executors.newFixedThreadPool(9999);
+    private final RecentStationArrivalTracker recentArrivalTracker;
+    private final DeviceAsyncLogPublisher devpAsyncLogPublisher;
 
     public ZyStationV4Thread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
         this.deviceConfig = deviceConfig;
         this.redisUtil = redisUtil;
+        this.recentArrivalTracker = new RecentStationArrivalTracker(redisUtil);
+        this.segmentExecutor = new StationMoveSegmentExecutor(deviceConfig, redisUtil, this::sendCommand);
+        this.devpAsyncLogPublisher = SpringUtils.getBean(DeviceAsyncLogPublisher.class);
     }
 
     @Override
@@ -92,7 +102,7 @@
                     }
                     if (step == 2) {
                         StationCommand cmd = (StationCommand) task.getData();
-                        executor.submit(() -> executeMoveWithSeg(cmd));
+                        executor.submit(() -> segmentExecutor.execute(cmd));
                     }
                     Thread.sleep(100);
                 } catch (Exception e) {
@@ -134,6 +144,7 @@
         }
 
         List<ZyStationStatusEntity> zyStationStatusEntities = zyStationConnectDriver.getStatus();
+        LinkedHashSet<Integer> taskNoSet = new LinkedHashSet<>();
         for (ZyStationStatusEntity statusEntity : zyStationStatusEntities) {
             for (StationProtocol stationProtocol : statusList) {
                 if (stationProtocol.getStationId().equals(statusEntity.getStationId())) {
@@ -151,8 +162,22 @@
                     stationProtocol.setBarcode(statusEntity.getBarcode());
                     stationProtocol.setRunBlock(statusEntity.isRunBlock());
                     stationProtocol.setEnableIn(statusEntity.isEnableIn());
+                    stationProtocol.setInBarcodeError(statusEntity.isInBarcodeError());
                     stationProtocol.setWeight(statusEntity.getWeight());
                     stationProtocol.setTaskWriteIdx(statusEntity.getTaskWriteIdx());
+                    stationProtocol.setTaskBufferItems(statusEntity.getTaskBufferItems());
+                    if (statusEntity.getTaskNo() != null && statusEntity.getTaskNo() > 0) {
+                        taskNoSet.add(statusEntity.getTaskNo());
+                    }
+                    if (statusEntity.getTaskBufferItems() != null) {
+                        statusEntity.getTaskBufferItems().forEach(item -> {
+                            Integer bufferTaskNo = item == null ? null : item.getTaskNo();
+                            if (bufferTaskNo != null && bufferTaskNo > 0) {
+                                taskNoSet.add(bufferTaskNo);
+                            }
+                        });
+                    }
+                    recentArrivalTracker.observe(statusEntity.getStationId(), statusEntity.getTaskNo(), statusEntity.isLoading());
                 }
 
                 if (!Cools.isEmpty(stationProtocol.getSystemWarning())) {
@@ -164,20 +189,45 @@
                 }
             }
         }
+        taskNoList = new ArrayList<>(taskNoSet);
 
         OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛", DateUtils.convert(new Date()), deviceConfig.getDeviceNo()));
+        StationErrLogSupport.sync(deviceConfig, redisUtil, statusList);
 
         if (System.currentTimeMillis() - deviceDataLogTime > deviceLogCollectTime) {
-            DeviceDataLog deviceDataLog = new DeviceDataLog();
-            deviceDataLog.setOriginData(JSON.toJSONString(zyStationStatusEntities));
-            deviceDataLog.setWcsData(JSON.toJSONString(statusList));
-            deviceDataLog.setType(String.valueOf(SlaveType.Devp));
-            deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo());
-            deviceDataLog.setCreateTime(new Date());
-
-            redisUtil.set(DeviceLogRedisKeyBuilder.build(deviceDataLog), deviceDataLog, 60 * 60 * 24);
+            Date createTime = new Date();
+            HashMap<Integer, ZyStationStatusEntity> originDataMap = buildStationStatusMap(zyStationStatusEntities);
+            for (int i = 0; i < statusList.size(); i++) {
+                StationProtocol stationProtocol = statusList.get(i);
+                if (stationProtocol == null || stationProtocol.getStationId() == null) {
+                    continue;
+                }
+                DeviceDataLog deviceDataLog = new DeviceDataLog();
+                ZyStationStatusEntity originEntity = originDataMap.get(stationProtocol.getStationId());
+                deviceDataLog.setOriginData(originEntity == null ? null : JSON.toJSONString(originEntity));
+                deviceDataLog.setWcsData(JSON.toJSONString(stationProtocol));
+                deviceDataLog.setType(String.valueOf(SlaveType.Devp));
+                deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo());
+                deviceDataLog.setStationId(stationProtocol.getStationId());
+                deviceDataLog.setCreateTime(createTime);
+                devpAsyncLogPublisher.publishLatest(deviceDataLog);
+            }
             deviceDataLogTime = System.currentTimeMillis();
         }
+    }
+
+    private HashMap<Integer, ZyStationStatusEntity> buildStationStatusMap(List<ZyStationStatusEntity> zyStationStatusEntities) {
+        HashMap<Integer, ZyStationStatusEntity> map = new HashMap<>();
+        if (zyStationStatusEntities == null) {
+            return map;
+        }
+        for (ZyStationStatusEntity statusEntity : zyStationStatusEntities) {
+            if (statusEntity == null || statusEntity.getStationId() == null) {
+                continue;
+            }
+            map.put(statusEntity.getStationId(), statusEntity);
+        }
+        return map;
     }
 
     @Override
@@ -213,7 +263,27 @@
     }
 
     @Override
+    public List<Integer> getAllTaskNoList() {
+        return taskNoList;
+    }
+
+    @Override
+    public boolean hasRecentArrival(Integer stationId, Integer taskNo) {
+        return recentArrivalTracker.hasRecentArrival(stationId, taskNo);
+    }
+
+    @Override
     public StationCommand getCommand(StationCommandType commandType, Integer taskNo, Integer stationId, Integer targetStationId, Integer palletSize) {
+        return getCommand(commandType, taskNo, stationId, targetStationId, palletSize, null);
+    }
+
+    @Override
+    public StationCommand getCommand(StationCommandType commandType,
+                                     Integer taskNo,
+                                     Integer stationId,
+                                     Integer targetStationId,
+                                     Integer palletSize,
+                                     Double pathLenFactor) {
         StationCommand stationCommand = new StationCommand();
         stationCommand.setTaskNo(taskNo);
         stationCommand.setStationId(stationId);
@@ -223,7 +293,7 @@
 
         if (commandType == StationCommandType.MOVE) {
             if (!stationId.equals(targetStationId)) {
-                List<NavigateNode> nodes = calcPathNavigateNodes(stationId, targetStationId);
+                List<NavigateNode> nodes = calcPathNavigateNodes(taskNo, stationId, targetStationId, pathLenFactor);
                 List<Integer> path = new ArrayList<>();
                 List<Integer> liftTransferPath = new ArrayList<>();
                 for (NavigateNode n : nodes) {
@@ -239,6 +309,11 @@
                     if (Boolean.TRUE.equals(n.getIsLiftTransferPoint())) {
                         liftTransferPath.add(stationNo);
                     }
+                }
+                if (path.isEmpty()) {
+                    log.warn("杈撻�佺嚎鍛戒护鐢熸垚澶辫触锛岃矾寰勪负绌猴紝taskNo={}, stationId={}, targetStationId={}",
+                            taskNo, stationId, targetStationId);
+                    return null;
                 }
                 stationCommand.setNavigatePath(path);
                 stationCommand.setLiftTransferPath(liftTransferPath);
@@ -256,6 +331,9 @@
             e.printStackTrace();
         } finally {
             BasStationOptService optService = SpringUtils.getBean(BasStationOptService.class);
+            if (optService == null) {
+                return commandResponse;
+            }
             List<ZyStationStatusEntity> statusListEntity = zyStationConnectDriver.getStatus();
             ZyStationStatusEntity matched = null;
             if (statusListEntity != null) {
@@ -278,12 +356,10 @@
                     null,
                     JSON.toJSONString(command),
                     JSON.toJSONString(matched),
-                    1,
+                    commandResponse != null && Boolean.TRUE.equals(commandResponse.getResult()) ? 1 : 0,
                     JSON.toJSONString(commandResponse)
             );
-            if (optService != null) {
-                optService.save(basStationOpt);
-            }
+            optService.save(basStationOpt);
         }
         return commandResponse;
     }
@@ -298,12 +374,15 @@
         return zyStationConnectDriver.readOriginCommand(address, length);
     }
 
-    private List<NavigateNode> calcPathNavigateNodes(Integer startStationId, Integer targetStationId) {
+    private List<NavigateNode> calcPathNavigateNodes(Integer taskNo,
+                                                     Integer startStationId,
+                                                     Integer targetStationId,
+                                                     Double pathLenFactor) {
         NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
         if (navigateUtils == null) {
             return new ArrayList<>();
         }
-        return navigateUtils.calcByStationId(startStationId, targetStationId);
+        return navigateUtils.calcOptimalPathByStationId(startStationId, targetStationId, taskNo, pathLenFactor);
     }
 
     private void executeMoveWithSeg(StationCommand original) {
@@ -378,20 +457,8 @@
             }
 
             int segCursor = 0;
-            while (true) {
-                CommandResponse commandResponse = sendCommand(segmentCommands.get(segCursor));
-                if (commandResponse == null) {
-                    try {
-                        Thread.sleep(200);
-                    } catch (Exception ignore) {}
-                    continue;
-                }
-                if (commandResponse.getResult()) {
-                    break;
-                }
-                try {
-                    Thread.sleep(200);
-                } catch (Exception ignore) {}
+            if (!sendSegmentWithRetry(segmentCommands.get(segCursor), original.getTaskNo())) {
+                return;
             }
 
             long runTime = System.currentTimeMillis();
@@ -432,18 +499,8 @@
                     int thresholdSegment = (int) Math.ceil(segLen * segmentAdvanceRatio);
                     if (remainingSegment <= thresholdSegment && segCursor < segmentCommands.size() - 1) {
                         segCursor++;
-                        while (true) {
-                            CommandResponse commandResponse = sendCommand(segmentCommands.get(segCursor));
-                            if (commandResponse == null) {
-                                Thread.sleep(200);
-                                continue;
-                            }
-
-                            if (commandResponse.getResult()) {
-                                break;
-                            }
-
-                            Thread.sleep(200);
+                        if (!sendSegmentWithRetry(segmentCommands.get(segCursor), original.getTaskNo())) {
+                            break;
                         }
                     }
                     Thread.sleep(500);
@@ -530,4 +587,41 @@
         }
         return null;
     }
+
+    private boolean sendSegmentWithRetry(StationCommand command, Integer taskNo) {
+        while (true) {
+            if (isTaskMoveReset(taskNo)) {
+                return false;
+            }
+            CommandResponse commandResponse = sendCommand(command);
+            if (commandResponse == null) {
+                sleepQuietly(200L);
+                continue;
+            }
+            if (commandResponse.getResult()) {
+                return true;
+            }
+            sleepQuietly(200L);
+        }
+    }
+
+    private boolean isTaskMoveReset(Integer taskNo) {
+        if (taskNo == null || redisUtil == null) {
+            return false;
+        }
+        Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo);
+        return cancel != null;
+    }
+
+    private void sleepQuietly(long millis) {
+        try {
+            Thread.sleep(millis);
+        } catch (Exception ignore) {
+        }
+    }
+
+    @Override
+    public boolean clearPath(Integer taskNo) {
+        return false;
+    }
 }

--
Gitblit v1.9.1