From fbc1ee007b2cbf95d33b6950fc95c0b4a34027d6 Mon Sep 17 00:00:00 2001
From: Junjie <540245094@qq.com>
Date: 星期四, 10 七月 2025 15:54:24 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java | 167 ++++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 114 insertions(+), 53 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 6071aae..0ff81b3 100644
--- a/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
@@ -20,30 +20,66 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
@Slf4j
@SuppressWarnings("all")
public class FakeNyShuttleThread implements ThreadHandler {
-
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 Integer gatewayPort;
+ 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();
private boolean fake = false;
- public FakeNyShuttleThread(RedisUtil redisUtil) {
+ public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort) {
this.redisUtil = redisUtil;
+ this.gatewayPort = gatewayPort;
}
@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;
+ }
+ List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
+ 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(gatewayPort);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+
+ if(serverSocket == null){
+ continue;
+ }
+
initFakeDeviceServer();
}catch (Exception e){
e.printStackTrace();
@@ -51,7 +87,7 @@
}
}
- private void initFakeDeviceServer() {
+ private synchronized void initFakeDeviceServer() {
FakeDeviceUtils fakeDeviceUtils = null;
try {
fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
@@ -59,64 +95,51 @@
if(fakeDeviceUtils == null){
return;
}
- String fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig();
- if(null != fakeDeviceConfig){
- List<DeviceConfig> deviceConfigs = JSON.parseArray(fakeDeviceConfig, DeviceConfig.class);
- for (DeviceConfig device : deviceConfigs) {
- if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) {
- continue;
- }
+ List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
+ for (DeviceConfig device : deviceConfigs) {
+ if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) {
+ continue;
+ }
- if (!device.getThreadImpl().equals("NyShuttleThread")) {
- continue;
- }
+ if (!device.getThreadImpl().equals("NyShuttleThread")) {
+ continue;
+ }
- if (fakeServerMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
- continue;
- }
+ if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
+ continue;
+ }
- Thread fakeThread = new Thread(() -> {
- try {
- ServerSocket serverSocket = new ServerSocket(device.getPort());
- Socket fakeSocket = serverSocket.accept();
+ log.info("{}:device is run,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));
- while (true) {
- if(fakeSocket == null) {
- fakeServerMap.remove(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
- break;
+ 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();
}
- handleClient(device);
+ fakeServerMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeSocket);
+ fakeStatusMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), JSON.parseObject(fakeStatus));
}
- }catch (Exception e){
- e.printStackTrace();
- }
- });
- fakeThread.start();
- Thread fakeCommandThread = new Thread(() -> {
- while (true) {
- try {
- excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
- }catch (Exception e){
- e.printStackTrace();
- }
+ handleClient(device);
}
- });
- fakeCommandThread.start();
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ });
+ 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);
- }
+ // init
+ ThreadHandler thread = new NyShuttleThread(device, redisUtil);;
+ new Thread(thread).start();
+ SlaveConnection.put(SlaveType.Shuttle, device.getDeviceNo(), thread);
}
}
@@ -130,6 +153,10 @@
try {
JSONObject fakeStatus = fakeStatusMap.get(key);
String commandType = command.getString("commandType");
+ if (commandType == null) {
+ return;
+ }
+
if (commandType.equals("liftUp")) {
fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
fakeStatusMap.put(key, fakeStatus);
@@ -155,6 +182,18 @@
//delay
Thread.sleep(2000);
fakeStatus.put("hasCharge", true);
+
+ Integer batteryPower = Integer.parseInt(fakeStatus.getString("batteryPower"));
+ while (true) {
+ Thread.sleep(10000);
+ batteryPower = batteryPower + 1;
+ fakeStatus.put("batteryPower", batteryPower);
+ fakeStatusMap.put(key, fakeStatus);
+ if(batteryPower >= 100) {
+ break;
+ }
+ }
+
fakeStatus.put("deviceStatus", 1);//璁惧绌洪棽
fakeStatusMap.put(key, fakeStatus);
}else if (commandType.equals("stopCharge")) {
@@ -212,8 +251,18 @@
private void handleClient(DeviceConfig device) throws IOException {
try {
Socket socket = fakeServerMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
+ if (socket == null) {
+ removeFake(device);
+ return;
+ }
+ InputStream inputStream = socket.getInputStream();
+ if(inputStream == null){
+ removeFake(device);
+ return;
+ }
+
// 鑾峰彇杈撳叆娴�
- BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
// 璇诲彇鏈嶅姟鍣ㄧ殑鍝嶅簲
StringBuffer sb = new StringBuffer();
char[] chars = new char[2048];//缂撳啿鍖�
@@ -234,6 +283,18 @@
}
}
+ 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());
+ }
+
public void processCommand(JSONObject result, DeviceConfig device) throws IOException {
String response = null;
JSONObject fakeStatus = fakeStatusMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
--
Gitblit v1.9.1