自动化立体仓库 - WCS系统
zjj
2023-11-21 ac0906df88505ec24ee910899a036676823f9d63
src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -7,16 +7,15 @@
import com.alibaba.fastjson.JSON;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnOpt;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.service.BasCrnOptService;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.core.CrnThread;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.*;
import com.zy.core.model.CrnSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.CommandPackage;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.model.protocol.CrnProtocol;
import lombok.Data;
@@ -24,6 +23,7 @@
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
/**
 * 堆垛机线程
@@ -45,6 +45,7 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        initCrnProtocol();
        this.connect();
        while (true) {
            try {
@@ -91,6 +92,22 @@
        }
    }
    /**
     * 初始化堆垛机
     */
    private void initCrnProtocol(){
        crnProtocol = new CrnProtocol();
        crnProtocol.setMode((short) 0);
        crnProtocol.setStatus((short) 0);
        crnProtocol.setTaskNo((short) 0);
        crnProtocol.setTaskFinish((short) 0);
        crnProtocol.setValid((short) 0);
        crnProtocol.setBay((short) 0);
        crnProtocol.setLevel((short) 0);
        crnProtocol.setRow((short) 0);
        crnProtocol.setLane((short) 0);
    }
    @Override
    public boolean connect() {
        boolean result = false;
@@ -105,6 +122,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());
//            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
//            deviceErrorService.addDeviceError("crn", slave.getId(), "堆垛机plc连接失败");
            initCrnProtocol();
        }
        siemensNet.ConnectClose();
        return result;
@@ -131,6 +151,10 @@
            crnProtocol.setLane(siemensNet.getByteTransform().TransInt16(result.Content, 20));  // 22
            // 异常
            crnProtocol.setAlarm1(0);
            crnProtocol.setAlarm2(0);
            crnProtocol.setAlarm3(0);
            crnProtocol.setAlarm4(0);
            boolean[] err1 = siemensNet.getByteTransform().TransBool(result.Content, 28, 1);    // 30
            if (err1[0]) { crnProtocol.setAlarm1(1); }
            if (err1[1]) { crnProtocol.setAlarm1(2); }
@@ -219,11 +243,6 @@
            if (err15[4]) { crnProtocol.setAlarm4(71); }
            if (err15[5]) { crnProtocol.setAlarm4(72); }
            if (err15[6]) { crnProtocol.setAlarm4(73); }
//            crnProtocol.setAlarm1(siemensNet.getByteTransform().TransInt32(result.Content, 28));    // 30
//            crnProtocol.setAlarm2(siemensNet.getByteTransform().TransInt32(result.Content, 32));    // 34
//            crnProtocol.setAlarm3(siemensNet.getByteTransform().TransInt32(result.Content, 36));    // 38
//            crnProtocol.setAlarm4(siemensNet.getByteTransform().TransInt32(result.Content, 40));    // 42
            boolean[] bool1 = siemensNet.getByteTransform().TransBool(result.Content, 60, 1);    // 62
            crnProtocol.setConnStatus(bool1[0]);
@@ -314,14 +333,19 @@
                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){}
            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
            deviceErrorService.deleteDeviceError("crn", 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());
            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
            deviceErrorService.addDeviceError("crn", slave.getId(), "读取堆垛机plc状态信息失败");
        }
    }
