From 1443e13e91ffb1bc6548722ef6a7a3b68d0ceed9 Mon Sep 17 00:00:00 2001
From: zy <zy@123>
Date: 星期三, 16 七月 2025 10:32:21 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/impl/NyShuttleThread.java |  132 +++++++++++++++++++++++++++++++++++++++----
 1 files changed, 119 insertions(+), 13 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 9a9aa99..2544c14 100644
--- a/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
@@ -6,6 +6,7 @@
 import com.zy.common.utils.DateUtils;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.News;
+import com.zy.core.enums.RedisKeyType;
 import com.zy.core.model.DeviceCommandMsgModel;
 import com.zy.core.model.DeviceMsgModel;
 import com.zy.core.properties.DeviceConfig;
@@ -13,11 +14,14 @@
 import com.zy.core.cache.OutputQueue;
 import com.zy.core.enums.SlaveType;
 import com.zy.core.thread.ShuttleThread;
+import com.zy.core.utils.FakeDeviceUtils;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.*;
 import java.net.InetAddress;
 import java.net.Socket;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
 import java.text.MessageFormat;
 import java.util.*;
 
@@ -106,8 +110,21 @@
             writer.write(command + "\r\n");
             writer.flush();
 //            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
-        }catch (Exception e) {
+        }catch (SocketException e) {
             e.printStackTrace();
+            closeSocket();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void closeSocket() {
+        try {
+            this.socket.close();
+        } catch (Exception e1) {
+
+        }finally {
+            this.socket = null;
         }
     }
 
@@ -140,7 +157,15 @@
                 }
             }
 
-            JSONObject result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
+            JSONObject result = null;
+            try {
+                result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
+            }catch (Exception e){}
+
+            if(result == null) {
+                return;
+            }
+
             JSONObject response = result.getJSONObject("response");
             JSONObject header = response.getJSONObject("header");
             JSONObject body = response.getJSONObject("body");
@@ -165,7 +190,7 @@
                     deviceMsgModel.setResultKey(resultKey);
                     deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel);
                 }else {
-                    log.info("鏀跺埌Server Command Data: {}", JSON.toJSONString(result));
+                    log.info("鏀跺埌Rcs Shuttle Command Data: {}", JSON.toJSONString(result));
                     DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
                     deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo());
                     deviceMsgModel.setDeviceMsgType("command");
@@ -175,7 +200,7 @@
                     deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel);
                 }
             } else if ("requestMsg".equals(msgType)) {
-                log.info("鏀跺埌Server Init Data: {}", JSON.toJSONString(result));
+                log.info("鏀跺埌Shuttle Init Data: {}", JSON.toJSONString(result));
                 String requestType = body.getString("requestType");
                 if (requestType.equals("init")) {
                     DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
@@ -186,8 +211,11 @@
                     deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel);
                 }
             }
-        } catch (Exception e) {
-//            e.printStackTrace();
+        } catch (SocketTimeoutException e) {
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            closeSocket();
         }
     }
 
@@ -247,13 +275,17 @@
                 return true;
             }
 
-            InetAddress address = InetAddress.getByName(deviceConfig.getIp());
-            if (address.isReachable(10000)) {
-                Socket socket = new Socket(deviceConfig.getIp(), deviceConfig.getPort());
-                socket.setSoTimeout(10000);
-                socket.setKeepAlive(true);
-                this.socket = socket;
-                log.info(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
+            if(this.deviceConfig.getFake()) {
+                return fakeConnect();
+            }else {
+                InetAddress address = InetAddress.getByName(deviceConfig.getIp());
+                if (address.isReachable(10000)) {
+                    Socket deviceSocket = new Socket(deviceConfig.getIp(), deviceConfig.getPort());
+                    deviceSocket.setSoTimeout(10000);
+                    deviceSocket.setKeepAlive(true);
+                    this.socket = deviceSocket;
+                    log.info(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
+                }
             }
         } catch (Exception e) {
             OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
@@ -261,6 +293,76 @@
         }
 
         return true;
+    }
+
+    private boolean fakeConnect() {
+        try {
+            FakeDeviceUtils fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
+            if(fakeDeviceUtils == null) {
+                return false;
+            }
+
+            boolean applyResult = fakeDeviceUtils.applyShuttleConnect(deviceConfig.getDeviceNo());
+            if(!applyResult) {
+                return false;
+            }
+
+            InetAddress address = InetAddress.getByName(deviceConfig.getIp());
+            if (address.isReachable(10000)) {
+                Socket deviceSocket = new Socket(deviceConfig.getIp(), deviceConfig.getPort());
+                deviceSocket.setSoTimeout(10000);
+                deviceSocket.setKeepAlive(true);
+
+                //铏氭嫙璁惧闇�瑕佷笂鎶ヨ澶囦俊鎭�
+                HashMap<String, Object> map = new HashMap<>();
+                map.put("msgType", "fakeDeviceFirstConnect");
+                map.put("deviceConfig", this.deviceConfig);
+
+                // 鑾峰彇杈撳嚭娴�
+                OutputStreamWriter writer = new OutputStreamWriter(deviceSocket.getOutputStream());
+                writer.write(JSON.toJSONString(map) + "\r\n");
+                writer.flush();
+
+                // 鑾峰彇杈撳叆娴�
+                BufferedReader reader = new BufferedReader(new InputStreamReader(deviceSocket.getInputStream()));
+                // 璇诲彇鏈嶅姟鍣ㄧ殑鍝嶅簲
+                StringBuffer sb = new StringBuffer();
+                char[] chars = new char[2048];//缂撳啿鍖�
+                while (true) {
+                    reader.read(chars);
+                    String trim = new String(chars);
+                    sb.append(trim);
+                    if (trim.lastIndexOf("\r\n") != -1) {
+                        break;
+                    }
+                }
+
+                JSONObject result = null;
+                try {
+                    result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
+                }catch (Exception e){}
+
+                if(result == null) {
+                    return false;
+                }
+
+                if(result.getInteger("deviceNo") != deviceConfig.getDeviceNo()) {
+                    return false;
+                }
+
+                if(!result.getString("status").equals("success")) {
+                    return false;
+                }
+
+                this.socket = deviceSocket;
+
+                log.info(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
+                return true;
+            }
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+        return false;
     }
 
     @Override
@@ -278,4 +380,8 @@
         this.stopThread = true;
     }
 
+    @Override
+    public Socket getSocket() {
+        return this.socket;
+    }
 }

--
Gitblit v1.9.1