From c31d38dc6668bb84c3ce231c53ef21ff7b191696 Mon Sep 17 00:00:00 2001 From: joylink_zhangsai <1021828630@qq.com> Date: Wed, 19 May 2021 10:21:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=AE=A2=E6=B5=81=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cbtc/passenger/PassengerFlowListener.java | 42 +++++++++++++ .../PassengerFlowSimulateService.java | 29 ++++----- .../LargePassengerFlowStrategyService.java | 59 +++++++++++++++---- 3 files changed, 104 insertions(+), 26 deletions(-) create mode 100644 src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowListener.java diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowListener.java b/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowListener.java new file mode 100644 index 000000000..80f6af2bc --- /dev/null +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowListener.java @@ -0,0 +1,42 @@ +package club.joylink.rtss.simulation.cbtc.passenger; + +import club.joylink.rtss.simulation.cbtc.Simulation; +import club.joylink.rtss.simulation.cbtc.event.SimulationResetEvent; +import club.joylink.rtss.simulation.cbtc.event.SimulationRunAsPlanEvent; +import club.joylink.rtss.simulation.cbtc.passenger.data.PassengerFlowData; +import club.joylink.rtss.simulation.cbtc.passenger.data.PassengerFlowSimulationData; +import club.joylink.rtss.simulation.cbtc.passenger.data.StandPassengerFlow; +import club.joylink.rtss.simulation.cbtc.passenger.data.TrainPassengerFlow; +import club.joylink.rtss.simulation.cbtc.passenger.strategy.LargePassengerFlowStrategyService; +import club.joylink.rtss.vo.client.map.MapVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import java.util.Map; +import java.util.Objects; + +@Component +public class PassengerFlowListener { + + @Autowired + private PassengerFlowSimulateService passengerFlowSimulateService; + + @Autowired + private LargePassengerFlowStrategyService largePassengerFlowStrategyService; + + @Async("nsExecutor") + @EventListener + public void handle(SimulationRunAsPlanEvent event) { + if (passengerFlowSimulateService.initPassengerFlow(event)) { + largePassengerFlowStrategyService.addJobs(event.getSimulation()); + } + } + + @EventListener + public void handle(SimulationResetEvent event) { + passengerFlowSimulateService.simulationReset(event); + largePassengerFlowStrategyService.removeJobs(event.getSimulation()); + } +} diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowSimulateService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowSimulateService.java index ed2adf4c1..a038a8b8c 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowSimulateService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/PassengerFlowSimulateService.java @@ -58,14 +58,14 @@ public class PassengerFlowSimulateService { @Autowired private MapPassengerFlowDataService mapPassengerFlowDataService; - @Autowired - @Lazy - private LargePassengerFlowStrategyService largePassengerFlowStrategyService; - public List getPassengerFlowSimulations() { return new ArrayList<>(passengerFlowSimulationDataMap.values()); } + public PassengerFlowSimulationData queryPassengerFlowSimulationData(String SimulationId) { + return passengerFlowSimulationDataMap.get(SimulationId); + } + public boolean changePassengerFlow(String group, Long passengerFlowId) { if(!Objects.equals(group2mapPassengerFlowID.get(group),passengerFlowId)){ log.info(String.format("仿真[%s]切换客流仿真数据从[%s]到[%s]", group, group2mapPassengerFlowID.get(group), passengerFlowId)); @@ -89,20 +89,22 @@ public class PassengerFlowSimulateService { group2mapPassengerFlowID.remove(simulation.getId()); } - @EventListener public void simulationReset(SimulationResetEvent event) { Simulation simulation = event.getSimulation(); log.info(String.format("仿真[%s]初始化,清理客流仿真数据", simulation.getId())); passengerFlowSimulationDataMap.remove(simulation.getId()); passengerFlowViewMap.remove(simulation.getId()); // 移除任务 - largePassengerFlowStrategyService.removeJobs(simulation); this.removeJobs(simulation); } - @Async("nsExecutor") - @EventListener - public void initPassengerFlow(SimulationRunAsPlanEvent event) { +// @Async("nsExecutor") +// @EventListener + + /** + * @return 客流初始化成功 + */ + public boolean initPassengerFlow(SimulationRunAsPlanEvent event) { Simulation simulation = event.getSimulation(); MapVO map = simulation.getBuildParams().getMap(); Long mapId = map.getId(); @@ -121,7 +123,7 @@ public class PassengerFlowSimulateService { } if (Objects.isNull(passengerFlowData)) { log.debug(String.format("仿真[%s]没有客流数据,不初始化客流", simulation.debugStr())); - return; + return false; } // 初始化客流数据 Map standPassengerFlowMap = this.loadStandPassengerFlow(simulation, passengerFlowData); @@ -133,16 +135,15 @@ public class PassengerFlowSimulateService { standPassengerFlowMap, trainPassengerFlowMap); passengerFlowSimulationDataMap.put(simulation.getId(), data); - // 添加任务 - largePassengerFlowStrategyService.addJobs(simulation); - addJobs(simulation); // 发送初始化数据 this.sendStandPassengerFlowData(simulation, standPassengerFlowMap); this.sendTrainPassengerFlowData(simulation, trainPassengerFlowMap); // 发送列车pis数据 this.sendTrainInitPisData(simulation, trainPassengerFlowMap); - log.debug(String.format("客流初始化成功")); + log.debug("客流初始化成功"); + addJobs(simulation); + return true; } private void sendTrainInitPisData(Simulation simulation, Map trainPassengerFlowMap) { diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/strategy/LargePassengerFlowStrategyService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/strategy/LargePassengerFlowStrategyService.java index bf0dbece6..65c080144 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/strategy/LargePassengerFlowStrategyService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/passenger/strategy/LargePassengerFlowStrategyService.java @@ -42,16 +42,12 @@ public class LargePassengerFlowStrategyService { private StrategyCalculateAndRecommendService strategyCalculateAndRecommendService; // @Scheduled(fixedRate = 10*1000) - public void checkLpf() { - List pfSimulationList = this.passengerFlowSimulateService.getPassengerFlowSimulations(); - if (CollectionUtils.isEmpty(pfSimulationList)) { - return; - } - for (PassengerFlowSimulationData passengerFlowSimulationData : pfSimulationList) { - Simulation simulation = this.passengerFlowSimulateService.getSimulationByGroup(passengerFlowSimulationData.getGroup()); + public void checkLpf(Simulation simulation) { + PassengerFlowSimulationData passengerFlowSimulationData = this.passengerFlowSimulateService.queryPassengerFlowSimulationData(simulation.getId()); + if (passengerFlowSimulationData != null) { if (passengerFlowSimulationData.getNextRecommendTime() != null && simulation.getSystemTime().isBefore(passengerFlowSimulationData.getNextRecommendTime())) { - continue; + return; } // 查询大客流站台 List standPassengerFlowList = passengerFlowSimulationData.getAllStandPassengerFlow(); @@ -67,9 +63,6 @@ public class LargePassengerFlowStrategyService { StrategyCalculateData strategyCalculateData = this.buildStrategyCalculateData(passengerFlowSimulationData); List recommendList = this.strategyCalculateAndRecommendService.calculateAndRecommend(strategyCalculateData.clone()); if (!CollectionUtils.isEmpty(recommendList)) { - if (simulation == null) { - return; - } Set users = simulation.getSimulationUserIds(); LpfStrategyRecommend recommend = new LpfStrategyRecommend(strategyCalculateData, recommendList); String body = JsonUtils.writeValueAsString(recommend); @@ -84,6 +77,48 @@ public class LargePassengerFlowStrategyService { passengerFlowSimulationData.updateNextRecommendTime(null); } } + +// List pfSimulationList = this.passengerFlowSimulateService.getPassengerFlowSimulations(); +// if (CollectionUtils.isEmpty(pfSimulationList)) { +// return; +// } +// for (PassengerFlowSimulationData passengerFlowSimulationData : pfSimulationList) { +// Simulation simulation = this.passengerFlowSimulateService.getSimulationByGroup(passengerFlowSimulationData.getGroup()); +// if (passengerFlowSimulationData.getNextRecommendTime() != null && +// simulation.getSystemTime().isBefore(passengerFlowSimulationData.getNextRecommendTime())) { +// continue; +// } +// // 查询大客流站台 +// List standPassengerFlowList = passengerFlowSimulationData.getAllStandPassengerFlow(); +// List lpfList = new ArrayList<>(); +// for (StandPassengerFlow standPassengerFlow : standPassengerFlowList) { +// if (standPassengerFlow.getPassengerQuantity() > Config.STAND_LPF_TRIGGER) { +// lpfList.add(standPassengerFlow); +// } +// } +// if (!lpfList.isEmpty()) { +// log.info(String.format("[%s]出现大客流,开始生成策略计算", lpfList.get(0).getStand().debugStr())); +// // 发现大客流,计算策略 +// StrategyCalculateData strategyCalculateData = this.buildStrategyCalculateData(passengerFlowSimulationData); +// List recommendList = this.strategyCalculateAndRecommendService.calculateAndRecommend(strategyCalculateData.clone()); +// if (!CollectionUtils.isEmpty(recommendList)) { +// if (simulation == null) { +// return; +// } +// Set users = simulation.getSimulationUserIds(); +// LpfStrategyRecommend recommend = new LpfStrategyRecommend(strategyCalculateData, recommendList); +// String body = JsonUtils.writeValueAsString(recommend); +// SocketMessageVO message = SocketMessageFactory +// .build(WebSocketMessageType.LPF_STRATEGY_RECOMMEND, simulation.getId(), body); +// this.stompMessageService.sendToUser(users, message); +// } +// // 设置下次推荐时间 +// passengerFlowSimulationData.updateNextRecommendTime(simulation.getSystemTime().plusMinutes(Config.CAL_INTERVAL)); +// } else { +// // 没有大客流,清除下次推荐时间 +// passengerFlowSimulationData.updateNextRecommendTime(null); +// } +// } } private StrategyCalculateData buildStrategyCalculateData(PassengerFlowSimulationData passengerFlowSimulationData) { @@ -102,7 +137,7 @@ public class LargePassengerFlowStrategyService { } public void addJobs(Simulation simulation) { - simulation.addJobIfAbsent(Simulation.JobName.checkLpf, this::checkLpf, 10 * 1000); + simulation.addJobIfAbsent(Simulation.JobName.checkLpf, () -> this.checkLpf(simulation), 10 * 1000); } public void removeJobs(Simulation simulation) {