From 084c97ce0069483ab1c7938755c89e5d7a834ad0 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 03 七月 2025 16:40:06 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java | 235 +++++++++++-----------------------------------------------
1 files changed, 45 insertions(+), 190 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 dcadce6..478e932 100644
--- a/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
@@ -3,15 +3,17 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.DateUtils;
+import com.core.common.SpringUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
+import com.zy.core.Utils.DeviceMsgUtils;
import com.zy.core.cache.OutputQueue;
+import com.zy.core.enums.SlaveType;
import com.zy.core.model.ShuttleSlave;
import com.zy.core.thread.ShuttleThread;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
-import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
@@ -29,9 +31,6 @@
private static final boolean DEBUG = false;//璋冭瘯妯″紡
- private List<JSONObject> socketReadResults = new ArrayList<>();
- private List<JSONObject> socketResults = new ArrayList<>();
-
public NyShuttleThread(ShuttleSlave slave, RedisUtil redisUtil) {
this.slave = slave;
this.redisUtil = redisUtil;
@@ -42,7 +41,7 @@
News.info("{}鍙峰洓鍚戣溅绾跨▼鍚姩", slave.getId());
this.connect();
- //鐩戝惉娑堟伅骞跺瓨鍌�
+ //鐩戝惉娑堟伅
Thread innerThread = new Thread(() -> {
while (true) {
try {
@@ -54,44 +53,50 @@
});
innerThread.start();
-// //璁惧鎵ц
-// Thread executeThread = new Thread(() -> {
-// while (true) {
-// try {
-// ShuttleAction shuttleAction = SpringUtils.getBean(ShuttleAction.class);
-// if (shuttleAction == null) {
-// continue;
-// }
-//
-// Object object = redisUtil.get(RedisKeyType.SHUTTLE_FLAG.key + slave.getId());
-// if (object == null) {
-// continue;
-// }
-//
-// Integer taskNo = Integer.valueOf(String.valueOf(object));
-// if (taskNo != 0) {
-// //瀛樺湪浠诲姟闇�瑕佹墽琛�
-// boolean result = shuttleAction.executeWork(slave.getId(), taskNo);
-// }
-//
-//// //灏忚溅绌洪棽涓旀湁璺戝簱绋嬪簭
-//// shuttleAction.moveLoc(slave.getId());
-//
-// //婕旂ず妯″紡
-// shuttleAction.demo(slave.getId());
-//
-// Thread.sleep(200);
-// } catch (Exception e) {
-// e.printStackTrace();
-// }
-// }
-// });
-// executeThread.start();
+ //鎵ц鎸囦护
+ Thread executeThread = new Thread(() -> {
+ while (true) {
+ try {
+ DeviceMsgUtils deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
+ Object deviceCommandMsg = deviceMsgUtils.getDeviceCommandMsg(SlaveType.Shuttle, slave.getId());
+ if (deviceCommandMsg == null) {
+ continue;
+ }
+ executeCommand(deviceCommandMsg);
+
+ Thread.sleep(200);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ executeThread.start();
+ }
+
+ private void executeCommand(Object deviceCommandMsg) {
+ try {
+ if (this.socket == null) {
+ return;
+ }
+
+ // 鑾峰彇杈撳嚭娴�
+ OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
+ writer.write(JSON.toJSONString(deviceCommandMsg) + "\r\n");
+ writer.flush();
+// System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
+ }catch (Exception e) {
+ e.printStackTrace();
+ }
}
private void listenSocketMessage() {
try {
if (this.socket == null) {
+ return;
+ }
+
+ DeviceMsgUtils deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
+ if(deviceMsgUtils == null) {
return;
}
@@ -110,73 +115,11 @@
}
JSONObject result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
-
- String msgType = result.getString("msgType");
- if ("responseMsg".equals(msgType)) {
- JSONObject response = result.getJSONObject("response");
- JSONObject body = response.getJSONObject("body");
- if (body.containsKey("workingMode")) {
- //read
- socketReadResults.add(body);
- return;
- }
- }
-
- if (!socketResults.isEmpty() && socketResults.size() >= 20) {
- socketResults.remove(0);//娓呯悊澶磋妭鐐�
- }
- socketResults.add(result);//娣诲姞鏁版嵁
+ deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, slave.getId(), result);
} catch (Exception e) {
-// e.printStackTrace();
+ e.printStackTrace();
}
}
-
-// public JSONObject getRequestBody(String type, String taskId) {
-// try {
-// // 鑾峰彇鏈嶅姟鍣ㄥ搷搴�
-// JSONObject result = null;
-// if (type.equals("readState")) {
-// type = "state";
-// }
-//
-// for (int i = 0; i < socketResults.size(); i++) {
-// JSONObject socketResult = socketResults.get(i);
-// if (!socketResult.get("msgType").equals("responseMsg")) {//涓嶆槸鍝嶅簲鍐呭
-// continue;
-// }
-//
-// JSONObject resultResponse = JSON.parseObject(socketResult.get("response").toString());
-// JSONObject resultBody = JSON.parseObject(resultResponse.get("body").toString());
-// String responseType = resultBody.get("responseType").toString();
-// if (DEBUG) {
-// result = socketResult;
-// break;
-// }
-//
-// if (!responseType.equals(type)) {
-// continue;//鍝嶅簲绫诲瀷涓庤姹傜被鍨嬩笉涓�鑷达紝涓嶅湪璋冭瘯妯″紡涓�
-// }
-//
-// if (taskId != null) {
-// String responseTaskId = resultBody.get("taskId").toString();
-// if (!responseTaskId.equals(taskId)) {
-// continue;//鍝嶅簲ID涓庤姹侷D涓嶄竴鑷达紝涓嶅湪璋冭瘯妯″紡涓�
-// }
-// }
-//
-// result = socketResult;
-// break;
-// }
-//
-// if (result == null) {
-// return null;//鏃犲搷搴旂粨鏋�
-// }
-//
-// return filterBodyData(result);//杩斿洖Body缁撴灉闆�
-// } catch (Exception e) {
-// return null;
-// }
-// }
@Override
public boolean connect() {
@@ -201,92 +144,4 @@
public void close() {
}
-
-// //鍙戝嚭璇锋眰
-// private JSONObject requestCommand(NyShuttleHttpCommand httpCommand) throws IOException {
-// try {
-// if (this.socket == null) {
-// return null;
-// }
-//
-// //鍘嬬缉鏁版嵁鍖�
-// JSONObject data = JSON.parseObject(JSON.toJSONString(httpCommand));
-// data.remove("nodes");
-//
-// // 鑾峰彇杈撳嚭娴�
-// OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
-// writer.write(JSON.toJSONString(data) + "\r\n");
-// writer.flush();
-//// System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
-//
-// String requestType = null;
-// String taskId = null;
-// try {
-// requestType = httpCommand.getRequest().getBody().get("requestType").toString();
-// taskId = httpCommand.getRequest().getBody().get("taskId").toString();
-// } catch (Exception e) {
-//// return null;
-// //taskId鍙兘鍙栫┖锛屼笉鎶ラ敊锛屾甯告儏鍐�
-// }
-//
-// // 鑾峰彇鏈嶅姟鍣ㄥ搷搴�
-// // 灏濊瘯10娆�
-// JSONObject result = null;
-// for (int i = 0; i < 10; i++) {
-// result = getRequestBody(requestType, taskId);
-// if (result == null) {
-// try {
-// Thread.sleep(100);
-// } catch (Exception e) {
-// e.printStackTrace();
-// }
-// }else {
-// break;
-// }
-// }
-// return result;//杩斿洖Body缁撴灉闆�
-// }catch (Exception e) {
-// e.printStackTrace();
-// }
-// return null;
-// }
-//
-// private void requestCommandAsync(NyShuttleHttpCommand httpCommand) throws IOException {
-// if (this.socket == null) {
-// return;
-// }
-//
-// try {
-// //鍘嬬缉鏁版嵁鍖�
-// JSONObject data = JSON.parseObject(JSON.toJSONString(httpCommand));
-// data.remove("nodes");
-//
-// // 鑾峰彇杈撳嚭娴�
-// OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
-// writer.write(JSON.toJSONString(data) + "\r\n");
-// writer.flush();
-//// System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
-// }catch (Exception e) {
-//// e.printStackTrace();
-//// System.out.println("socket write error");
-// this.socket.close();
-// this.socket = null;
-// }
-// }
-//
-// private JSONObject filterBodyData(JSONObject data) {
-// Object response = data.get("response");
-// if (response == null) {
-// return null;
-// }
-//
-// JSONObject result = JSON.parseObject(response.toString());
-// Object body = result.get("body");
-// if (body == null) {
-// return null;
-// }
-// JSONObject jsonBody = JSON.parseObject(body.toString());
-// return jsonBody;
-// }
-
}
--
Gitblit v1.9.1