From 987d05ba7038d87efe72b4877be911e65f7a7ecb Mon Sep 17 00:00:00 2001
From: Junjie <xjj@123>
Date: 星期日, 28 四月 2024 16:29:47 +0800
Subject: [PATCH] #

---
 zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyShuttleThread.java |  179 +++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 112 insertions(+), 67 deletions(-)

diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyShuttleThread.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyShuttleThread.java
index 036ce19..2edd07e 100644
--- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyShuttleThread.java
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyShuttleThread.java
@@ -56,6 +56,10 @@
     private ShuttleProtocol shuttleProtocol;
     private Socket socket;
 
+    private static final boolean DEBUG = true;//璋冭瘯妯″紡
+
+    private List<JSONObject> socketResults = new ArrayList<>();
+
     public NyShuttleThread(Device device, RedisUtil redisUtil) {
         this.device = device;
         this.redisUtil = redisUtil;
@@ -65,6 +69,15 @@
     public void run() {
         News.info("{}鍙峰洓鍚戣溅绾跨▼鍚姩", device.getDeviceNo());
         this.connect();
+
+        //鐩戝惉娑堟伅骞跺瓨鍌�
+        Thread innerThread = new Thread(() -> {
+            while (true) {
+                listenSocketMessage();
+            }
+        });
+        innerThread.start();
+
         while (true) {
             try {
                 read();
@@ -75,6 +88,65 @@
         }
     }
 
+    private void listenSocketMessage() {
+        try {
+            if (this.socket == null) {
+                return;
+            }
+
+            // 鑾峰彇杈撳叆娴�
+            BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
+            // 璇诲彇鏈嶅姟鍣ㄧ殑鍝嶅簲
+            StringBuffer sb = new StringBuffer();
+            String response = null;
+            char[] chars = new char[2048];//缂撳啿鍖�
+            do {
+                reader.read(chars);
+                String trim = new String(chars).trim();
+                sb.append(trim);
+                if (sb.lastIndexOf("\r\n") != -1) {
+                    break;
+                }
+            } while (response != null);
+//            System.out.println("Received response from server: " + sb);
+
+            JSONObject result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
+            if (!socketResults.isEmpty() && socketResults.size() >= 5) {
+                socketResults.remove(0);//娓呯悊澶磋妭鐐�
+            }
+            socketResults.add(result);//娣诲姞鏁版嵁
+        } catch (Exception e) {
+//            e.printStackTrace();
+        }
+    }
+
+    public JSONObject getRequestBody(Integer requestId) {
+        // 鑾峰彇鏈嶅姟鍣ㄥ搷搴�
+        JSONObject result = null;
+        for (int i = 0; i < socketResults.size(); i++) {
+            JSONObject socketResult = socketResults.get(i);
+            if (!socketResult.get("msgType").equals("responseMsg")) {//涓嶆槸鍝嶅簲鍐呭
+                continue;
+            }
+
+            JSONObject resultResponse = JSON.parseObject(socketResult.get("response").toString());
+            JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
+            int responseId = Integer.parseInt(resultHeader.get("responseId").toString());
+            if (!DEBUG && responseId != requestId) {
+                continue;//鍝嶅簲ID涓庤姹侷D涓嶄竴鑷达紝涓嶅湪璋冭瘯妯″紡涓�
+            }
+
+            result = socketResult;
+            break;
+        }
+
+        if (result == null) {
+            return null;//鏃犲搷搴旂粨鏋�
+        }
+
+        return filterBodyData(result);//杩斿洖Body缁撴灉闆�
+    }
+
     private void read() {
         try {
             if (this.socket == null || this.socket.isClosed()) {
@@ -82,7 +154,7 @@
                 this.connect();
             }
             readStatus();
-            listenInit();
+            listenInit();//鐩戝惉鍒濆鍖栦簨浠�
         } catch (Exception e) {
             e.printStackTrace();
             OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戣鍙栧洓鍚戠┛姊溅鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
@@ -112,7 +184,7 @@
                 //灏忚溅璁惧鐘舵��
                 shuttleProtocol.setDeviceStatus(data.getInteger("free"));
                 //灏忚溅妯″紡
-                shuttleProtocol.setDeviceStatus(data.getInteger("workingMode"));
+                shuttleProtocol.setMode(data.getInteger("workingMode"));
                 //褰撳墠浜岀淮鐮�
                 shuttleProtocol.setCurrentCode(data.getString("coord"));
                 //鐢垫睜鐢甸噺
@@ -217,42 +289,34 @@
      */
     public void listenInit() {
         try {
-            // 鑾峰彇杈撳叆娴�
-            BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
+            // 鑾峰彇鏈嶅姟鍣ㄥ搷搴�
+            JSONObject result = null;
+            int removeIdx = -1;
+            for (int i = 0; i < socketResults.size(); i++) {
+                JSONObject socketResult = socketResults.get(i);
+                if (!socketResult.get("msgType").equals("requestMsg")) {//涓嶆槸璇锋眰鍐呭
+                    continue;
+                }
 
-            // 璇诲彇鏈嶅姟鍣ㄧ殑鍝嶅簲
-            StringBuffer sb = new StringBuffer();
-            String response = null;
-            char[] chars = new char[2048];//缂撳啿鍖�
-            do {
-                reader.read(chars);
-                String trim = new String(chars).trim();
-                sb.append(trim);
-                if (sb.lastIndexOf("\r\n") != -1) {
+                JSONObject resultResponse = JSON.parseObject(socketResult.get("request").toString());
+                JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
+                JSONObject resultBody = JSON.parseObject(resultResponse.get("body").toString());
+                String requestType = resultBody.getString("requestType");
+                Integer requestId = resultHeader.getInteger("requestId");
+                if (requestType.equals("init")) {
+                    //灏忚溅澶嶄綅璇锋眰
+                    ShuttleCommand initCommand = getInitCommand(requestId);
+                    //鍙戝嚭璇锋眰
+                    NyShuttleHttpCommand httpCommand = JSON.parseObject(initCommand.getBody(), NyShuttleHttpCommand.class);
+                    JSONObject requestResult = requestCommand(httpCommand);
+
+                    removeIdx = i;//姝ゆ暟鎹凡缁忓鐞嗭紝浠庣粨鏋滈泦涓墧闄�
                     break;
                 }
-            } while (response != null);
-//            System.out.println("Received response from server: " + sb);
-
-            JSONObject result = JSON.parseObject(sb.toString());//寰楀埌璇锋眰缁撴灉闆�
-            if (!result.get("msgType").equals("requestMsg")) {//涓嶆槸璇锋眰鍐呭
-                return;
             }
 
-            JSONObject resultResponse = JSON.parseObject(result.get("request").toString());
-            JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
-            JSONObject resultBody = JSON.parseObject(resultResponse.get("body").toString());
-            String requestType = resultBody.getString("requestType");
-            Integer requestId = resultHeader.getInteger("requestId");
-            if (requestType.equals("init")) {
-                //灏忚溅澶嶄綅璇锋眰
-                ShuttleCommand initCommand = getInitCommand(requestId);
-                //鍙戝嚭璇锋眰
-                NyShuttleHttpCommand httpCommand = JSON.parseObject(initCommand.getBody(), NyShuttleHttpCommand.class);
-                JSONObject requestResult = requestCommand(httpCommand);
-                if (requestResult == null) {
-                    return;//璇锋眰澶辫触
-                }
+            if (removeIdx != -1) {
+                socketResults.remove(removeIdx);
             }
         } catch (Exception e) {
             e.printStackTrace();
@@ -645,14 +709,9 @@
     public boolean connect() {
         try {
             Socket socket = new Socket(device.getIp(),device.getPort());
-            socket.setSoTimeout(60000);
+            socket.setSoTimeout(10000);
             socket.setKeepAlive(true);
             this.socket = socket;
-            if (null == shuttleProtocol) {
-                shuttleProtocol = new ShuttleProtocol();
-                shuttleProtocol.setShuttleNo(Integer.parseInt(device.getDeviceNo()));
-            }
-            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
             log.info(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
         } catch (IOException e) {
             OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
@@ -719,43 +778,29 @@
         JSONObject data = JSON.parseObject(JSON.toJSONString(httpCommand));
         data.remove("nodes");
 
-        // 鑾峰彇杈撳叆娴佸拰杈撳嚭娴�
-        BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
+        // 鑾峰彇杈撳嚭娴�
         OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
         writer.write(JSON.toJSONString(data) + "\r\n");
         writer.flush();
 //            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
 
-        // 璇诲彇鏈嶅姟鍣ㄧ殑鍝嶅簲
-        StringBuffer sb = new StringBuffer();
-        String response = null;
-        char[] chars = new char[2048];//缂撳啿鍖�
-        do {
-            reader.read(chars);
-            String trim = new String(chars).trim();
-            sb.append(trim);
-            if (sb.lastIndexOf("\r\n") != -1) {
-                break;
+        // 鑾峰彇鏈嶅姟鍣ㄥ搷搴�
+        // 灏濊瘯10娆�
+        JSONObject result = null;
+        for (int i = 0; i < 10; i++) {
+            result = getRequestBody(httpCommand.getRequest().getHeader().getRequestId());
+            if (result == null) {
+                try {
+                    Thread.sleep(100);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
             }
-        } while (response != null);
-//            System.out.println("Received response from server: " + sb);
-
-        JSONObject result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
-        if (!result.get("msgType").equals("responseMsg")) {//涓嶆槸鍝嶅簲鍐呭
-            return null;
         }
-
-        JSONObject resultResponse = JSON.parseObject(result.get("response").toString());
-        JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
-        int responseId = Integer.parseInt(resultHeader.get("responseId").toString());
-        if (responseId != httpCommand.getRequest().getHeader().getRequestId()) {
-            return null;//鍝嶅簲ID涓庤姹侷D涓嶄竴鑷达紝涓嶅湪璋冭瘯妯″紡涓�
-        }
-
-        return filterBodyData(result);//杩斿洖Body缁撴灉闆�
+        return result;//杩斿洖Body缁撴灉闆�
     }
 
-    private static JSONObject filterBodyData(JSONObject data) {
+    private JSONObject filterBodyData(JSONObject data) {
         Object response = data.get("response");
         if (response == null) {
             return null;

--
Gitblit v1.9.1