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 |   52 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/NyShuttleThread.java b/src/main/java/com/zy/core/thread/NyShuttleThread.java
index 1ab039c..788a303f 100644
--- a/src/main/java/com/zy/core/thread/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/NyShuttleThread.java
@@ -28,6 +28,8 @@
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 
+import java.io.IOException;
+import java.net.Socket;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -43,6 +45,7 @@
     private ShuttleSlave slave;
     private NyShuttleProtocol shuttleProtocol;
     private RedisUtil redisUtil;
+    private Socket socket;
 
     public NyShuttleThread(ShuttleSlave slave,RedisUtil redisUtil) {
         this.slave = slave;
@@ -84,6 +87,10 @@
 
     private void read() {
         try {
+            if (this.socket == null || this.socket.isClosed()) {
+                //閾炬帴鏂紑閲嶆柊閾炬帴
+                this.connect();
+            }
             readStatus();
             //鍥涘悜绌挎杞︾┖闂层�佹湁浠诲姟銆佹爣璁颁负true銆佸瓨鍦ㄤ换鍔℃寚浠わ紝闇�瑕佹墽琛屼换鍔$殑涓嬩竴鏉℃寚浠�
             if (shuttleProtocol.getFree() == ShuttleStatusType.IDLE.id
@@ -108,9 +115,10 @@
 
             //----------璇诲彇鍥涘悜绌挎杞︾姸鎬�-----------
             NyShuttleHttpCommand readStatusCommand = NyHttpUtils.getReadStatusCommand(slave.getId());
-            JSONObject jsonObject = NyHttpUtils.requestCommand(readStatusCommand);
+            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"));
@@ -159,7 +167,7 @@
                     errCode.add(Integer.parseInt(o.toString()));
                 }
                 //鏁呴殰鐮�
-                shuttleProtocol.setErrCode(errCode);
+                shuttleProtocol.setErrCode(errCode.get(0));
                 //鎬婚噷绋嬫暟
                 shuttleProtocol.setStatusSum(jsonObject.getObject("statusSum", NyShuttleProtocol.StatusSumClass.class));
                 //闈炶嚜鍔ㄧ姸鎬佹椂闂磋鏃�
@@ -236,12 +244,29 @@
             }
         } 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();
+            }
         }
     }
 
     @Override
     public boolean connect() {
+        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()));
+        }
         return true;
     }
 
@@ -268,7 +293,21 @@
         }
 
         //鍙戝嚭璇锋眰
-        JSONObject result = NyHttpUtils.requestCommand(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;//璇锋眰澶辫触
         }
@@ -399,7 +438,8 @@
                     null,
                     null,
                     JSON.toJSONString(command),
-                    null
+                    null,
+                    JSON.toJSONString(shuttleProtocol)
             );
             shuttleOptService.insert(opt);
         }

--
Gitblit v1.9.1