From 8a1502682476d035936b956d6a991f1257d96f81 Mon Sep 17 00:00:00 2001
From: zy <zy@123>
Date: 星期五, 01 八月 2025 16:09:38 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java | 146 +++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 130 insertions(+), 16 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..13c0017 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.*;
@@ -30,6 +34,7 @@
private Socket socket;
private boolean stopThread = false;
private HashMap<Integer, String> resultKeyMap = new HashMap<Integer, String>();
+ private long lastConnectTime = System.currentTimeMillis();
public NyShuttleThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
this.deviceConfig = deviceConfig;
@@ -106,12 +111,26 @@
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;
+ }
+ }
+
private void listenSocketMessage() {
+ StringBuffer sb = new StringBuffer();
try {
if (this.socket == null) {
return;
@@ -129,18 +148,27 @@
// 鑾峰彇杈撳叆娴�
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;
}
}
- 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 +193,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 +203,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 +214,11 @@
deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel);
}
}
- } catch (Exception e) {
-// e.printStackTrace();
+ } catch (SocketTimeoutException e) {
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ closeSocket();
}
}
@@ -247,13 +278,22 @@
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(System.currentTimeMillis() - lastConnectTime < 1000 * 10) {
+ 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;
+ this.lastConnectTime = System.currentTimeMillis();
+ 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 +301,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 +388,8 @@
this.stopThread = true;
}
+ @Override
+ public Socket getSocket() {
+ return this.socket;
+ }
}
--
Gitblit v1.9.1