Junjie
2023-04-14 f57741a11f0cce1f7ac0f2c8ab0627c046cb855b
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -4,20 +4,17 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
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;
@@ -25,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;
@@ -78,6 +68,10 @@
    @Autowired
    private BasSteErrLogService basSteErrLogService;
    @Autowired
    private BasShuttleErrLogService basShuttleErrLogService;
    @Autowired
    private BasShuttleErrService basShuttleErrService;
    @Autowired
    private BasCrnErrorMapper basCrnErrorMapper;
    @Autowired
    private BasSteService basSteService;
@@ -95,6 +89,8 @@
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetlMapper orderDetlMapper;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 组托
@@ -114,59 +110,51 @@
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                // 尺寸检测异常
                boolean back = false;
                String errMsg = "异常:";
                if (staProtocol.isFrontErr()) {
                    errMsg = errMsg+"前超限;";
                    back = true;
                }
                if (staProtocol.isBackErr()) {
                    errMsg = errMsg+"后超限";
                    back = true;
                }
                if (staProtocol.isHighErr()) {
                    errMsg = errMsg+"高超限";
                    back = true;
                }
                if (staProtocol.isLeftErr()) {
                    errMsg = errMsg+"左超限";
                    back = true;
                }
                if (staProtocol.isRightErr()) {
                    errMsg = errMsg+"右超限";
                    back = true;
                }
                if (staProtocol.isWeightErr()) {
                    errMsg = errMsg+"超重";
                    back = true;
                }
                if (staProtocol.isBarcodeErr()) {
                    errMsg = errMsg+"扫码失败";
                    back = true;
                }
                // 退回
                if (back) {
                    // led 异常显示
                    LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                    if (ledThread != null) {
                        MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errMsg));
                    }
                    continue;
                }
                // 判断是否满足入库条件
                if (staProtocol.isAutoing() && staProtocol.isLoading()
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk() && (workNo == 0 || (workNo >= 9990 && workNo <= 9999))
                        && staProtocol.isPakMk()) {
                    // 尺寸检测异常
                    boolean back = false;
                    String errMsg = "";
                    if (staProtocol.isFrontErr()) {
                        errMsg = "前超限";
                        back = true;
                    }
                    if (!back && staProtocol.isBackErr()) {
                        errMsg = "后超限";
                        back = true;
                    }
                    if (!back && staProtocol.isHighErr()) {
                        errMsg = "高超限";
                        back = true;
                    }
                    if (!back && staProtocol.isLeftErr()) {
                        errMsg = "左超限";
                        back = true;
                    }
                    if (!back && staProtocol.isRightErr()) {
                        errMsg = "右超限";
                        back = true;
                    }
                    if (!back && staProtocol.isWeightErr()) {
                        errMsg = "超重";
                        back = true;
                    }
                    if (!back && staProtocol.isBarcodeErr()) {
                        errMsg = "扫码失败";
                        back = true;
                    }
                    // 退回
                    if (back) {
                        News.warn("扫码入库失败,{}入库站因{}异常,托盘已被退回", inSta.getStaNo(), errMsg);
                        staProtocol.setWorkNo((short) 32002);
                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
                        devpThread.setPakMk(staProtocol.getSiteId(), false);
                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                        // led 异常显示
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                        if (ledThread != null) {
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errMsg));
                        }
                        continue;
                    }
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
@@ -176,7 +164,7 @@
                    String barcode = barcodeThread.getBarcode();
                    if(!Cools.isEmpty(barcode)) {
//                        News.info("{}号条码扫描器检测条码信息:{}", inSta.getBarcode(), barcode);
                        if("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
                        if("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)|| "00000000".equals(barcode)) {
                            staProtocol.setWorkNo((short) 32002);
                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
@@ -407,7 +395,6 @@
                    if (staProtocol.getWorkNo() > 0 && staProtocol.getWorkNo() < 9990) {
                        wrkMast = wrkMastMapper.selectPickStep(staProtocol.getWorkNo());
                        if (null == wrkMast) {
                            News.error("{}任务号错误,暂无拣料任务!", staProtocol.getWorkNo());
                            continue;
                        }
                    } else {
@@ -619,6 +606,894 @@
        }
    }
//    /**
//     * 入库  ===>>  四向穿梭车入库作业下发
//     */
//    public synchronized void shuttleIoInExecute() {
//        // 根据输送线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 synchronized void shuttleIoInExecute() {
        // 根据输送线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);//源库位(小车当前位置)
                            String locNo = wrkMast.getLocNo();//当前工作档库位号
                            Integer lev = Integer.parseInt(locNo.substring(locNo.length() - 2, locNo.length()));//当前工作档库位层高
                            Integer currentLev = Integer.parseInt(currentLocNo.substring(currentLocNo.length() - 2, currentLocNo.length()));//小车当前层高
                            //获取提升机
                            LiftSlave liftSlave = slaveProperties.getLift().get(0);
                            //提升机库位号
                            String liftLocNo = liftSlave.getLiftLocNo(currentLev);
                            if (wrkMast.getWrkSts() == 8 || Boolean.parseBoolean(searchIdleShuttle.get("sameLay").toString())) {
                                //同一层直接取货无需经过提升机
                                //直接计算车到提升机取货再到库位路径指令
                                List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftLocNo, locNo);
                                if (wrkMast.getWrkSts() == 8) {
                                    //此时车在提升机内部,需要多下达一步指令让车移动到提升机口
                                    BasDevp basDevp = basDevpService.selectById(109);//获取提升机信息
                                    ShuttleCommand moveCommand = new ShuttleCommand();
                                    moveCommand.setCommandWord((short) 1);
                                    moveCommand.setStartCodeNum(Short.parseShort(basDevp.getQrCodeValue()));//提升机内部二维码
                                    moveCommand.setDistCodeNum(commands.get(0).getStartCodeNum());//提升机口二维码
                                    moveCommand.setStartToDistDistance(1300);
                                    moveCommand.setMiddleToDistDistance(0);
                                    moveCommand.setRunDirection(commands.get(0).getRunDirection());
                                    moveCommand.setRunSpeed((short) 1000);
                                    moveCommand.setCommandEnd((short) 1);
                                    commands.add(0, moveCommand);//将该指令添加到队头
                                }
                                assignCommand.setCommands(commands);
                                //分配目标库位
                                shuttleProtocol.setLocNo(wrkMast.getLocNo());
                                //目标库位
                                assignCommand.setLocNo(wrkMast.getLocNo());
                                wrkMast.setWrkSts(9L);//小车入库中
                            }else {
                                //不同层,将目标库位分配成提升机库位号
                                //小车移动到提升机口,计算路径
                                List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftLocNo, ShuttleTaskModeType.PAK_IN.id);
                                //分配目标库位
                                shuttleProtocol.setLocNo(liftLocNo);
                                //目标库位
                                assignCommand.setLocNo(liftLocNo);
                                assignCommand.setCommands(commands);
                                wrkMast.setWrkSts(5L);//小车迁移状态
                            }
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }
                    }
                }
            }
        }
    }
    //获取起点-终点指令。mapType:1=》无货地图,2=》有货地图
    public synchronized List<ShuttleCommand> shuttleAssignCommand(String startLocNo, String locNO,Integer mapType) {
        //计算小车起点到中点所需命令
        List<NavigateNode> calc = NavigateUtils.calc(startLocNo, locNO, mapType);
        List<ShuttleCommand> commands = new ArrayList<>();
        if (calc == null) {
            return null;
        }
        //获取分段路径
        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(calc);
        //将每一段路径分成command指令
        for (ArrayList<NavigateNode> nodes : data) {
            //开始路径
            NavigateNode startPath = nodes.get(0);
            //目标路径
            NavigateNode endPath = nodes.get(nodes.size() - 1);
            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
            ShuttleCommand command = new ShuttleCommand();
            command.setCommandWord((short) 1);
            //通过xy坐标小车二维码
            Short startCodeNum = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());
            command.setStartCodeNum(startCodeNum);
            command.setMiddleCodeNum((short) 1);
            //通过xy坐标小车二维码
            Short distCodeNum = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());
            command.setDistCodeNum(distCodeNum);
            command.setStartToDistDistance(allDistance);
            command.setMiddleToDistDistance(0);
            command.setRunDirection(ShuttleRunDirection.get(startPath.getDirection()).id);
            command.setPalletLift((short) 1);
            command.setForceMoveDistance(0);
            command.setChargeSwitch((short) 2);
            command.setIOControl((short) 0);
            command.setRunSpeed((short) 1000);
            command.setRadarTmp((short) 0);
            command.setCommandEnd((short) 1);
            commands.add(command);
        }
        return commands;
    }
    //获取起点-中点-终点指令
    public synchronized List<ShuttleCommand> shuttleAssignCommand(String startLocNo, String middleLocNo, String locNO) {
        //计算小车起点到中点所需命令
        List<NavigateNode> calc = NavigateUtils.calc(startLocNo, middleLocNo, 1);//小车无货,走入库地图
        List<ShuttleCommand> commands = new ArrayList<>();
        if (calc == null) {
            return null;
        }
        //获取分段路径
        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(calc);
        //将每一段路径分成command指令
        for (ArrayList<NavigateNode> nodes : data) {
            //开始路径
            NavigateNode startPath = nodes.get(0);
            //目标路径
            NavigateNode endPath = nodes.get(nodes.size() - 1);
            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
            ShuttleCommand command = new ShuttleCommand();
            command.setCommandWord((short) 1);
            //通过xy坐标小车二维码
            Short startCodeNum = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());
            command.setStartCodeNum(startCodeNum);
            command.setMiddleCodeNum((short) 1);
            //通过xy坐标小车二维码
            Short distCodeNum = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());
            command.setDistCodeNum(distCodeNum);
            command.setStartToDistDistance(allDistance);
            command.setMiddleToDistDistance(0);
            command.setRunDirection(ShuttleRunDirection.get(startPath.getDirection()).id);
            command.setPalletLift((short) 1);
            command.setForceMoveDistance(0);
            command.setChargeSwitch((short) 2);
            command.setIOControl((short) 0);
            command.setRunSpeed((short) 1000);
            command.setRadarTmp((short) 0);
            command.setCommandEnd((short) 1);
            commands.add(command);
        }
        //小车指令到达目标位置后,再发出一条顶升指令
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 2);
        command.setPalletLift((short) 1);
        command.setCommandEnd((short) 1);
        commands.add(command);
        //计算小车中点到终点所需命令
        List<NavigateNode> calc2 = NavigateUtils.calc(middleLocNo, locNO, 2);//小车有货,走出库地图(出库地图有专用货道)
        if (calc2 == null) {
            return null;
        }
        //获取分段路径
        ArrayList<ArrayList<NavigateNode>> data2 = NavigateUtils.getSectionPath(calc2);
        for (ArrayList<NavigateNode> nodes : data2) {
            //开始路径
            NavigateNode startPath = nodes.get(0);
            //目标路径
            NavigateNode endPath = nodes.get(nodes.size() - 1);
            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
            command = new ShuttleCommand();
            command.setCommandWord((short) 1);
            //通过xy坐标小车二维码
            Short startCodeNum = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());
            command.setStartCodeNum(startCodeNum);
            command.setMiddleCodeNum((short) 1);
            //通过xy坐标小车二维码
            Short distCodeNum = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());
            command.setDistCodeNum(distCodeNum);
            command.setStartToDistDistance(allDistance);
            command.setMiddleToDistDistance(0);
            command.setRunDirection(ShuttleRunDirection.get(startPath.getDirection()).id);
            command.setPalletLift((short) 1);
            command.setForceMoveDistance(0);
            command.setChargeSwitch((short) 2);
            command.setIOControl((short) 0);
            command.setRunSpeed((short) 1000);
            command.setRadarTmp((short) 0);
            command.setCommandEnd((short) 1);
            commands.add(command);
        }
        //小车指令到达目标位置后,再发出一条托盘下降指令
        command = new ShuttleCommand();
        command.setCommandWord((short) 2);
        command.setPalletLift((short) 2);
        command.setCommandEnd((short) 1);
        commands.add(command);
        return commands;
    }
    /**
     * 出库  ===>>  四向穿梭车出库作业下发
     */
    public synchronized void shuttleIoOutExecute() {
        for (WrkMast wrkMast : wrkMastMapper.selectBy2125()) {
            //提取一条待出库任务
            if (wrkMast != null) {
                String outStaLocNo = null;//出库站点库位号
                //获取出库站点
                for (DevpSlave devpSlave : slaveProperties.getDevp()) {
                    for (DevpSlave.StaRack staOutRack : devpSlave.getRackOutStn()) {
                        if (staOutRack.getStaNo().equals(wrkMast.getStaNo())) {
                            //出库站点和工作档出库站点一致
                            outStaLocNo = CommonUtils.getLocNoFromRBL(staOutRack.getRow(), staOutRack.getBay(), staOutRack.getLev());
                        }
                    }
                }
                if (wrkMast.getWrkSts() == 21
                        || wrkMast.getWrkSts() == 25
                        || wrkMast.getWrkSts() == 31) {
                    ShuttleThread shuttleThread = null;
                    HashMap<String, Object> searchIdleShuttle = null;
                    if (wrkMast.getWrkSts() == 21) {
                        //寻找最近且空闲的四向穿梭车
                        searchIdleShuttle = this.searchIdleShuttle(wrkMast);
                        shuttleThread = (ShuttleThread) searchIdleShuttle.get("result");
                    }else if(wrkMast.getWrkSts() == 31 || wrkMast.getWrkSts() == 25) {
                        //继续完成之前小车未完成的任务
                        shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
                    }
                    if (shuttleThread == null) {
                        continue;
                    }
                    ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                    if (shuttleProtocol == null) {
                        continue;
                    }
                    if (!shuttleProtocol.isIdle()) {
                        continue;
                    }
                    if (outStaLocNo == null) {
                        continue;
                    }
                    wrkMast.setShuttleNo(shuttleProtocol.getShuttleNo().intValue());//给工作档分配四向穿梭车号
                    //源库位(小车当前位置)
                    String currentLocNo = shuttleProtocol.getCurrentLocNo();
                    //小车当前层高
                    Integer currentLev = Integer.parseInt(currentLocNo.substring(currentLocNo.length() - 2, currentLocNo.length()));
                    //获取提升机
                    LiftSlave liftSlave = slaveProperties.getLift().get(0);
                    //提升机库位号
                    String liftLocNo = liftSlave.getLiftLocNo(currentLev);
                    //分配任务号
                    shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
                    //分配源库位
                    shuttleProtocol.setSourceLocNo(currentLocNo);
                    ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                    //四向穿梭车号
                    assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                    //任务号
                    assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                    //入出库模式
                    assignCommand.setTaskMode(ShuttleTaskModeType.PAK_OUT.id.shortValue());
                    assignCommand.setSourceLocNo(currentLocNo);
                    if (wrkMast.getWrkSts() == 21) {
                        //判断小车和库位是否在同一层
                        if (Boolean.parseBoolean(searchIdleShuttle.get("sameLay").toString())) {
                            //同一层(将小车移动到货物位置)
                            List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, wrkMast.getSourceLocNo(), liftLocNo);
                            //分配目标库位
                            shuttleProtocol.setLocNo(wrkMast.getSourceLocNo());
                            //目标库位
                            assignCommand.setLocNo(wrkMast.getSourceLocNo());
                            assignCommand.setCommands(commands);
                            wrkMast.setWrkSts(26L);//小车搬运中
                        }else {
                            //不同层,将目标库位分配成提升机库位号(将小车移动到提升机位置)
                            //小车到提升机口指令
                            List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftLocNo, ShuttleTaskModeType.PAK_IN.id);
                            shuttleProtocol.setLocNo(liftLocNo);
                            //目标库位
                            assignCommand.setLocNo(liftLocNo);
                            assignCommand.setCommands(commands);
                            wrkMast.setWrkSts(22L);//小车迁移状态
                        }
                    } else if (wrkMast.getWrkSts() == 25) {
                        List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, wrkMast.getSourceLocNo(), liftLocNo);
                        //此时车在提升机内部,需要多下达一步指令让车移动到提升机口
                        BasDevp basDevp = basDevpService.selectById(109);//获取提升机信息
                        ShuttleCommand moveCommand = new ShuttleCommand();
                        moveCommand.setCommandWord((short) 1);//小车移动指令字
                        moveCommand.setStartCodeNum(Short.parseShort(basDevp.getQrCodeValue()));//提升机二维码
                        moveCommand.setDistCodeNum(commands.get(0).getStartCodeNum());
                        moveCommand.setStartToDistDistance(1300);
                        moveCommand.setMiddleToDistDistance(0);
                        moveCommand.setRunDirection(commands.get(0).getRunDirection());
                        moveCommand.setRunSpeed((short) 1000);
                        moveCommand.setCommandEnd((short) 1);
                        commands.add(0, moveCommand);//将该指令添加到队头
                        //分配目标库位
                        shuttleProtocol.setLocNo(wrkMast.getSourceLocNo());
                        //目标库位
                        assignCommand.setLocNo(wrkMast.getSourceLocNo());
                        assignCommand.setCommands(commands);
                        wrkMast.setWrkSts(26L);//小车搬运中
                    }
                    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.selectByWorkNo(shuttleProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
                    switch (wrkMast.getWrkSts().intValue()) {
                        case 9:
                            wrkMast.setWrkSts(14L);
                            break;
                        case 5:
                            wrkMast.setWrkSts(6L);
                            break;
                        case 22:
                            wrkMast.setWrkSts(23L);
                            break;
                        case 26:
                            wrkMast.setWrkSts(27L);
                            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());
                    }
                }
            }
            //四向穿梭车状态为充电状态
            if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.CHARGING_WAITING.id  //充电标识
                    && shuttleProtocol.getTaskNo() != 0) {
                //查询是否有充电任务
                WrkCharge wrkCharge = wrkChargeMapper.selectByWorkNo(shuttleProtocol.getTaskNo().intValue());
                if (wrkCharge != null) {
                    switch (wrkCharge.getWrkSts().intValue()) {
                        case 52://四向穿梭车迁移到提升机口
                            wrkCharge.setWrkSts(53L);//迁移完成
                            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);
                            break;
                        case 56:
                            if (shuttleProtocol.getBatteryPower() == 1000) {
                                wrkCharge.setWrkSts(60L);//充电完成
                            }
                            break;
                        default:
                    }
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        if (wrkCharge.getWrkSts() == 60) {
                            //设置四向穿梭车为空闲状态
                            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.selectLiftStep623();
            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.getIoType() == 101 ? wrkMast.getSourceLocNo() : 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
            if (wrkMast.getIoType() == 101) {
                //出库任务
                wrkMast.setWrkSts(24L);//移动任务
            }else {
                //入库任务
                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.selectByWorkNo724(liftProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
                    switch (wrkMast.getWrkSts().intValue()) {
                        case 7:
                            wrkMast.setWrkSts(8L);
                            break;
                        case 24:
                            wrkMast.setWrkSts(25L);
                            break;
                        default:
                    }
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        //设置提升机为空闲状态
                        liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                        //任务号清零
                        liftProtocol.setTaskNo((short) 0);
                        //标记复位
                        liftProtocol.setPakMk(true);
                        //任务指令清零
                        liftProtocol.setAssignCommand(null);
                        //提升机解锁
                        liftProtocol.setLiftLock(false);
                        News.info("提升机已确认且任务完成状态,复位。提升机号={}", liftProtocol.getLiftNo());
                    } else {
                        News.error("提升机已确认且任务完成状态,复位失败,但未找到工作档。提升机号={},工作号={}", liftProtocol.getLiftNo(), liftProtocol.getTaskNo());
                    }
                }
                //查询是否有充电任务
                WrkCharge wrkCharge = wrkChargeMapper.selectByWorkNo(liftProtocol.getTaskNo().intValue());
                if (wrkCharge != null) {
                    switch (wrkCharge.getWrkSts().intValue()) {
                        case 54://提升机搬运中
                            wrkCharge.setWrkSts(55L);//提升机搬运完成
                            break;
                        default:
                    }
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        //设置提升机为空闲状态
                        liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                        //任务号清零
                        liftProtocol.setTaskNo((short) 0);
                        //标记复位
                        liftProtocol.setPakMk(true);
                        //任务指令清零
                        liftProtocol.setAssignCommand(null);
                        //提升机解锁
                        liftProtocol.setLiftLock(false);
                        News.info("提升机已确认且任务完成状态,复位。提升机号={}", liftProtocol.getLiftNo());
                    } else {
                        News.error("提升机已确认且任务完成状态,复位失败,但未找到工作档。提升机号={},工作号={}", liftProtocol.getLiftNo(), liftProtocol.getTaskNo());
                    }
                }
            }
        }
    }
    /**
     * 入出库  ===>>  堆垛机入出库作业下发
     */
