From 2c1e3b7b10c0d4afbf09a9151e132f1ee85b9c6f Mon Sep 17 00:00:00 2001
From: Junjie <540245094@qq.com>
Date: 星期六, 02 八月 2025 11:15:25 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/action/ShuttleAction.java |  163 +++++++++++++++++++++++++++++-------------------------
 1 files changed, 87 insertions(+), 76 deletions(-)

diff --git a/src/main/java/com/zy/core/action/ShuttleAction.java b/src/main/java/com/zy/core/action/ShuttleAction.java
index 393d2cb..55bba24 100644
--- a/src/main/java/com/zy/core/action/ShuttleAction.java
+++ b/src/main/java/com/zy/core/action/ShuttleAction.java
@@ -16,7 +16,6 @@
 import com.zy.common.model.NavigateNode;
 import com.zy.common.service.CommonService;
 import com.zy.common.utils.NavigateMapUtils;
-import com.zy.common.utils.NavigatePositionConvert;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.News;
 import com.zy.core.cache.SlaveConnection;
@@ -71,11 +70,8 @@
             return false;
         }
 
-        //娓呯┖涓嬪彂绱㈠紩
-        Set<String> keys = redisUtil.searchKeys(RedisKeyType.SHUTTLE_SEND_COMMAND_INDEX.key + shuttleNo);
-        for (String key : keys) {
-            redisUtil.del(key);
-        }
+        //Clear Traffic Control
+        trafficControlThread.forceCancelTrafficControl(shuttleNo);
 
         ShuttleRedisCommand redisCommand = new ShuttleRedisCommand();
 
@@ -91,7 +87,7 @@
         return false;
     }
 
-    public synchronized boolean executeWork(Integer shuttleNo, Integer taskNo) {
+    public boolean executeWork(Integer shuttleNo, Integer taskNo) {
         Object obj = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + taskNo);
         if (obj == null) {
             return false;
@@ -123,8 +119,10 @@
             return false;
         }
 
+        News.info("execute check command {},{}", shuttleNo, taskNo);
         //妫�娴嬪懡浠�
         int checked = checkCommand(redisCommand, shuttleNo);
+        News.info("execute check command complete {},{}", shuttleNo, taskNo);
         if (checked == 0) {
             return false;
         }
@@ -163,9 +161,26 @@
 
         //绉诲姩鍛戒护锛岄攣瀹氳矾寰�
         if (command.getMode() == ShuttleCommandModeType.MOVE.id) {
+            TrafficControlThread trafficControlThread = (TrafficControlThread) SlaveConnection.get(SlaveType.TrafficControl, 1);
+            if (trafficControlThread == null) {
+                return false;
+            }
+
             List<NavigateNode> nodes = JSON.parseArray(JSON.toJSONString(command.getNodes()), NavigateNode.class);
-            //鐢宠绠″埗
-            boolean apply = applyTrafficControl(commands, nodes, shuttleNo, taskNo);
+
+            Object object = redisUtil.get(RedisKeyType.TRAFFIC_CONTROL_LOCK_APPLY.key + shuttleNo);
+            if (object == null) {
+                //鐢宠绠″埗
+                News.info("execute apply control {},{}", shuttleNo, taskNo);
+                redisUtil.set(RedisKeyType.TRAFFIC_CONTROL_LOCK_APPLY.key + shuttleNo, "lock", 10);
+                applyTrafficControl(commands, nodes, shuttleNo, taskNo);
+                News.info("execute apply control complete {},{}", shuttleNo, taskNo);
+            }
+
+            News.info("execute query control {},{}", shuttleNo, taskNo);
+            //鏌ヨ绠″埗
+            boolean apply = queryTrafficControl(shuttleNo, taskNo);
+            News.info("execute query control complete {},{}", shuttleNo, taskNo);
             if(!apply){
                 return false;//鐢宠澶辫触
             }
@@ -183,12 +198,15 @@
             if (!result) {
                 News.error("{} device can't lock path!", shuttleNo);
                 shuttleThread.offerSystemMsg("{} device can't lock path!", shuttleNo);
+                trafficControlThread.trafficReportError(shuttleNo, taskNo);
                 return false;//璺緞閿佸畾澶辫触
             }
         }
 
+        News.info("execute send command {},{}", shuttleNo, taskNo);
         // 涓嬪彂鍛戒护
         CommandResponse response = write(command, shuttleNo);
+        News.info("execute send command complete {},{}", shuttleNo, taskNo);
 
         //淇濆瓨鍛戒护鏃ュ織
         BasShuttleOpt basShuttleOpt = new BasShuttleOpt();
@@ -213,9 +231,6 @@
         }
 
         shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.WORKING);
-
-        //瀛樺偍涓嬪彂鎸囦护绱㈠紩
-        redisUtil.set(RedisKeyType.SHUTTLE_SEND_COMMAND_INDEX.key + shuttleNo + "_" + taskNo + "_" + commandStep, commandStep);
 
         commandStep += 1;
         //鏇存柊redis鏁版嵁
