From 103ca74d9b67ce4f766b5f77451741a76fa696b9 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期日, 22 三月 2026 09:50:10 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java |  141 ++++++++++++++++++++++++++++++++++++----------
 1 files changed, 110 insertions(+), 31 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 2ec6539..064b5cd 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
@@ -30,6 +30,9 @@
 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.system.entity.Config;
+import com.zy.system.service.ConfigService;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 
@@ -41,6 +44,8 @@
 @Data
 @Slf4j
 public class ZyStationV4Thread implements Runnable, com.zy.core.thread.StationThread {
+    private static final String CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = "stationCommandSegmentAdvanceRatio";
+    private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d;
 
     private List<StationProtocol> statusList = new ArrayList<>();
     private DeviceConfig deviceConfig;
@@ -170,7 +175,7 @@
             deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo());
             deviceDataLog.setCreateTime(new Date());
 
-            redisUtil.set(RedisKeyType.DEVICE_LOG_KEY.key + System.currentTimeMillis(), deviceDataLog, 60 * 60 * 24);
+            redisUtil.set(DeviceLogRedisKeyBuilder.build(deviceDataLog), deviceDataLog, 60 * 60 * 24);
             deviceDataLogTime = System.currentTimeMillis();
         }
     }
@@ -209,6 +214,16 @@
 
     @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);
@@ -218,7 +233,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) {
@@ -234,6 +249,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);
@@ -293,15 +313,19 @@
         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.calcByStationId(startStationId, targetStationId, taskNo, pathLenFactor);
     }
 
     private void executeMoveWithSeg(StationCommand original) {
+        double segmentAdvanceRatio = loadSegmentAdvanceRatio();
         if(original.getCommandType() == StationCommandType.MOVE){
             List<Integer> path = JSON.parseArray(JSON.toJSONString(original.getNavigatePath(), SerializerFeature.DisableCircularReferenceDetect), Integer.class);
             List<Integer> liftTransferPath = JSON.parseArray(JSON.toJSONString(original.getLiftTransferPath(), SerializerFeature.DisableCircularReferenceDetect), Integer.class);
@@ -372,20 +396,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();
@@ -423,21 +435,11 @@
                     int currentSegStartIndex = segCursor == 0 ? 0 : segmentEndIndices.get(segCursor - 1);
                     int segLen = currentSegEndIndex - currentSegStartIndex + 1;
                     int remainingSegment = Math.max(0, currentSegEndIndex - currentIndex);
-                    int thresholdSegment = (int) Math.ceil(segLen * 0.3);
+                    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);
@@ -448,6 +450,51 @@
             }
         }else {
             sendCommand(original);
+        }
+    }
+
+    private double loadSegmentAdvanceRatio() {
+        try {
+            ConfigService configService = SpringUtils.getBean(ConfigService.class);
+            if (configService == null) {
+                return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
+            }
+            Config config = configService.getOne(new QueryWrapper<Config>()
+                    .eq("code", CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO));
+            if (config == null || Cools.isEmpty(config.getValue())) {
+                return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
+            }
+            return normalizeSegmentAdvanceRatio(config.getValue());
+        } catch (Exception ignore) {
+            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
+        }
+    }
+
+    private double normalizeSegmentAdvanceRatio(String valueText) {
+        if (valueText == null) {
+            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
+        }
+        String text = valueText.trim();
+        if (text.isEmpty()) {
+            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
+        }
+        if (text.endsWith("%")) {
+            text = text.substring(0, text.length() - 1).trim();
+        }
+        try {
+            double ratio = Double.parseDouble(text);
+            if (ratio > 1d && ratio <= 100d) {
+                ratio = ratio / 100d;
+            }
+            if (ratio < 0d) {
+                return 0d;
+            }
+            if (ratio > 1d) {
+                return 1d;
+            }
+            return ratio;
+        } catch (Exception ignore) {
+            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
         }
     }
 
@@ -479,4 +526,36 @@
         }
         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) {
+        }
+    }
 }

--
Gitblit v1.9.1