From ebd2f4397a92c6a5096de1b86d59154363344720 Mon Sep 17 00:00:00 2001 From: vincentlu <t1341870251@gmail.com> Date: 星期二, 13 五月 2025 08:48:15 +0800 Subject: [PATCH] # --- zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/astart/MapDataDispatcher.java | 143 ++++++++++++++++++++++++++--------------------- 1 files changed, 78 insertions(+), 65 deletions(-) diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/astart/MapDataDispatcher.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/astart/MapDataDispatcher.java index 25766b9..ea7ac84 100644 --- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/astart/MapDataDispatcher.java +++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/astart/MapDataDispatcher.java @@ -6,7 +6,7 @@ import com.zy.acs.common.utils.RedisSupport; import com.zy.acs.framework.common.Cools; import com.zy.acs.framework.exception.CoolException; -import com.zy.acs.manager.core.domain.SortCodeDto; +import com.zy.acs.manager.common.utils.MapDataUtils; import com.zy.acs.manager.core.service.astart.domain.DynamicNode; import com.zy.acs.manager.core.utils.RouteGenerator; import com.zy.acs.manager.manager.entity.Code; @@ -15,19 +15,27 @@ import com.zy.acs.manager.manager.service.CodeService; import com.zy.acs.manager.manager.service.RouteService; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.event.EventListener; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.stereotype.Service; +import javax.annotation.PostConstruct; import java.util.*; -import java.util.stream.Collectors; /** * Created by vincent on 6/6/2024 */ @Slf4j +@Service public class MapDataDispatcher { public static final Integer MAP_DEFAULT_LEV = 1; private final RedisSupport redis = RedisSupport.defaultRedisSupport; + + List<Double> xIdxList; + List<Double> yIdxList; private String[][] codeMatrix; @@ -35,17 +43,26 @@ private int[][] turnMatrix; - private String[][] cdaMatrix; + private Double[][][] cdaMatrix; public Map<String, Boolean> routeCdaMap = new HashMap<>(); - private final CodeService codeService; + @Autowired + private CodeService codeService; - private final RouteService routeService; + @Autowired + private RouteService routeService; - public MapDataDispatcher(CodeService codeService, RouteService routeService) { - this.codeService = codeService; - this.routeService = routeService; + @PostConstruct + public void init() { + String[][] codeMatrix = this.getCodeMatrix(null); + this.initRouteMap(null); + if (codeMatrix.length > 0) { + this.getMapMatrix(null, null); + this.getTurnMatrix(null); + this.getCdaMatrix(null); + this.getDynamicMatrix(null); + } } public String[][] getWaveMatrix(Integer lev) { @@ -88,13 +105,14 @@ redis.setValue(RedisConstant.AGV_MAP_ASTAR_DYNAMIC_FLAG, String.valueOf(lev), JSON.toJSONString(dynamicMatrix)); } - public String[][] getCdaMatrix(Integer lev) { + public Double[][][] getCdaMatrix(Integer lev) { lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); // redis if (null == this.cdaMatrix) { String cdaMatrixStr = redis.getValue(RedisConstant.AGV_MAP_ASTAR_CDA_FLAG, String.valueOf(lev)); if (!Cools.isEmpty(cdaMatrixStr)) { - this.cdaMatrix = JSON.parseObject(cdaMatrixStr, String[][].class); + String[][] cdaStrMatrix = JSON.parseObject(cdaMatrixStr, String[][].class); + this.cdaMatrix = MapDataUtils.preComputeCdaMatrix(cdaStrMatrix); } } // init @@ -109,9 +127,9 @@ return this.cdaMatrix; } - public void setCdaMatrix(Integer lev, String[][] cdaMatrix) { - redis.setValue(RedisConstant.AGV_MAP_ASTAR_CDA_FLAG, String.valueOf(lev), JSON.toJSONString(cdaMatrix)); - this.cdaMatrix = cdaMatrix; + public void setCdaMatrix(Integer lev, String[][] cdaStrMatrix) { + redis.setValue(RedisConstant.AGV_MAP_ASTAR_CDA_FLAG, String.valueOf(lev), JSON.toJSONString(cdaStrMatrix)); + this.cdaMatrix = MapDataUtils.preComputeCdaMatrix(cdaStrMatrix); } public int[][] getTurnMatrix(Integer lev) { @@ -193,7 +211,6 @@ } public String[][] initWaveMatrix(Integer lev) { - log.info("There is initializing Wave Matrix......"); lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); String[][] codeMatrix = getCodeMatrix(lev); @@ -217,6 +234,9 @@ lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); String[][] codeMatrix = getCodeMatrix(lev); + if (null == codeMatrix || codeMatrix.length == 0) { + return new DynamicNode[0][0]; + } DynamicNode[][] dynamicMatrix = new DynamicNode[codeMatrix.length][codeMatrix[0].length]; for (int i = 0; i < codeMatrix.length; i++) { @@ -236,15 +256,22 @@ log.info("There is initializing Cda Matrix......"); lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); + if (Cools.isEmpty(xIdxList, yIdxList)) { + this.initCodeMatrix(lev); + } + String[][] codeMatrix = getCodeMatrix(lev); String[][] cdaMatrix = new String[codeMatrix.length][codeMatrix[0].length]; for (int i = 0; i < codeMatrix.length; i++) { for (int j = 0; j < codeMatrix[i].length; j++) { if (CodeNodeType.NONE.val.equals(codeMatrix[i][j])) { - cdaMatrix[i][j] = CdaNodeType.DISABLE.val; + List<Double> cdaArr = new ArrayList<>(); + cdaArr.add(xIdxList.get(j)); + cdaArr.add(yIdxList.get(i)); + cdaMatrix[i][j] = JSON.toJSONString(cdaArr); } else { - Code currCode = codeService.selectByData(codeMatrix[i][j]); + Code currCode = codeService.getCacheByData(codeMatrix[i][j]); if (null != currCode) { List<Double> cdaArr = new ArrayList<>(); cdaArr.add(currCode.getX()); @@ -273,7 +300,7 @@ if (CodeNodeType.NONE.val.equals(codeMatrix[i][j])) { turnMatrix[i][j] = TurnNodeType.NONE.val; } else { - Code currCode = codeService.selectByData(codeMatrix[i][j]); + Code currCode = codeService.getCacheByData(codeMatrix[i][j]); List<String> neighborCodeList = routeService.findCodeDataOfSingle(currCode.getId()); switch (neighborCodeList.size()) { @@ -357,9 +384,9 @@ return; } for (Route route : routeList) { - Code startCode = codeService.getById(route.getStartCode()); + Code startCode = codeService.getCacheById(route.getStartCode()); int[] startCodeIdx = getCodeMatrixIdx(lev, startCode.getData()); - Code endCode = codeService.getById(route.getEndCode()); + Code endCode = codeService.getCacheById(route.getEndCode()); int[] codeMatrixIdx = getCodeMatrixIdx(lev, endCode.getData()); String routeKey = RouteGenerator.generateRouteKey(startCode.getData(), endCode.getData()); @@ -385,13 +412,9 @@ .orderByAsc(Code::getY) .orderByAsc(Code::getX) ); -// int xCount = codeService.selectDistinctCountFromX(); -// int yCount = codeService.selectDistinctCountFromY(); -// // [][] 绗竴涓槸鎺掔殑鏁伴噺 绗簩涓槸鍒楃殑鏁伴噺 -// String[][] codeMatrix = new String[yCount][xCount]; - List<Double> xIdxList = new ArrayList<>(); - List<Double> yIdxList = new ArrayList<>(); + xIdxList = new ArrayList<>(); + yIdxList = new ArrayList<>(); for (Code code : codeList) { Double x = code.getX(); Double y = code.getY(); @@ -409,24 +432,9 @@ // [][] 绗竴涓槸鎺掔殑鏁伴噺 绗簩涓槸鍒楃殑鏁伴噺 String[][] codeMatrix = new String[yIdxList.size()][xIdxList.size()]; - int idxX = -1; - int idxY = -1; - double lastX = -1; - double lastY = -1; for (Code code : codeList) { Double x = code.getX(); Double y = code.getY(); -// if (y != lastY) { -// idxY ++; -// idxX = 0; -// lastY = y; -// lastX = x; -// } -// if (x != lastX) { -// idxX++; -// lastX = x; -// } -// codeMatrix[idxY][idxX] = code.getData(); codeMatrix[yIdxList.indexOf(y)][xIdxList.indexOf(x)] = code.getData(); } @@ -461,50 +469,55 @@ return null; } - public List<String> queryCodeListFromDynamicNode(Integer lev, String nodeType) { - if (Cools.isEmpty(nodeType)) { + public List<int[]> getCodeMatrixIdxList(Integer lev, List<String> codeDataList) { + if (Cools.isEmpty(codeDataList)) { return new ArrayList<>(); } lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); - - List<SortCodeDto> codeList = new ArrayList<>(); - DynamicNode[][] dynamicMatrix = getDynamicMatrix(lev); String[][] codeMatrix = this.getCodeMatrix(lev); + + List<int[]> codeMatrixIdxList = new ArrayList<>(); + + Map<String, int[]> map = new HashMap<>(); + Set<String> codeDataSet = new HashSet<>(codeDataList); + for (int i = 0; i < codeMatrix.length; i++) { for (int j = 0; j < codeMatrix[i].length; j++) { - DynamicNode dynamicNode = dynamicMatrix[i][j]; - if (nodeType.equals(dynamicNode.getVehicle())) { - codeList.add(new SortCodeDto(codeMatrix[i][j], dynamicNode.getSerial())); + String codeData = codeMatrix[i][j]; + if (codeDataSet.contains(codeData)) { + map.put(codeData, new int[]{i, j}); } } } - codeList.sort(Comparator.comparingInt(SortCodeDto::getSerial)); - return codeList.stream().map(SortCodeDto::getCode).collect(Collectors.toList()); + + for (String codeData : codeDataList) { + int[] codeMatrixIdx = map.get(codeData); + if (codeMatrixIdx != null) { + codeMatrixIdxList.add(codeMatrixIdx); + } + } + return codeMatrixIdxList; } - public void modifyDynamicMatrix(Integer lev, List<String> codeList, String vehicle) { - this.modifyDynamicMatrix(lev, codeList, vehicle, false); + public void modifyDynamicMatrix(Integer lev, List<int[]> codeIdxList, String vehicle) { + this.modifyDynamicMatrix(lev, codeIdxList, vehicle, false); } - public synchronized void modifyDynamicMatrix(Integer lev, List<String> codeList, String vehicle, boolean reset) { + public synchronized void modifyDynamicMatrix(Integer lev, List<int[]> codeIdxList, String vehicle, boolean reset) { + if (Cools.isEmpty(vehicle)) { + return; + } lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); DynamicNode[][] dynamicMatrix = getDynamicMatrix(lev); if (!reset) { - if (Cools.isEmpty(codeList, vehicle)) { - return; - } - long time = System.currentTimeMillis() / 1000; +// long time = System.currentTimeMillis() / 1000; int serial = 1; - for (String code : codeList) { - int[] node = getCodeMatrixIdx(lev, code); - dynamicMatrix[node[0]][node[1]] = new DynamicNode(vehicle, serial, time); + for (int[] codeMatrixIdx : codeIdxList) { + dynamicMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]] = new DynamicNode(vehicle, serial); serial++; } } else { - if (Cools.isEmpty(vehicle)) { - return; - } for (int i = 0; i < dynamicMatrix.length; i++) { for (int j = 0; j < dynamicMatrix[i].length; j++) { DynamicNode dynamicNode = dynamicMatrix[i][j]; @@ -518,8 +531,8 @@ setDynamicMatrix(lev, dynamicMatrix); } - public void clearDynamicMatrixByCodeList(Integer lev, List<String> codeList) { - this.modifyDynamicMatrix(lev, codeList, DynamicNodeType.ACCESS.val); + public void clearDynamicMatrixByCodeList(Integer lev, List<int[]> codeIdxList) { + this.modifyDynamicMatrix(lev, codeIdxList, DynamicNodeType.ACCESS.val); } public int[][] filterMapData(int[][] mapMatrix, Integer lev, List<String> lockNodes) { -- Gitblit v1.9.1