From c7f699711682ff7a99d4304f35d12d49e25235cc Mon Sep 17 00:00:00 2001
From: zy <zy@123>
Date: 星期一, 14 七月 2025 16:50:44 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/common/utils/RedisUtil.java           |    3 +
 src/main/java/com/zy/core/thread/impl/NyShuttleThread.java |  132 +++++++++++++++++++++++++++-----------------
 src/main/java/com/zy/core/utils/FakeDeviceUtils.java       |    9 +++
 src/main/java/com/zy/core/enums/RedisKeyType.java          |    3 +
 4 files changed, 96 insertions(+), 51 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/RedisUtil.java b/src/main/java/com/zy/common/utils/RedisUtil.java
index ba150e5..b97506c 100644
--- a/src/main/java/com/zy/common/utils/RedisUtil.java
+++ b/src/main/java/com/zy/common/utils/RedisUtil.java
@@ -1,6 +1,7 @@
 package com.zy.common.utils;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisCallback;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Component;
@@ -134,6 +135,7 @@
     public boolean set(String key, Object value) {
         try {
             redisTemplate.opsForValue().set(key, value);
+            redisTemplate.execute((RedisCallback<Void>) connection -> null);
             long start = System.currentTimeMillis();
             while (System.currentTimeMillis() - start < 10000) {//鏈夋晥鏈�10s
                 Object o = redisTemplate.opsForValue().get(key);
@@ -167,6 +169,7 @@
             } else {
                 set(key, value);
             }
+            redisTemplate.execute((RedisCallback<Void>) connection -> null);
             return true;
         } catch (Exception e) {
             e.printStackTrace();
diff --git a/src/main/java/com/zy/core/enums/RedisKeyType.java b/src/main/java/com/zy/core/enums/RedisKeyType.java
index 23b7baa..3fa64e7 100644
--- a/src/main/java/com/zy/core/enums/RedisKeyType.java
+++ b/src/main/java/com/zy/core/enums/RedisKeyType.java
@@ -17,6 +17,9 @@
     DEVICE_CONFIG("deviceConfig"),
     //铏氭嫙璁惧閰嶇疆鏂囦欢
     FAKE_DEVICE_CONFIG("fakeDeviceConfig"),
+
+    //铏氭嫙灏忚溅鐢宠杩炴帴鏉冮檺
+    FAKE_SHUTTLE_APPLY_CONNECT("fakeShuttleApplyConnect"),
     ;
 
     public String key;
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 549f350..2f17b9d 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,6 +14,7 @@
 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.*;
@@ -270,59 +272,17 @@
                 return true;
             }
 
-            InetAddress address = InetAddress.getByName(deviceConfig.getIp());
-            if (address.isReachable(10000)) {
-                Socket deviceSocket = new Socket(deviceConfig.getIp(), deviceConfig.getPort());
-                deviceSocket.setSoTimeout(10000);
-                deviceSocket.setKeepAlive(true);
-
-                if (this.deviceConfig.getFake()) {
-                    //铏氭嫙璁惧闇�瑕佷笂鎶ヨ澶囦俊鎭�
-                    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;
-                    }
-
+            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;
-                }else {
-                    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()));
                 }
-                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()));
@@ -332,6 +292,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
     public void close() {
 
diff --git a/src/main/java/com/zy/core/utils/FakeDeviceUtils.java b/src/main/java/com/zy/core/utils/FakeDeviceUtils.java
index c268c07..a77f72f 100644
--- a/src/main/java/com/zy/core/utils/FakeDeviceUtils.java
+++ b/src/main/java/com/zy/core/utils/FakeDeviceUtils.java
@@ -27,6 +27,15 @@
     @Autowired
     private RedisUtil redisUtil;
 
+    public synchronized boolean applyShuttleConnect(Integer deviceNo) {
+        Object object = redisUtil.get(RedisKeyType.FAKE_SHUTTLE_APPLY_CONNECT.key);
+        if(object != null) {
+            return false;
+        }
+
+        return redisUtil.set(RedisKeyType.FAKE_SHUTTLE_APPLY_CONNECT.key, deviceNo, 10);
+    }
+
     public List<DeviceConfig> getFakeDeviceConfig() {
         List<DeviceConfig> list = new ArrayList<>();
         Object obj = redisUtil.get(RedisKeyType.FAKE_DEVICE_CONFIG.key);

--
Gitblit v1.9.1