From c4b6b51afdd3374735ed5f358457987eaa6e476f Mon Sep 17 00:00:00 2001
From: Junjie <540245094@qq.com>
Date: 星期五, 05 九月 2025 16:55:20 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/impl/NyShuttleThread.java |  241 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 207 insertions(+), 34 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java b/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
index 05d895e..53fbe7f 100644
--- a/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
@@ -5,7 +5,6 @@
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.core.common.DateUtils;
 import com.core.common.SpringUtils;
-import com.core.exception.CoolException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.zy.asrs.entity.BasShuttle;
 import com.zy.asrs.entity.DeviceConfig;
@@ -20,8 +19,11 @@
 import com.zy.common.utils.NavigateMapData;
 import com.zy.common.utils.NavigatePositionConvert;
 import com.zy.common.utils.RedisUtil;
+import com.zy.common.utils.ShuttleOperaUtils;
 import com.zy.core.News;
+import com.zy.core.action.ShuttleAction;
 import com.zy.core.model.DeviceCommandMsgModel;
+import com.zy.core.model.command.ShuttleAssignCommand;
 import com.zy.core.utils.DeviceMsgUtils;
 import com.zy.core.cache.OutputQueue;
 import com.zy.core.enums.*;
@@ -49,6 +51,7 @@
     private DeviceConfig deviceConfig;
     private RedisUtil redisUtil;
     private ShuttleProtocol shuttleProtocol;
+    private ShuttleAction shuttleAction = null;
 
     private static final boolean DEBUG = false;//璋冭瘯妯″紡
 
@@ -89,14 +92,11 @@
 //        Thread executeThread = new Thread(() -> {
 //            while (true) {
 //                try {
-//                    ShuttleAction shuttleAction = null;
-//                    try {
-//                        shuttleAction = SpringUtils.getBean(ShuttleAction.class);
-//                    }catch (Exception e){
-//                        continue;
-//                    }
-//
 //                    if (shuttleAction == null) {
+//                        try {
+//                            shuttleAction = SpringUtils.getBean(ShuttleAction.class);
+//                        }catch (Exception e){
+//                        }
 //                        continue;
 //                    }
 //
@@ -752,19 +752,24 @@
 
     @Override
     public synchronized boolean setProtocolStatus(ShuttleProtocolStatusType status) {
+        if (status.equals(ShuttleProtocolStatusType.IDLE)) {
+            this.shuttleProtocol.setIdleTime(System.currentTimeMillis());
+        }
         this.shuttleProtocol.setProtocolStatus(status);
         return true;
     }
 
     @Override
     public synchronized boolean setTaskNo(Integer taskNo) {
+        redisUtil.set(RedisKeyType.SHUTTLE_FLAG.key + shuttleProtocol.getShuttleNo(), taskNo);
         this.shuttleProtocol.setTaskNo(taskNo);
         return true;
     }
 
     @Override
     public synchronized boolean setSyncTaskNo(Integer taskNo) {
-        this.shuttleProtocol.setSyncTaskNo(taskNo);
+        redisUtil.setSync(RedisKeyType.SHUTTLE_FLAG.key + shuttleProtocol.getShuttleNo(), taskNo);
+        this.shuttleProtocol.setTaskNo(taskNo);
         return true;
     }
 
@@ -821,6 +826,13 @@
     }
 
     @Override
+    public boolean setTrafficControl(boolean enable, List<NavigateNode> nodeList) {
+        shuttleProtocol.setTrafficControl(enable);
+        shuttleProtocol.setTrafficControlNodes(nodeList);
+        return true;
+    }
+
+    @Override
     public void updateDeviceDataLogTime(long time) {
         shuttleProtocol.setDeviceDataLog(time);
     }
@@ -842,6 +854,119 @@
     }
 
     @Override
