#
Junjie
2025-07-09 44b027ef477f5af53491783c7c66771814951c70
#
8个文件已修改
189 ■■■■ 已修改文件
pom.xml 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/ServerBootstrap.java 22 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/controller/OpenController.java 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/properties/DeviceConfig.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java 90 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/utils/DeviceMsgUtils.java 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/utils/FakeDeviceUtils.java 21 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -72,6 +72,16 @@
    <build>
        <finalName>gateway</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
src/main/java/com/zy/core/ServerBootstrap.java
@@ -1,6 +1,5 @@
package com.zy.core;
import com.alibaba.fastjson.JSON;
import com.zy.common.exception.CoolException;
import com.zy.common.utils.RedisUtil;
import com.zy.core.cache.MessageQueue;
@@ -13,6 +12,7 @@
import com.zy.core.utils.DeviceMsgUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@@ -27,6 +27,8 @@
@Component
public class ServerBootstrap {
    @Value("${deviceMsgConfig.gatewayPort}")
    private int gatewayPort;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
@@ -49,13 +51,7 @@
    private void initMq(){
        // 初始化设备mq
        String deviceConfig = deviceMsgUtils.getDeviceConfig();
        if(null == deviceConfig){
            News.error("初始化设备时未检测到配置文件...................");
            return;
        }
        List<DeviceConfig> deviceConfigs = JSON.parseArray(deviceConfig, DeviceConfig.class);
        List<DeviceConfig> deviceConfigs = deviceMsgUtils.getDeviceConfig();
        for (DeviceConfig device : deviceConfigs) {
            SlaveType type = SlaveType.findInstance(device.getDeviceType());
            if(null == type){
@@ -66,13 +62,7 @@
    }
    private void initThread(){
        String deviceConfig = deviceMsgUtils.getDeviceConfig();
        if(null == deviceConfig){
            News.error("初始化设备时未检测到配置文件...................");
            return;
        }
        List<DeviceConfig> deviceConfigs = JSON.parseArray(deviceConfig, DeviceConfig.class);
        List<DeviceConfig> deviceConfigs = deviceMsgUtils.getDeviceConfig();
        for (DeviceConfig device : deviceConfigs) {
            if (device.getDeviceType().equals(String.valueOf(SlaveType.ForkLift))) {
                initForkLiftThread(device);
@@ -83,7 +73,7 @@
    }
    private void initFakeThread(){
        ThreadHandler thread = new FakeNyShuttleThread(redisUtil);
        ThreadHandler thread = new FakeNyShuttleThread(redisUtil, gatewayPort);
        new Thread(thread).start();
    }
src/main/java/com/zy/core/controller/OpenController.java
@@ -1,15 +1,11 @@
package com.zy.core.controller;
import com.alibaba.fastjson.JSON;
import com.zy.common.R;
import com.zy.core.News;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.param.AddFakeDeviceParam;
import com.zy.core.model.param.DeleteDeviceParam;
import com.zy.core.properties.DeviceConfig;
import com.zy.core.thread.ForkLiftThread;
import com.zy.core.thread.ShuttleThread;
import com.zy.core.utils.DeviceMsgUtils;
import com.zy.core.utils.FakeDeviceUtils;
@@ -33,17 +29,12 @@
        List<DeviceConfig> deviceList = new ArrayList<>();
        List<DeviceConfig> configList = new ArrayList<>();
        String deviceConfig = deviceMsgUtils.getDeviceConfig();
        if(null != deviceConfig){
            List<DeviceConfig> deviceConfigs = JSON.parseArray(deviceConfig, DeviceConfig.class);
            configList.addAll(deviceConfigs);
        }
        String fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig();
        if(null != fakeDeviceConfig){
            List<DeviceConfig> deviceConfigs = JSON.parseArray(fakeDeviceConfig, DeviceConfig.class);
            configList.addAll(deviceConfigs);
        }
        List<DeviceConfig> deviceConfigs = deviceMsgUtils.getDeviceConfig();
        configList.addAll(deviceConfigs);
        List<DeviceConfig> fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig();
        configList.addAll(fakeDeviceConfig);
        for (DeviceConfig config : configList) {
            SlaveType slaveType = SlaveType.findInstance(config.getDeviceType());
src/main/java/com/zy/core/properties/DeviceConfig.java
@@ -15,6 +15,8 @@
    private Integer deviceNo;
    private Integer gatewayId;
    private Boolean fake = false;
    private String fakeInitStatus;
src/main/java/com/zy/core/thread/fake/FakeNyShuttleThread.java
@@ -25,10 +25,10 @@
@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();
@@ -36,8 +36,9 @@
    private boolean fake = false;
    public FakeNyShuttleThread(RedisUtil redisUtil) {
    public FakeNyShuttleThread(RedisUtil redisUtil, Integer gatewayPort) {
        this.redisUtil = redisUtil;
        this.gatewayPort = gatewayPort;
    }
    @Override
@@ -54,11 +55,7 @@
                    if(fakeDeviceUtils == null){
                        continue;
                    }
                    String fakeDeviceConfig = fakeDeviceUtils.getFakeDeviceConfig();
                    if(null == fakeDeviceConfig) {
                        continue;
                    }
                    List<DeviceConfig> deviceConfigs = JSON.parseArray(fakeDeviceConfig, DeviceConfig.class);
                    List<DeviceConfig> deviceConfigs = fakeDeviceUtils.getFakeDeviceConfig();
                    for (DeviceConfig device : deviceConfigs) {
                        excuteFakeCommand(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo());
                    }
@@ -73,7 +70,7 @@
            try {
                try {
                    if (serverSocket == null) {
                        serverSocket = new ServerSocket(8888);
                        serverSocket = new ServerSocket(gatewayPort);
                    }
                }catch (Exception e){
                    e.printStackTrace();
@@ -98,54 +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 (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
                    continue;
                }
            if (fakeThreadMap.containsKey(String.valueOf(SlaveType.Shuttle) + device.getDeviceNo())) {
                continue;
            }
                log.info("{}:device is run,devices:{}", device.getDeviceNo(),JSON.toJSONString(fakeServerMap));
            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 {
                        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));
            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();
                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);
            }
                        handleClient(device);
                    }
                }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);
        }
    }
src/main/java/com/zy/core/utils/DeviceMsgUtils.java
@@ -1,11 +1,13 @@
package com.zy.core.utils;
import com.alibaba.fastjson.JSON;
import com.zy.common.exception.CoolException;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.DeviceCommandMsgModel;
import com.zy.core.model.DeviceMsgModel;
import com.zy.core.properties.DeviceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -20,16 +22,25 @@
    @Value("${deviceMsgConfig.destroyAfterReading}")
    private boolean destroyAfterReading;
    @Value("${deviceMsgConfig.gatewayId}")
    private Integer gatewayId;
    @Autowired
    private RedisUtil redisUtil;
    public String getDeviceConfig() {
    public List<DeviceConfig> getDeviceConfig() {
        List<DeviceConfig> list = new ArrayList<>();
        Object obj = redisUtil.get(RedisKeyType.DEVICE_CONFIG.key);
        if(null == obj){
            return null;
            return list;
        }
        return obj.toString();
        List<DeviceConfig> deviceConfigs = JSON.parseArray(obj.toString(), DeviceConfig.class);
        for (DeviceConfig deviceConfig : deviceConfigs) {
            if(deviceConfig.getGatewayId().equals(gatewayId)){
                list.add(deviceConfig);
            }
        }
        return list;
    }
    public DeviceCommandMsgModel getDeviceCommandMsg(SlaveType deviceType, Integer deviceId) {
src/main/java/com/zy/core/utils/FakeDeviceUtils.java
@@ -1,7 +1,6 @@
package com.zy.core.utils;
import com.alibaba.fastjson.JSON;
import com.zy.common.R;
import com.zy.common.exception.CoolException;
import com.zy.common.utils.RedisUtil;
import com.zy.core.ThreadHandler;
@@ -14,6 +13,7 @@
import com.zy.core.thread.impl.LfdZyForkLiftMasterThread;
import com.zy.core.thread.impl.NyShuttleThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@@ -22,17 +22,26 @@
@Component
public class FakeDeviceUtils {
    @Value("${deviceMsgConfig.gatewayId}")
    private Integer gatewayId;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DeviceMsgUtils deviceMsgUtils;
    public String getFakeDeviceConfig() {
    public List<DeviceConfig> getFakeDeviceConfig() {
        List<DeviceConfig> list = new ArrayList<>();
        Object obj = redisUtil.get(RedisKeyType.FAKE_DEVICE_CONFIG.key);
        if(null == obj){
            return null;
            return list;
        }
        return obj.toString();
        List<DeviceConfig> deviceConfigs = JSON.parseArray(obj.toString(), DeviceConfig.class);
        for (DeviceConfig deviceConfig : deviceConfigs) {
            if(deviceConfig.getGatewayId().equals(gatewayId)){
                list.add(deviceConfig);
            }
        }
        return list;
    }
    public void addFakeDevice(DeviceConfig deviceConfig) {
src/main/resources/application.yml
@@ -24,4 +24,8 @@
deviceMsgConfig:
  # 读取数据后自动删除
  destroyAfterReading: true
  destroyAfterReading: true
  # 网关编号
  gatewayId: 1
  # 网关端口
  gatewayPort: 8888