自动化立体仓库 - WCS系统
Junjie
2023-03-31 87c4efe93e6fe31a9c989b4aa5cc0c8f559b845d
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -10,15 +10,11 @@
import com.zy.asrs.mapper.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.MatDto;
import com.zy.common.model.SearchLocParam;
import com.zy.common.model.StartupDto;
import com.zy.common.model.*;
import com.zy.common.model.enums.WrkChargeType;
import com.zy.common.service.CommonService;
import com.zy.common.service.erp.ErpService;
import com.zy.common.utils.CollectionUtils;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.*;
import com.zy.core.CrnThread;
import com.zy.core.DevpThread;
import com.zy.core.News;
@@ -26,17 +22,10 @@
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
import com.zy.core.model.*;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.model.command.LedCommand;
import com.zy.core.model.command.SteCommand;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.model.protocol.SteProtocol;
import com.zy.core.model.command.*;
import com.zy.core.model.protocol.*;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.BarcodeThread;
import com.zy.core.thread.LedThread;
import com.zy.core.thread.SiemensDevpThread;
import com.zy.core.thread.SteThread;
import com.zy.core.thread.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -96,6 +85,8 @@
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetlMapper orderDetlMapper;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 组托
@@ -612,6 +603,391 @@
    }
    /**
     * 入出库  ===>>  四向穿梭车入出库作业下发
     */
    public synchronized void shuttleIoExecute() {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库站
            for (DevpSlave.StaRack rackInStn : devp.getRackInStn()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(rackInStn.getStaNo());
                StaProtocol staProtocol105 = devpThread.getStation().get(105);
                staProtocol105.setWorkNo((short) 752);
                staProtocol105.setStaNo((short) 100);
                StaProtocol staProtocol106 = devpThread.getStation().get(106);
                staProtocol106.setWorkNo((short) 753);
                staProtocol106.setStaNo((short) 100);
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                // 判断是否满足入库条件
                if (true || staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()) {
                    WrkMast wrkMast = wrkMastMapper.selectRackInStep48(workNo, staProtocol.getSiteId());
                    if (wrkMast != null) {
                        if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() == 8) {
                            ShuttleThread shuttleThread = null;
                            HashMap<String, Object> searchIdleShuttle = null;
                            if (wrkMast.getWrkSts() == 4) {
                                //寻找最近且空闲的四向穿梭车
                                searchIdleShuttle = this.searchIdleShuttle(wrkMast);
                                shuttleThread = (ShuttleThread) searchIdleShuttle.get("result");
                            }else {
                                //状态8,四向穿梭车已在提升机口,等待命令进行入库搬运动作
                                Integer shuttleNo = wrkMast.getShuttleNo();//四向穿梭车号
                                shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
                            }
                            if (shuttleThread == null) {
                                continue;
                            }
                            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                            if (!shuttleProtocol.isIdle()) {
                                continue;
                            }
                            wrkMast.setShuttleNo(shuttleProtocol.getShuttleNo().intValue());//给工作档分配四向穿梭车号
                            //分配任务号
                            shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //分配源库位
                            shuttleProtocol.setSourceLocNo(wrkMast.getSourceLocNo());
                            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                            //四向穿梭车号
                            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                            //任务号
                            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //入出库模式
                            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());
                            //源库位(小车当前位置)
                            String currentLocNo = shuttleProtocol.getCurrentLocNo();
                            assignCommand.setSourceLocNo(currentLocNo);
                            if (wrkMast.getWrkSts() == 8 || Boolean.parseBoolean(searchIdleShuttle.get("sameLay").toString())) {
                                //同一层
                                //分配目标库位
                                shuttleProtocol.setLocNo(wrkMast.getLocNo());
                                //目标库位
                                assignCommand.setLocNo(wrkMast.getLocNo());
                                wrkMast.setWrkSts(9L);//小车入库中
                            }else {
                                //不同层,将目标库位分配成提升机库位号
                                //小车当前层高
                                Integer currentLev = Integer.parseInt(currentLocNo.substring(currentLocNo.length() - 2, currentLocNo.length()));
                                //获取提升机
                                LiftSlave liftSlave = slaveProperties.getLift().get(0);
                                //提升机库位号
                                String liftLocNo = liftSlave.getLiftLocNo(currentLev);
                                shuttleProtocol.setLocNo(liftLocNo);
                                //目标库位
                                assignCommand.setLocNo(liftLocNo);
                                wrkMast.setWrkSts(5L);//小车迁移状态
                            }
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }
                    }
                }
            }
        }
    }
    /**
     * 搜索空闲且最近的四向穿梭车
     */
    public HashMap<String,Object> searchIdleShuttle(WrkMast wrkMast) {
        HashMap<String, Object> map = new HashMap<>();
        String locNo = wrkMast.getWrkSts() < 10 ? wrkMast.getLocNo() : wrkMast.getSourceLocNo();//库位号
        LocMast locMast = locMastService.queryByLoc(locNo);//找到库位记录
        String lay = locNo.substring(locNo.length() - 2, locNo.length());//当前工作档库位层高
        ShuttleThread recentShuttle = null;//当前距离最近的四向穿梭车线程
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车线程
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
            if (!shuttleProtocol.isIdle()) {
                continue;
            }
            String shuttleLocNo = shuttleProtocol.getCurrentLocNo();//二维码对应库位号
            String shuttleLocNoLay = shuttleLocNo.substring(shuttleLocNo.length() - 2, shuttleLocNo.length());//库位号对应层高
            if (lay.equals(shuttleLocNoLay)) {
                //当前四向穿梭车和工作档任务在同一层,则调配该车辆
                map.put("sameLay", true);//同一层
                map.put("result", shuttleThread);
                return map;
            }
            //更新当前最近的四向穿梭车
            if (recentShuttle == null) {
                recentShuttle = shuttleThread;
            }else {
                ShuttleProtocol recentShuttleProtocol = recentShuttle.getShuttleProtocol();//目前最近穿梭车
                String recentShuttleLocNo = recentShuttleProtocol.getCurrentLocNo();//二维码对应库位号
                String recentShuttleLocNoLay = recentShuttleLocNo.substring(recentShuttleLocNo.length() - 2, recentShuttleLocNo.length());//库位号对应层高
                int recentShuttleLocNoLayInt = Integer.parseInt(recentShuttleLocNoLay);
                int layInt = Integer.parseInt(lay);
                int shuttleLocNoLayInt = Integer.parseInt(shuttleLocNoLay);
                int i = Math.abs(layInt - recentShuttleLocNoLayInt);//工作档楼层减最近穿梭车楼层,取绝对值
                int j = Math.abs(layInt - shuttleLocNoLayInt);//工作档楼层减当前穿梭车楼层,取绝对值
                if (i < j) {
                    //更新最近穿梭车
                    recentShuttle = shuttleThread;
                } else if (i == j) {
                    //楼层相同情况
                    //找距离出库点最近的车
                    if (!(recentShuttleProtocol.isIdle() && shuttleProtocol.isIdle())) {
                        //只要有一辆车不是空闲则不进行调度
                        map.put("sameLay", false);//不同层
                        map.put("result", null);
                        return map;
                    }
                    //获取提升机
                    LiftSlave liftSlave = slaveProperties.getLift().get(0);
                    //提升机库位号
                    String recentLiftLocNo = liftSlave.getLiftLocNo(recentShuttleLocNoLayInt);
                    String shuttleLiftLocNo = liftSlave.getLiftLocNo(shuttleLocNoLayInt);
                    //当前最近四向穿梭车到提升机路径
                    List<NavigateNode> recentShuttlePath = NavigateUtils.calc(recentShuttleLocNo, recentLiftLocNo, ShuttleTaskModeType.PAK_IN.id);
                    //当前楼层四向穿梭车到提升机路径
                    List<NavigateNode> shuttlePath = NavigateUtils.calc(shuttleLocNo, shuttleLiftLocNo, ShuttleTaskModeType.PAK_IN.id);
                    //判断哪一个路径最短
                    if (shuttlePath.size() < recentShuttlePath.size()) {
                        //如果当前楼层的车路径更小,则更新最近穿梭车
                        recentShuttle = shuttleThread;
                    }
                }
            }
        }
        map.put("sameLay", false);//不同层
        map.put("result", recentShuttle);
        return map;
    }
    /**
     * 四向穿梭车任务完成
     */
    public synchronized void shuttleFinished() {
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车信息
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
            //四向穿梭车状态为等待确认
            if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.WAITING.id && shuttleProtocol.getTaskNo() != 0) {
                //将任务档标记为完成
                WrkMast wrkMast = wrkMastMapper.selectByWorkNo59(shuttleProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
                    switch (wrkMast.getWrkSts().intValue()) {
                        case 9:
                            wrkMast.setWrkSts(14L);
                            break;
                        case 5:
                            wrkMast.setWrkSts(6L);
                            break;
                        default:
                    }
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        //设置四向穿梭车为空闲状态
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                        //任务号清零
                        shuttleProtocol.setTaskNo((short) 0);
                        //源库位清零
                        shuttleProtocol.setSourceLocNo(null);
                        //目标库位清零
                        shuttleProtocol.setLocNo(null);
                        //标记复位
                        shuttleProtocol.setPakMk(true);
                        //任务指令清零
                        shuttleProtocol.setAssignCommand(null);
                        News.info("四向穿梭车已确认且任务完成状态,复位。四向穿梭车号={}", shuttleProtocol.getShuttleNo());
                    } else {
                        News.error("四向穿梭车已确认且任务完成状态,复位失败,但未找到工作档。四向穿梭车号={},工作号={}", shuttleProtocol.getShuttleNo(), shuttleProtocol.getTaskNo());
                    }
                }
            }
        }
    }
    /**
     * 提升机任务
     */
    public synchronized void liftIoExecute() {
        for (LiftSlave liftSlave : slaveProperties.getLift()) {
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
            if (liftThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                continue;
            }
            //判断提升机是否处于空闲状态
            if (!liftProtocol.isIdle()) {
                continue;
            }
            //搜索是否有待处理的任务
            WrkMast wrkMast = wrkMastMapper.selectLiftStep6();
            if (wrkMast == null) {
                continue;
            }
            //给提升机分配任务
            liftProtocol.setLiftLock(true);//锁定提升机
            liftProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());//设置任务号
            liftProtocol.setShuttleNo(wrkMast.getShuttleNo().shortValue());//设置四向穿梭车号
            liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
            //找到四向穿梭车的线程
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, liftProtocol.getShuttleNo().intValue());
            if (shuttleThread == null) {
                continue;
            }
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
            //命令list
            ArrayList<LiftCommand> commands = new ArrayList<>();
            //当前穿梭车库位号
            String currentLocNo = shuttleProtocol.getCurrentLocNo();
            //当前穿梭车楼层
            int currentLocNoLey = Integer.parseInt(currentLocNo.substring(currentLocNo.length() - 2, currentLocNo.length()));
            //工作档目标库位号
            String wrkMastLocNo = wrkMast.getLocNo();
            //工作档目标库位楼层
            int wrkMastLocNoLey = Integer.parseInt(wrkMastLocNo.substring(wrkMastLocNo.length() - 2, wrkMastLocNo.length()));
            //提升机当前楼层
            int liftLev = liftProtocol.getLev().intValue();
            if (liftLev != currentLocNoLey) {
                //不同楼层
                LiftCommand command1 = new LiftCommand();
                command1.setLiftNo(liftProtocol.getLiftNo());//提升机号
                command1.setTaskNo(liftProtocol.getTaskNo());//任务号
                command1.setRun((short) 1);//升降
                command1.setDistPosition((short) currentLocNoLey);//目标楼层(穿梭车所在楼层)
                command1.setLiftLock(true);//锁定提升机
                commands.add(command1);//将命令添加进list
            }
            //输送线将四向穿梭车移动进来
            LiftCommand command2 = new LiftCommand();
            command2.setLiftNo(liftProtocol.getLiftNo());//提升机号
            command2.setTaskNo(liftProtocol.getTaskNo());//任务号
            command2.setRun((short) 6);//输送线运作
            command2.setLiftLock(true);//锁定提升机
            commands.add(command2);//将命令添加进list
            //提升机搬运四向穿梭车
            LiftCommand command3 = new LiftCommand();
            command3.setLiftNo(liftProtocol.getLiftNo());//提升机号
            command3.setTaskNo(liftProtocol.getTaskNo());//任务号
            command3.setRun((short) 1);//升降
            command3.setDistPosition((short) wrkMastLocNoLey);//工作档目标楼层
            command3.setLiftLock(true);//锁定提升机
            commands.add(command3);//将命令添加进list
            //提升机到达指定楼层,输送线将四向穿梭车移出去
            //输送线将四向穿梭车移动进来
            LiftCommand command4 = new LiftCommand();
            command4.setLiftNo(liftProtocol.getLiftNo());//提升机号
            command4.setTaskNo(liftProtocol.getTaskNo());//任务号
            command4.setRun((short) 3);//输送线运作
            command4.setLiftLock(true);//锁定提升机
            commands.add(command4);//将命令添加进list
            wrkMast.setWrkSts(7L);//移动任务
            //所需命令组合完毕,更新数据库,提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(liftProtocol.getTaskNo());
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
            }
        }
    }
    /**
     * 提升机任务完成
     */
    public synchronized void liftFinished() {
        for (LiftSlave liftSlave : slaveProperties.getLift()) {
            //获取提升机信息
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                continue;
            }
            //提升机为等待确认
            if (liftProtocol.getProtocolStatus() == LiftProtocolStatusType.WAITING.id && liftProtocol.getTaskNo() != 0) {
                //将任务档标记为完成
                WrkMast wrkMast = wrkMastMapper.selectByWorkNo7(liftProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
                    wrkMast.setWrkSts(8L);
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        //设置提升机为空闲状态
                        liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                        //任务号清零
                        liftProtocol.setTaskNo((short) 0);
                        //标记复位
                        liftProtocol.setPakMk(true);
                        //任务指令清零
                        liftProtocol.setAssignCommand(null);
                        News.info("提升机已确认且任务完成状态,复位。提升机号={}", liftProtocol.getLiftNo());
                    } else {
                        News.error("提升机已确认且任务完成状态,复位失败,但未找到工作档。提升机号={},工作号={}", liftProtocol.getLiftNo(), liftProtocol.getTaskNo());
                    }
                }
            }
        }
    }
    /**
     * 入出库  ===>>  堆垛机入出库作业下发
     */
    public synchronized void crnIoExecute(){
@@ -715,6 +1091,51 @@
            }
            if (!locMast.getLocSts().equals("S") && !locMast.getLocSts().equals("Q")) {
                News.error("入库操作库位状态不符合--状态, 库位号={},库位状态={}", wrkMast.getLocNo(), locMast.getLocSts());
                continue;
            }
            List<LocMast> locMasts = null;
            boolean sign=false;
            if (locMast.getRow1()>=5){
                locMasts=locMastService.selectList(new EntityWrapper<LocMast>().ge("row1", 5)
                        .eq("bay1",locMast.getBay1()).eq("lev1",locMast.getLev1()));
                for (LocMast locMast1:locMasts){
                    if (locMast1.getRow1()>locMast.getRow1()){
                        if (!locMast1.getLocSts().equals("F") && !locMast1.getLocSts().equals("D")){
                            if (!locMast1.getLocSts().equals("X")){
                                sign=true;
                                break;
                            }
                        }
                    }else if (locMast1.getRow1()<locMast.getRow1()){
                        if (locMast1.getLocSts().equals("F") || locMast1.getLocSts().equals("D")){
                            News.error("当前工作档目标库位所在排前边存在货物!", wrkMast.getWrkNo());
                            sign=true;
                            break;
                        }
                    }
                }
            }else {
                locMasts=locMastService.selectList(new EntityWrapper<LocMast>().le("row1", 4)
                        .eq("bay1",locMast.getBay1()).eq("lev1",locMast.getLev1()));
                for (LocMast locMast1:locMasts){
                    if (locMast1.getRow1()>locMast.getRow1()){
                        if (locMast1.getLocSts().equals("F") || locMast1.getLocSts().equals("D")){
                            News.error("当前工作档目标库位所在排前边存在货物!", wrkMast.getWrkNo());
                            sign=true;
                            break;
                        }
                    }else if (locMast1.getRow1()<locMast.getRow1()){
                        if (!locMast1.getLocSts().equals("F") && !locMast1.getLocSts().equals("D")){
                            if (!locMast1.getLocSts().equals("X")){
                                sign=true;
                                break;
                            }
                        }
                    }
                }
            }
            if (sign){
                continue;
            }
