#
Junjie
2024-04-09 f84448a10d99a0fa82e71088051e3517637edaa7
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/action/ShuttleAction.java
@@ -2,15 +2,20 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.wcs.core.entity.BasShuttle;
import com.zy.asrs.wcs.core.entity.Loc;
import com.zy.asrs.wcs.core.entity.Task;
import com.zy.asrs.wcs.core.model.NavigateNode;
import com.zy.asrs.wcs.core.model.command.ShuttleAssignCommand;
import com.zy.asrs.wcs.core.model.command.ShuttleCommand;
import com.zy.asrs.wcs.core.model.command.ShuttleRedisCommand;
import com.zy.asrs.wcs.core.model.enums.ShuttleCommandModeType;
import com.zy.asrs.wcs.core.service.BasShuttleService;
import com.zy.asrs.wcs.core.service.LocService;
import com.zy.asrs.wcs.core.utils.NavigateMapUtils;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.core.utils.ShuttleDispatcher;
import com.zy.asrs.wcs.core.utils.Utils;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
@@ -23,6 +28,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@@ -34,6 +40,10 @@
    private NavigateMapUtils navigateMapUtils;
    @Autowired
    private BasShuttleService basShuttleService;
    @Autowired
    private LocService locService;
    @Autowired
    private ShuttleDispatcher shuttleDispatcher;
    public synchronized boolean assignWork(Device device, ShuttleAssignCommand assignCommand) {
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
@@ -85,10 +95,8 @@
            return false;
        }
        //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令
        if (!shuttleProtocol.getIdle()
                || shuttleProtocol.getTaskNo() == 0
                || !shuttleProtocol.getPakMk()) {
        //判断设备是否空闲
        if (!shuttleThread.isIdle()) {
            return false;
        }
@@ -136,8 +144,8 @@
                if (!shuttleProtocol.getHasLift()) {
                    command.setComplete(true);
                }
            } else if (command.getMode() == ShuttleCommandModeType.CHARGE.id) {
                // 充电开关
            } else if (command.getMode() == ShuttleCommandModeType.CHARGE_OPEN.id) {
                // 充电开
                //判断小车充电状态
                if (shuttleProtocol.getHasCharge()) {
                    command.setComplete(true);
@@ -197,6 +205,148 @@
        return true;
    }
    //跑库程序
    public synchronized void moveLoc(Device device) {
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
        if (shuttleThread == null) {
            return;
        }
        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
        if (shuttleProtocol == null) {
            return;
        }
        //小车开启跑库模式
        if (!shuttleProtocol.getMoveLoc()) {
            return;
        }
        //小车空闲
        if (!shuttleThread.isIdle()) {
            return;
        }
        int lev = Utils.getLev(shuttleProtocol.getCurrentLocNo());//小车当前楼层
        if (shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
            //跑库结束
            shuttleProtocol.setMoveLoc(false);
            shuttleProtocol.setMoveType(0);
            shuttleProtocol.setXStart(0);
            shuttleProtocol.setXTarget(0);
            shuttleProtocol.setXCurrent(0);
            shuttleProtocol.setYStart(0);
            shuttleProtocol.setYTarget(0);
            shuttleProtocol.setYCurrent(0);
            return;
        }
        if (shuttleProtocol.getMoveType() == 0) {//跑轨道
            ArrayList<String> locs = new ArrayList<>();
            for (int i = shuttleProtocol.getXCurrent(); i <= shuttleProtocol.getXTarget(); i++) {
                String locNo = Utils.getLocNo(i, shuttleProtocol.getYCurrent(), lev);
                locs.add(locNo);
            }
            List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                    .in(Loc::getLocNo, locs));
            if (locList.isEmpty()) {
                //空库位
                shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
                return;
            }
            Loc start = locList.get(0);
            Loc target = locList.get(locList.size() - 1);
            //判断小车是否在起点位置
            if (!shuttleProtocol.getCurrentLocNo().equals(start.getLocNo())) {//不在起点位置,调度去起点位置
                shuttleDispatcher.generateMoveTask(device, start.getLocNo());
            }else {
                //在起点位置,调度去目标位置
                if (shuttleProtocol.getCurrentLocNo().equals(target.getLocNo())) {
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);//小车和目标位置一致,跳过
                }else {
                    shuttleDispatcher.generateMoveTask(device, start.getLocNo());
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
                }
            }
        } else if (shuttleProtocol.getMoveType() == 1) {//跑库位
            Integer xCurrent = shuttleProtocol.getXCurrent();
            if (xCurrent > shuttleProtocol.getXTarget()) {//当X值大于X目标值,进行归零且Y方向+1
                shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
                shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
                return;
            }
            Integer yCurrent = shuttleProtocol.getYCurrent();
            String locNo = Utils.getLocNo(xCurrent, yCurrent, lev);
            Loc target = locService.getOne(new LambdaQueryWrapper<Loc>()
                    .eq(Loc::getLocNo, locNo)
                    .eq(Loc::getHostId, device.getHostId()));
            if (target == null) {
                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
                return;
            }
//            if (!target.getLocSts().equals("O")) {
//                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
//                return;
//            }
            //调度去目标位置
            if (shuttleProtocol.getCurrentLocNo().equals(target.getLocNo())) {
                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);//小车和目标位置一致,跳过
            } else {
                shuttleDispatcher.generateMoveTask(device, target.getLocNo());
                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
            }
        } else if (shuttleProtocol.getMoveType() == 2) {//母轨道循环跑
            Integer xCurrent = shuttleProtocol.getXCurrent();
            Integer yCurrent = shuttleProtocol.getYCurrent();
            String locNo = Utils.getLocNo(xCurrent, yCurrent, lev);
            //调度去目标位置
            if (shuttleProtocol.getCurrentLocNo().equals(locNo)) {
                if (yCurrent.equals(shuttleProtocol.getYStart())) {
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYTarget());//小车和目标位置一致,切换库位
                } else {
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());//小车和目标位置一致,切换库位
                }
            } else {
                Task result = shuttleDispatcher.generateMoveTask(device, locNo);
                if (result != null) {//调度成功
                    if (yCurrent.equals(shuttleProtocol.getYStart())) {
                        shuttleProtocol.setYCurrent(shuttleProtocol.getYTarget());//切换库位
                    } else {
                        shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());//切换库位
                    }
                }
            }
        } else if (shuttleProtocol.getMoveType() == 3) {//子轨道循环跑
            Integer xCurrent = shuttleProtocol.getXCurrent();
            Integer yCurrent = shuttleProtocol.getYCurrent();
            String locNo = Utils.getLocNo(xCurrent, yCurrent, lev);
            //调度去目标位置
            if (shuttleProtocol.getCurrentLocNo().equals(locNo)) {
                if (xCurrent.equals(shuttleProtocol.getXStart())) {
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXTarget());//小车和目标位置一致,切换库位
                } else {
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());//小车和目标位置一致,切换库位
                }
            } else {
                Task result = shuttleDispatcher.generateMoveTask(device, locNo);
                if (result != null) {//调度成功
                    if (xCurrent.equals(shuttleProtocol.getXStart())) {
                        shuttleProtocol.setXCurrent(shuttleProtocol.getXTarget());//切换库位
                    } else {
                        shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());//切换库位
                    }
                }
            }
        }
    }
    private synchronized boolean write(ShuttleCommand command, Device device) {
        if (null == command) {
            News.error("四向穿梭车写入命令为空");
@@ -211,7 +361,8 @@
        } else if (command.getMode() == ShuttleCommandModeType.PALLET_LIFT.id
                || command.getMode() == ShuttleCommandModeType.PALLET_DOWN.id) {//顶升
            result = shuttleThread.lift(command);
        } else if (command.getMode() == ShuttleCommandModeType.CHARGE.id) {//充电
        } else if (command.getMode() == ShuttleCommandModeType.CHARGE_OPEN.id
                || command.getMode() == ShuttleCommandModeType.CHARGE_CLOSE.id) {//充电
            result = shuttleThread.charge(command);
        } else if (command.getMode() == ShuttleCommandModeType.RESET.id) {//复位
            result = shuttleThread.reset(command);