自动化立体仓库 - WCS系统
#
yxFwq
2024-07-20 e95cc36944a886ccf63f90ccdf883aaf6ab81f90
src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -19,6 +19,7 @@
import com.zy.core.model.CrnSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.model.cpmmandParam.CrnCommandParam;
import com.zy.core.model.protocol.CrnProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -48,6 +49,7 @@
     * 堆垛机是否在回原点运动中标记
     */
    private boolean backHpFlag = false;
    private boolean backHpFlagTwo = false;
    public SiemensCrnThread(CrnSlave slave) {
        this.slave = slave;
@@ -57,6 +59,7 @@
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        this.connect();
        sign = System.currentTimeMillis();
        while (isRunning) {
            try {
                int step = 1;
@@ -112,6 +115,10 @@
                        command2.setDestinationPosY((short)0);     // 目标库位列
                        command2.setDestinationPosZ((short)0);     // 目标库位层
                        write5(command2);
                        break;
                    // 双工位同时作业
                    case 9:
                        write9((CrnCommandParam) task.getData());
                        break;
                    default:
                        break;
@@ -252,8 +259,8 @@
                // 复位信号
                if (!Cools.isEmpty(crnProtocol.getStatusType()) && (crnProtocol.getStatusType().equals(CrnStatusType.WAITING) || crnProtocol.getStatusType().equals(CrnStatusType.SEPARATE_WAITING))) {
                    log.error("-------------------------------------------第一步、[堆垛机号:{}, 工作号:{}, 载货台:{}]==>> 状态为10,等待确认!!",
                            slave.getId(),crnProtocol.getTaskNo(), crnProtocol.getLoaded()==1 ? "有物" : "无物");
                    log.error("-------------------------工位1-------------------------第一步、[堆垛机号:{}, 工作号:{}, 载货台:{}]==>> 状态为{},等待确认!!",
                            slave.getId(),crnProtocol.getTaskNo(), crnProtocol.getLoaded()==1 ? "有物" : "无物",crnProtocol.getStatusType());
                    if (resetFlag) {
                        if(crnProtocol.getTaskNo()==9999){
                            backHpFlag = false;
@@ -264,14 +271,20 @@
                            resetFlag = false;
                        }
                    }
                }
                // 复位信号
                if (!Cools.isEmpty(crnProtocol.getStatusTypeTwo()) && (crnProtocol.getStatusTypeTwo().equals(CrnStatusType.WAITING) || crnProtocol.getStatusTypeTwo().equals(CrnStatusType.SEPARATE_WAITING))) {
                    log.error("-------------------------工位2-------------------------第一步、[堆垛机号:{}, 工作号:{}, 载货台:{}]==>> 状态为{},等待确认!!",
                            slave.getId(),crnProtocol.getTaskNoTwo(), crnProtocol.getLoadedTwo()==1 ? "有物" : "无物",crnProtocol.getStatusTypeTwo());
                    if (resetFlagTwo) {
                        if(crnProtocol.getTaskNo()==9999){
                            backHpFlag = false;
                        if(crnProtocol.getTaskNoTwo()==9999){
                            backHpFlagTwo = false;
                        }
                        CrnCommand crnCommand = new CrnCommand();
                        crnCommand.setAckFinish((short)1);
                        if (write5(crnCommand)) {
                            resetFlag = false;
                            resetFlagTwo = false;
                        }
                    }
                }
@@ -344,7 +357,7 @@
        do {
            try{
                if(!result.IsSuccess){
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                    result = siemensNet.Write("DB100.0", array);
                    Thread.sleep(100);
                    writeCount++;
@@ -367,12 +380,12 @@
                            || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                    ){
                        try{
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        }catch (Exception e){
                            try{
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSON(command),JSON.toJSON(resultRead));
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(resultRead));
                            }catch (Exception e1){
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSON(command));
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSONString(command));
                            }
                        }
                        try{
@@ -380,12 +393,12 @@
                        }catch (Exception e){
                        }
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                        result = siemensNet.Write("DB100.0", array);
                        writeCount++;
                        continue;
                    } else {
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        break;
                    }
                }
@@ -442,8 +455,8 @@
        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)));
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
            return true;
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
@@ -485,7 +498,7 @@
        do {
            try{
                if(!result.IsSuccess){
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                    result = siemensNet.Write("DB100.20", array);
                    Thread.sleep(100);
                    writeCount++;
@@ -508,12 +521,12 @@
                            || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                    ){
                        try{
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        }catch (Exception e){
                            try{
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSON(command),JSON.toJSON(resultRead));
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(resultRead));
                            }catch (Exception e1){
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSON(command));
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSONString(command));
                            }
                        }
                        try{
@@ -521,12 +534,12 @@
                        }catch (Exception e){
                        }
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                        result = siemensNet.Write("DB100.20", array);
                        writeCount++;
                        continue;
                    } else {
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        break;
                    }
                }