@@ -726,6 +1601,51 @@
                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;
            }
            // 检测是否存在出库任务
            if (null != wrkMastMapper.selectPakout(slave.getId(), null)) {
                News.error("{}入库任务无法作业,因存在出库中任务!", wrkMast.getWrkNo());
@@ -733,7 +1653,9 @@
            }
            // 置顶任务
            wrkMast.setIoPri((double) 9999);
            if (wrkMast.getIoPri()<8000){
                wrkMast.setIoPri(wrkMast.getIoPri()+9000d);
            }
            wrkMastMapper.updateById(wrkMast);
                // 目标库位 ===>> 最外层库位
@@ -899,6 +1821,36 @@
                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;
            }
            // 同库位组进行校验
            List<String> groupLocNo = Utils.getGroupLocNo(wrkMast.getSourceLocNo());
@@ -948,7 +1900,9 @@
                }
                // 置顶任务
                wrkMast.setIoPri((double) 9998);
                if (wrkMast.getIoPri()<8000){
                    wrkMast.setIoPri(wrkMast.getIoPri()+8999d);
                }
                wrkMastMapper.updateById(wrkMast);
                // 最外层库位,直接堆垛机出库
@@ -1244,7 +2198,7 @@
            SteProtocol steProtocol = steThread.getSteProtocol();
            if (steProtocol == null) { continue; }
            if (!steProtocol.isIdle()) { continue; }
            if (steProtocol.getRow() == 1) { continue; }
