自动化立体仓库 - WMS系统
chen.llin
2 天以前 1815bcadb613f8951c02031176d2b54dcfa5a393
agv出入库根据pda扫描库位识别入库站点
5个文件已修改
281 ■■■■ 已修改文件
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/task/handler/WorkMastHandler.java 203 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/properties/AgvProperties.java 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-dev.yml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-prod.yml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java
@@ -208,21 +208,33 @@
            throw new CoolException(barcode+ ":条码存在agv搬运任务!");
        }
        // 根据whs_type选择站点和机器人组
        // 根据whs_type和库位编号前缀选择站点和机器人组
        Long whsType = locCache.getWhsType();
        String locNo = locCache.getLocNo();
        List<String> targetStations;
        String robotGroup;
        // 判断库位编号前缀:CA开头只做入库,WA开头才会被出库分配缓存区(从配置读取)
        String inboundOnlyPrefix = agvProperties.getLocationPrefix().getInboundOnly();
        String cacheAreaPrefix = agvProperties.getLocationPrefix().getCacheArea();
        boolean isCA = locNo != null && locNo.startsWith(inboundOnlyPrefix);
        boolean isWA = locNo != null && locNo.startsWith(cacheAreaPrefix);
        
        if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea())) {
            // whs_type = 1: 入库区,使用东侧站点和Group-001
            targetStations = agvProperties.getEastStations();
            robotGroup = agvProperties.getRobotGroupEast();
            log.info("库位whs_type={},使用入库区配置(东侧站点和Group-001)", whsType);
            log.info("库位whs_type={},库位编号={}({}),使用入库区配置({}站点和{})",
                whsType, locNo, isCA ? "CA" : (isWA ? "WA" : "其他"),
                agvProperties.getEastDisplayName(), robotGroup);
        } else if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getCacheArea())) {
            // whs_type = 2: 缓存区,使用西侧站点和Group-002
            // 注意:如果有CA开头的入库,但是标记在西侧的(whs_type=2),也会分配到西侧的站点入库
            targetStations = agvProperties.getWestStations();
            robotGroup = agvProperties.getRobotGroupWest();
            log.info("库位whs_type={},使用缓存区配置(西侧站点和Group-002)", whsType);
            log.info("库位whs_type={},库位编号={}({}),使用缓存区配置({}站点和{})",
                whsType, locNo, isCA ? "CA" : (isWA ? "WA" : "其他"),
                agvProperties.getWestDisplayName(), robotGroup);
        } else {
            // whs_type为空或其他值,根据type判断(兼容旧逻辑)
            if (type == 1) {
@@ -316,7 +328,7 @@
        } else {
            // 没有可入站点,记录日志但不阻止下单,站点分配将在定时任务中处理
            String groupName = (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea())) 
                    ? "东侧" : "西侧";
                    ? agvProperties.getEastDisplayName() : agvProperties.getWestDisplayName();
            log.warn("{}可用站点({})中没有可入站点(in_enable='Y'且canining='Y'),暂不分配站点,将在定时任务中分配", groupName, canInSites);
        }
