#
Junjie
2025-07-09 b821b6c3719f5eac49bfff58af77a9b4ed97f544
#
2个文件已修改
93 ■■■■■ 已修改文件
src/main/java/com/zy/core/ServerBootstrap.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java 89 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/ServerBootstrap.java
@@ -40,10 +40,10 @@
        Thread.sleep(2000);
        // 初始化消息队列
        initMq();
        // 初始化下位机线程
        initThread();
        // 初始化虚拟设备线程
        initFakeThread();
        // 初始化下位机线程
        initThread();
        News.info("核心控制层已启动...............................................");
    }
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());