//            if (steProtocol.getRow() == 1) { continue; }
            String locNo = wrkMast.getWrkSts() < 10 ? wrkMast.getLocNo() : wrkMast.getSourceLocNo();
            // 如果在同一个库位组
            if (Utils.getGroupRow(locNo, true).equals(Utils.getGroupRow(steProtocol.getRow().intValue(), true))
@@ -1613,6 +2567,17 @@
                    WrkCharge wrkCharge = wrkChargeService.selectById(crnProtocol.getTaskNo());
                    if (wrkCharge == null) {
                        News.error("堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                        wrkMast = wrkMastMapper.selectCrnWaiting2(crnProtocol.getTaskNo().intValue());
                        if (wrkMast.getIoType() != 11) {
                            // 修改成功后复位堆垛机
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                // 堆垛机复位
                                crnThread.setResetFlag(true);
                                News.error("堆垛机处于等待确认且任务完成状态,复位,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                            } else {
                                News.error("堆垛机处于等待确认且任务完成状态,复位失败,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                            }
                        }
                        continue;
                    } else {
                        // 小车搬至指定库位完成
@@ -1835,6 +2800,22 @@
                            }
                        }
                        // 移库 ===>> 堆垛机搬至目标库位组 完成
                    } else if (wrkMast.getWrkSts() == 16) {
                        // 16.吊车出库中 ==> 17.出库完成
                        wrkMast.setWrkSts(17L);
                        Date now = new Date();
                        wrkMast.setCrnEndTime(now);
                        wrkMast.setModiTime(now);
                        LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());
                        Integer outCrnNo = locMastService.getOutCrnNo(locMast);
                        wrkMast.setCrnNo(outCrnNo);
                        // 修改成功后复位堆垛机
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            // 堆垛机复位
                            crnThread.setResetFlag(true);
                        } else {
                            News.error("修改工作档状态 16.吊车出库中 ==> 17.出库完成 失败!!,工作号={}", wrkMast.getWrkNo());
                        }
                    }
                }
            }