src/main/java/com/zy/asrs/task/handler/WorkMastHandler.java
@@ -5,6 +5,7 @@
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.enums.LocStsType;
import com.zy.asrs.mapper.BasDevpMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.service.impl.BasStationServiceImpl;
import com.zy.asrs.task.AbstractHandler;
@@ -19,11 +20,9 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
@@ -72,6 +71,15 @@
    
    @Autowired
    private WrkMastLogService wrkMastLogService;
    @Autowired
    private BasDevpMapper basDevpMapper;
    /**
     * 站点轮询计数器,用于平均分配站点
     * Key: 站点组标识(如 "east" 或 "west"),Value: 当前轮询索引
     */
    private final Map<String, AtomicInteger> siteRoundRobinCounters = new ConcurrentHashMap<>();
    public ReturnT<String> start(WrkMast wrkMast) {
        // 4.入库完成
@@ -730,20 +738,6 @@
        
        log.info("出库任务完成,生成{}任务,任务ID:{},托盘码:{}", isEmptyPallet ? "空托出库" : "满托出库", outTask.getId(), outTask.getBarcode());
        
        // 分配缓存库位(whs_type=2)
        LocCache cacheLoc = locCacheService.selectOne(new EntityWrapper<LocCache>()
                .eq("whs_type", agvProperties.getWhsTypeMapping().getCacheArea()) // whs_type=2 缓存区
                .eq("frozen", 0)
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type) // O.闲置
                .ne("full_plt", isEmptyPallet ? "Y" : "N") // 空托不选满板库位,满托不选空板库位
                .orderAsc(Arrays.asList("row1", "bay1", "lev1"))
                .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
        if (cacheLoc == null) {
            log.warn("没有可用的缓存库位,无法生成{}任务,任务ID:{}", isEmptyPallet ? "空托出库" : "满托出库", outTask.getId());
            return;
        }
        // 获取出库站点(出库任务的staNo是出库站点,将作为空托/满托出库任务的源站点)
        String outboundStaNo = outTask.getStaNo();
        if (outboundStaNo == null || outboundStaNo.isEmpty()) {
@@ -751,12 +745,55 @@
            return;
        }
        
        // 根据缓存区配置选择站点和机器人组(西侧)
        List<String> cacheStations = agvProperties.getWestStations();
        String robotGroup = agvProperties.getRobotGroupWest();
        // 根据出库站点判断是东侧还是西侧
        Set<String> eastStations = new HashSet<>(agvProperties.getEastStations());
        Set<String> westStations = new HashSet<>(agvProperties.getWestStations());
        List<String> cacheStations;
        String robotGroup;
        Long targetWhsType;
        String sideName;
        if (eastStations.contains(outboundStaNo)) {
            // 东侧出库站点,查找东侧WA库位(whs_type=1)
            cacheStations = agvProperties.getEastStations();
            robotGroup = agvProperties.getRobotGroupEast();
            targetWhsType = agvProperties.getWhsTypeMapping().getInboundArea(); // whs_type=1
            sideName = agvProperties.getEastDisplayName(); // 从配置读取显示名称
            log.info("出库站点{}在{},查找{}WA库位(whs_type={})", outboundStaNo, sideName, sideName, targetWhsType);
        } else if (westStations.contains(outboundStaNo)) {
            // 西侧出库站点,查找西侧WA库位(whs_type=2)
            cacheStations = agvProperties.getWestStations();
            robotGroup = agvProperties.getRobotGroupWest();
            targetWhsType = agvProperties.getWhsTypeMapping().getCacheArea(); // whs_type=2
            sideName = agvProperties.getWestDisplayName(); // 从配置读取显示名称
            log.info("出库站点{}在{},查找{}WA库位(whs_type={})", outboundStaNo, sideName, sideName, targetWhsType);
        } else {
            log.warn("出库站点{}不在配置的站点列表中,无法判断{}/{},任务ID:{}",
                outboundStaNo, agvProperties.getEastDisplayName(), agvProperties.getWestDisplayName(), outTask.getId());
            return;
        }
        
        if (cacheStations.isEmpty()) {
            log.warn("缓存区没有配置站点,无法生成{}任务,任务ID:{}", isEmptyPallet ? "空托出库" : "满托出库", outTask.getId());
            log.warn("{}侧没有配置站点,无法生成{}任务,任务ID:{}",
                sideName, isEmptyPallet ? "空托出库" : "满托出库", outTask.getId());
            return;
        }
        // 分配缓存库位:只查找WA开头的库位(CA开头只做入库,WA开头才会被出库分配缓存区)
        String cacheAreaPrefix = agvProperties.getLocationPrefix().getCacheArea();
        LocCache cacheLoc = locCacheService.selectOne(new EntityWrapper<LocCache>()
                .eq("whs_type", targetWhsType) // 根据出库站点判断的whs_type
                .like("loc_no", cacheAreaPrefix + "%") // 只查找WA开头的库位(从配置读取)
                .eq("frozen", 0)
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type) // O.闲置
                .ne("full_plt", isEmptyPallet ? "Y" : "N") // 空托不选满板库位,满托不选空板库位
                .orderAsc(Arrays.asList("row1", "bay1", "lev1"))
                .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
        if (cacheLoc == null) {
            log.warn("{}侧没有可用的{}缓存库位,不生成{}AGV任务,任务ID:{}",
                sideName, cacheAreaPrefix, isEmptyPallet ? "空托出库" : "满托出库", outTask.getId());
            return;
        }
        
@@ -814,8 +851,12 @@
                outboundStaNo, transportingTasks.size(), outTask.getId());
        }
        
        // 选择缓存区目标站点(使用第一个可用站点,或可以优化为选择任务最少的站点)
        String cacheStaNo = cacheStations.get(0);
        // 选择缓存区目标站点(使用和入库一样的分配策略:轮询、最少任务数)
        String cacheStaNo = allocateCacheStation(cacheStations, ioType);
        if (cacheStaNo == null) {
            log.warn("无法为出库任务ID:{}分配缓存区站点,所有站点都在使用中", outTask.getId());
            return;
        }
        
        // 生成工作号
        int workNo = commonService.getWorkNo(WorkNoType.PAKOUT.type);
