From 8e1cb9a0ff0f25f3c29fa05407e5ab35d3b484de Mon Sep 17 00:00:00 2001
From: zy <zy@123>
Date: 星期五, 25 七月 2025 13:49:51 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java |  229 +++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 164 insertions(+), 65 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 e6fed97..5a58ada 100644
--- a/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
@@ -6,10 +6,12 @@
 import com.zy.common.SpringUtils;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.News;
-import com.zy.core.ThreadHandler;
 import com.zy.core.cache.SlaveConnection;
+import com.zy.core.enums.RedisKeyType;
 import com.zy.core.enums.SlaveType;
+import com.zy.core.model.param.UpdateFakeThreadStatusParam;
 import com.zy.core.properties.DeviceConfig;
+import com.zy.core.thread.FakeThread;
 import com.zy.core.thread.impl.NyShuttleThread;
 import com.zy.core.utils.FakeDeviceUtils;
 import lombok.extern.slf4j.Slf4j;
@@ -17,19 +19,19 @@
 import java.io.*;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
 @Slf4j
 @SuppressWarnings("all")
-public class FakeNyShuttleThread implements ThreadHandler {
+public class FakeNyShuttleThread implements FakeThread {
 
     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 boolean enableFake;
+    private boolean enableFakeDeviceThread;
     private ConcurrentHashMap<String, Thread> fakeThreadMap = new ConcurrentHashMap();
     private ConcurrentHashMap<String, Socket> fakeServerMap = new ConcurrentHashMap();
     private ConcurrentHashMap<String, JSONObject> fakeStatusMap = new ConcurrentHashMap();
@@ -37,28 +39,42 @@
 
     private boolean fake = false;
 
-    public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort) {
+    public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort, boolean enableFake, boolean enableFakeDeviceThread) {
         this.redisUtil = redisUtil;
         this.gatewayPort = gatewayPort;
+        this.enableFake = enableFake;
+        this.enableFakeDeviceThread = enableFakeDeviceThread;
     }
 
     @Override
     public void run() {
         News.info("Fake Server is Started");
 
+        FakeDeviceUtils fakeDeviceUtils = null;
+        while (true) {
+            try {
+                fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
+            }catch (Exception e){}
+
+            if(fakeDeviceUtils == null){
+                continue;
+            }
+
+            break;
+        }
+
         acceptorThread();
         fakeCommandThread();
 
-        while (true) {
-            try {
-                initFakeDeviceServer();
-            }catch (Exception e){
-                e.printStackTrace();
-            }
-        }
+        initDeviceThread(fakeDeviceUtils);
+        initFakeDeviceServer(fakeDeviceUtils);
     }
 
     private void acceptorThread() {
+        if(!enableFake){
+            return;
+        }
+
         Thread acceptorThread = new Thread(() -> {
             log.info("{}:acceptorThread is start");
             try {
@@ -140,6 +156,10 @@
     }
 
     private void fakeCommandThread() {
+        if(!enableFake){
+            return;
+        }
+
         Thread fakeCommandThread = new Thread(() -> {
             while (true) {
                 try {
@@ -152,7 +172,7 @@
                     }
                     List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
                     for (DeviceConfig device : deviceConfigs) {
-                        excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
+                        excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), device);
                     }
                 }catch (Exception e){
                     e.printStackTrace();
@@ -162,14 +182,11 @@
         fakeCommandThread.start();
     }
 
-    private synchronized void initFakeDeviceServer() {
-        FakeDeviceUtils fakeDeviceUtils = null;
-        try {
-            fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
-        }catch (Exception e){}
-        if(fakeDeviceUtils == null){
+    private synchronized void initFakeDeviceServer(FakeDeviceUtils fakeDeviceUtils) {
+        if(!enableFake){
             return;
         }
+
         List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
         for (DeviceConfig device : deviceConfigs) {
             if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) {
@@ -182,19 +199,6 @@
 
             if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
                 continue;
-            }
-
-            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));
@@ -213,14 +217,38 @@
             });
             fakeThread.start();
             fakeThreadMap.put(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo(), fakeThread);
+        }
+    }
+
+    private void initDeviceThread(FakeDeviceUtils fakeDeviceUtils) {
+        if (!enableFakeDeviceThread) {
+            return;
+        }
+        List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
+        for (DeviceConfig device : deviceConfigs) {
+            if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) {
+                continue;
+            }
+
+            if (!device.getThreadImpl().equals("NyShuttleThread")) {
+                continue;
+            }
+
+            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);
+            }
 
             try {
-                Thread.sleep(2000);
+                Thread.sleep(1000);
             }catch (Exception e){}
         }
     }
 
