Junjie
2023-03-29 32bf433492f540b1392c19757124ebb91d0e74bd
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,13 @@
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.command.*;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.ShuttleProtocol;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.model.protocol.SteProtocol;
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 +88,8 @@
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetlMapper orderDetlMapper;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 组托
@@ -612,6 +606,236 @@
    }
    /**
     * 入出库  ===>>  四向穿梭车入出库作业下发
     */
    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) {
                            //寻找最近且空闲的四向穿梭车
                            HashMap<String, Object> searchIdleShuttle = this.searchIdleShuttle(wrkMast);
                            ShuttleThread shuttleThread = (ShuttleThread) searchIdleShuttle.get("result");
                            if (shuttleThread == null) {
                                continue;
                            }
                            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                            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());
                            //源库位(小车当前位置)
                            Short currentCode = shuttleProtocol.getCurrentCode();
                            LocMast currentLocMast = locMastService.queryByQrCode(currentCode.toString());
                            assignCommand.setSourceLocNo(currentLocMast.getLocNo());
                            if (Boolean.parseBoolean(searchIdleShuttle.get("sameLay").toString())) {
                                //同一层
                                //分配目标库位
                                shuttleProtocol.setLocNo(wrkMast.getLocNo());
                                //目标库位
                                assignCommand.setDistLocNo(wrkMast.getLocNo());
                                wrkMast.setWrkSts(9L);//小车入库中
                            }else {
                                //不同层,将目标库位分配成提升机库位号
                                //分配目标库位
                                shuttleProtocol.setLocNo("1800201");
                                //目标库位
                                assignCommand.setDistLocNo("1800201");
                                wrkMast.setWrkSts(5L);//小车迁移状态
                            }
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }else {
                            //状态8,四向穿梭车已在提升机口,等待命令进行入库搬运动作
                            Integer shuttleNo = wrkMast.getShuttleNo();//四向穿梭车号
                            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
                            if (shuttleThread == null) {
                                continue;
                            }
                            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                            //分配目标库位
                            shuttleProtocol.setLocNo(wrkMast.getLocNo());
                            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                            //四向穿梭车号
                            assignCommand.setShuttleNo(shuttleNo.shortValue());
                            //任务号
                            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //入出库模式
                            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());
                            //源库位(小车当前位置)
                            Short currentCode = shuttleProtocol.getCurrentCode();
                            LocMast currentLocMast = locMastService.queryByQrCode(currentCode.toString());
                            assignCommand.setSourceLocNo(currentLocMast.getLocNo());
                            //目标库位
                            assignCommand.setDistLocNo(wrkMast.getLocNo());
                            wrkMast.setWrkSts(9L);//小车入库中
                            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;
            }
            Short currentCode = shuttleProtocol.getCurrentCode();//四向穿梭车当前位置二维码
            LocMast shuttleLocMast = locMastService.queryByQrCode(currentCode.toString());//找到二维码对应的库位记录
            String shuttleLocNo = shuttleLocMast.getLocNo();//二维码对应库位号
            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();//目前最近穿梭车
                Short recentShuttleCurrentCode = recentShuttleProtocol.getCurrentCode();//目前最近穿梭车位置二维码
                LocMast recentShuttleLocMast = locMastService.queryByQrCode(recentShuttleCurrentCode.toString());//找到二维码对应的库位记录
                String recentShuttleLocNo = recentShuttleLocMast.getLocNo();//二维码对应库位号
                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) {
                    //楼层相同情况
                    //找距离出库点最近的车
                    //当前最近四向穿梭车到提升机路径
                    List<NavigateNode> recentShuttlePath = NavigateUtils.calc(recentShuttleLocNo, "1800201", ShuttleTaskModeType.PAK_IN.id);
                    //当前楼层四向穿梭车到提升机路径
                    List<NavigateNode> shuttlePath = NavigateUtils.calc(shuttleLocNo, "1800201", 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.selectByWorkNo9(shuttleProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
                    wrkMast.setWrkSts(14L);
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        //设置四向穿梭车为空闲状态
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                        //任务号清零
                        shuttleProtocol.setTaskNo((short) 0);
                        //源库位清零
                        shuttleProtocol.setSourceLocNo(null);
                        //目标库位清零
                        shuttleProtocol.setLocNo(null);
                        //标记复位
                        shuttleProtocol.setPakMk(true);
                        News.error("四向穿梭车已确认且任务完成状态,复位。堆垛机号={}", shuttleProtocol.getShuttleNo());
                    } else {
                        News.error("四向穿梭车已确认且任务完成状态,复位失败,但未找到工作档。四向穿梭车号={},工作号={}", shuttleProtocol.getShuttleNo(), shuttleProtocol.getTaskNo());
                    }
                }
            }
        }
    }
    /**
     * 入出库  ===>>  堆垛机入出库作业下发
     */
    public synchronized void crnIoExecute(){
@@ -715,6 +939,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 +1160,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 +2809,9 @@
     */
    @SuppressWarnings("serial")
    public synchronized void loopSteCharge() {
        if (!Cools.isEmpty(wrkMastMapper.selectAllC())){
            return;
        }
        if (null != wrkChargeService.selectWorking(null, WrkChargeType.reset)) {
            return;
        }