From 2b6156951c18ea9ff60ed5578db633cebbe99fa7 Mon Sep 17 00:00:00 2001
From: Junjie <xjj@123>
Date: 星期四, 25 九月 2025 10:46:16 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java | 43 ++++++++++++++++++++++++++++++++-----------
1 files changed, 32 insertions(+), 11 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 ccaffca..9f54ac8 100644
--- a/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
@@ -15,6 +15,7 @@
import com.zy.core.enums.SlaveType;
import com.zy.core.thread.ShuttleThread;
import com.zy.core.utils.FakeDeviceUtils;
+import com.zy.core.utils.TimeoutExecutor;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
@@ -24,6 +25,7 @@
import java.net.SocketTimeoutException;
import java.text.MessageFormat;
import java.util.*;
+import java.util.concurrent.TimeUnit;
@Slf4j
@SuppressWarnings("all")
@@ -54,8 +56,13 @@
try {
this.connect();
+ // 鍦ㄥ惊鐜腑浣跨敤
+ boolean result = TimeoutExecutor.executeWithTimeout(
+ () -> this.listenSocketMessage(),
+ 30, // 30绉掕秴鏃�
+ TimeUnit.SECONDS
+ );
Thread.sleep(200);
- listenSocketMessage();
} catch (Exception e) {
e.printStackTrace();
}
@@ -82,6 +89,8 @@
if (deviceCommandMsg == null) {
continue;
}
+
+ this.connect();
executeCommand(deviceCommandMsg);
Thread.sleep(200);
@@ -129,10 +138,11 @@
}
}
- private void listenSocketMessage() {
+ private boolean listenSocketMessage() {
+ StringBuffer sb = new StringBuffer();
try {
if (this.socket == null) {
- return;
+ return false;
}
DeviceMsgUtils deviceMsgUtils = null;
@@ -141,19 +151,20 @@
} catch (Exception e) {
}
if (deviceMsgUtils == null) {
- return;
+ return false;
}
// 鑾峰彇杈撳叆娴�
BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.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) {
+ int lastIndexOf = trim.lastIndexOf("\r\n");
+ if (lastIndexOf != -1) {
+ trim = trim.substring(0, lastIndexOf);
+ sb.append(trim);
break;
}
}
@@ -164,12 +175,20 @@
}catch (Exception e){}
if(result == null) {
- return;
+ return false;
}
+ JSONObject header = null;
+ JSONObject body = null;
JSONObject response = result.getJSONObject("response");
- JSONObject header = response.getJSONObject("header");
- JSONObject body = response.getJSONObject("body");
+ if (response == null) {
+ JSONObject request = result.getJSONObject("request");
+ header = request.getJSONObject("header");
+ body = request.getJSONObject("body");
+ }else {
+ header = response.getJSONObject("header");
+ body = response.getJSONObject("body");
+ }
String msgType = result.getString("msgType");
@@ -213,11 +232,13 @@
}
}
} catch (SocketTimeoutException e) {
-
+ return false;
} catch (IOException e) {
e.printStackTrace();
closeSocket();
+ return false;
}
+ return true;
}
public JSONObject parseSocketResult(JSONObject data) {
--
Gitblit v1.9.1