-    private void excuteFakeCommand(String key) {
+    private void excuteFakeCommand(String key, DeviceConfig deviceConfig) {
         if (!fakeCommandMap.containsKey(key)) {
             return;
         }
@@ -228,6 +256,7 @@
         fakeCommandMap.remove(key);
         new Thread(() -> {
             try {
+                log.info("Fake Shuttle Command Running: {}", JSON.toJSONString(command));
                 JSONObject fakeStatus = fakeStatusMap.get(key);
                 String commandType = command.getString("commandType");
                 if (commandType == null) {
@@ -243,7 +272,7 @@
                     fakeStatus.put("hasLift", true);
                     fakeStatus.put("deviceStatus", 1);//璁惧绌洪棽
                     fakeStatusMap.put(key, fakeStatus);
-                }else if (commandType.equals("liftDown")) {
+                } else if (commandType.equals("liftDown")) {
                     fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
                     fakeStatusMap.put(key, fakeStatus);
 
@@ -252,7 +281,7 @@
                     fakeStatus.put("hasLift", false);
                     fakeStatus.put("deviceStatus", 1);//璁惧绌洪棽
                     fakeStatusMap.put(key, fakeStatus);
-                }else if (commandType.equals("charge")) {
+                } else if (commandType.equals("charge")) {
                     fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
                     fakeStatusMap.put(key, fakeStatus);
 
@@ -266,14 +295,14 @@
                         batteryPower = batteryPower + 1;
                         fakeStatus.put("batteryPower", batteryPower);
                         fakeStatusMap.put(key, fakeStatus);
-                        if(batteryPower >= 100) {
+                        if (batteryPower >= 100) {
                             break;
                         }
                     }
 
                     fakeStatus.put("deviceStatus", 1);//璁惧绌洪棽
                     fakeStatusMap.put(key, fakeStatus);
-                }else if (commandType.equals("stopCharge")) {
+                } else if (commandType.equals("stopCharge")) {
                     fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
                     fakeStatusMap.put(key, fakeStatus);
 
@@ -282,7 +311,7 @@
                     fakeStatus.put("hasCharge", false);
                     fakeStatus.put("deviceStatus", 1);//璁惧绌洪棽
                     fakeStatusMap.put(key, fakeStatus);
-                }else if (commandType.equals("updateFloor")) {
+                } else if (commandType.equals("updateFloor")) {
                     fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
                     fakeStatusMap.put(key, fakeStatus);
 
@@ -296,40 +325,87 @@
                     fakeStatus.put("currentCode", JSON.toJSONString(point));
                     fakeStatus.put("deviceStatus", 1);//璁惧绌洪棽
                     fakeStatusMap.put(key, fakeStatus);
-                }else if (commandType.equals("move")) {
-                    fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
-                    fakeStatusMap.put(key, fakeStatus);
-                    Thread.sleep(2000);
+                } else if (commandType.equals("move")) {
+                    boolean firstMove = true;
+
+                    String baseCommandKey = RedisKeyType.FAKE_SHUTTLE_MOVE_EXECUTE_COMMAND_LIST.key + deviceConfig.getDeviceNo() + "_";
+                    Set<String> keys = redisUtil.searchKeys(baseCommandKey);
+
+                    if (!keys.isEmpty()) {
+                        firstMove = false;
+                    }
 
                     JSONObject body = command.getJSONObject("commandBody");
                     String pathList = body.getString("path");
                     List<JSONObject> list = JSON.parseArray(pathList, JSONObject.class);
-                    for (JSONObject path : list) {
-                        String currentCode = fakeStatus.getString("currentCode");
-                        JSONObject point = JSON.parseObject(currentCode);
-                        point.put("x", path.getInteger("xp"));
-                        point.put("y", path.getInteger("yp"));
-                        point.put("z", path.getInteger("z"));
-                        fakeStatus.put("currentCode", JSON.toJSONString(point));
-                        fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
-                        fakeStatusMap.put(key, fakeStatus);
-                        Thread.sleep(2000);
+
+                    String finalKey = baseCommandKey + System.currentTimeMillis();
+                    redisUtil.set(finalKey, list);
+
+                    if(!firstMove) {
+                        return;
+                    }
+
+                    fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
+                    fakeStatusMap.put(key, fakeStatus);
+                    Thread.sleep(2000);
+
+                    while (true) {
+                        Set<String> searchKeys = redisUtil.searchKeys(baseCommandKey);
+                        if(searchKeys.isEmpty()) {
+                            break;
+                        }
+
+                        TreeSet<String> treeSet = new TreeSet<>();
+                        for (String tmpKey : searchKeys) {
+                            treeSet.add(tmpKey);
+                        }
+
+                        String first = treeSet.first();
+
+                        Object object = redisUtil.get(first);
+                        if(object == null) {
+                            break;
+                        }
+
+                        List<JSONObject> executePathList = (List<JSONObject>) object;
+                        for (JSONObject path : executePathList) {
+                            while (true) {
+                                JSONObject realFakeStatus = fakeStatusMap.get(key);
+                                Integer errorCode = realFakeStatus.getInteger("errorCode");
+                                if (errorCode > 0) {
+                                    continue;
+                                }
+                                break;
+                            }
+
+                            String currentCode = fakeStatus.getString("currentCode");
+                            JSONObject point = JSON.parseObject(currentCode);
+                            point.put("x", path.getInteger("xp"));
+                            point.put("y", path.getInteger("yp"));
+                            point.put("z", path.getInteger("z"));
+                            fakeStatus.put("currentCode", JSON.toJSONString(point));
+                            fakeStatus.put("deviceStatus", 0);//璁惧蹇欑
+                            fakeStatusMap.put(key, fakeStatus);
+                            Thread.sleep(2000);
+                        }
+                        redisUtil.del(first);
                     }
 
                     fakeStatus.put("deviceStatus", 1);//璁惧绌洪棽
                     fakeStatusMap.put(key, fakeStatus);
                 }
-            }catch (Exception e){
+            } catch (Exception e) {
                 e.printStackTrace();
             }
         }).start();
     }
 
     private void handleClient(DeviceConfig device) throws IOException {
+        StringBuffer sb = new StringBuffer();
         try {
             Socket socket = fakeServerMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
             if (socket == null) {
-                removeFake(device);
                 return;
             }
             InputStream inputStream = socket.getInputStream();
@@ -341,13 +417,14 @@
             // 鑾峰彇杈撳叆娴�
             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) {
+                int lastIndexOf = trim.lastIndexOf("\r\n");
+                if (lastIndexOf != -1) {
+                    trim = trim.substring(0, lastIndexOf);
+                    sb.append(trim);
                     break;
                 }
             }
@@ -356,17 +433,12 @@
 //            log.info("鏀跺埌Client Data: {}", JSON.toJSONString(result));
             processCommand(result, device);
         } catch (Exception e) {
+            log.error("handleClient deviceNo:{},recevie:{}", device.getDeviceNo(), sb);
             e.printStackTrace();
         }
     }
 
     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());
@@ -493,4 +565,31 @@
 
     }
 
+    @Override
+    public ConcurrentHashMap<String, Thread> getFakeThreadMap() {
+        return this.fakeThreadMap;
+    }
+
+    @Override
+    public ConcurrentHashMap<String, JSONObject> getFakeStatusMap() {
+        return this.fakeStatusMap;
+    }
+
+    @Override
+    public ConcurrentHashMap<String, JSONObject> getFakeCommandMap() {
+        return this.fakeCommandMap;
+    }
+
+    @Override
+    public boolean updateFakeStatus(UpdateFakeThreadStatusParam param) {
+        String key = param.getDeviceType() + param.getDeviceNo();
+        JSONObject result = fakeStatusMap.get(key);
+        if (result == null) {
+            return false;
+        }
+
+        JSONObject newData = JSON.parseObject(param.getData());
+        this.fakeStatusMap.put(key, newData);
+        return true;
+    }
 }

--
Gitblit v1.9.1