@@ -1879,15 +2860,17 @@
                                } else {
                                    steThread.setResetFlag(true);
                                }
                            } else if (wrkCharge.getWrkSts() == 29) {
                                // 29.开始充电 ===>> 30.完成充电
                                wrkCharge.setWrkSts(30L);
                                if (!wrkChargeService.updateById(wrkCharge)) {
                                    News.error("修改充电任务状态 29.开始充电 ===>> 30.完成充电 失败!!,工作号={}", wrkCharge.getWrkNo());
                                } else {
                                    steThread.setResetFlag(true);
                                }
                            } else if (wrkCharge.getWrkSts() == 32) {
                            }
//                            else if (wrkCharge.getWrkSts() == 29) {
//                                // 29.开始充电 ===>> 30.完成充电
//                                wrkCharge.setWrkSts(30L);
//                                if (!wrkChargeService.updateById(wrkCharge)) {
//                                    News.error("修改充电任务状态 29.开始充电 ===>> 30.完成充电 失败!!,工作号={}", wrkCharge.getWrkNo());
//                                } else {
//                                    steThread.setResetFlag(true);
//                                }
//                            }
                            else if (wrkCharge.getWrkSts() == 32) {
                                // 32.小车走行 ===>> 33.小车待搬
                                wrkCharge.setWrkSts(33L);
                                if (!wrkChargeService.updateById(wrkCharge)) {
@@ -1990,10 +2973,129 @@
     */
    public void recErr() {
        try {
            this.recShuttleErr();
            this.recCrnErr();
            this.recSteErr();
        } catch (Exception e) {
            News.error("recErr fail", e);
        }
    }
    /**
     * 四向穿梭车异常信息记录
     */
    private void recShuttleErr() {
        Date now = new Date();
        for (ShuttleSlave shuttleSlave : slaveProperties.getShuttle()) {
            // 获取堆垛机信息
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleSlave.getId());
            if (shuttleThread == null) {
                continue;
            }
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
            if (shuttleProtocol.getTaskNo() != 0) {
                //有任务
                BasShuttleErrLog latest = basShuttleErrLogService.findLatestByTaskNo(shuttleSlave.getId(), shuttleProtocol.getTaskNo().intValue());
                // 有异常
                if (latest == null) {
                    if (shuttleProtocol.getStatusErrorCode() != null && shuttleProtocol.getStatusErrorCode() > 0) {
                        WrkMast wrkMast = wrkMastMapper.selectById(shuttleProtocol.getTaskNo());
                        if (wrkMast == null) {
                            continue;
                        }
                        BasShuttleErr basShuttleErr = basShuttleErrService.queryByCode(shuttleProtocol.getStatusErrorCode().intValue());
                        String errName = basShuttleErr==null? "未知异常":basShuttleErr.getErrName();
                        BasShuttleErrLog basShuttleErrLog = new BasShuttleErrLog(
                                null,    // 编号
                                wrkMast.getWrkNo(),    // 工作号
                                now,    // 发生时间
                                null,    // 结束时间
                                wrkMast.getWrkSts(),    // 工作状态
                                wrkMast.getIoType(),    // 入出库类型
                                shuttleSlave.getId(),    // 四向穿梭车
                                null,    // plc
                                wrkMast.getLocNo(),    // 目标库位
                                wrkMast.getStaNo(),    // 目标站
                                wrkMast.getSourceStaNo(),    // 源站
                                wrkMast.getSourceLocNo(),    // 源库位
                                wrkMast.getBarcode(),    // 条码
                                (int) shuttleProtocol.getStatusErrorCode(),    // 异常码
                                errName,    // 异常
                                1,    // 异常情况
                                now,    // 添加时间
                                null,    // 添加人员
                                now,    // 修改时间
                                null,    // 修改人员
                                "任务中异常"    // 备注
                        );
                        if (!basShuttleErrLogService.insert(basShuttleErrLog)) {
                            News.error("四向穿梭车plc异常记录失败 ===>> [id:{}] [error:{}]", shuttleSlave.getId(), errName);
                        }
                    }
                } else {
                    // 异常修复
                    if (shuttleProtocol.getStatusErrorCode() == null || shuttleProtocol.getStatusErrorCode() == 0) {
                        latest.setEndTime(now);
                        latest.setUpdateTime(now);
                        latest.setStatus(2);
                        if (!basShuttleErrLogService.updateById(latest)) {
                            News.error("四向穿梭车plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", shuttleSlave.getId(), latest.getId());
                        }
                    }
                }
            }else {
                //无任务
                BasShuttleErrLog latest = basShuttleErrLogService.findLatest(shuttleSlave.getId());
                // 有异常
                if (shuttleProtocol.getStatusErrorCode() != null && shuttleProtocol.getStatusErrorCode() > 0) {
                    // 记录新异常
                    if (latest == null || (latest.getErrCode() != shuttleProtocol.getStatusErrorCode().intValue())) {
                        BasShuttleErr basShuttleErr = basShuttleErrService.queryByCode(shuttleProtocol.getStatusErrorCode().intValue());
                        String errName = basShuttleErr==null? "未知异常":basShuttleErr.getErrName();
                        BasShuttleErrLog basShuttleErrLog = new BasShuttleErrLog(
                                null,    // 编号
                                null,    // 工作号
                                now,    // 发生时间
                                null,    // 结束时间
                                null,    // 工作状态
                                null,    // 入出库类型
                                shuttleSlave.getId(),    // 四向穿梭车
                                null,    // plc
                                null,    // 目标库位
                                null,    // 目标站
                                null,    // 源站
                                null,    // 源库位
                                null,    // 条码
                                (int)shuttleProtocol.getStatusErrorCode(),    // 异常码
                                errName,    // 异常
                                1,    // 异常情况
                                now,    // 添加时间
                                null,    // 添加人员
                                now,    // 修改时间
                                null,    // 修改人员
                                "无任务异常"    // 备注
                        );
                        if (!basShuttleErrLogService.insert(basShuttleErrLog)) {
                            News.error("四向穿梭车plc异常记录失败 ===>> [id:{}] [error:{}]", shuttleSlave.getId(), errName);
                        }
                    }
                    // 无异常
                } else {
                    // 异常修复
                    if (latest != null && latest.getStatus() == 1) {
                        latest.setEndTime(now);
                        latest.setUpdateTime(now);
                        latest.setStatus(2);
                        if (!basShuttleErrLogService.updateById(latest)) {
                            News.error("四向穿梭车plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", shuttleSlave.getId(), latest.getId());
                        }
                    }
                }
            }
        }
    }
@@ -2366,18 +3468,11 @@
                        break;
                }
                ledCommand.setSourceLocNo(wrkMast.getSourceLocNo());
                ledCommand.setLocNo(wrkMast.getLocNo());
                ledCommand.setStaNo(wrkMast.getStaNo());
                if (wrkMast.getIoType() != 110) {
                if (wrkMast.getIoType() != 110 && wrkMast.getIoType() != 10) {
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
                    try {
                        WrkDetl wrkDetl = wrkDetls.get(0);
                        if (!Cools.isEmpty(wrkDetl.getOrderNo())) {
                            OrderDetl orderDetl = orderDetlMapper.selectItemNoneOfBatch(wrkDetl.getOrderNo(), wrkDetl.getMatnr());
                            ledCommand.getMatDtos().add(new MatDto(orderDetl));
                        }
                    } catch (Exception e) {
                        News.error("led execute fail", e);
                    }
                    wrkDetls.forEach(wrkDetl -> ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMaktx(), wrkDetl.getAnfme(),wrkDetl.getSpecs())));
                }
                commands.add(ledCommand);
            }
