#
Junjie
2024-06-12 848b1bc9331722a43b062b6bfb013b7e1804b7f4
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/action/ShuttleAction.java
@@ -1,8 +1,10 @@
package com.zy.asrs.wcs.core.action;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.asrs.wcs.common.ExecuteSupport;
import com.zy.asrs.wcs.core.entity.Loc;
@@ -11,15 +13,13 @@
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.LocStsType;
import com.zy.asrs.wcs.core.model.enums.MotionCtgType;
import com.zy.asrs.wcs.core.model.enums.ShuttleCommandModeType;
import com.zy.asrs.wcs.core.model.enums.ShuttleTaskModeType;
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.core.utils.*;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.constant.DeviceRedisConstant;
@@ -28,6 +28,8 @@
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import com.zy.asrs.wcs.system.entity.Dict;
import com.zy.asrs.wcs.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -50,6 +52,10 @@
    private ShuttleDispatcher shuttleDispatcher;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private DictService dictService;
    @Autowired
    private ConveyorDispatcher conveyorDispatcher;
    public synchronized boolean assignWork(Device device, ShuttleAssignCommand assignCommand) {
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
@@ -69,7 +75,7 @@
        redisCommand.setCommandStep(0);//命令执行步序
        redisCommand.setAssignCommand(assignCommand);//命令
        //任务数据保存到redis
        if (redisUtil.set(DeviceRedisConstant.SHUTTLE_WORK_FLAG + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand))) {
        if (redisUtil.set(DeviceRedisConstant.SHUTTLE_WORK_FLAG + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect))) {
            if (assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_IN.id
                    || assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_OUT.id
                    || assignCommand.getTaskMode() == ShuttleTaskModeType.MOVE_LOC_NO.id
@@ -134,7 +140,15 @@
                if (command.getTargetLocNo().equals(shuttleProtocol.getCurrentLocNo())) {
                    command.setComplete(true);
                    //解锁锁定路径,上一条路径
                    List<NavigateNode> nodes = JSON.parseArray(JSON.toJSONString(command.getNodes()), NavigateNode.class);//进行深度copy
                    List<NavigateNode> nodes = null;
                    try {
                        String nodesStr = objectMapper.writeValueAsString(command.getNodes());
                        nodes = objectMapper.readValue(nodesStr, new TypeReference<List<NavigateNode>>() {
                        });
                    } catch (JsonProcessingException e) {
                        throw new RuntimeException(e);
                    }
                    if (nodes != null) {
                        NavigateNode targetNode = assignCommand.getNodes().get(assignCommand.getNodes().size() - 1);//最终节点
                        NavigateNode node = nodes.get(nodes.size() - 1);
@@ -174,7 +188,7 @@
            }
            // 更新redis数据
            redisUtil.set(DeviceRedisConstant.SHUTTLE_WORK_FLAG + redisCommand.getTaskNo(), JSON.toJSONString(redisCommand));
            redisUtil.set(DeviceRedisConstant.SHUTTLE_WORK_FLAG + redisCommand.getTaskNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect));
            if (!command.getComplete()) {
                return false;
@@ -250,7 +264,7 @@
        if (shuttleThread == null) {
            return;
        }
        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(false);
        if (shuttleProtocol == null) {
            return;
        }
@@ -266,77 +280,240 @@
        }
        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);
            //根据地图方向决定跑x或y
            Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>()
                    .eq(Dict::getFlag, "direction_map")
                    .eq(Dict::getStatus, 1));
            if (dict == null) {
                //跑库结束
                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;
            }
            String direction = dict.getValue();
            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());
            if (direction.equals("y")) {//跑x轴方向,跑完x轴再切换y轴
                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>()
                        .eq(Loc::getLocSts, LocStsType.O.val())
                        .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 {
                    //在起点位置,调度去目标位置
                    shuttleDispatcher.generateMoveTask(device, target.getLocNo());
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);//切换y轴
                    if(shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
                        //y轴也跑完了,结束跑库
                        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;
                    }
                }
            }else {//跑y轴方向,跑完y轴再切换x轴
                ArrayList<String> locs = new ArrayList<>();
                for (int i = shuttleProtocol.getYCurrent(); i <= shuttleProtocol.getYTarget(); i++) {
                    String locNo = Utils.getLocNo(shuttleProtocol.getXCurrent(), i, lev);
                    locs.add(locNo);
                }
                List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                        .eq(Loc::getLocSts, LocStsType.O.val())
                        .in(Loc::getLocNo, locs));
                if (locList.isEmpty()) {
                    //空库位
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 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 {
                    //在起点位置,调度去目标位置
                    shuttleDispatcher.generateMoveTask(device, target.getLocNo());
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);//切换x轴
                    if(shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
                        //y轴也跑完了,结束跑库
                        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;
                    }
                }
            }
        } 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);
            //根据地图方向决定跑x或y
            Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>()
                    .eq(Dict::getFlag, "direction_map")
                    .eq(Dict::getStatus, 1));
            if (dict == null) {
                //跑库结束
                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;
            }
            String direction = dict.getValue();
            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 (direction.equals("y")) {//跑x轴方向,跑完x轴再切换y轴
                Integer xCurrent = shuttleProtocol.getXCurrent();
//            if (!target.getLocSts().equals("O")) {
//                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
//                return;
//            }
                //获取待跑库位号
                String locNo = Utils.getLocNo(xCurrent, shuttleProtocol.getYCurrent(), lev);
                Loc target = locService.getOne(new LambdaQueryWrapper<Loc>()
                        .eq(Loc::getLocNo, locNo)
                        .eq(Loc::getLocSts, LocStsType.O.val())
                        .eq(Loc::getHostId, device.getHostId()));
                if (target == null || shuttleProtocol.getCurrentLocNo().equals(locNo)) {//库位不存在或小车已在当前位置
                    shuttleProtocol.setXCurrent(xCurrent + 1);
                    if (shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
                        //x轴跑完,切换y轴
                        shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
                        shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
            //调度去目标位置
            if (shuttleProtocol.getCurrentLocNo().equals(target.getLocNo())) {
                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);//小车和目标位置一致,跳过
            } else {
                shuttleDispatcher.generateMoveTask(device, target.getLocNo());
                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
                        if(shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
                            //y轴也跑完了,结束跑库
                            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;
                        }
                    }
                    return;
                }
                //调度去库位
                Task task = shuttleDispatcher.generateMoveTask(device, locNo);
                if (task == null) {
                    return;//调度失败
                }
                shuttleProtocol.setXCurrent(xCurrent + 1);
                if (shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
                    //x轴跑完,切换y轴
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
                    if(shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
                        //y轴也跑完了,结束跑库
                        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;
                    }
                }
            }else {//跑y轴方向,跑完y轴再切换x轴
                Integer yCurrent = shuttleProtocol.getYCurrent();
                //获取待跑库位号
                String locNo = Utils.getLocNo(shuttleProtocol.getXCurrent(), yCurrent, lev);
                Loc target = locService.getOne(new LambdaQueryWrapper<Loc>()
                        .eq(Loc::getLocNo, locNo)
                        .eq(Loc::getLocSts, LocStsType.O.val())
                        .eq(Loc::getHostId, device.getHostId()));
                if (target == null || shuttleProtocol.getCurrentLocNo().equals(locNo)) {//库位不存在或小车已在当前位置
                    shuttleProtocol.setYCurrent(yCurrent + 1);
                    if (shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
                        //y轴跑完,切换x轴
                        shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());
                        shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
                        if(shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
                            //x轴也跑完了,结束跑库
                            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;
                        }
                    }
                }
                //调度去库位
                Task task = shuttleDispatcher.generateMoveTask(device, locNo);
                if (task == null) {
                    return;//调度失败
                }
                shuttleProtocol.setYCurrent(yCurrent + 1);
                if (shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
                    //y轴跑完,切换x轴
                    shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());
                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
                    if(shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
                        //x轴也跑完了,结束跑库
                        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;
                    }
                }
            }
        } else if (shuttleProtocol.getMoveType() == 2) {//母轨道循环跑
            Integer xCurrent = shuttleProtocol.getXCurrent();
@@ -382,6 +559,66 @@
                    }
                }
            }
        } else if (shuttleProtocol.getMoveType() == 4) {//取放货
            Integer xCurrent = shuttleProtocol.getXCurrent();
            if (xCurrent > shuttleProtocol.getXTarget()) {//当X值大于X目标值
                shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
                shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());
                return;
            }
            //判断x轴货位是否放满
            boolean flag = true;
            for (Loc loc : locService.list(new LambdaQueryWrapper<Loc>()
                    .eq(Loc::getHostId, device.getHostId())
                    .eq(Loc::getRow, xCurrent)
                    .ge(Loc::getBay, shuttleProtocol.getYStart())
                    .le(Loc::getBay, shuttleProtocol.getYTarget()))) {
                if (loc.getLocSts() != LocStsType.F.val()) {
                    flag = false;//未满
                    break;
                }
            }
            if (flag) {
                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
                return;
            }
            //搜索有货库位
            List<Loc> list = locService.list(new LambdaQueryWrapper<Loc>()
                    .eq(Loc::getLocSts, LocStsType.F.val())
                    .eq(Loc::getHostId, device.getHostId())
                    .notIn(Loc::getRow, xCurrent)
                    .eq(Loc::getStatus, 1));
            if (list.isEmpty()) {
                return;
            }
            Loc start = list.get(0);
            List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                    .eq(Loc::getHostId, device.getHostId())
                    .eq(Loc::getStatus, 1)
                    .eq(Loc::getLocSts, LocStsType.O.val())
                    .eq(Loc::getRow, xCurrent)
                    .orderByDesc(Loc::getBay)
                    .orderByAsc(Loc::getRow));
            if (locList.isEmpty()) {
                return;
            }
            Loc target = locList.get(0);
            if (target == null) {
                return;
            }
            //调度去目标位置
            if (!shuttleProtocol.getCurrentLocNo().equals(target.getLocNo())) {
                Task task = shuttleDispatcher.generateManuaTakeMoveTask(device, start.getLocNo(), target.getLocNo());
//                if(task != null) {//调度成功
//                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
//                }
            }
        }
    }