#
vincentlu
2025-12-31 4937f052ec2c91b3fdcbb669aef7991912cde98e
zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java
@@ -91,36 +91,59 @@
        return result;
    }
    public synchronized Agv execute(Task task) {
    public synchronized String execute(Task task, AllocateSupport inbound, AllocateSupport normal) {
        // inbound roller station
        Sta rollerOriSta = getInboundRollerSta(task);
        if (rollerOriSta != null) {
            List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task), true);
            FilterLaneDto filterLaneDto = this.filterThroughLane(task, availableAgvNos);
            if (null != filterLaneDto) {
                String agvNo = this.checkoutAgvForInboundRoller(task, rollerOriSta, filterLaneDto.getActualAvailableAgvNos());
                if (!Cools.isEmpty(agvNo)) {
                    task.setOriLaneHash(filterLaneDto.getOriginLane().getHashCode());
                    task.setDestLaneHash(filterLaneDto.getDestinationLane().getHashCode());
                    return agvService.selectByUuid(agvNo);
                }
            }
        String inboundAgv = tryAllocateForRoller(task, rollerOriSta, true);
        if (!Cools.isEmpty(inboundAgv)) {
            inbound.success(task, inboundAgv);
            return inboundAgv;
        }
        // outbound roller station
        Sta rollerDestSta = getOutboundRollerSta(task);
        if (rollerDestSta != null) {
            List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task), true);
            FilterLaneDto filterLaneDto = this.filterThroughLane(task, availableAgvNos);
            if (null != filterLaneDto) {
                String agvNo = this.checkoutAgvForOutboundRoller(task, rollerDestSta, filterLaneDto.getActualAvailableAgvNos());
                if (!Cools.isEmpty(agvNo)) {
                    task.setOriLaneHash(filterLaneDto.getOriginLane().getHashCode());
                    task.setDestLaneHash(filterLaneDto.getDestinationLane().getHashCode());
                    return agvService.selectByUuid(agvNo);
                }
            }
        String outboundAgv = tryAllocateForRoller(task, rollerDestSta, false);
        if (!Cools.isEmpty(outboundAgv)) {
            normal.success(task, outboundAgv);
            return outboundAgv;
        }
        return this.normalExecute(task);
        String normalAgv = this.normalExecute(task);
        if (!Cools.isEmpty(normalAgv)) {
            normal.success(task, normalAgv);
            return normalAgv;
        }
        return null;
    }
    private String tryAllocateForRoller(Task task, Sta rollerSta, boolean inbound) {
        if (rollerSta == null) {
            return null;
        }
        List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task), true);
        FilterLaneDto filterLaneDto = this.filterThroughLane(task, availableAgvNos);
        if (filterLaneDto == null) {
            return null;
        }
        String agvNo = inbound
                ? this.checkoutAgvForInboundRoller(task, rollerSta, filterLaneDto.getActualAvailableAgvNos())
                : this.checkoutAgvForOutboundRoller(task, rollerSta, filterLaneDto.getActualAvailableAgvNos());
        if (Cools.isEmpty(agvNo)) {
            return null;
        }
        // record lane hash for later dispatch/traffic-control logic
        if (filterLaneDto.getOriginLane() != null) {
            task.setOriLaneHash(filterLaneDto.getOriginLane().getHashCode());
        }
        if (filterLaneDto.getDestinationLane() != null) {
            task.setDestLaneHash(filterLaneDto.getDestinationLane().getHashCode());
        }
        return agvNo;
    }
    /**
@@ -132,7 +155,7 @@
     *
     *      it can break the limit of the number of agv backpack
     */
    public synchronized Agv normalExecute(Task task) {
    public synchronized String normalExecute(Task task) {
        List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task), false);
//        List<String> availableAgvNos = this.getAvailableAgvNos(null);
        if (Cools.isEmpty(availableAgvNos)) {
@@ -148,14 +171,12 @@
        Lane originLane = filterLaneDto.getOriginLane();
        Lane destinationLane = filterLaneDto.getDestinationLane();
        List<String> actualAvailableAgvNos = filterLaneDto.getActualAvailableAgvNos();
        if (Cools.isEmpty(actualAvailableAgvNos)) {
            return null;
        }
        // choose min number of running task
        actualAvailableAgvNos.sort(new Comparator<String>() {
            @Override
            public int compare(String agvNo1, String agvNo2) {
                return calcAllocateWeight(agvNo1, task) - calcAllocateWeight(agvNo2, task);
            }
        });
        actualAvailableAgvNos.sort(Comparator.comparingInt(agvNo -> calcAllocateWeight(agvNo, task)));
        if (null != originLane) {
            task.setOriLaneHash(originLane.getHashCode());
@@ -164,7 +185,7 @@
            task.setDestLaneHash(destinationLane.getHashCode());
        }
        return agvService.selectByUuid(actualAvailableAgvNos.stream().findFirst().orElse(null));
        return actualAvailableAgvNos.get(0);
    }
    private String checkoutAgvForInboundRoller(Task task, Sta sta, List<String> availableAgvNos) {
@@ -196,14 +217,14 @@
                    )
            );
            if (taskCnt == 0) {
                break;
                continue;
            }
            // has enough backpack space to load
            Integer backpack = agvService.getBackpack(agvId);
            int countRemainingBackpack = segmentService.countRemainingBackpack(null, agvId);
            if (countRemainingBackpack >= backpack) {
                break;
                continue;
            }
            return agvNo;