@@ -863,6 +904,118 @@
                isEmptyPallet ? "空托出库" : "满托出库", cacheTask.getId(), workNo, outboundStaNo, cacheStaNo, cacheLoc.getLocNo());
    }
    /**
     * 为出库到缓存区的任务分配站点(使用和入库一样的分配策略)
     * @param cacheStations 缓存区站点列表
     * @param ioType 任务类型(101=全板出库,110=空板出库)
     * @return 分配的站点编号,如果无法分配则返回null
     */
    private String allocateCacheStation(List<String> cacheStations, Integer ioType) {
        if (cacheStations == null || cacheStations.isEmpty()) {
            log.warn("缓存区站点列表为空,无法分配站点");
            return null;
        }
        // 将站点字符串列表转换为整数列表
        List<Integer> siteIntList = cacheStations.stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        // 查询所有缓存区站点的设备信息(包含任务数)
        List<BasDevp> devList = basDevpMapper.selectList(new EntityWrapper<BasDevp>()
                .in("dev_no", siteIntList)
        );
        if (devList.isEmpty()) {
            log.warn("缓存区站点{}在设备表中不存在", cacheStations);
            return cacheStations.get(0); // 降级:返回第一个站点
        }
        // 按入库任务数排序(出库到缓存区也使用in_qty字段)
        devList.sort(Comparator.comparing(BasDevp::getInQty));
        // 获取最少任务数
        int minInQty = devList.get(0).getInQty();
        // 筛选出任务数最少的站点列表
        List<BasDevp> minTaskSites = devList.stream()
                .filter(dev -> dev.getInQty() == minInQty)
                .collect(Collectors.toList());
        // 根据配置选择分配策略
        String strategy = agvProperties.getSiteAllocation().getStrategy();
        boolean enableRoundRobin = agvProperties.getSiteAllocation().isEnableRoundRobin();
        List<BasDevp> orderedSites = new ArrayList<>();
        String groupKey = "west"; // 缓存区使用西侧
        if (minTaskSites.size() > 1 && enableRoundRobin && "round-robin".equals(strategy)) {
            // 轮询分配:当多个站点任务数相同时,使用轮询
            AtomicInteger counter = siteRoundRobinCounters.computeIfAbsent(groupKey, k -> new AtomicInteger(0));
            int startIndex = counter.get() % minTaskSites.size();
            // 将轮询选中的站点放在最前面
            orderedSites.addAll(minTaskSites.subList(startIndex, minTaskSites.size()));
            orderedSites.addAll(minTaskSites.subList(0, startIndex));
            // 添加其他站点(任务数更多的)
            orderedSites.addAll(devList.stream()
                    .filter(dev -> dev.getInQty() > minInQty)
                    .collect(Collectors.toList()));
            log.debug("使用轮询分配策略,站点组:{},轮询起始索引:{}", groupKey, startIndex);
        } else if (minTaskSites.size() > 1 && enableRoundRobin && "random".equals(strategy)) {
            // 随机分配:先随机排序任务数最少的站点
            List<BasDevp> shuffledMinSites = new ArrayList<>(minTaskSites);
            Collections.shuffle(shuffledMinSites);
            orderedSites.addAll(shuffledMinSites);
            // 添加其他站点(任务数更多的)
            orderedSites.addAll(devList.stream()
                    .filter(dev -> dev.getInQty() > minInQty)
                    .collect(Collectors.toList()));
            log.debug("使用随机分配策略");
        } else {
            // 默认:按入库任务数排序(已经排序好了)
            orderedSites = devList;
        }
        // 依次检查每个站点是否在搬运,找到第一个空闲站点就分配
        String selectedSite = null;
        List<Integer> checkIoTypes = Arrays.asList(101, 110); // 出库到缓存区的任务类型
        for (BasDevp dev : orderedSites) {
            String staNo = String.valueOf(dev.getDevNo());
            // 检查该站点是否有正在搬运的同类型任务
            List<Task> transportingTasks = taskService.selectList(
                new EntityWrapper<Task>()
                    .eq("sta_no", staNo)
                    .eq("task_type", "agv")
                    .eq("wrk_sts", 8L) // 只检查正在搬运状态的任务
                    .in("io_type", checkIoTypes)
            );
            if (!transportingTasks.isEmpty()) {
                log.debug("缓存区站点{}有{}个正在搬运的出库AGV任务,检查下一个站点",
                    staNo, transportingTasks.size());
                continue; // 该站点正在搬运,检查下一个站点
            }
            // 找到第一个空闲站点,分配
            selectedSite = staNo;
            log.info("出库到缓存区任务按规则应分配到站点{},该站点空闲,分配成功", staNo);
            break;
        }
        // 如果所有站点都在搬运,则不分配站点
        if (selectedSite == null) {
            log.warn("所有缓存区站点都有正在搬运的出库任务,暂不分配站点,等待空闲");
            return null;
        }
        // 更新站点任务数(出库到缓存区也使用in_qty字段)
        basDevpMapper.incrementInQty(Integer.parseInt(selectedSite));
        return selectedSite;
    }
    @Transactional(rollbackFor = Exception.class)
    public ReturnT<String> agvDoIn(Task wrkMast) {
        Integer ioType = wrkMast.getIoType();
src/main/java/com/zy/common/properties/AgvProperties.java
@@ -44,6 +44,11 @@
    private SiteAllocationStrategy siteAllocation = new SiteAllocationStrategy();
    /**
     * 库位前缀配置
     */
    private LocationPrefix locationPrefix = new LocationPrefix();
    /**
     * whs_type映射配置内部类
     */
    @Data
@@ -73,6 +78,11 @@
         * 站点列表
         */
        private List<String> stations = new ArrayList<>();
        /**
         * 侧边显示名称(用于日志和提示信息,如"东侧"、"西侧")
         */
        private String displayName = "";
    }
    /**
@@ -106,6 +116,22 @@
    }
    /**
     * 获取东侧显示名称
     */
    public String getEastDisplayName() {
        return east != null && east.getDisplayName() != null && !east.getDisplayName().isEmpty()
            ? east.getDisplayName() : "东侧";
    }
    /**
     * 获取西侧显示名称
     */
    public String getWestDisplayName() {
        return west != null && west.getDisplayName() != null && !west.getDisplayName().isEmpty()
            ? west.getDisplayName() : "西侧";
    }
    /**
     * 站点分配策略配置内部类
     */
    @Data
