From d3cc456ffe53a2423c80377d4d96b79459405e29 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期六, 09 九月 2023 16:21:09 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/NyShuttleThread.java |  148 +++++++++++++++++++++++++++++--------------------
 1 files changed, 87 insertions(+), 61 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/NyShuttleThread.java b/src/main/java/com/zy/core/thread/NyShuttleThread.java
index ed9db90..788a303f 100644
--- a/src/main/java/com/zy/core/thread/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/NyShuttleThread.java
@@ -87,7 +87,7 @@
 
     private void read() {
         try {
-            if (this.socket == null || !this.socket.isConnected()) {
+            if (this.socket == null || this.socket.isClosed()) {
                 //閾炬帴鏂紑閲嶆柊閾炬帴
                 this.connect();
             }
@@ -117,7 +117,8 @@
             NyShuttleHttpCommand readStatusCommand = NyHttpUtils.getReadStatusCommand(slave.getId());
             JSONObject jsonObject = NyHttpUtils.requestCommand(socket, readStatusCommand);
             if (jsonObject == null) {
-                OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.OFFLINE);
+                OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
             }else {
                 //鎵嬪姩鐘舵��/鑷姩鐘舵��
                 shuttleProtocol.setWorkingMode(jsonObject.getInteger("workingMode"));
@@ -243,7 +244,15 @@
             }
         } catch (Exception e) {
             e.printStackTrace();
-            OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+            OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+            try {
+                this.socket.close();
+                this.socket = null;
+                Thread.sleep(1000);
+                this.connect();
+            } catch (IOException | InterruptedException exception) {
+                e.printStackTrace();
+            }
         }
     }
 
@@ -252,7 +261,9 @@
         try {
             Socket socket = new Socket(slave.getIp(),slave.getPort());
             socket.setSoTimeout(60000);
+            socket.setKeepAlive(true);
             this.socket = socket;
+            log.info(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
         } catch (IOException e) {
             OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
         }
@@ -282,7 +293,21 @@
         }
 
         //鍙戝嚭璇锋眰
-        JSONObject result = NyHttpUtils.requestCommand(socket, command);
+        JSONObject result = null;
+        try {
+            result = NyHttpUtils.requestCommand(socket, command);
+        } catch (IOException e) {
+            try {
+                this.socket.close();
+                this.socket = null;
+                Thread.sleep(1000);
+                this.connect();
+            } catch (IOException exception) {
+                exception.printStackTrace();
+            } catch (InterruptedException ex) {
+                throw new RuntimeException(ex);
+            }
+        }
         if (result == null) {
             return false;//璇锋眰澶辫触
         }
@@ -332,62 +357,62 @@
         NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(shuttleProtocol.getCurrentLocNo()));
         //鍙栧嚭鍛戒护
         NyShuttleHttpCommand command = commands.get(commandStep);//褰撳墠鍛戒护
