自动化立体仓库 - WCS系统
#
18516761980
2022-07-12 94841ffcd268fddb374922a6372b377345ff12a2
src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -5,7 +5,6 @@
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.core.common.Arith;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnOpt;
@@ -15,9 +14,7 @@
import com.zy.core.CrnThread;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.CrnStatusType;
import com.zy.core.enums.CrnTaskModeType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.*;
import com.zy.core.model.CrnSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.CrnCommand;
@@ -94,10 +91,36 @@
        }
    }
    /**
     * 初始化堆垛机状态
     */
    private void initCrn() {
        if (null == crnProtocol) {
            crnProtocol = new CrnProtocol();
        }
        crnProtocol.setMode((short) -1);
//        crnProtocol.setTaskNo((short)0);
        crnProtocol.setStatus((short)-1);
        crnProtocol.setBay((short)0);
        crnProtocol.setLevel((short)0);
        crnProtocol.setForkPos((short) -1);
        crnProtocol.setLiftPos((short) -1);
        crnProtocol.setWalkPos((short)0);
        crnProtocol.setLoaded((short)0);
        crnProtocol.setAlarm((short)0);
        crnProtocol.setxSpeed((short) 0);
        crnProtocol.setySpeed((short) 0);
        crnProtocol.setzSpeed((short) 0);
        crnProtocol.setxDistance((short) 0);
        crnProtocol.setyDistance((short) 0);
        crnProtocol.setxDuration((short) 0);
        crnProtocol.setyDuration((short) 0);
    }
    @Override
    public boolean connect() {
        boolean result = false;
        siemensNet = new SiemensS7Net(SiemensPLCS.S300, slave.getIp());
        siemensNet = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
        siemensNet.setRack(slave.getRack().byteValue());
        siemensNet.setSlot(slave.getSlot().byteValue());
        OperateResult connect = siemensNet.ConnectServer();
@@ -108,8 +131,9 @@
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】堆垛机plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.error("堆垛机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            initCrn();
        }
        siemensNet.ConnectClose();
//        siemensNet.ConnectClose();
        return result;
    }
@@ -117,77 +141,91 @@
     * 读取状态
     */
    private void readStatus(){
        OperateResultExOne<byte[]> result = siemensNet.Read("DB8.18", (short) 62);
        if (result.IsSuccess) {
            if (null == crnProtocol) {
                crnProtocol = new CrnProtocol();
            }
            crnProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0));
            crnProtocol.setTaskNo(siemensNet.getByteTransform().TransInt16(result.Content, 2));
            crnProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 4));
            crnProtocol.setBay(siemensNet.getByteTransform().TransInt16(result.Content, 6));
            crnProtocol.setLevel(siemensNet.getByteTransform().TransInt16(result.Content, 8));
            crnProtocol.setForkPos(siemensNet.getByteTransform().TransInt16(result.Content, 10));
            crnProtocol.setLiftPos(siemensNet.getByteTransform().TransInt16(result.Content, 12));
            crnProtocol.setWalkPos(siemensNet.getByteTransform().TransInt16(result.Content, 14));
