From 7d154cd8bcd274a53ad573e95c574ad936b4e249 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 07 七月 2025 07:46:00 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java | 347 ++++++++++++++++++++++++++-------------------------------
1 files changed, 156 insertions(+), 191 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..fbb504c 100644
--- a/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
@@ -2,18 +2,21 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
-import com.core.common.DateUtils;
+import com.zy.common.SpringUtils;
+import com.zy.common.utils.DateUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
+import com.zy.core.model.DeviceCommandMsgModel;
+import com.zy.core.model.DeviceMsgModel;
+import com.zy.core.properties.DeviceConfig;
+import com.zy.core.utils.DeviceMsgUtils;
import com.zy.core.cache.OutputQueue;
-import com.zy.core.model.ShuttleSlave;
+import com.zy.core.enums.SlaveType;
import com.zy.core.thread.ShuttleThread;
+import io.netty.handler.timeout.ReadTimeoutException;
import lombok.extern.slf4j.Slf4j;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
+import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.text.MessageFormat;
@@ -23,29 +26,30 @@
@SuppressWarnings("all")
public class NyShuttleThread implements ShuttleThread {
- private ShuttleSlave slave;
+ private DeviceConfig deviceConfig;
private RedisUtil redisUtil;
private Socket socket;
+ private boolean stopThread = false;
- 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;
+ public NyShuttleThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
+ this.deviceConfig = deviceConfig;
this.redisUtil = redisUtil;
}
@Override
public void run() {
- News.info("{}鍙峰洓鍚戣溅绾跨▼鍚姩", slave.getId());
- this.connect();
+ News.info("{}鍙峰洓鍚戣溅绾跨▼鍚姩", deviceConfig.getDeviceNo());
- //鐩戝惉娑堟伅骞跺瓨鍌�
+ //鐩戝惉娑堟伅
Thread innerThread = new Thread(() -> {
while (true) {
+ if(stopThread) {
+ break;
+ }
+
try {
+ this.connect();
+ Thread.sleep(200);
listenSocketMessage();
} catch (Exception e) {
e.printStackTrace();
@@ -54,44 +58,66 @@
});
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) {
+ if(stopThread) {
+ break;
+ }
+
+ try {
+ DeviceMsgUtils deviceMsgUtils = null;
+ try {
+ deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
+ }catch (Exception e){}
+ if (deviceMsgUtils == null) {
+ continue;
+ }
+ DeviceCommandMsgModel deviceCommandMsg = deviceMsgUtils.getDeviceCommandMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo());
+ if (deviceCommandMsg == null) {
+ continue;
+ }
+ executeCommand(deviceCommandMsg);
+
+ Thread.sleep(200);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ executeThread.start();
+ }
+
+ private void executeCommand(DeviceCommandMsgModel deviceCommandMsg) {
+ try {
+ if (this.socket == null) {
+ return;
+ }
+
+ Object command = deviceCommandMsg.getCommand();
+
+ // 鑾峰彇杈撳嚭娴�
+ OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
+ writer.write(JSON.toJSONString(command) + "\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 = null;
+ try {
+ deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
+ } catch (Exception e) {
+ }
+ if (deviceMsgUtils == null) {
return;
}
@@ -110,6 +136,7 @@
}
JSONObject result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
+ log.info("鏀跺埌Server Data: {}", JSON.toJSONString(result));
String msgType = result.getString("msgType");
if ("responseMsg".equals(msgType)) {
@@ -117,80 +144,95 @@
JSONObject body = response.getJSONObject("body");
if (body.containsKey("workingMode")) {
//read
- socketReadResults.add(body);
+ JSONObject data = parseSocketResult(body);
+
+ DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
+ deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo());
+ deviceMsgModel.setDeviceMsgType("status");
+ deviceMsgModel.setDeviceMsg(data);
+ deviceMsgModel.setDeviceOriginMsg(sb.toString());
+ deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel);
return;
}
}
- if (!socketResults.isEmpty() && socketResults.size() >= 20) {
- socketResults.remove(0);//娓呯悊澶磋妭鐐�
- }
- socketResults.add(result);//娣诲姞鏁版嵁
+ DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
+ deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo());
+ deviceMsgModel.setDeviceMsgType("command");
+ deviceMsgModel.setDeviceMsg(result);
+ deviceMsgModel.setDeviceOriginMsg(sb.toString());
+ deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, deviceConfig.getDeviceNo(), deviceMsgModel);
} catch (Exception e) {
// 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;
-// }
-// }
+ public JSONObject parseSocketResult(JSONObject data) {
+ JSONObject device = new JSONObject();
+
+ //灏忚溅璁惧鐘舵��
+ device.put("deviceStatus", data.getInteger("free"));
+ //灏忚溅妯″紡
+ device.put("mode", data.getInteger("workingMode"));
+ //褰撳墠浜岀淮鐮�
+ device.put("currentCode", data.getString("point"));
+ //鐢垫睜鐢甸噺
+ device.put("batteryPower", data.getString("powerPercent"));
+ //鐢垫睜鐢靛帇
+ device.put("batteryVoltage", data.getInteger("voltage"));
+ //鏁呴殰
+ device.put("errorCode", data.getJSONArray("errCode").getString(0));
+
+ //鏄惁椤跺崌
+ device.put("hasLift", data.getInteger("liftPosition") == 2 ? true : false);
+ //鏄惁鏈夋墭鐩�
+ device.put("hasPallet", data.getInteger("loadState") == 1 ? true : false);
+ //琛岄┒鏂瑰悜
+ device.put("runDirection", data.getString("runDir") == null ? "none" : data.getString("runDir"));
+ //鏄惁涓哄厖鐢电姸鎬�
+ device.put("hasCharge", data.getInteger("chargState") == 1 ? true : false);
+ //杩愯閫熷害
+ device.put("speed", data.getInteger("speed"));
+
+ //*********璇诲彇鎵╁睍瀛楁**********
+
+ JSONObject extend = new JSONObject();
+ device.put("extend", extend);
+
+ //绠″埗鐘舵��
+ extend.put("suspendState", data.getInteger("suspendState"));
+ //鏈�楂樼數鑺數鍘�(mV)
+ extend.put("maxCellVoltage", data.getInteger("maxCellVoltage"));
+ //鏈�浣庣數鑺數鍘�(mV)
+ extend.put("minCellVoltage", data.getInteger("minCellVoltage"));
+ //鐢垫睜鐢靛帇
+ extend.put("voltage", data.getInteger("voltage"));
+ //鍏呮斁鐢靛惊鐜鏁�
+ extend.put("chargeCycleTimes", data.getInteger("chargeCycleTimes"));
+ //鍓╀綑鐢甸噺
+ extend.put("surplusQuantity", data.getInteger("surplusQuantity"));
+ //鎬荤數閲�
+ extend.put("countQuantity", data.getInteger("countQuantity"));
+ return device;
+ }
@Override
public boolean connect() {
try {
- InetAddress address = InetAddress.getByName(slave.getIp());
+ if(this.socket != null) {
+ return true;
+ }
+
+ InetAddress address = InetAddress.getByName(deviceConfig.getIp());
if (address.isReachable(10000)) {
- Socket socket = new Socket(slave.getIp(), slave.getPort());
+ 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()), slave.getId(), slave.getIp(), slave.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()), slave.getId(), slave.getIp(), slave.getPort()));
+ OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
return false;
}
@@ -202,91 +244,14 @@
}
-// //鍙戝嚭璇锋眰
-// 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;
-// }
+ @Override
+ public DeviceConfig getDeviceConfig() {
+ return this.deviceConfig;
+ }
+
+ @Override
+ public void stopThread() {
+ this.stopThread = true;
+ }
}
--
Gitblit v1.9.1