#
Junjie
2023-09-18 b79a7ba5f841d203dc3e374692c781442f52c85c
src/main/java/com/zy/core/thread/NyShuttleThread.java
@@ -28,6 +28,8 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.Socket;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
@@ -43,6 +45,7 @@
    private ShuttleSlave slave;
    private NyShuttleProtocol shuttleProtocol;
    private RedisUtil redisUtil;
    private Socket socket;
    public NyShuttleThread(ShuttleSlave slave,RedisUtil redisUtil) {
        this.slave = slave;
@@ -84,6 +87,10 @@
    private void read() {
        try {
            if (this.socket == null || this.socket.isClosed()) {
                //链接断开重新链接
                this.connect();
            }
            readStatus();
            //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令
            if (shuttleProtocol.getFree() == ShuttleStatusType.IDLE.id
@@ -108,9 +115,10 @@
            //----------读取四向穿梭车状态-----------
            NyShuttleHttpCommand readStatusCommand = NyHttpUtils.getReadStatusCommand(slave.getId());
            JSONObject jsonObject = NyHttpUtils.requestCommand(readStatusCommand);
            JSONObject jsonObject = NyHttpUtils.requestCommand(socket, readStatusCommand);
            if (jsonObject == null) {
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.OFFLINE);
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车Socket状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            }else {
                //手动状态/自动状态
                shuttleProtocol.setWorkingMode(jsonObject.getInteger("workingMode"));
@@ -159,7 +167,7 @@
                    errCode.add(Integer.parseInt(o.toString()));
                }
                //故障码
                shuttleProtocol.setErrCode(errCode);
                shuttleProtocol.setErrCode(errCode.get(0));
                //总里程数
                shuttleProtocol.setStatusSum(jsonObject.getObject("statusSum", NyShuttleProtocol.StatusSumClass.class));
                //非自动状态时间计时
@@ -236,12 +244,29 @@
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            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();
            }
        }
    }
    @Override
    public boolean connect() {
        try {
            Socket socket = new Socket(slave.getIp(),slave.getPort());
            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()));
        }
        return true;
    }
@@ -268,7 +293,21 @@
        }
        //发出请求
        JSONObject result = NyHttpUtils.requestCommand(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;//请求失败
        }
@@ -355,10 +394,10 @@
            return false;//小车状态忙,禁止执行命令
        }
        //检测小车是否要进提升机,如需要进提升机则调度提升机
        if (!checkLiftStation(wrkNo)) {
            return false;
        }
//        //检测小车是否要进提升机,如需要进提升机则调度提升机
//        if (!checkLiftStation(wrkNo)) {
//            return false;
//        }
        //检测穿梭车是否在提升机内
        if (!checkShuttleInTheLift(wrkNo)) {
@@ -399,7 +438,8 @@
                    null,
                    null,
                    JSON.toJSONString(command),
                    null
                    null,
                    JSON.toJSONString(shuttleProtocol)
            );
            shuttleOptService.insert(opt);
        }