-//        if (commandStep != 0) {
-//            //鍒ゆ柇涓婁竴鏉℃寚浠ゆ槸鍚﹀畬鎴�
-//            NyShuttleHttpCommand lastCommand = commands.get(commandStep - 1);
-//            String requestType = lastCommand.getRequest().getBody().get("requestType").toString();
-//            if (requestType.equals("move") || requestType.equals("intoLift") || requestType.equals("outLift")) {
-//                //绉诲姩鍛戒护銆佸嚭鍏ユ彁鍗囨満鍛戒护
-//                NyShuttleProtocol.NyShuttlePointClass target = JSON.parseObject(lastCommand.getRequest().getBody().get("target").toString(), NyShuttleProtocol.NyShuttlePointClass.class);
-//                if (shuttleProtocol.getPoint().equals(target)) {
-//                    //涓婁竴鏉℃寚浠ょ殑鐩爣浣嶇疆鍜屽綋鍓嶅皬杞︿綅缃浉鍚岋紝鍒欒瀹氫笂涓�鏉′换鍔″畬鎴�
-//                    lastCommand.setComplete(true);
-//                    //瑙i攣閿佸畾璺緞锛屼笂涓�鏉¤矾寰勫拰褰撳墠璺緞
-//                    List<NavigateNode> nodes = lastCommand.getNodes();
-//                    nodes.addAll(command.getNodes());
-//                    navigateMapData.writeNavigateNodeToRedisMap(nodes, false);//瑙i攣璺緞
-//                }
-//            }else {
-//                lastCommand.setComplete(true);//鍏朵粬鍛戒护榛樿璁や负瀹屾垚
-//            }
-//            //浠诲姟鏁版嵁淇濆瓨鍒皉edis
-//            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
-//
-//            if (!lastCommand.getComplete()) {
-//                //涓婁竴鏉′换鍔℃湭瀹屾垚锛岀姝笅鍙戝懡浠�
-//                return false;
-//            }
-//        }
-//
-//        List<NavigateNode> nextNodes = null;//涓嬩竴姝ュ懡浠よ璧拌矾寰�
-//        if (commandStep + 1 < commands.size()) {
-//            NyShuttleHttpCommand nextCommand = commands.get(commandStep + 1);//涓嬩竴姝ュ懡浠�
-//            nextNodes = nextCommand.getNodes();//涓嬩竴姝ュ懡浠よ璧拌矾寰�
-//        }
-//
-//        if (shuttleProtocol.getFree() == ShuttleStatusType.BUSY.id) {
-//            return false;//灏忚溅鐘舵�佸繖锛岀姝㈡墽琛屽懡浠�
-//        }
-//
-//        //妫�娴嬪皬杞︽槸鍚﹁杩涙彁鍗囨満锛屽闇�瑕佽繘鎻愬崌鏈哄垯璋冨害鎻愬崌鏈�
-//        if (!checkLiftStation(wrkNo)) {
-//            return false;
-//        }
-//
-//        //妫�娴嬬┛姊溅鏄惁鍦ㄦ彁鍗囨満鍐�
-//        if (!checkShuttleInTheLift(wrkNo)) {
-//            return false;
-//        }
-//
-//        //妫�娴嬭矾寰勬槸鍚﹀彲琛岃蛋
-//        if (!checkPath(command.getNodes(), nextNodes, redisCommand)) {
-//            return false;
-//        }
-//
-//        //閿佸畾璺緞锛岄攣瀹氬綋鍓嶈矾寰勫拰涓嬩竴姝ヨ矾寰�
-//        List<NavigateNode> nodes = command.getNodes();
-//        nodes.addAll(nextNodes);
-//        navigateMapData.writeNavigateNodeToRedisMap(nodes, true);//鎵�浣跨敤鐨勮矾寰勮繘琛岄攣瀹氱鐢�
+        if (commandStep != 0) {
+            //鍒ゆ柇涓婁竴鏉℃寚浠ゆ槸鍚﹀畬鎴�
+            NyShuttleHttpCommand lastCommand = commands.get(commandStep - 1);
+            String requestType = lastCommand.getRequest().getBody().get("requestType").toString();
+            if (requestType.equals("move") || requestType.equals("intoLift") || requestType.equals("outLift")) {
+                //绉诲姩鍛戒护銆佸嚭鍏ユ彁鍗囨満鍛戒护
+                NyShuttleProtocol.NyShuttlePointClass target = JSON.parseObject(lastCommand.getRequest().getBody().get("target").toString(), NyShuttleProtocol.NyShuttlePointClass.class);
+                if (shuttleProtocol.getPoint().equals(target)) {
+                    //涓婁竴鏉℃寚浠ょ殑鐩爣浣嶇疆鍜屽綋鍓嶅皬杞︿綅缃浉鍚岋紝鍒欒瀹氫笂涓�鏉′换鍔″畬鎴�
+                    lastCommand.setComplete(true);
+                    //瑙i攣閿佸畾璺緞锛屼笂涓�鏉¤矾寰勫拰褰撳墠璺緞
+                    List<NavigateNode> nodes = lastCommand.getNodes();
+                    nodes.addAll(command.getNodes());
+                    navigateMapData.writeNavigateNodeToRedisMap(nodes, false);//瑙i攣璺緞
+                }
+            }else {
+                lastCommand.setComplete(true);//鍏朵粬鍛戒护榛樿璁や负瀹屾垚
+            }
+            //浠诲姟鏁版嵁淇濆瓨鍒皉edis
+            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
+
+            if (!lastCommand.getComplete()) {
+                //涓婁竴鏉′换鍔℃湭瀹屾垚锛岀姝笅鍙戝懡浠�
+                return false;
+            }
+        }
+
+        List<NavigateNode> nextNodes = null;//涓嬩竴姝ュ懡浠よ璧拌矾寰�
+        if (commandStep + 1 < commands.size()) {
+            NyShuttleHttpCommand nextCommand = commands.get(commandStep + 1);//涓嬩竴姝ュ懡浠�
+            nextNodes = nextCommand.getNodes();//涓嬩竴姝ュ懡浠よ璧拌矾寰�
+        }
+
+        if (shuttleProtocol.getFree() == ShuttleStatusType.BUSY.id) {
+            return false;//灏忚溅鐘舵�佸繖锛岀姝㈡墽琛屽懡浠�
+        }
+
+        //妫�娴嬪皬杞︽槸鍚﹁杩涙彁鍗囨満锛屽闇�瑕佽繘鎻愬崌鏈哄垯璋冨害鎻愬崌鏈�
+        if (!checkLiftStation(wrkNo)) {
+            return false;
+        }
+
+        //妫�娴嬬┛姊溅鏄惁鍦ㄦ彁鍗囨満鍐�
+        if (!checkShuttleInTheLift(wrkNo)) {
+            return false;
+        }
+
+        //妫�娴嬭矾寰勬槸鍚﹀彲琛岃蛋
+        if (!checkPath(command.getNodes(), nextNodes, redisCommand)) {
+            return false;
+        }
+
+        //閿佸畾璺緞锛岄攣瀹氬綋鍓嶈矾寰勫拰涓嬩竴姝ヨ矾寰�
+        List<NavigateNode> nodes = command.getNodes();
+        nodes.addAll(nextNodes);
+        navigateMapData.writeNavigateNodeToRedisMap(nodes, true);//鎵�浣跨敤鐨勮矾寰勮繘琛岄攣瀹氱鐢�
 
         //鍙墽琛屽懡浠�
         if (!write(command)) {
@@ -413,7 +438,8 @@
                     null,
                     null,
                     JSON.toJSONString(command),
-                    null
+                    null,
+                    JSON.toJSONString(shuttleProtocol)
             );
             shuttleOptService.insert(opt);
         }

--
Gitblit v1.9.1