From b821b6c3719f5eac49bfff58af77a9b4ed97f544 Mon Sep 17 00:00:00 2001
From: Junjie <540245094@qq.com>
Date: 星期三, 09 七月 2025 10:11:36 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java | 89 +++++++++++++++++++++++++++++++-------------
1 files changed, 62 insertions(+), 27 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 53e7eee..8a74dff 100644
--- a/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
@@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
@Slf4j
@SuppressWarnings("all")
@@ -29,6 +28,8 @@
private RedisUtil redisUtil;
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 HashMap<String, Thread> fakeThreadMap = new HashMap();
private HashMap<String, Socket> fakeServerMap = new HashMap();
private HashMap<String, JSONObject> fakeStatusMap = new HashMap();
private HashMap<String, JSONObject> fakeCommandMap = new HashMap();
@@ -42,8 +43,46 @@
@Override
public void run() {
News.info("Fake Server is Started");
+
+ Thread fakeCommandThread = new Thread(() -> {
+ while (true) {
+ try {
+ FakeDeviceUtils fakeDeviceUtils = null;
+ try {
+ fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
+ }catch (Exception e){}
+ if(fakeDeviceUtils == null){
+ continue;
+ }
+ String fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig();
+ if(null == fakeDeviceConfig) {
+ continue;
+ }
+ List<DeviceConfig> deviceConfigs = JSON.parseArray(fakeDeviceConfig, DeviceConfig.class);
+ for (DeviceConfig device : deviceConfigs) {
+ excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ }
+ });
+ fakeCommandThread.start();
+
while (true) {
try {
+ try {
+ if (serverSocket == null) {
+ serverSocket = new ServerSocket(8888);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+
+ if(serverSocket == null){
+ continue;
+ }
+
initFakeDeviceServer();
}catch (Exception e){
e.printStackTrace();
@@ -51,7 +90,7 @@
}
}
- private void initFakeDeviceServer() {
+ private synchronized void initFakeDeviceServer() {
FakeDeviceUtils fakeDeviceUtils = null;
try {
fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
@@ -71,26 +110,26 @@
continue;
}
- if (fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
+ if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
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 {
- ServerSocket serverSocket = new ServerSocket(device.getPort());
- 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));
while (true) {
- if(fakeSocket == null) {
- fakeServerMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
- break;
+ 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));
}
handleClient(device);
@@ -100,17 +139,7 @@
}
});
fakeThread.start();
-
- Thread fakeCommandThread = new Thread(() -> {
- while (true) {
- try {
- excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- });
- fakeCommandThread.start();
+ fakeThreadMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeThread);
// init
ThreadHandler thread = new NyShuttleThread(device, redisUtil);;
@@ -261,6 +290,12 @@
}
private void removeFake(DeviceConfig device) {
+ Thread thread = fakeThreadMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
+ if (thread != null) {
+ thread.interrupt();
+ }
+
+ fakeThreadMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
fakeServerMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
fakeStatusMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
fakeCommandMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
--
Gitblit v1.9.1