From 1443e13e91ffb1bc6548722ef6a7a3b68d0ceed9 Mon Sep 17 00:00:00 2001
From: zy <zy@123>
Date: 星期三, 16 七月 2025 10:32:21 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java | 166 +++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 125 insertions(+), 41 deletions(-)
diff --git a/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java b/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
index 9f7f165..2333ccc 100644
--- a/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
@@ -18,6 +18,7 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@@ -29,6 +30,7 @@
private JSONObject fakeStatusDemo = JSONObject.parseObject("{\"mode\":1,\"extend\":{\"countQuantity\":400,\"suspendState\":0,\"minCellVoltage\":3279,\"chargeCycleTimes\":0,\"maxCellVoltage\":3281,\"surplusQuantity\":204,\"voltage\":5248},\"hasLift\":false,\"hasPallet\":false,\"batteryVoltage\":5248,\"runDirection\":\"2\",\"currentCode\":\"{\\\"x\\\":19,\\\"y\\\":11,\\\"z\\\":2}\",\"errorCode\":\"0\",\"hasCharge\":false,\"batteryPower\":\"51\",\"speed\":0,\"deviceStatus\":1}");
private ServerSocket serverSocket;
private Integer gatewayPort;
+ private boolean enableFakeAndDeviceThread;
private ConcurrentHashMap<String, Thread> fakeThreadMap = new ConcurrentHashMap();
private ConcurrentHashMap<String, Socket> fakeServerMap = new ConcurrentHashMap();
private ConcurrentHashMap<String, JSONObject> fakeStatusMap = new ConcurrentHashMap();
@@ -36,15 +38,110 @@
private boolean fake = false;
- public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort) {
+ public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort, boolean enableFakeAndDeviceThread) {
this.redisUtil = redisUtil;
this.gatewayPort = gatewayPort;
+ this.enableFakeAndDeviceThread = enableFakeAndDeviceThread;
}
@Override
public void run() {
News.info("Fake Server is Started");
+ acceptorThread();
+ fakeCommandThread();
+
+ while (true) {
+ try {
+ initFakeDeviceServer();
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private void acceptorThread() {
+ Thread acceptorThread = new Thread(() -> {
+ log.info("{}:acceptorThread is start");
+ try {
+ while (true) {
+ try {
+ if (serverSocket == null) {
+ serverSocket = new ServerSocket(gatewayPort);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+
+ if(serverSocket == null){
+ continue;
+ }
+
+ Socket fakeSocket = serverSocket.accept();
+ InputStream inputStream = fakeSocket.getInputStream();
+ if(inputStream == null){
+ continue;
+ }
+ // 鑾峰彇杈撳叆娴�
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+ // 璇诲彇鏈嶅姟鍣ㄧ殑鍝嶅簲
+ 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;
+ }
+ }
+
+// log.info("acceptorThread is end:{}", sb.toString());
+ JSONObject result = null;
+ try {
+ result = JSON.parseObject(sb.toString());
+ }catch (Exception e) {
+ log.error("error body:{}", sb.toString());
+ e.printStackTrace();
+ }
+ if(result == null){
+ continue;
+ }
+
+ Object msgType = result.get("msgType");
+ if (msgType != null) {
+ if ("fakeDeviceFirstConnect".equals(msgType)) {
+ DeviceConfig device = JSON.parseObject(JSON.toJSONString(result.get("deviceConfig")), DeviceConfig.class);
+ log.info("{}:device is start,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap));
+
+ String fakeStatus = JSON.toJSONString(fakeStatusDemo);
+ if (!Cools.isEmpty(device.getFakeInitStatus())) {
+ fakeStatus = device.getFakeInitStatus();
+ }
+
+ fakeServerMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeSocket);
+ fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), JSON.parseObject(fakeStatus));
+
+ HashMap<String, Object> map = new HashMap<>();
+ map.put("deviceNo", device.getDeviceNo());
+ map.put("status", "success");
+ // 鑾峰彇杈撳嚭娴�
+ OutputStreamWriter writer = new OutputStreamWriter(fakeSocket.getOutputStream());
+ writer.write(JSON.toJSONString(map) + "\r\n");
+ writer.flush();
+ }
+ }
+
+ Thread.sleep(1000);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ });
+ acceptorThread.start();
+ }
+
+ private void fakeCommandThread() {
Thread fakeCommandThread = new Thread(() -> {
while (true) {
try {
@@ -65,26 +162,6 @@
}
});
fakeCommandThread.start();
-
- while (true) {
- try {
- try {
- if (serverSocket == null) {
- serverSocket = new ServerSocket(gatewayPort);
- }
- }catch (Exception e){
- e.printStackTrace();
- }
-
- if(serverSocket == null){
- continue;
- }
-
- initFakeDeviceServer();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
}
private synchronized void initFakeDeviceServer() {
@@ -109,25 +186,30 @@
continue;
}
+ if (enableFakeAndDeviceThread) {
+ NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getDeviceNo());
+ if (shuttleThread == null) {
+ // init
+ NyShuttleThread thread = new NyShuttleThread(device, redisUtil);;
+ new Thread(thread).start();
+ SlaveConnection.put(SlaveType.Shuttle, device.getDeviceNo(), thread);
+ }else {
+ Socket socket = shuttleThread.getSocket();
+ if(socket == null){
+ continue;
+ }
+ }
+ }
+
log.info("{}:device is run,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap));
Thread fakeThread = new Thread(() -> {
log.info("{}:device is start handle client", device.getDeviceNo());
try {
while (true) {
- if(!fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())){
- log.info("{}:device is start,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap));
- Socket fakeSocket = serverSocket.accept();
- String fakeStatus = JSON.toJSONString(fakeStatusDemo);
- if (!Cools.isEmpty(device.getFakeInitStatus())) {
- fakeStatus = device.getFakeInitStatus();
- }
-
- fakeServerMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeSocket);
- fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), JSON.parseObject(fakeStatus));
+ if(fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())){
+ handleClient(device);
}
-
- handleClient(device);
}
}catch (Exception e){
e.printStackTrace();
@@ -136,10 +218,9 @@
fakeThread.start();
fakeThreadMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeThread);
- // init
- ThreadHandler thread = new NyShuttleThread(device, redisUtil);;
- new Thread(thread).start();
- SlaveConnection.put(SlaveType.Shuttle, device.getDeviceNo(), thread);
+ try {
+ Thread.sleep(2000);
+ }catch (Exception e){}
}
}
@@ -331,7 +412,7 @@
fakeCommandMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeCommand);
response = genereateFakeCommandResponse(requestId, taskId, requestType);
} else if (requestType.equals("readState")) {
- response = genereateFakeStatusResponse(requestId, fakeStatus);
+ response = genereateFakeStatusResponse(requestId, fakeStatus, device.getDeviceNo());
}
fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeStatus);
@@ -343,11 +424,11 @@
}
}
- public String genereateFakeStatusResponse(Integer taskId, JSONObject fakeStatus) {
+ public String genereateFakeStatusResponse(Integer taskId, JSONObject fakeStatus, Integer deviceNo) {
JSONObject result = new JSONObject();
JSONObject response = new JSONObject();
result.put("msgType", "responseMsg");
- result.put("robotId", 5001);
+ result.put("robotId", deviceNo);
result.put("response", response);
JSONObject header = new JSONObject();
@@ -364,7 +445,10 @@
body.put("point", fakeStatus.getString("currentCode"));
body.put("powerPercent", fakeStatus.getString("batteryPower"));
body.put("voltage", fakeStatus.getInteger("batteryVoltage"));
- body.put("errCode", new ArrayList<Integer>(){{add(fakeStatus.getInteger("errorCode"));add(0);}});
+ body.put("errCode", new ArrayList<Integer>() {{
+ add(fakeStatus.getInteger("errorCode"));
+ add(0);
+ }});
body.put("liftPosition", fakeStatus.getBoolean("hasLift") == true ? 2 : 1);
body.put("loadState", fakeStatus.getBoolean("hasPallet") == true ? 1 : 0);
body.put("runDir", fakeStatus.getString("runDirection"));
--
Gitblit v1.9.1