@@ -2390,9 +3485,20 @@
            }
            // 命令下发 -------------------------------------------------------------------------------
            if (!commands.isEmpty()) {
                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
                    News.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                    continue;
                if (led.getId()>3){
                    if (!MessageQueue.offer(SlaveType.Led, led.getId()-3, new Task(1, commands))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId()-3, led.getIp(), led.getPort());
                        continue;
                    }else {
                        ledThread.setLedMk(false);
                    }
                }else {
                    if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                        continue;
                    }else {
                        ledThread.setLedMk(false);
                    }
                }
            }
@@ -2422,6 +3528,35 @@
     */
    public void ledReset() {
        for (LedSlave led : slaveProperties.getLed()) {
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
            // 命令集合
            boolean reset = true;
            for (Integer staNo : led.getStaArr()) {
                // 获取叉车站点
                StaProtocol staProtocol = devpThread.getStation().get(staNo);
                if (staProtocol == null) {
                    continue;
                }
                if (staProtocol.getWorkNo() != 0 && staProtocol.isLoading()) {
                    reset = false;
                    break;
                }
            }
            // 获取led线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
            // led显示默认内容
            if (reset && !ledThread.isLedMk()) {
                ledThread.setLedMk(true);
                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(4, new ArrayList<>()))) {
                    News.error(" - {}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                } else {
                }
            }
        }
        for (LedSlave led : slaveProperties.getLed()) {
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
            // 命令集合
@@ -2436,9 +3571,10 @@
                }
            }
            // 获取led线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getDevpPlcId());
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
            // led显示默认内容
            if (reset) {
            if (reset && !ledThread.isLedMk()) {
                ledThread.setLedMk(true);
                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(2, new ArrayList<>()))) {
                    News.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                }