@@ -891,6 +1312,36 @@
            LocMast sourceSta = locMastService.selectById(wrkMast.getSourceLocNo());
            if (!sourceSta.getLocSts().equals("R") &&!sourceSta.getLocSts().equals("P")) {
                News.error("出库操作库位状态不符合--状态, 库位号={},库位状态={}", wrkMast.getLocNo(), sourceSta.getLocSts());
                continue;
            }
            List<LocMast> locMasts = null;
            boolean sign=false;
            if (sourceSta.getRow1()>=5){
                locMasts=locMastService.selectList(new EntityWrapper<LocMast>().ge("row1", 5)
                        .eq("bay1",sourceSta.getBay1()).eq("lev1",sourceSta.getLev1()));
                for (LocMast locMast1:locMasts){
                    if (locMast1.getRow1()<sourceSta.getRow1()){
                        if (!locMast1.getLocSts().equals("O")){
                            News.error("当前工作档源库位所在排前边存在货物!", wrkMast.getWrkNo());
                            sign=true;
                            break;
                        }
                    }
                }
            }else {
                locMasts=locMastService.selectList(new EntityWrapper<LocMast>().le("row1", 4)
                        .eq("bay1",sourceSta.getBay1()).eq("lev1",sourceSta.getLev1()));
                for (LocMast locMast1:locMasts){
                    if (locMast1.getRow1()>sourceSta.getRow1()){
                        if (!locMast1.getLocSts().equals("O")){
                            News.error("当前工作档目标库位所在排前边存在货物!", wrkMast.getWrkNo());
                            sign=true;
                            break;
                        }
                    }
                }
            }
            if (sign){
                continue;
            }
@@ -2510,6 +2961,9 @@
     */
    @SuppressWarnings("serial")
    public synchronized void loopSteCharge() {
        if (!Cools.isEmpty(wrkMastMapper.selectAllC())){
            return;
        }
        if (null != wrkChargeService.selectWorking(null, WrkChargeType.reset)) {
            return;
        }