|  |  |  | 
|---|
|  |  |  | package com.zy.acs.manager.core.service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.zy.acs.common.constant.RedisConstant; | 
|---|
|  |  |  | import com.zy.acs.common.utils.GsonUtils; | 
|---|
|  |  |  | import com.zy.acs.common.utils.RedisSupport; | 
|---|
|  |  |  | import com.zy.acs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.acs.framework.common.SnowflakeIdWorker; | 
|---|
|  |  |  | import com.zy.acs.manager.core.domain.Lane; | 
|---|
|  |  |  | import com.zy.acs.manager.core.service.astart.MapDataDispatcher; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.entity.Code; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.enums.StatusType; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.CodeService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.RouteService; | 
|---|
|  |  |  | import com.zy.acs.manager.system.service.ConfigService; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang.time.StopWatch; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.PostConstruct; | 
|---|
|  |  |  | import java.security.MessageDigest; | 
|---|
|  |  |  | import java.security.NoSuchAlgorithmException; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 10/25/2024 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class LaneService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private boolean initialized = Boolean.FALSE; | 
|---|
|  |  |  | private final RedisSupport redis = RedisSupport.defaultRedisSupport; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final List<Lane> lanes = new ArrayList<>(); | 
|---|
|  |  |  | private List<Lane> lanes = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final Map<String, List<String>> adjacencyCodeMap = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final Map<String, List<int[]>> laneCodeIdxMap = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final Map<String, Lane> codeLaneMap = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private boolean initialized = Boolean.FALSE; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CodeService codeService; | 
|---|
|  |  |  | 
|---|
|  |  |  | private RouteService routeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SnowflakeIdWorker snowflakeIdWorker; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ConfigService configService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MapDataDispatcher mapDataDispatcher; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // service ------------------------------------------------------- | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Boolean isInitialized() { | 
|---|
|  |  |  | return this.initialized; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public List<String> getLanePoints(String codeData) { | 
|---|
|  |  |  | public Lane search(String codeData) { | 
|---|
|  |  |  | if (Cools.isEmpty(codeData) || !this.initialized) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (Lane lane : this.lanes) { | 
|---|
|  |  |  | if (lane.getCodes().contains(codeData)) { | 
|---|
|  |  |  | return lane.getCodes(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | //        if (result == null) { | 
|---|
|  |  |  | //            for (Lane lane : this.lanes) { | 
|---|
|  |  |  | //                if (lane.getCodes().contains(codeData)) { | 
|---|
|  |  |  | //                    return lane; | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | return this.codeLaneMap.get(codeData); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public List<int[]> getLaneCodeIdxList(String codeData) { | 
|---|
|  |  |  | if (Cools.isEmpty(codeData)) { | 
|---|
|  |  |  | return new ArrayList<>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<int[]> list = this.laneCodeIdxMap.get(codeData); | 
|---|
|  |  |  | if (null == list) { | 
|---|
|  |  |  | return new ArrayList<>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // launcher ------------------------------------------------------- | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostConstruct | 
|---|
|  |  |  | public synchronized void init() { | 
|---|
|  |  |  | List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1)); | 
|---|
|  |  |  | public void init() { | 
|---|
|  |  |  | Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; | 
|---|
|  |  |  | String laneDataStr = redis.getValue(RedisConstant.MAP_LANE_DATA, String.valueOf(lev)); | 
|---|
|  |  |  | if (!Cools.isEmpty(laneDataStr)) { | 
|---|
|  |  |  | this.lanes = GsonUtils.fromJsonToList(laneDataStr, Lane.class); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | StopWatch stopWatch = new StopWatch(); | 
|---|
|  |  |  | stopWatch.start(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.initLaneData(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | stopWatch.stop(); | 
|---|
|  |  |  | log.info("the rcs system calculated lane data which has spend {} millisecond......", stopWatch.getTime()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | redis.setValue(RedisConstant.MAP_LANE_DATA, String.valueOf(lev), GsonUtils.toJson(this.lanes)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.generateLaneCodeIdx(null); | 
|---|
|  |  |  | this.initialized = Boolean.TRUE; | 
|---|
|  |  |  | //        System.out.println(GsonUtils.toJson(this.lanes)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void initLaneData() { | 
|---|
|  |  |  | log.info("the rcs system is starting to initialize lane data..."); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, StatusType.ENABLE.val)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.fillAdjacencyCodeMap(codeList); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.mergeDeadEndLane(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.deadInteractionPoint(); | 
|---|
|  |  |  | this.deleteInteractionPoint(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.filterLanesWithFewPoints(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.initialized = Boolean.TRUE; | 
|---|
|  |  |  | this.generateLaneHash(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("the lane data initialization has been completed in rcs system."); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void fillAdjacencyCodeMap(List<Code> codeList) { | 
|---|
|  |  |  | for (Code code : codeList) { | 
|---|
|  |  |  | List<Long> adjacencyNode = routeService.getAdjacencyNode(code.getId()); | 
|---|
|  |  |  | this.adjacencyCodeMap.put(code.getData(), adjacencyNode.stream().map(node -> ( | 
|---|
|  |  |  | codeService.getById(node).getData() | 
|---|
|  |  |  | codeService.getCacheById(node).getData() | 
|---|
|  |  |  | )).collect(Collectors.toList())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Code parentCode = codeService.selectByData(parent); | 
|---|
|  |  |  | Code currentCode = codeService.selectByData(current); | 
|---|
|  |  |  | Code neighborCode = codeService.selectByData(neighbor); | 
|---|
|  |  |  | Code parentCode = codeService.getCacheByData(parent); | 
|---|
|  |  |  | Code currentCode = codeService.getCacheByData(current); | 
|---|
|  |  |  | Code neighborCode = codeService.getCacheByData(neighbor); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | double direction1 = this.calculateDirection(parentCode, currentCode); | 
|---|
|  |  |  | double direction2 = this.calculateDirection(currentCode, neighborCode); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void deadInteractionPoint() { | 
|---|
|  |  |  | private void deleteInteractionPoint() { | 
|---|
|  |  |  | for (Lane lane : this.lanes) { | 
|---|
|  |  |  | lane.removeInteraction(this.adjacencyCodeMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void filterLanesWithFewPoints() { | 
|---|
|  |  |  | this.lanes.removeIf(next -> next.getCodes().size() <= 2); | 
|---|
|  |  |  | Integer maxAgvCountInLane = configService.getVal("maxAgvCountInLane", Integer.class); | 
|---|
|  |  |  | this.lanes.removeIf(next -> next.getCodes().size() <= Optional.ofNullable(maxAgvCountInLane).orElse(2)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void generateLaneHash() { | 
|---|
|  |  |  | for (Lane lane : this.lanes) { | 
|---|
|  |  |  | String hashCode = generateDigest(lane.getCodes()); | 
|---|
|  |  |  | lane.setHashCode(hashCode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String generateDigest(List<String> list) { | 
|---|
|  |  |  | MessageDigest md = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | md = MessageDigest.getInstance("SHA-256"); | 
|---|
|  |  |  | } catch (NoSuchAlgorithmException e) { | 
|---|
|  |  |  | log.error("generateDigest", e); | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String combined = list.stream().sorted().collect(Collectors.joining(",")); | 
|---|
|  |  |  | byte[] hashBytes = md.digest(combined.getBytes()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | StringBuilder sb = new StringBuilder(); | 
|---|
|  |  |  | for (byte b : hashBytes) { | 
|---|
|  |  |  | sb.append(String.format("%02x", b)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return sb.toString(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void generateLaneCodeIdx(Integer lev) { | 
|---|
|  |  |  | log.info("There is initializing Lane CodeIdxMap......"); | 
|---|
|  |  |  | if (Cools.isEmpty(this.lanes)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (Lane lane : this.lanes) { | 
|---|
|  |  |  | List<int[]> codeIdxList = new ArrayList<>(); | 
|---|
|  |  |  | for (String code : lane.getCodes()) { | 
|---|
|  |  |  | int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, code); | 
|---|
|  |  |  | codeIdxList.add(codeMatrixIdx); | 
|---|
|  |  |  | this.laneCodeIdxMap.put(code, codeIdxList); | 
|---|
|  |  |  | this.codeLaneMap.put(code, lane); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|