#
zy
2025-07-16 e6107b0c8721ff8f0a039fc4719f52cf352edbde
#
3个文件已修改
101 ■■■■ 已修改文件
src/main/java/com/zy/core/ServerBootstrap.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java 87 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/ServerBootstrap.java
@@ -30,8 +30,8 @@
    @Value("${deviceMsgConfig.gatewayPort}")
    private int gatewayPort;
    @Value("${deviceMsgConfig.enableFakeAndDeviceThread}")
    private boolean enableFakeAndDeviceThread;
    @Value("${deviceMsgConfig.enableFakeDeviceThread}")
    private boolean enableFakeDeviceThread;
    @Value("${deviceMsgConfig.enableFake}")
    private boolean enableFake;
    @Autowired
@@ -78,11 +78,7 @@
    }
    private void initFakeThread(){
        if (!enableFake) {
            return;
        }
        ThreadHandler thread = new FakeNyShuttleThread(redisUtil, gatewayPort, enableFakeAndDeviceThread);
        ThreadHandler thread = new FakeNyShuttleThread(redisUtil, gatewayPort, enableFake, enableFakeDeviceThread);
        new Thread(thread).start();
        ThreadHandler thread2 = new FakeZyForkLiftThread(redisUtil);
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
@@ -30,7 +30,8 @@
    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 enableFakeAndDeviceThread;
    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();
@@ -38,26 +39,35 @@
    private boolean fake = false;
    public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort, boolean enableFakeAndDeviceThread) {
    public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort, boolean enableFake, boolean enableFakeDeviceThread) {
        this.redisUtil = redisUtil;
        this.gatewayPort = gatewayPort;
        this.enableFakeAndDeviceThread = enableFakeAndDeviceThread;
        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() {
@@ -164,14 +174,7 @@
        fakeCommandThread.start();
    }
    private synchronized void initFakeDeviceServer() {
        FakeDeviceUtils fakeDeviceUtils = null;
        try {
            fakeDeviceUtils = SpringUtils.getBean(FakeDeviceUtils.class);
        }catch (Exception e){}
        if(fakeDeviceUtils == null){
            return;
        }
    private synchronized void initFakeDeviceServer(FakeDeviceUtils fakeDeviceUtils) {
        List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
        for (DeviceConfig device : deviceConfigs) {
            if (!device.getDeviceType().equals(String.valueOf(SlaveType.Shuttle))) {
@@ -184,21 +187,6 @@
            if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
                continue;
            }
            if (enableFakeAndDeviceThread) {
                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));
@@ -217,9 +205,33 @@
            });
            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){}
        }
    }
@@ -333,7 +345,6 @@
        try {
            Socket socket = fakeServerMap.get(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
            if (socket == null) {
                removeFake(device);
                return;
            }
            InputStream inputStream = socket.getInputStream();
@@ -365,12 +376,6 @@
    }
    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());
src/main/resources/application.yml
@@ -31,5 +31,5 @@
  gatewayPort: 8888
  # 启动模拟器
  enableFake: true
  # 启动模拟器时并启动设备线程
  enableFakeAndDeviceThread: false
  # 启动模拟器设备线程
  enableFakeDeviceThread: false