//            crnProtocol.setTaskFinish(siemensNet.getByteTransform().TransInt16(result.Content, 16));
            crnProtocol.setLoaded(siemensNet.getByteTransform().TransInt16(result.Content, 18));
            crnProtocol.setAlarm1(siemensNet.getByteTransform().TransInt16(result.Content, 20));
            // 异常信息
            crnProtocol.setError1(siemensNet.getByteTransform().TransBool(result.Content, 22, 2));
            crnProtocol.setError2(siemensNet.getByteTransform().TransBool(result.Content, 24, 2));
            crnProtocol.setError3(siemensNet.getByteTransform().TransBool(result.Content, 26, 2));
            crnProtocol.setError4(siemensNet.getByteTransform().TransBool(result.Content, 28, 2));
            crnProtocol.setError5(siemensNet.getByteTransform().TransBool(result.Content, 30, 2));
            crnProtocol.setError6(siemensNet.getByteTransform().TransBool(result.Content, 32, 2));
            float xSpeed = siemensNet.getByteTransform().TransSingle(result.Content, 34);
            crnProtocol.setXSpeed((float) Arith.multiplys(3, xSpeed, 1.15));
            crnProtocol.setYSpeed(siemensNet.getByteTransform().TransSingle(result.Content, 38));
            float zSpeed = siemensNet.getByteTransform().TransSingle(result.Content, 42);
            crnProtocol.setZSpeed((float) Arith.multiplys(3, zSpeed, 5));
            crnProtocol.setXDistance(siemensNet.getByteTransform().TransSingle(result.Content, 46));
            crnProtocol.setYDistance(siemensNet.getByteTransform().TransSingle(result.Content, 50));
            crnProtocol.setXDuration(siemensNet.getByteTransform().TransSingle(result.Content, 54));
            crnProtocol.setYDuration(siemensNet.getByteTransform().TransSingle(result.Content, 58));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
            // 复位信号
            if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
                if (resetFlag) {
                    CrnCommand crnCommand = new CrnCommand();
                    crnCommand.setAckFinish((short)1);
                    if (write(crnCommand)) {
                        resetFlag = false;
                    }
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 70);
            if (result.IsSuccess) {
                if (null == crnProtocol) {
                    crnProtocol = new CrnProtocol();
                }
            }
                crnProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0));
                crnProtocol.setTaskNo(siemensNet.getByteTransform().TransInt16(result.Content, 2));
                crnProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 4));
                crnProtocol.setBay(siemensNet.getByteTransform().TransInt16(result.Content, 6));
                crnProtocol.setLevel(siemensNet.getByteTransform().TransInt16(result.Content, 8));
                crnProtocol.setForkPos(siemensNet.getByteTransform().TransInt16(result.Content, 10));
                crnProtocol.setLiftPos(siemensNet.getByteTransform().TransInt16(result.Content, 12));
                crnProtocol.setWalkPos(siemensNet.getByteTransform().TransInt16(result.Content, 14));
                crnProtocol.setLoaded(siemensNet.getByteTransform().TransInt16(result.Content, 16));
                crnProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 18));
                crnProtocol.setTemp1(siemensNet.getByteTransform().TransInt16(result.Content, 20));
                crnProtocol.setTemp2(siemensNet.getByteTransform().TransInt16(result.Content, 22));
                crnProtocol.setTemp3(siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnProtocol.setTemp4(siemensNet.getByteTransform().TransInt16(result.Content, 26));
                crnProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 28));
                crnProtocol.setySpeed(siemensNet.getByteTransform().TransInt16(result.Content, 30));
                crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 32));
                crnProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 34));
                crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 36));
                crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 38));
                crnProtocol.setyDuration(siemensNet.getByteTransform().TransInt16(result.Content, 40));
            // 根据实时信息更新数据库
            BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
            BasCrnp basCrnp = new BasCrnp();
            basCrnp.setCrnNo(slave.getId());
            if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
                log.error("堆垛机plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            }
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.error("读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
                // 复位信号
                if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
                    if (resetFlag) {
                        CrnCommand crnCommand = new CrnCommand();
                        crnCommand.setAckFinish((short)1);
                        if (write(crnCommand)) {
                            resetFlag = false;
                        }
                    }
                }
                try {
                    // 根据实时信息更新数据库
                    BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
                    BasCrnp basCrnp = new BasCrnp();
                    basCrnp.setCrnNo(slave.getId());
                    basCrnp.setCrnSts((int)crnProtocol.getMode());
                    if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
                        log.error("堆垛机plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
                    }
                } catch (Exception ignore){}
            } else {
                initCrn();
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
                log.error("读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            initCrn();
        }
    }
    private void convertRow(CrnCommand crnCommand) {
        if (crnCommand.getSourcePosX() != null && crnCommand.getSourcePosX() != 0) {
            crnCommand.setSourcePosX((short) (crnCommand.getSourcePosX() - ((slave.getId() - 1) * 4)));
        }
        if (crnCommand.getDestinationPosX() != null && crnCommand.getDestinationPosX() != 0) {
            crnCommand.setDestinationPosX((short) (crnCommand.getDestinationPosX() - ((slave.getId() - 1) * 4)));
        }
    }
    /**
     * 写入数据
     */
    private boolean write(CrnCommand command){
    private boolean write(CrnCommand command) throws InterruptedException {
        if (null == command) {
            log.error("堆垛机写入命令为空");
            return false;
        }
        convertRow(command);
        command.setCrnNo(slave.getId());
        short[] array = new short[9];
        short[] array = new short[12];
        array[0] = command.getAckFinish();
        array[1] = command.getTaskNo();
        array[2] = command.getTaskMode();
@@ -197,42 +235,53 @@
        array[6] = command.getDestinationPosX();
        array[7] = command.getDestinationPosY();
        array[8] = command.getDestinationPosZ();
        OperateResult result = siemensNet.Write("DB8.0", array);
        if (result.IsSuccess) {
        array[9] = command.getSourceStaNo();
        array[10] = command.getDestinationStaNo();
        array[11] = command.getCommand();
        OperateResult result = siemensNet.Write("DB100.0", array);
            try {
                // 日志记录
                BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
                BasCrnOpt basCrnOpt = new BasCrnOpt(
                        command.getTaskNo().intValue(),    // 任务号
                        command.getCrnNo(),    // 堆垛机[非空]
                        new Date(),    // 下发时间
                        command.getTaskModeType().toString(),    // 模式
                        command.getSourcePosX().intValue(),    // 源排
                        command.getSourcePosY().intValue(),    // 源列
                        command.getSourcePosZ().intValue(),    // 源层
                        null,    // 源站
                        command.getDestinationPosX().intValue(),    // 目标排
                        command.getDestinationPosY().intValue(),    // 目标列
                        command.getDestinationPosZ().intValue(),    // 目标层
                        null,    // 目标站
                        null,    // 响应结果
                        null,    // 修改时间
                        null    // 修改人员
                );
                bean.insert(basCrnOpt);
            } catch (Exception ignore) {}
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            result = siemensNet.Write("DB100.22", commandFinish);
        }
        try {
            // 日志记录
            BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
            BasCrnOpt basCrnOpt = new BasCrnOpt(
                    command.getTaskNo().intValue(),    // 任务号
                    command.getCrnNo(),    // 堆垛机[非空]
                    new Date(),    // 下发时间
                    command.getTaskModeType().toString(),    // 模式
                    command.getSourcePosX().intValue(),    // 源排
                    command.getSourcePosY().intValue(),    // 源列
                    command.getSourcePosZ().intValue(),    // 源层
                    null,    // 源站
                    command.getDestinationPosX().intValue(),    // 目标排
                    command.getDestinationPosY().intValue(),    // 目标列
                    command.getDestinationPosZ().intValue(),    // 目标层
                    null,    // 目标站
                    null,    // 响应结果
                    null,    // 修改时间
                    null    // 修改人员
            );
            bean.insert(basCrnOpt);
        } catch (Exception ignore) {}
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            return true;
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.error("写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
    @Override
    public void close() {
        siemensNet.ConnectClose();