@@ -336,6 +360,7 @@
        if (command.getTaskNo() == 0 && command.getAckFinish() == 0) {
            command.setTaskNo((short) 9999);
        }
        command.setCrnNo(slave.getId());
        short[] array = new short[9];
        if (command.getAckFinish() == 0) {
@@ -343,25 +368,69 @@
        } else {
            array[0] = 0;
        }
        array[1] = command.getSourcePosZ();
        array[2] = command.getSourcePosY();
        array[3] = command.getSourcePosX();
        array[4] = command.getDestinationPosZ();
        array[5] = command.getDestinationPosY();
        array[6] = command.getDestinationPosX();
        array[7] = command.getSourceStaNo();
        array[8] = command.getDestinationStaNo();
        if (command.getSourcePosZ()==(short)1 && command.getSourcePosY()==(short)1){
            array[1] = (short)0;
            array[2] = (short)0;
        }else if (command.getSourcePosZ()==(short)1 && command.getSourcePosY()==(short)41){
            array[1] = (short)0;
            array[2] = (short)42;
        }else {
            array[1] = command.getSourcePosZ();
            array[2] = command.getSourcePosY();
        }
        if (command.getSourcePosX()==(short)3){
            array[3] = (short)1;
        }else if (command.getSourcePosX()==(short)4){
            array[3] = (short)2;
        }else {
            array[3] = command.getSourcePosX();
        }
        if (command.getDestinationPosZ()==(short)1 && command.getDestinationPosY()==(short)1){
            array[4] = (short)0;
            array[5] = (short)0;
        }else if (command.getDestinationPosZ()==(short)1 && command.getDestinationPosY()==(short)41){
            array[4] = (short)0;
            array[5] = (short)42;
        }else {
            array[4] = command.getDestinationPosZ();
            array[5] = command.getDestinationPosY();
        }
        if (command.getDestinationPosX()==(short)3){
            array[6] = (short)1;
        }else if (command.getDestinationPosX()==(short)4){
            array[6] = (short)2;
        }else {
            array[6] = command.getDestinationPosX();
        }
        if ((array[1]==(short)0 && array[2] == (short)0) || (array[1]==(short)0 && array[2] == (short)42)){
            array[7]=(short)1;
        }else {
            array[7]=(short)0;
        }
//        array[7] = command.getSourceStaNo();
        if ((array[4]==(short)0 && array[5] == (short)0) || (array[4]==(short)0 && array[5] == (short)42)){
            array[8]=(short)1;
        }else {
            array[8]=(short)0;
        }
//        array[8] = command.getDestinationStaNo();
        // 作业信息
        OperateResult result = siemensNet.Write("DB1000.0", array);
        OperateResult result = siemensNet.Write("DB10.0", array);
        // 任务号 + 完成位
        short[] array2 = new short[2];
        array2[0] = command.getTaskNo();
        array2[1] = command.getAckFinish();
        OperateResult result1 = siemensNet.Write("DB1000.24", array2);
        OperateResult result1 = siemensNet.Write("DB10.24", array2);
        // 结束位
        if (command.getAckFinish() == 0) {
            OperateResult result2 = siemensNet.Write("DB1000.28.1", true);
            OperateResult result2 = siemensNet.Write("DB10.28.1", true);
        }
        // 日志记录
@@ -389,14 +458,37 @@
            }
        } catch (Exception ignore) {}
        if (result.IsSuccess && result1.IsSuccess) {
        //更新命令日志
        CommandInfoLogService commandInfoLogService = SpringUtils.getBean(CommandInfoLogService.class);
        CommandInfoService commandInfoService = SpringUtils.getBean(CommandInfoService.class);
        CommandInfo commandInfo = commandInfoService.selectById(command.getCommandId());
        CommandInfoLog commandInfoLog = JSON.parseObject(JSON.toJSONString(commandInfo), CommandInfoLog.class);
        commandInfoLog.setId(null);
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
        if (result.IsSuccess && result1.IsSuccess) {
            log.warn("堆垛机命令下发[id:{},时间:{}] >>>>> {}", slave.getId(), DateUtils.convert(new Date(), DateUtils.yyyyMMddHHmmsssss_F), JSON.toJSON(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            //更新指令日志
            commandInfoLog.setDeviceLog("指令下发成功");
            commandInfoLogService.insert(commandInfoLog);
            //更新任务步序
            TaskWrkService taskWrkService = SpringUtils.getBean(TaskWrkService.class);
            TaskWrk taskWrk = taskWrkService.selectByWrkNo(command.getTaskNo().intValue());
            if (taskWrk != null) {
                taskWrk.setCommandStep(taskWrk.getCommandStep() + 1);//更新指令步序
                taskWrkService.updateById(taskWrk);
            }
            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());
            //更新指令日志
            commandInfoLog.setDeviceLog("指令下发失败");
            commandInfoLogService.insert(commandInfoLog);
            return false;
        }
    }