@@ -257,52 +272,46 @@
             return 0;
         }
 
-        //涓婁竴鏉℃寚浠�
-        String searchKey = RedisKeyType.SHUTTLE_SEND_COMMAND_INDEX.key + shuttleNo + "_" + redisCommand.getWrkNo() + "_";
-        Set<String> keys = redisUtil.searchKeys(searchKey);
-        TreeSet<Integer> treeSet = new TreeSet<>();
-        for (String key : keys) {
-            String[] split = key.split(searchKey);
-            treeSet.add(Integer.parseInt(split[1]));
+        boolean supportContinuously = false;
+        int checkIdx = commandStep - 2;
+        if (checkIdx < 0) {
+            checkIdx = commandStep - 1;
         }
-        if (treeSet.isEmpty()) {
-            return 1;
+        ShuttleCommand last2Command = commands.get(checkIdx);
+        if(last2Command.getComplete()){
+            supportContinuously = true;
+            checkIdx = commandStep - 1;
         }
 
-        String firstKey = searchKey + treeSet.first();
-        Integer lastCommandIdx = (Integer) redisUtil.get(firstKey);
-        ShuttleCommand lastCommand = commands.get(lastCommandIdx);
+        ShuttleCommand lastCommand = commands.get(checkIdx);
         if (!lastCommand.getComplete()) {
             //妫�娴嬫洿鏂板懡浠ゅ畬鎴�
-            boolean checked = updateCommandComplete(lastCommandIdx, commands, shuttleNo);
+            boolean checked = updateCommandComplete(checkIdx, commands, shuttleNo);
             if (checked) {
-                //鍒犻櫎绱㈠紩
-                redisUtil.del(firstKey);
-
                 // 鏇存柊redis鏁版嵁
                 redisUtil.set(RedisKeyType.SHUTTLE_WORK_FLAG.key + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect));
             }else {
                 //灏忚溅绉诲姩杩炵画涓嬪彂鎸囦护
                 if (assignCommand.getShuttleMoveCommandsContinuously()) {
-                    if (treeSet.size() <= 1) {
-                        if(commandStep >= commands.size()) {
-                            return 0;
-                        }
+                    if (!supportContinuously) {
+                        return 0;
+                    }
 
-                        //绉诲姩鎸囦护
-                        if(lastCommand.getMode() != ShuttleCommandModeType.MOVE.id) {
-                            return 0;
-                        }
+                    //绉诲姩鎸囦护
+                    if(lastCommand.getMode() != ShuttleCommandModeType.MOVE.id) {
+                        return 0;
+                    }
 
-                        List<NavigateNode> nodes = lastCommand.getNodes();
-                        NavigateNode startNode = nodes.get(0);
-                        if (!startNode.getLinePartAllowGo()) {//鐩寸嚎娈甸儴鍒嗭紝鍏佽鐩存帴琛岃蛋
-                            return 0;
-                        }
+                    List<NavigateNode> nodes = lastCommand.getNodes();
+                    NavigateNode startNode = nodes.get(0);
+                    if (!startNode.getLinePartAllowGo()) {//鐩寸嚎娈甸儴鍒嗭紝鍏佽鐩存帴琛岃蛋
+                        return 0;
+                    }
 
-                        //鐩寸嚎娈垫暟鎹爣璇�
-                        Long linePartFlag = startNode.getLinePartFlag();
+                    //鐩寸嚎娈垫暟鎹爣璇�
+                    Long linePartFlag = startNode.getLinePartFlag();
 
+                    if(commandStep < commands.size()){
                         //鍙栨寚浠�
                         ShuttleCommand currentCommand = commands.get(commandStep);
                         if(currentCommand.getMode() != ShuttleCommandModeType.MOVE.id) {
@@ -319,7 +328,6 @@
                             //鏁版嵁鏍囪瘑涓�鑷�
                             return 2;//鍏佽灏忚溅绉诲姩杩炵画涓嬪彂鎸囦护
                         }
-
                     }
                 }
 
@@ -354,7 +362,8 @@
             }
 
             //鐢宠鍙栨秷绠″埗
-            trafficControlThread.cancelTrafficControl(shuttleNo, shuttleProtocol.getTaskNo());
+            cancelTrafficControl(shuttleNo, shuttleProtocol.getTaskNo());
+
             //鍒犻櫎redis
             redisUtil.del(RedisKeyType.SHUTTLE_WORK_FLAG.key + redisCommand.getWrkNo());
             return 0;//绂佹鍐嶄笅鍙戝懡浠�
@@ -420,33 +429,21 @@
             if (trafficControlThread == null) {
                 return false;
             }
-
-            TrafficControlDataModel trafficControlDataModel = trafficControlThread.queryTrafficControl(shuttleNo);
-            if (trafficControlDataModel != null) {
-                //鏈夌鍒朵俊鎭紝杩涜妫�娴�
-                if (!trafficControlDataModel.getTaskNo().equals(shuttleProtocol.getTaskNo())) {
-                    return false;//浠诲姟涓嶄竴鑷�
-                }
-
-                //妫�娴嬫槸鍚﹀埌缁堢偣
-                List<NavigateNode> totalNodeList = trafficControlDataModel.getTotalNodeList();
-                NavigateNode trafficTargetNode = totalNodeList.get(totalNodeList.size() - 1);
-                String trafficTargetLoc = Utils.getLocNo(trafficTargetNode.getX(), trafficTargetNode.getY(), trafficTargetNode.getZ());
-
-                //鍒ゆ柇灏忚溅鏄惁鍒扮粓鐐�
-                if(shuttleProtocol.getCurrentLocNo().equals(trafficTargetLoc)) {
-                    //涓婃姤浜ょ
-                    trafficControlThread.trafficReport(command.getNodesDeepCopy(), shuttleNo, shuttleProtocol.getTaskNo());
-                }
-            }
+            //涓婃姤浜ょ
+            News.info("execute check command report traffic {},{}", shuttleNo, shuttleProtocol.getTaskNo());
+            trafficControlThread.trafficReport(command.getNodesDeepCopy(), shuttleNo, shuttleProtocol.getTaskNo());
+            News.info("execute check command report traffic complete {},{}", shuttleNo, shuttleProtocol.getTaskNo());
 
             String currentLocNo = shuttleProtocol.getCurrentLocNo();
+            if (currentLocNo == null) {
+                return false;
+            }
             if (targetPoints.contains(Utils.getRow(currentLocNo) + "-" + Utils.getBay(currentLocNo))) {
-                command.setComplete(true);
                 boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(shuttleProtocol.getCurrentLocNo()), shuttleProtocol.getShuttleNo(), nodes, false);//瑙i攣璺緞
                 if (!result) {
                     return false;//瑙i攣澶辫触
                 }
+                command.setComplete(true);
             }else {
                 return false;
             }
@@ -503,23 +500,33 @@
             return false;
         }
 
-        NavigateNode startNode = nodeList.get(0);
-        Long linePartFlag = startNode.getLinePartFlag();
-
         List<NavigateNode> totalNodeList = new ArrayList<>();
         for (ShuttleCommand command : commands) {
             if (command.getMode() == ShuttleCommandModeType.MOVE.id) {
-                NavigateNode node = command.getNodes().get(0);
-                Long nodeLinePartFlag = node.getLinePartFlag();
-                if (nodeLinePartFlag.equals(linePartFlag)) {
-                    List<NavigateNode> deepCopy = command.getNodesDeepCopy();
-                    if (deepCopy != null) {
-                        totalNodeList.addAll(deepCopy);
-                    }
+                List<NavigateNode> deepCopy = command.getNodesDeepCopy();
+                if (deepCopy != null) {
+                    totalNodeList.addAll(deepCopy);
                 }
             }
         }
         return trafficControlThread.applyTrafficControl(totalNodeList, nodeList, shuttleNo, taskNo);
+    }
+
+    //鏌ヨ鏄惁鐢宠绠″埗鎴愬姛
+    public boolean queryTrafficControl(Integer shuttleNo, Integer taskNo) {
+        Object object = redisUtil.get(RedisKeyType.TRAFFIC_CONTROL_SUCCESS_APPLY.key + shuttleNo + "_" + taskNo);
+        if (object == null) {
+            return false;
+        }
+        return true;
+    }
+
+    public boolean cancelTrafficControl(Integer shuttleNo, Integer taskNo) {
+        TrafficControlDataModel model = new TrafficControlDataModel();
+        model.setShuttleNo(shuttleNo);
+        model.setTaskNo(taskNo);
+        redisUtil.set(RedisKeyType.TRAFFIC_CONTROL_CANCEL_LIST.key + shuttleNo + "_" + taskNo, model);
+        return true;
     }
 
 //    /**
@@ -778,6 +785,10 @@
         } catch (Exception e) {
             e.printStackTrace();
         }
+    }
+
+    public synchronized boolean clearPath(Integer shuttleNo) {
+        return navigateMapUtils.clearPath(shuttleNo);
     }
 
 //    //璺戝簱绋嬪簭
@@ -1149,7 +1160,7 @@
 //        }
 //    }
 
-    private synchronized CommandResponse write(ShuttleCommand command, Integer shuttleNo) {
+    private CommandResponse write(ShuttleCommand command, Integer shuttleNo) {
         CommandResponse response = new CommandResponse(false);
         if (null == command) {
             News.error("鍥涘悜绌挎杞﹀啓鍏ュ懡浠や负绌�");

--
Gitblit v1.9.1