自动化立体仓库 - WCS系统
Junjie
2023-07-19 b02710d5e8077a125d2ad1959b096922aca8898a
src/main/java/com/zy/core/thread/LiftThread.java
@@ -65,7 +65,7 @@
                switch (step) {
                    // 读数据
                    case 1:
                        readStatus();
                        read();
                        break;
                    // 写入数据
                    case 2:
@@ -109,6 +109,28 @@
    @Override
    public void close() {
        modbusTcpNet.ConnectClose();
    }
    private void read() {
        try {
            readStatus();
            //提升机处于运行状态,将标记置为true
            if (liftProtocol.getRunning()) {
                liftProtocol.setPakMk(true);
            }
            //提升机处于未运行、就绪、标记true、有任务号
            if (!liftProtocol.getRunning()
                    && liftProtocol.getPakMk()
                    && liftProtocol.getTaskNo() != 0) {
                //还有未完成的命令
                executeWork(liftProtocol.getTaskNo());
            }
        } catch (Exception e) {
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】提升机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            initLift();
        }
    }
    private void readStatus() {
@@ -175,19 +197,6 @@
                ///读取提升机状态-end
                //提升机处于运行状态,将标记置为true
                if (liftProtocol.getRunning()) {
                    liftProtocol.setPakMk(true);
                }
                //提升机处于未运行、就绪、标记true、有任务号
                if (!liftProtocol.getRunning()
                        && liftProtocol.getPakMk()
                        && liftProtocol.getTaskNo() != 0) {
                    //还有未完成的命令
                    executeWork(liftProtocol.getTaskNo());
                }
                //将提升机状态保存至数据库
                BasLiftService liftService = SpringUtils.getBean(BasLiftService.class);
                BasLift basLift = liftService.selectById(liftProtocol.getLiftNo());
@@ -244,6 +253,54 @@
        }
        command.setLiftNo(slave.getId().shortValue());
        short[] array = getCommandArr(command);//获取命令报文
        try {
            Thread.sleep(1000);//命令下发前休眠1s
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        OperateResult result = modbusTcpNet.Write("41088", array);
        if (result != null && result.IsSuccess) {
            News.info("提升机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            try {
                Thread.sleep(1000);//命令下发后休眠
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 0; i < 5; i++) {
                if (command.getRun().intValue() == 4 || command.getRun().intValue() == 5) {
                    break;//系统复位和链条停止转动不需要重发
                }
                readStatus();//重新读取状态
                if (liftProtocol.getRunning()) {
                    break;
                }
                //判断是否运行中,如不运行,重新下发命令
                result = modbusTcpNet.Write("41088", array);
                News.info("提升机命令下发[id:{}] >>>>> {},次数:{}", slave.getId(), JSON.toJSON(command), i);
                OutputQueue.LIFT.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2},次数:{}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command), i));
                try {
                    Thread.sleep(300);//命令下发后休眠
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;
        } else {
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】写入提升机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}],次数:{}", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            News.error("写入提升机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
    //获取命令报文
    private short[] getCommandArr(LiftCommand command) {
        // 开始任务
        short[] array = new short[30];
        //开始运行
@@ -284,23 +341,7 @@
            tmp[1] = array[1];
            array = tmp;
        }
        try {
            Thread.sleep(1000);//命令下发前休眠1s
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        OperateResult result = modbusTcpNet.Write("41088", array);;
        if (result != null && result.IsSuccess) {
            News.info("提升机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            return true;
        } else {
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】写入提升机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            News.error("写入提升机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
        return array;
    }
    //分配任务
@@ -448,6 +489,7 @@
            //保存数据到数据库做流水
            BasLiftOptService liftOptService = SpringUtils.getBean(BasLiftOptService.class);
            if (liftOptService != null) {
                short[] commandArr = getCommandArr(command);//获取命令报文
                BasLiftOpt opt = new BasLiftOpt(
                        redisCommand.getWrkNo().intValue(),
                        redisCommand.getLiftNo().intValue(),
@@ -455,7 +497,8 @@
                        null,
                        null,
                        null,
                        JSON.toJSONString(command)
                        JSON.toJSONString(command),
                        JSON.toJSONString(commandArr)
                );
                liftOptService.insert(opt);
            }
@@ -595,6 +638,7 @@
     * 初始化提升机
     */
    private void initLift() {
        this.connect();
        if (null == liftProtocol) {
            liftProtocol = new LiftProtocol();
        }