#
Junjie
2025-07-03 084c97ce0069483ab1c7938755c89e5d7a834ad0
#
3个文件已修改
363 ■■■■■ 已修改文件
src/main/java/com/zy/core/Utils/DeviceMsgUtils.java 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java 235 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 90 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/Utils/DeviceMsgUtils.java
@@ -23,6 +23,20 @@
    @Autowired
    private RedisUtil redisUtil;
    public Object getDeviceCommandMsg(SlaveType deviceType, Integer deviceId) {
        TreeSet<String> listKey = getDeviceCommandMsgListKey(deviceType, deviceId);
        if (listKey.isEmpty()) {
            return null;
        }
        String firstKey = listKey.first();
        Object data = redisUtil.get(firstKey);
        if (destroyAfterReading) {
            redisUtil.del(firstKey);
        }
        return data;
    }
    public DeviceMsgModel getDeviceMsg(SlaveType deviceType, Integer deviceId) {
        TreeSet<String> listKey = getDeviceMsgListKey(deviceType, deviceId);
        if (listKey.isEmpty()) {
@@ -53,7 +67,17 @@
        redisUtil.set(key, msgModel, 60 * 60);
    }
    public String sendCommand(SlaveType deviceType, Integer deviceId, Object command) {
    public String sendDeviceMsg(SlaveType deviceType, Integer deviceId, Object command) {
        String key = parseDeviceMsgKey(deviceType, deviceId) + System.currentTimeMillis();
        DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
        deviceMsgModel.setDeviceId(deviceId);
        deviceMsgModel.setDeviceMsg(command);
        redisUtil.set(key, deviceMsgModel, 60 * 60 * 24);
        return key;
    }
    public String sendDeviceCommand(SlaveType deviceType, Integer deviceId, Object command) {
        String key = parseDeviceCommandMsgKey(deviceType, deviceId) + System.currentTimeMillis();
        redisUtil.set(key, command, 60 * 60 * 24);
        return key;
@@ -71,6 +95,18 @@
        return treeSet;
    }
    public TreeSet<String> getDeviceCommandMsgListKey(SlaveType deviceType, Integer deviceId) {
        String listKey = parseDeviceCommandMsgKey(deviceType, deviceId);
        Set<String> keys = redisUtil.searchKeys(listKey);
        TreeSet<String> treeSet = new TreeSet<>();
        for (String key : keys) {
            treeSet.add(key);
        }
        return treeSet;
    }
    public String parseDeviceMsgKey(SlaveType deviceType, Integer deviceId) {
        if (deviceType == null) {
            throw new CoolException("设备类型为空");
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
@@ -3,15 +3,17 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.Utils.DeviceMsgUtils;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.ShuttleSlave;
import com.zy.core.thread.ShuttleThread;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
@@ -29,9 +31,6 @@
    private static final boolean DEBUG = false;//调试模式
    private List<JSONObject> socketReadResults = new ArrayList<>();
    private List<JSONObject> socketResults = new ArrayList<>();
    public NyShuttleThread(ShuttleSlave slave, RedisUtil redisUtil) {
        this.slave = slave;
        this.redisUtil = redisUtil;
@@ -42,7 +41,7 @@
        News.info("{}号四向车线程启动", slave.getId());
        this.connect();
        //监听消息并存储
        //监听消息
        Thread innerThread = new Thread(() -> {
            while (true) {
                try {
@@ -54,44 +53,50 @@
        });
        innerThread.start();
//        //设备执行
//        Thread executeThread = new Thread(() -> {
//            while (true) {
//                try {
//                    ShuttleAction shuttleAction = SpringUtils.getBean(ShuttleAction.class);
//                    if (shuttleAction == null) {
//                        continue;
//                    }
//
//                    Object object = redisUtil.get(RedisKeyType.SHUTTLE_FLAG.key + slave.getId());
//                    if (object == null) {
//                        continue;
//                    }
//
//                    Integer taskNo = Integer.valueOf(String.valueOf(object));
//                    if (taskNo != 0) {
//                        //存在任务需要执行
//                        boolean result = shuttleAction.executeWork(slave.getId(), taskNo);
//                    }
//
////                    //小车空闲且有跑库程序
////                    shuttleAction.moveLoc(slave.getId());
//
//                    //演示模式
//                    shuttleAction.demo(slave.getId());
//
//                    Thread.sleep(200);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//        executeThread.start();
        //执行指令
        Thread executeThread = new Thread(() -> {
            while (true) {
                try {
                    DeviceMsgUtils deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
                    Object deviceCommandMsg = deviceMsgUtils.getDeviceCommandMsg(SlaveType.Shuttle, slave.getId());
                    if (deviceCommandMsg == null) {
                        continue;
                    }
                    executeCommand(deviceCommandMsg);
                    Thread.sleep(200);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        executeThread.start();
    }
    private void executeCommand(Object deviceCommandMsg) {
        try {
            if (this.socket == null) {
                return;
            }
            // 获取输出流
            OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
            writer.write(JSON.toJSONString(deviceCommandMsg) + "\r\n");
            writer.flush();
//            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void listenSocketMessage() {
        try {
            if (this.socket == null) {
                return;
            }
            DeviceMsgUtils deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
            if(deviceMsgUtils == null) {
                return;
            }
@@ -110,73 +115,11 @@
            }
            JSONObject result = JSON.parseObject(sb.toString());//得到响应结果集
            String msgType = result.getString("msgType");
            if ("responseMsg".equals(msgType)) {
                JSONObject response = result.getJSONObject("response");
                JSONObject body = response.getJSONObject("body");
                if (body.containsKey("workingMode")) {
                    //read
                    socketReadResults.add(body);
                    return;
                }
            }
            if (!socketResults.isEmpty() && socketResults.size() >= 20) {
                socketResults.remove(0);//清理头节点
            }
            socketResults.add(result);//添加数据
            deviceMsgUtils.sendDeviceMsg(SlaveType.Shuttle, slave.getId(), result);
        } catch (Exception e) {
//            e.printStackTrace();
            e.printStackTrace();
        }
    }
//    public JSONObject getRequestBody(String type, String taskId) {
//        try {
//            // 获取服务器响应
//            JSONObject result = null;
//            if (type.equals("readState")) {
//                type = "state";
//            }
//
//            for (int i = 0; i < socketResults.size(); i++) {
//                JSONObject socketResult = socketResults.get(i);
//                if (!socketResult.get("msgType").equals("responseMsg")) {//不是响应内容
//                    continue;
//                }
//
//                JSONObject resultResponse = JSON.parseObject(socketResult.get("response").toString());
//                JSONObject resultBody = JSON.parseObject(resultResponse.get("body").toString());
//                String responseType = resultBody.get("responseType").toString();
//                if (DEBUG) {
//                    result = socketResult;
//                    break;
//                }
//
//                if (!responseType.equals(type)) {
//                    continue;//响应类型与请求类型不一致,不在调试模式下
//                }
//
//                if (taskId != null) {
//                    String responseTaskId = resultBody.get("taskId").toString();
//                    if (!responseTaskId.equals(taskId)) {
//                        continue;//响应ID与请求ID不一致,不在调试模式下
//                    }
//                }
//
//                result = socketResult;
//                break;
//            }
//
//            if (result == null) {
//                return null;//无响应结果
//            }
//
//            return filterBodyData(result);//返回Body结果集
//        } catch (Exception e) {
//            return null;
//        }
//    }
    @Override
    public boolean connect() {
@@ -201,92 +144,4 @@
    public void close() {
    }
//    //发出请求
//    private JSONObject requestCommand(NyShuttleHttpCommand httpCommand) throws IOException {
//        try {
//            if (this.socket == null) {
//                return null;
//            }
//
//            //压缩数据包
//            JSONObject data = JSON.parseObject(JSON.toJSONString(httpCommand));
//            data.remove("nodes");
//
//            // 获取输出流
//            OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
//            writer.write(JSON.toJSONString(data) + "\r\n");
//            writer.flush();
////            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
//
//            String requestType = null;
//            String taskId = null;
//            try {
//                requestType = httpCommand.getRequest().getBody().get("requestType").toString();
//                taskId = httpCommand.getRequest().getBody().get("taskId").toString();
//            } catch (Exception e) {
////            return null;
//                //taskId可能取空,不报错,正常情况
//            }
//
//            // 获取服务器响应
//            // 尝试10次
//            JSONObject result = null;
//            for (int i = 0; i < 10; i++) {
//                result = getRequestBody(requestType, taskId);
//                if (result == null) {
//                    try {
//                        Thread.sleep(100);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }else {
//                    break;
//                }
//            }
//            return result;//返回Body结果集
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    private void requestCommandAsync(NyShuttleHttpCommand httpCommand) throws IOException {
//        if (this.socket == null) {
//            return;
//        }
//
//       try {
//           //压缩数据包
//           JSONObject data = JSON.parseObject(JSON.toJSONString(httpCommand));
//           data.remove("nodes");
//
//           // 获取输出流
//           OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream());
//           writer.write(JSON.toJSONString(data) + "\r\n");
//           writer.flush();
////            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
//       }catch (Exception e) {
////           e.printStackTrace();
////           System.out.println("socket write error");
//           this.socket.close();
//           this.socket = null;
//       }
//    }
//
//    private JSONObject filterBodyData(JSONObject data) {
//        Object response = data.get("response");
//        if (response == null) {
//            return null;
//        }
//
//        JSONObject result = JSON.parseObject(response.toString());
//        Object body = result.get("body");
//        if (body == null) {
//            return null;
//        }
//        JSONObject jsonBody = JSON.parseObject(body.toString());
//        return jsonBody;
//    }
}
src/main/resources/application.yml
@@ -44,58 +44,58 @@
deviceMsgConfig:
  # 读取数据后自动删除
  destroyAfterReading: true
  destroyAfterReading: false
# 下位机配置
wcs-slave:
  # 四向穿梭车1
  shuttle[0]:
    id: 1
    ip: 10.10.20.11
    ip: 192.168.4.77
    port: 8888
    rack: 0
    slot: 0
    threadImpl: NyShuttleThread
  # 四向穿梭车2
  shuttle[1]:
    id: 2
    ip: 10.10.20.12
    port: 8888
    rack: 0
    slot: 0
    threadImpl: NyShuttleThread
  # 货叉提升机主线程
  forkLiftMaster[0]:
    id: 99
    ip: 10.10.20.20
    port: 102
    rack: 0
    slot: 0
    threadImpl: LfdZyForkLiftMasterThread
  # 货叉提升机1
  forkLift[0]:
    id: 1
    ip: 10.10.20.20
    port: 102
    rack: 0
    slot: 0
    threadImpl: LfdZyForkLiftSlaveThread
    masterId: 99
    staRow: 9
    staBay: 6
    sta[0]:
      staNo: 1001
      lev: 1
      liftNo: ${wcs-slave.forkLift[0].id}
    sta[1]:
      staNo: 1002
      lev: 2
      liftNo: ${wcs-slave.forkLift[0].id}
    sta[2]:
      staNo: 1003
      lev: 3
      liftNo: ${wcs-slave.forkLift[0].id}
    sta[3]:
      staNo: 1004
      lev: 4
      liftNo: ${wcs-slave.forkLift[0].id}
#  # 四向穿梭车2
#  shuttle[1]:
#    id: 2
#    ip: 10.10.20.12
#    port: 8888
#    rack: 0
#    slot: 0
#    threadImpl: NyShuttleThread
#  # 货叉提升机主线程
#  forkLiftMaster[0]:
#    id: 99
#    ip: 10.10.20.20
#    port: 102
#    rack: 0
#    slot: 0
#    threadImpl: LfdZyForkLiftMasterThread
#  # 货叉提升机1
#  forkLift[0]:
#    id: 1
#    ip: 10.10.20.20
#    port: 102
#    rack: 0
#    slot: 0
#    threadImpl: LfdZyForkLiftSlaveThread
#    masterId: 99
#    staRow: 9
#    staBay: 6
#    sta[0]:
#      staNo: 1001
#      lev: 1
#      liftNo: ${wcs-slave.forkLift[0].id}
#    sta[1]:
#      staNo: 1002
#      lev: 2
#      liftNo: ${wcs-slave.forkLift[0].id}
#    sta[2]:
#      staNo: 1003
#      lev: 3
#      liftNo: ${wcs-slave.forkLift[0].id}
#    sta[3]:
#      staNo: 1004
#      lev: 4
#      liftNo: ${wcs-slave.forkLift[0].id}