|  |  |  | 
|---|
|  |  |  | package com.zy.acs.manager.core.service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.zy.acs.common.utils.GsonUtils; | 
|---|
|  |  |  | 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.manager.entity.Code; | 
|---|
|  |  |  | 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.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 RouteService routeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SnowflakeIdWorker snowflakeIdWorker; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ConfigService configService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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 lane; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostConstruct | 
|---|
|  |  |  | public synchronized void init() { | 
|---|
|  |  |  | log.info("the rcs system is starting to initialize lane data..."); | 
|---|
|  |  |  | List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.fillAdjacencyCodeMap(codeList); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.mergeDeadEndLane(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.deadInteractionPoint(); | 
|---|
|  |  |  | this.deleteInteractionPoint(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.filterLanesWithFewPoints(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.generateLaneHash(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.initialized = Boolean.TRUE; | 
|---|
|  |  |  | log.info("the lane data initialization has been completed in rcs system."); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | System.out.println(GsonUtils.toJson(this.lanes)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void fillAdjacencyCodeMap(List<Code> codeList) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|