自动化立体仓库 - WCS系统
#
Junjie
2023-09-04 6d611bd596f57f0079c36bdb6a7686613f1bbb13
#
2个文件已修改
103 ■■■■■ 已修改文件
src/main/java/com/zy/common/utils/NyHttpUtils.java 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/thread/NyShuttleThread.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/utils/NyHttpUtils.java
@@ -16,6 +16,8 @@
 */
public class NyHttpUtils {
    private static final boolean DEBUG = true;//调试模式
    //获取HTTP请求标准结构体
    public static NyShuttleHttpCommand getHttpStandard(Integer shuttleNo, Integer wrkNo) {
        NyShuttleHttpCommand httpStandard = new NyShuttleHttpCommand();
@@ -279,54 +281,44 @@
    }
    //发出请求
    public static JSONObject requestCommand(Socket socket, NyShuttleHttpCommand httpCommand) {
        try {
            if (socket == null) {
                return null;
            }
            // 获取输入流和输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
            writer.write(JSON.toJSONString(httpCommand) + "\r\n");
            writer.flush();
    public static JSONObject requestCommand(Socket socket, NyShuttleHttpCommand httpCommand) throws IOException {
        if (socket == null) {
            return null;
        }
        // 获取输入流和输出流
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
        writer.write(JSON.toJSONString(httpCommand) + "\r\n");
        writer.flush();
//            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
            // 读取服务器的响应
            StringBuffer sb = new StringBuffer();
            String response = null;
            char[] chars = new char[2048];//缓冲区
            do {
                reader.read(chars);
                String trim = new String(chars).trim();
                sb.append(trim);
                if (sb.lastIndexOf("\r\n") != -1) {
                    break;
                }
            } while (response != null);
        // 读取服务器的响应
        StringBuffer sb = new StringBuffer();
        String response = null;
        char[] chars = new char[2048];//缓冲区
        do {
            reader.read(chars);
            String trim = new String(chars).trim();
            sb.append(trim);
            if (sb.lastIndexOf("\r\n") != -1) {
                break;
            }
        } while (response != null);
//            System.out.println("Received response from server: " + sb);
            JSONObject result = JSON.parseObject(sb.toString());//得到响应结果集
            if (!result.get("msgType").equals("responseMsg")) {//不是响应内容
                return null;
            }
            JSONObject resultResponse = JSON.parseObject(result.get("response").toString());
            JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
            int responseId = Integer.parseInt(resultHeader.get("responseId").toString());
//            if (responseId != httpCommand.getRequest().getHeader().getRequestId()) {
//                return null;//响应ID与请求ID不一致
//            }
            return filterBodyData(result);//返回Body结果集
        } catch (Exception e) {
            e.printStackTrace();
            try {
                socket.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        JSONObject result = JSON.parseObject(sb.toString());//得到响应结果集
        if (!result.get("msgType").equals("responseMsg")) {//不是响应内容
            return null;
        }
        return null;
        JSONObject resultResponse = JSON.parseObject(result.get("response").toString());
        JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
        int responseId = Integer.parseInt(resultHeader.get("responseId").toString());
        if (!DEBUG && responseId != httpCommand.getRequest().getHeader().getRequestId()) {
            return null;//响应ID与请求ID不一致,不在调试模式下
        }
        return filterBodyData(result);//返回Body结果集
    }
    public static JSONObject filterBodyData(JSONObject data) {
src/main/java/com/zy/core/thread/NyShuttleThread.java
@@ -244,6 +244,14 @@
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车Socket状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            try {
                this.socket.close();
                this.socket = null;
                Thread.sleep(1000);
                this.connect();
            } catch (IOException | InterruptedException exception) {
                e.printStackTrace();
            }
        }
    }
@@ -254,6 +262,7 @@
            socket.setSoTimeout(60000);
            socket.setKeepAlive(true);
            this.socket = socket;
            log.info(MessageFormat.format("【{0}】四向穿梭车Socket链接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
        } catch (IOException e) {
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车Socket链接失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
        }
@@ -283,7 +292,21 @@
        }
        //发出请求
        JSONObject result = NyHttpUtils.requestCommand(socket, command);
        JSONObject result = null;
        try {
            result = NyHttpUtils.requestCommand(socket, command);
        } catch (IOException e) {
            try {
                this.socket.close();
                this.socket = null;
                Thread.sleep(1000);
                this.connect();
            } catch (IOException exception) {
                exception.printStackTrace();
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        }
        if (result == null) {
            return false;//请求失败
        }