@@ -2447,10 +3583,281 @@
    }
    /**
     * 四向穿梭车电量检测 ===>> 发起充电
     */
    public synchronized void loopShuttleCharge() {
        for (DevpSlave devpSlave : slaveProperties.getDevp()) {
            SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, devpSlave.getId());
            for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
                //获取四向穿梭车线程
                ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
                ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                if (shuttleProtocol == null) {
                    continue;
                }
                //判断当前小车是否满足需要充电要求
                if (!shuttleProtocol.isRequireCharge()) {
                    continue;
                }
                WrkCharge wrkCharge = wrkChargeService.selectWorking(null, WrkChargeType.charge);
                if (wrkCharge != null) {//已有充电任务
                    continue;
                }
                ShuttleChargeType shuttleCharge = null;
                String locNo;
                for (ShuttleChargeType chargeType : ShuttleChargeType.values()) {
                    switch (chargeType.id) {
                        case 1:
                            if (devpThread.charge0 == false) {
                                ShuttleChargeType first = ShuttleChargeType.FIRST;
                                locNo = first.locNo;
                                if (wrkChargeService.selectWorkingOfCharge(first.id) == null) {
                                    shuttleCharge = first;
                                }
                            }
                            break;
                        default:
                    }
                }
                if (shuttleCharge == null) {
                    continue;
                }
                String chargeLocNo = shuttleCharge.locNo;
                wrkCharge = new WrkCharge();
                wrkCharge.setShuttleNo(shuttle.getId());
                wrkCharge.setCharge(shuttleCharge.id);
                wrkCharge.setWrkNo(commonService.getChargeWorkNo(4));
                wrkCharge.setWrkSts(51L);   // 21.准备充电
                wrkCharge.setIoPri((double) 10);
                wrkCharge.setLocNo(chargeLocNo);
                wrkCharge.setMemo("charge");
                wrkCharge.setAppeTime(new Date());
                if (!wrkChargeService.insert(wrkCharge)) {
                    News.error("保存{}号四向穿梭车充电任务失败!!!", shuttle.getId());
                    continue;
                }
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);//充电中
                News.info("保存{}号四向穿梭车充电任务成功!!!", shuttle.getId());
            }
        }
    }
    /**
     * 执行四向穿梭车充电任务
     */
    public synchronized void executeShuttleCharge() {
        WrkCharge wrkCharge = wrkChargeService.selectWorking(null, WrkChargeType.charge);
        if (wrkCharge == null) {
            return;
        }
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkCharge.getShuttleNo());
        if (shuttleThread == null) {
            return;
        }
        ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
        if (shuttleProtocol == null) {
            return;
        }
        //当前穿梭车库位号
        String currentLocNo = shuttleProtocol.getCurrentLocNo();
        //小车当前层高
        Integer currentLev = Integer.parseInt(currentLocNo.substring(currentLocNo.length() - 2, currentLocNo.length()));
        //获取提升机
        LiftSlave liftSlave = slaveProperties.getLift().get(0);
        //提升机库位号
        String liftLocNo = liftSlave.getLiftLocNo(currentLev);
        //充电库位号
        String chargeLocNo = wrkCharge.getLocNo();
        //充电库位层高
        Integer chargeLocNoLev = Integer.parseInt(chargeLocNo.substring(chargeLocNo.length() - 2, chargeLocNo.length()));
        if (wrkCharge.getWrkSts() == 51 || wrkCharge.getWrkSts() == 55) {
            if (currentLev == chargeLocNoLev) {
                //同一层无需经过提升机
                //直接计算车到充电库位
                //获取小车到充电库位路径指令
                List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, chargeLocNo, ShuttleTaskModeType.PAK_IN.id);
                //进行充电中
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);
                ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                assignCommand.setTaskMode((short) 9);//充电
                assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());
                assignCommand.setCharge(true);//充电任务
                //创建充电指令
                ShuttleCommand command = new ShuttleCommand();
                command.setCommandWord((short) 5);//充电
                command.setShuttleNo(shuttleProtocol.getShuttleNo());
                command.setChargeSwitch((short) 1);//开始充电
                command.setCommandEnd((short) 1);
                commands.add(command);
                //指令集分配
                assignCommand.setCommands(commands);
                wrkCharge.setWrkSts(56L);//充电中状态
                if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                }
            }else {
                //不同层,将目标库位分配成提升机库位号
                //小车移动到提升机口,计算路径
                List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftLocNo, ShuttleTaskModeType.PAK_IN.id);
                //分配目标库位
                shuttleProtocol.setLocNo(liftLocNo);
                ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                assignCommand.setTaskMode((short) 9);//充电
                assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());
                assignCommand.setCharge(true);//充电任务
                //目标库位
                assignCommand.setLocNo(liftLocNo);
                //源库位
                assignCommand.setSourceLocNo(currentLocNo);
                assignCommand.setCommands(commands);
                wrkCharge.setWrkSts(52L);//小车迁移状态
                if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                }
            }
        }else if(wrkCharge.getWrkSts() == 53){
            //小车已经达到提升机口
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
            if (liftThread == null) {
                return;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                return;
            }
            //判断提升机是否处于空闲状态
            if (!liftProtocol.isIdle()) {
                return;
            }
            //给提升机分配任务
            liftProtocol.setLiftLock(true);//锁定提升机
            liftProtocol.setTaskNo(wrkCharge.getWrkNo().shortValue());//设置任务号
            liftProtocol.setShuttleNo(wrkCharge.getShuttleNo().shortValue());//设置四向穿梭车号
            liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
            //命令list
            ArrayList<LiftCommand> commands = new ArrayList<>();
            //提升机当前楼层
            int liftLev = liftProtocol.getLev().intValue();
            if (liftLev != currentLev) {
                //穿梭车和提升机处于不同楼层
                LiftCommand command1 = new LiftCommand();
                command1.setLiftNo(liftProtocol.getLiftNo());//提升机号
                command1.setTaskNo(liftProtocol.getTaskNo());//任务号
                command1.setRun((short) 1);//升降
                command1.setDistPosition(currentLev.shortValue());//目标楼层(穿梭车所在楼层)
                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(chargeLocNoLev.shortValue());//充电库位目标楼层
            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
            wrkCharge.setWrkSts(54L);//提升机搬运中
            //所需命令组合完毕,更新数据库,提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(liftProtocol.getTaskNo());
            if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
            }
        } else if (wrkCharge.getWrkSts() == 56) {
            //充电中
            //判断小车是否充满电量,满电1000或电压54V以上
            if (shuttleProtocol.getBatteryPower() >= 1000 || shuttleProtocol.getCurrentVoltage() >= 540) {
                //充满,断开充电
                List<ShuttleCommand> commands = new ArrayList<>();
                ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                assignCommand.setTaskMode((short) 0);
                assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());
                assignCommand.setCharge(true);
                //创建充电指令
                ShuttleCommand command = new ShuttleCommand();
                command.setCommandWord((short) 5);//充电
                command.setShuttleNo(shuttleProtocol.getShuttleNo());
                command.setChargeSwitch((short) 2);//断开充电
                command.setCommandEnd((short) 1);
                commands.add(command);
                //指令集分配
                assignCommand.setCommands(commands);
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING_WAITING);
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
            if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.CHARGING_WAITING.id) {
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);
            }
        }
    }
    /**
     * 小车电量检测 ===>> 发起充电
     */
    @SuppressWarnings("serial")
    public synchronized void loopSteCharge() {
        if (!Cools.isEmpty(wrkMastMapper.selectAllC())){
            return;
        }
        if (null != wrkChargeService.selectWorking(null, WrkChargeType.reset)) {
            return;
        }
@@ -2486,7 +3893,7 @@
                    SteChargeType steCharge = null;
                    do {
                        String locNo;
                        if (!devpThread.charge0) {
                        if (devpThread.charge0) {
                            SteChargeType first = SteChargeType.FIRST;
                            locNo = first.locNo;
                            if (basSteService.hasCarOfLocNo(locNo) == null
@@ -2537,7 +3944,7 @@
                    }
                    // 处于充电库位组
                    if (steProtocol.getRow().intValue() == Utils.getRow(chargeLocNo)
                    if ( 0<steProtocol.getRow().intValue() && steProtocol.getRow().intValue()<=4
                            && steProtocol.getBay().intValue() == Utils.getBay(chargeLocNo)
                            && steProtocol.getLev().intValue() == Utils.getLev(chargeLocNo)) {
                        // 修改工作档状态 21.准备充电 => 24.小车到达
@@ -2564,10 +3971,26 @@
     */
    public synchronized void executeSteCharge() {
        WrkCharge wrkCharge = wrkChargeService.selectWorking(null, WrkChargeType.charge);
        if (null == wrkCharge) { return; }
        Integer steNo = wrkCharge.getSteNo();
        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, 1);
        SteProtocol steProtocol = steThread.getSteProtocol();
        if (null == wrkCharge) {
            try{
                if (steProtocol.getChargeStatus()==(short)0 && steProtocol.isBrushConnect()){
                    SteCommand steCommand = new SteCommand();
                    steCommand.setSteNo(1); // 穿梭车编号
                    steCommand.setTaskNo(10060); // 工作号
                    steCommand.setTaskMode(SteTaskModeType.CLOSE_CHARGE); // 任务模式: 断开充电
                    if (!MessageQueue.offer(SlaveType.Ste, 1, new Task(2, steCommand))) {
                        News.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", wrkCharge.getSteNo(), JSON.toJSON(steCommand));
                    }
                }
            }catch (Exception e){
                return;
            }
            return;
        }
        Integer steNo = wrkCharge.getSteNo();
        BasSte basSte = basSteService.selectById(steNo);
        if (Cools.isEmpty(steProtocol, basSte)) { return; }
        if (steProtocol.getMode() == 0
@@ -2581,9 +4004,35 @@
        if (!steProtocol.isEnable()) {
            return;
        }
        if (steProtocol.getCharge() > Float.parseFloat(basSte.getChargeLine())) {
        if (steProtocol.getCharge() > Float.parseFloat(basSte.getChargeLine())+40) {
            MessageQueue.offer(SlaveType.Devp, 1, new Task(3, 999));//中断充电标记
            wrkCharge.setWrkSts(30L);
            wrkChargeMapper.updateById(wrkCharge);
            if (!wrkChargeService.updateById(wrkCharge)) {
                News.error("修改充电任务状态 29.开始充电 ===>> 30.完成充电 失败!!,工作号={}", wrkCharge.getWrkNo());
            }else {
                SteCommand steCommand = new SteCommand();
                steCommand.setSteNo(wrkCharge.getSteNo()); // 穿梭车编号
                steCommand.setTaskNo(wrkCharge.getWrkNo()); // 工作号
                steCommand.setTaskMode(SteTaskModeType.CLOSE_CHARGE); // 任务模式: 断开充电
                if (!MessageQueue.offer(SlaveType.Ste, wrkCharge.getSteNo(), new Task(2, steCommand))) {
                    News.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", wrkCharge.getSteNo(), JSON.toJSON(steCommand));
                }
            }
            return;
        }else if (steProtocol.getCharge() > Float.parseFloat(basSte.getChargeLine()) && steProtocol.getChargeStatus()==(short)0 && steProtocol.isBrushConnect()) {
            MessageQueue.offer(SlaveType.Devp, 1, new Task(3, 999));//中断充电标记
            wrkCharge.setWrkSts(30L);
            if (!wrkChargeService.updateById(wrkCharge)) {
                News.error("修改充电任务状态 29.开始充电 ===>> 30.完成充电 失败!!,工作号={}", wrkCharge.getWrkNo());
            }else {
                SteCommand steCommand = new SteCommand();
                steCommand.setSteNo(wrkCharge.getSteNo()); // 穿梭车编号
                steCommand.setTaskNo(wrkCharge.getWrkNo()); // 工作号
                steCommand.setTaskMode(SteTaskModeType.CLOSE_CHARGE); // 任务模式: 断开充电
                if (!MessageQueue.offer(SlaveType.Ste, wrkCharge.getSteNo(), new Task(2, steCommand))) {
                    News.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", wrkCharge.getSteNo(), JSON.toJSON(steCommand));
                }
            }
            return;
        }
        try {
@@ -2644,7 +4093,7 @@
                    SteCommand steCommand = new SteCommand();
                    steCommand.setSteNo(wrkCharge.getSteNo()); // 穿梭车编号
                    steCommand.setTaskNo(wrkCharge.getWrkNo()); // 工作号
                    steCommand.setTaskMode(SteTaskModeType.GO_ORIGIN);  // 去右端
                    steCommand.setTaskMode(SteTaskModeType.BACK_ORIGIN);  // 去右端
                    steCommand.setRow(Utils.getGroupRow(steProtocol.getRow().intValue(), false).shortValue());
                    steCommand.setBay(steProtocol.getBay());
@@ -2663,6 +4112,7 @@
                    }
                }
            } else if (wrkCharge.getWrkSts() == 26) {
                //26.等待充电 ===>
                // 穿梭车下发充电任务
                SteCommand steCommand = new SteCommand();
                steCommand.setSteNo(wrkCharge.getSteNo()); // 穿梭车编号
@@ -2675,6 +4125,27 @@
                    wrkCharge.setWrkSts(29L);
                    if (!wrkChargeService.updateById(wrkCharge)) {
                        News.error("修改充电任务状态 28.充电就绪 ===>> 29.开始充电 失败!!,工作号={}", wrkCharge.getWrkNo());
                    }
                }
            } else if (wrkCharge.getWrkSts() == 29) {
                Float idle2 = steProtocol.isIdle2();
                String chargeLine = SpringUtils.getBean(BasSteService.class).selectById(1).getChargeLine();
                if (idle2<(Float.parseFloat(chargeLine)+2)){
                    MessageQueue.offer(SlaveType.Devp, 1, new Task(3, 666));//充电标记
                }else if (idle2 >= 100f){
                    MessageQueue.offer(SlaveType.Devp, 1, new Task(3, 999));//中断充电标记
                    // 29.开始充电 ===>> 30.完成充电
                    wrkCharge.setWrkSts(30L);
                    if (!wrkChargeService.updateById(wrkCharge)) {
                        News.error("修改充电任务状态 29.开始充电 ===>> 30.完成充电 失败!!,工作号={}", wrkCharge.getWrkNo());
                    }else {
                        SteCommand steCommand = new SteCommand();
                        steCommand.setSteNo(wrkCharge.getSteNo()); // 穿梭车编号
                        steCommand.setTaskNo(wrkCharge.getWrkNo()); // 工作号
                        steCommand.setTaskMode(SteTaskModeType.CLOSE_CHARGE); // 任务模式: 断开充电
                        if (!MessageQueue.offer(SlaveType.Ste, wrkCharge.getSteNo(), new Task(2, steCommand))) {
                            News.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", wrkCharge.getSteNo(), JSON.toJSON(steCommand));
                        }
                    }
                }
            }
@@ -3201,6 +4672,63 @@
        }
    }
    /**
     * 出入库模式切换
     */
    public synchronized void outAndIn() {
        try {
            // 根据输送线plc遍历
            for (DevpSlave devp : slaveProperties.getDevp()) {
                for (DevpSlave.Sta inSta : devp.getInSta()) {
                    Thread.sleep(500);
                    boolean a=true,b=true;
                    List<WrkMast> wrkMasts = wrkMastMapper.selectAll(inSta.getStaNo());
                    if (Cools.isEmpty(wrkMasts)){
                        b=false;
                    }else {
                        for (WrkMast wrkMast:wrkMasts){
                            if (wrkMast.getSourceStaNo() > wrkMast.getStaNo()){
                                a=false;
                                break;
                            }
                        }
                    }
                    switch (inSta.getStaNo()) {
                        case 102://1F
                            if (a && b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 11));
                            }else if (b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 12));
                            }else {
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 10));
                            }
                            break;
                        case 202://2F
                            if (a && b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 21));
                            }else if (b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 22));
                            }else {
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 20));
                            }
                            break;
                        case 302://3F
                            if (a && b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 31));
                            }else if (b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 32));
                            }else {
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 30));
                            }
                            break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}