@@ -125,4 +151,20 @@
         */
        private boolean enableRoundRobin = true;
    }
    /**
     * 库位前缀配置内部类
     */
    @Data
    public static class LocationPrefix {
        /**
         * CA前缀:只做入库的库位前缀(默认"CA")
         */
        private String inboundOnly = "CA";
        /**
         * WA前缀:会被出库分配缓存区的库位前缀(默认"WA")
         */
        private String cacheArea = "WA";
    }
}
src/main/resources/application-dev.yml
@@ -71,6 +71,7 @@
  # 东侧配置
  east:
    robotGroup: "Group-001"
    displayName: "东侧"  # 侧边显示名称(用于日志和提示信息)
    stations:
      - "1001"
      - "1003"
@@ -79,6 +80,7 @@
  # 西侧配置
  west:
    robotGroup: "Group-002"
    displayName: "西侧"  # 侧边显示名称(用于日志和提示信息)
    stations:
      - "1042"
      - "1044"
@@ -96,6 +98,12 @@
    strategy: round-robin
    # 是否启用平均分配:当多个站点任务数相同时,true=使用轮询分配,false=总是选择第一个
    enableRoundRobin: true
  # 库位前缀配置
  locationPrefix:
    # CA前缀:只做入库的库位前缀
    inboundOnly: "CA"
    # WA前缀:会被出库分配缓存区的库位前缀
    cacheArea: "WA"
# 越库配置
cross-dock:
src/main/resources/application-prod.yml
@@ -71,6 +71,7 @@
  # 东侧配置
  east:
    robotGroup: "Group-001"
    displayName: "东侧"  # 侧边显示名称(用于日志和提示信息)
    stations:
      - "1001"
      - "1003"
@@ -79,6 +80,7 @@
  # 西侧配置
  west:
    robotGroup: "Group-002"
    displayName: "西侧"  # 侧边显示名称(用于日志和提示信息)
    stations:
      - "1042"
      - "1044"
@@ -96,6 +98,12 @@
    strategy: round-robin
    # 是否启用平均分配:当多个站点任务数相同时,true=使用轮询分配,false=总是选择第一个
    enableRoundRobin: true
  # 库位前缀配置
  locationPrefix:
    # CA前缀:只做入库的库位前缀
    inboundOnly: "CA"
    # WA前缀:会被出库分配缓存区的库位前缀
    cacheArea: "WA"
# 越库配置
cross-dock: