From 3fef108b925349b467f0639aede38e0a918a2a7f Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期日, 13 七月 2025 22:00:33 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java |  134 ++++++++++++++++++++++++++++++++------------
 1 files changed, 98 insertions(+), 36 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..8362d60 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;
 
@@ -45,6 +46,94 @@
     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 = JSON.parseObject(sb.toString());
+                    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 +154,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() {
@@ -115,19 +184,9 @@
                 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();
@@ -331,7 +390,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 +402,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 +423,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