@@ -583,6 +596,175 @@
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
            return true;
        } else {
            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;
        }
    }
    /**
     * 写入数据  双工位
     */
    private boolean write9(CrnCommandParam command) throws InterruptedException {
        if (null == command) {
            log.error("堆垛机写入命令为空");
            return false;
        }
        command.setCrnNo(slave.getId());
        short[] array = new short[22];
        array[0] = command.getAckFinish();
        array[1] = command.getTaskNo();
        array[2] = command.getTaskMode();
        array[3] = command.getSourcePosX();
        array[4] = command.getSourcePosY();
        array[5] = command.getSourcePosZ();
        array[6] = command.getDestinationPosX();
        array[7] = command.getDestinationPosY();
        array[8] = command.getDestinationPosZ();
        array[9] = command.getCommand();
        array[10] = command.getAckFinish2();
        array[11] = command.getTaskNo2();
        array[12] = command.getTaskMode2();
        array[13] = command.getSourcePosX2();
        array[14] = command.getSourcePosY2();
        array[15] = command.getSourcePosZ2();
        array[16] = command.getDestinationPosX2();
        array[17] = command.getDestinationPosY2();
        array[18] = command.getDestinationPosZ2();
        array[19] = command.getCommand();
        array[20] = (short)1;
        array[21] = (short)1;
        OperateResult result18 = siemensNet.Write("DB100.18", (short)0);
        OperateResult result38 = siemensNet.Write("DB100.38", (short)0);
        OperateResult result = siemensNet.Write("DB100.0", array);
//        OperateResult result = siemensNet.Write("DB100.20", array);
        log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), array);
        //堆垛机任务写入后,回读一次,看是否成功
        Thread.sleep(200);
        int writeCount = 1;
        do {
            try{
                if(!result.IsSuccess){
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                    result = siemensNet.Write("DB100.0", array);
                    Thread.sleep(100);
                    writeCount++;
                    continue;
                }
                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 40);
                if (resultRead.IsSuccess) {
                    CrnCommandParam one = new CrnCommandParam();
                    one.setTaskNo(siemensNet.getByteTransform().TransInt16(resultRead.Content, 2));
                    one.setTaskMode(siemensNet.getByteTransform().TransInt16(resultRead.Content, 4));
                    one.setSourcePosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 6));
                    one.setSourcePosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8));
                    one.setSourcePosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 10));
                    one.setDestinationPosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 12));
                    one.setDestinationPosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 14));
                    one.setDestinationPosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 16));
                    one.setTaskNo2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 22));
                    one.setTaskMode2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 24));
                    one.setSourcePosX2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 26));
                    one.setSourcePosY2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 28));
                    one.setSourcePosZ2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 30));
                    one.setDestinationPosX2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 32));
                    one.setDestinationPosY2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 34));
                    one.setDestinationPosZ2(siemensNet.getByteTransform().TransInt16(resultRead.Content, 36));
                    if (!command.getTaskNo().equals(one.getTaskNo()) || !command.getTaskMode().equals(one.getTaskMode())
                            || !command.getSourcePosX().equals(one.getSourcePosX()) || !command.getSourcePosY().equals(one.getSourcePosY())
                            || !command.getSourcePosZ().equals(one.getSourcePosZ()) || !command.getDestinationPosX().equals(one.getDestinationPosX())
                            || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                            ||
                            !command.getTaskNo2().equals(one.getTaskNo2()) || !command.getTaskMode2().equals(one.getTaskMode2())
                            || !command.getSourcePosX2().equals(one.getSourcePosX2()) || !command.getSourcePosY2().equals(one.getSourcePosY2())
                            || !command.getSourcePosZ2().equals(one.getSourcePosZ2()) || !command.getDestinationPosX2().equals(one.getDestinationPosX2())
                            || !command.getDestinationPosY2().equals(one.getDestinationPosY2()) || !command.getDestinationPosZ2().equals(one.getDestinationPosZ2())
                    ){
                        try{
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                        }catch (Exception e){
                            try{
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSON(command),JSON.toJSON(resultRead));
                            }catch (Exception e1){
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSON(command));
                            }
                        }
                        try{
                            Thread.sleep(100);
                        }catch (Exception e){
                        }
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                        result = siemensNet.Write("DB100.0", array);
                        writeCount++;
                        continue;
                    } else {
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                        break;
                    }
                }
            }catch (Exception e){
                log.error("堆垛机命令地址写入后回读出错,异常:"+e);
            }
            writeCount++;
        } while (writeCount<6);
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            Thread.sleep(100L);
            result18 = siemensNet.Write("DB100.18", commandFinish);
            result38 = siemensNet.Write("DB100.38", commandFinish);
            int signFinish = 1;
            while (signFinish<5){
                OperateResultExOne<byte[]> result10018 = siemensNet.Read("DB100.18", (short) 2);
                OperateResultExOne<byte[]> result10038 = siemensNet.Read("DB100.38", (short) 2);
                short transInt1618 = siemensNet.getByteTransform().TransInt16(result10018.Content, 0);
                short transInt1638 = siemensNet.getByteTransform().TransInt16(result10038.Content, 0);
                if (transInt1618 != commandFinish || transInt1638 != commandFinish){
                    log.info("下发DB100.18/DB100.38  回读失败" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.18/DB100.38  回读失败" + "array:"+ JSON.toJSONString(array));
                    result18 = siemensNet.Write("DB100.18", commandFinish);
                    result38 = siemensNet.Write("DB100.38", commandFinish);
                    signFinish++;
                }else {
                    log.info("下发DB100.18/DB100.38" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.18/DB100.38" + "array:"+ JSON.toJSONString(array));
                    break;
                }
            }
        }
        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;