+    public boolean restartCalcPath() {
+        ConfigService configService = SpringUtils.getBean(ConfigService.class);
+        if (configService == null) {
+            return false;
+        }
+
+        ShuttleAction shuttleAction = SpringUtils.getBean(ShuttleAction.class);
+        if (shuttleAction == null) {
+            return false;
+        }
+
+        ShuttleOperaUtils shuttleOperaUtils = SpringUtils.getBean(ShuttleOperaUtils.class);
+        if (shuttleOperaUtils == null) {
+            return false;
+        }
+
+        ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class);
+        if (objectMapper == null) {
+            return false;
+        }
+
+        boolean trafficControlRestartCalcPath = false;
+        Config trafficControlRestartCalcPathConfig = configService.selectOne(new EntityWrapper<Config>()
+                .eq("code", "trafficControlRestartCalcPath")
+        );
+        if(trafficControlRestartCalcPathConfig != null) {
+            trafficControlRestartCalcPath = trafficControlRestartCalcPathConfig.getValue().equals("Y") ? true : false;
+        }
+
+        if (!trafficControlRestartCalcPath) {
+            return false;
+        }
+
+        if (shuttleProtocol.getTaskNo() == 0) {
+            return false;
+        }
+
+        if (!this.isDeviceIdle()) {
+            return false;
+        }
+
+        Object object = redisUtil.get(RedisKeyType.SHUTTLE_RESTART_CALC_PATH.key + shuttleProtocol.getShuttleNo());
+        if (object != null) {
+            return false;
+        }
+
+        Integer taskNo = shuttleProtocol.getTaskNo();
+        Object obj = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + taskNo);
+        if (obj == null) {
+            return false;
+        }
+
+        ShuttleRedisCommand redisCommand = null;
+        try {
+            redisCommand = objectMapper.readValue(String.valueOf(obj), ShuttleRedisCommand.class);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        if (redisCommand == null) {
+            return false;
+        }
+
+        ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand();
+        String locNo = assignCommand.getLocNo();
+        List<NavigationMapType> mapTypes = assignCommand.getMapTypes();
+        if (locNo == null) {
+            return false;
+        }
+
+        if (mapTypes == null) {
+            return false;
+        }
+
+        List<NavigationMapType> restartCalcMapTypes = new ArrayList<>(mapTypes);
+        restartCalcMapTypes.add(NavigationMapType.SHUTTLE);
+
+        String currentLocNo = shuttleProtocol.getCurrentLocNo();
+        List<ShuttleCommand> commands = shuttleOperaUtils.getStartToTargetCommands(currentLocNo, locNo, restartCalcMapTypes, assignCommand, this);
+        if (commands == null) {
+            return false;
+        }
+
+        if (assignCommand.getTaskMode() == ShuttleTaskModeType.TRANSPORT.id) {
+            List<ShuttleCommand> originCommands = assignCommand.getCommands();
+            if (originCommands == null) {
+                return false;
+            }
+
+            ShuttleCommand firstCommand = originCommands.get(0);
+            ShuttleCommand endCommand = originCommands.get(originCommands.size() - 1);
+
+            if (firstCommand.getMode() != ShuttleCommandModeType.PALLET_LIFT.id) {
+                return false;
+            }
+
+            if (endCommand.getMode() != ShuttleCommandModeType.PALLET_DOWN.id) {
+                return false;
+            }
+
+            commands.add(0, firstCommand);
+            commands.add(endCommand);
+        }
+
+        assignCommand.setCommands(commands);
+
+        //涓嬪彂浠诲姟
+        shuttleAction.assignWork(shuttleProtocol.getShuttleNo(), assignCommand);
+        redisUtil.set(RedisKeyType.SHUTTLE_RESTART_CALC_PATH.key + shuttleProtocol.getShuttleNo(), true, 60);
+        return true;
+    }
+
+    @Override
     public ShuttleCommand getMoveCommand(Integer taskNo, String startCodeNum, String distCodeNum, Integer allDistance, Integer runDirection, Integer runSpeed, List<NavigateNode> nodes) {
         NavigateMapData navigateMapData = SpringUtils.getBean(NavigateMapData.class);
         NyShuttleHttpCommand httpStandard = getHttpStandard(deviceConfig.getDeviceNo(), taskNo);
@@ -853,12 +978,11 @@
         HashMap<String, Object> body = new HashMap<>();
         body.put("requestType", "move");//绉诲姩鍛戒护
         body.put("taskId", taskId);//TaskID闇�瑕侀殢鏈�
-//        body.put("start", navigateNodeToNyPointNode(NavigatePositionConvert.codeToNode(startCodeNum, device.getHostId())));//璧风偣
-//        body.put("target", navigateNodeToNyPointNode(NavigatePositionConvert.codeToNode(distCodeNum, device.getHostId())));//缁堢偣
         body.put("path", path);
         request.setBody(body);
 
         NavigateNode startNode = nodes.get(0);
+        List<List<MapNode>> mapData = navigateMapData.getJsonData(startNode.getZ(), NavigationMapType.getMapTypes(NavigationMapType.NONE), null, null);
         for (NavigateNode node : nodes) {
             HashMap<String, Object> data = new HashMap<>();
             String codeNum = NavigatePositionConvert.xyToPosition(node.getX(), node.getY(), node.getZ());
@@ -867,7 +991,6 @@
             int yp = Integer.parseInt(String.valueOf(nyNode.get("x")));
             int z = Integer.parseInt(String.valueOf(nyNode.get("z")));
 
-            List<List<MapNode>> mapData = navigateMapData.getJsonData(startNode.getZ(), NavigationMapType.getMapTypes(NavigationMapType.NONE), null, null);
             List<MapNode> mapNodes = mapData.get(node.getX());
             MapNode mapNode = mapNodes.get(node.getY());
 
@@ -881,18 +1004,70 @@
 
         httpStandard.setRequest(request);
 
-        LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
-        LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
-                .eq("qr_code_value", distCodeNum));
-        if (locMast == null) {
-            throw new CoolException("搴撲綅淇℃伅涓嶅瓨鍦�");
-        }
+        Object object = redisUtil.get(RedisKeyType.LOC_MAP.key);
+        HashMap<String, String> locMap = (HashMap<String, String>) object;
+        String targetLocNo = locMap.get(distCodeNum);
 
         ShuttleCommand command = new ShuttleCommand();
         command.setShuttleNo(deviceConfig.getDeviceNo());
         command.setBody(JSON.toJSONString(httpStandard));
         command.setMode(ShuttleCommandModeType.MOVE.id);
-        command.setTargetLocNo(locMast.getLocNo());
+        command.setTargetLocNo(targetLocNo);
+        command.setTaskNo(taskId);
+        return command;
+    }
+
+    @Override
+    public ShuttleCommand getMoveLiftCommand(Integer taskNo, String startCodeNum, String distCodeNum, Integer allDistance, Integer runDirection, Integer runSpeed, List<NavigateNode> nodes, Boolean moveIn) {
+        NavigateMapData navigateMapData = SpringUtils.getBean(NavigateMapData.class);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(deviceConfig.getDeviceNo(), taskNo);
+        NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
+
+        ArrayList<HashMap<String, Object>> path = new ArrayList<>();
+
+        Integer taskId = getTaskId();
+        HashMap<String, Object> body = new HashMap<>();
+        if (moveIn) {
+            body.put("requestType", "intoLift");//杩涙彁鍗囨満
+        }else {
+            body.put("requestType", "outLift");//鍑烘彁鍗囨満
+        }
+        body.put("taskId", taskId);//TaskID闇�瑕侀殢鏈�
+        body.put("path", path);
+        request.setBody(body);
+
+        NavigateNode startNode = nodes.get(0);
+        List<List<MapNode>> mapData = navigateMapData.getJsonData(startNode.getZ(), NavigationMapType.getMapTypes(NavigationMapType.NONE), null, null);
+        for (NavigateNode node : nodes) {
+            HashMap<String, Object> data = new HashMap<>();
+            String codeNum = NavigatePositionConvert.xyToPosition(node.getX(), node.getY(), node.getZ());
+            Map<String, Object> nyNode = navigateNodeToNyPointNode(NavigatePositionConvert.codeToNode(codeNum));
+            int xp = Integer.parseInt(String.valueOf(nyNode.get("y")));
+            int yp = Integer.parseInt(String.valueOf(nyNode.get("x")));
+            int z = Integer.parseInt(String.valueOf(nyNode.get("z")));
+
+            List<MapNode> mapNodes = mapData.get(node.getX());
+            MapNode mapNode = mapNodes.get(node.getY());
+
+            data.put("xp", xp);
+            data.put("yp", yp);
+            data.put("z", z);
+            data.put("x", mapNode.getXBase());
+            data.put("y", mapNode.getYBase());
+            path.add(data);
+        }
+
+        httpStandard.setRequest(request);
+
+        Object object = redisUtil.get(RedisKeyType.LOC_MAP.key);
+        HashMap<String, String> locMap = (HashMap<String, String>) object;
+        String targetLocNo = locMap.get(distCodeNum);
+
+        ShuttleCommand command = new ShuttleCommand();
+        command.setShuttleNo(deviceConfig.getDeviceNo());
+        command.setBody(JSON.toJSONString(httpStandard));
+        command.setMode(ShuttleCommandModeType.MOVE.id);
+        command.setTargetLocNo(targetLocNo);
         command.setTaskNo(taskId);
         return command;
     }
