#
Junjie
2025-07-03 084c97ce0069483ab1c7938755c89e5d7a834ad0
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;
//    }
}