@@ -961,7 +1136,6 @@
 
     //鑾峰彇灏忚溅澶嶄綅鍝嶅簲鍛戒护
     public ShuttleCommand getInitCommand(Integer taskNo, Integer code) {
-        LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
         NavigateMapData navigateMapData = SpringUtils.getBean(NavigateMapData.class);
         NyShuttleHttpCommand httpStandard = getHttpStandard(deviceConfig.getDeviceNo(), taskNo);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
@@ -977,15 +1151,14 @@
         map.put("z", lev);
 
         String mapStr = JSON.toJSONString(map);
-        LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
-                .eq("qr_code_value", mapStr));
-        if (locMast == null) {
-            throw new CoolException("鍦板潃鐮佷笉瀛樺湪");
-        }
+
+        Object object = redisUtil.get(RedisKeyType.LOC_MAP.key);
+        HashMap<String, String> locMap = (HashMap<String, String>) object;
+        String targetLocNo = locMap.get(mapStr);
 
         List<List<MapNode>> mapData = navigateMapData.getJsonData(lev, NavigationMapType.getMapTypes(NavigationMapType.NONE), null, null);
-        List<MapNode> mapNodes = mapData.get(locMast.getRow1());
-        MapNode mapNode = mapNodes.get(locMast.getBay1());
+        List<MapNode> mapNodes = mapData.get(Utils.getRow(targetLocNo));
+        MapNode mapNode = mapNodes.get(Utils.getBay(targetLocNo));
 
         HashMap<String, Object> location = new HashMap<>();
         location.put("xp", row);
@@ -1091,16 +1264,16 @@
 
     private JSONObject queryCommandStatus(String resultKey) {
         // 鑾峰彇鏈嶅姟鍣ㄥ搷搴�
-        // 灏濊瘯50娆�
         JSONObject result = null;
-        for (int i = 0; i < 50; i++) {
+        long startTime = System.currentTimeMillis();
+        while (true) {
+            if((System.currentTimeMillis() - startTime) > 1000 * 10) {
+                break;
+            }
+
             result = getRequestBody(resultKey);
             if (result == null) {
-                try {
-                    Thread.sleep(500);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
+                continue;
             }else {
                 break;
             }

--
Gitblit v1.9.1