大客流代码结构修改

This commit is contained in:
joylink_zhangsai 2021-05-19 10:21:38 +08:00
parent e0e8147ff9
commit c31d38dc66
3 changed files with 104 additions and 26 deletions

View File

@ -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());
}
}

View File

@ -58,14 +58,14 @@ public class PassengerFlowSimulateService {
@Autowired @Autowired
private MapPassengerFlowDataService mapPassengerFlowDataService; private MapPassengerFlowDataService mapPassengerFlowDataService;
@Autowired
@Lazy
private LargePassengerFlowStrategyService largePassengerFlowStrategyService;
public List<PassengerFlowSimulationData> getPassengerFlowSimulations() { public List<PassengerFlowSimulationData> getPassengerFlowSimulations() {
return new ArrayList<>(passengerFlowSimulationDataMap.values()); return new ArrayList<>(passengerFlowSimulationDataMap.values());
} }
public PassengerFlowSimulationData queryPassengerFlowSimulationData(String SimulationId) {
return passengerFlowSimulationDataMap.get(SimulationId);
}
public boolean changePassengerFlow(String group, Long passengerFlowId) { public boolean changePassengerFlow(String group, Long passengerFlowId) {
if(!Objects.equals(group2mapPassengerFlowID.get(group),passengerFlowId)){ if(!Objects.equals(group2mapPassengerFlowID.get(group),passengerFlowId)){
log.info(String.format("仿真[%s]切换客流仿真数据从[%s]到[%s]", group, 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()); group2mapPassengerFlowID.remove(simulation.getId());
} }
@EventListener
public void simulationReset(SimulationResetEvent event) { public void simulationReset(SimulationResetEvent event) {
Simulation simulation = event.getSimulation(); Simulation simulation = event.getSimulation();
log.info(String.format("仿真[%s]初始化,清理客流仿真数据", simulation.getId())); log.info(String.format("仿真[%s]初始化,清理客流仿真数据", simulation.getId()));
passengerFlowSimulationDataMap.remove(simulation.getId()); passengerFlowSimulationDataMap.remove(simulation.getId());
passengerFlowViewMap.remove(simulation.getId()); passengerFlowViewMap.remove(simulation.getId());
// 移除任务 // 移除任务
largePassengerFlowStrategyService.removeJobs(simulation);
this.removeJobs(simulation); this.removeJobs(simulation);
} }
@Async("nsExecutor") // @Async("nsExecutor")
@EventListener // @EventListener
public void initPassengerFlow(SimulationRunAsPlanEvent event) {
/**
* @return 客流初始化成功
*/
public boolean initPassengerFlow(SimulationRunAsPlanEvent event) {
Simulation simulation = event.getSimulation(); Simulation simulation = event.getSimulation();
MapVO map = simulation.getBuildParams().getMap(); MapVO map = simulation.getBuildParams().getMap();
Long mapId = map.getId(); Long mapId = map.getId();
@ -121,7 +123,7 @@ public class PassengerFlowSimulateService {
} }
if (Objects.isNull(passengerFlowData)) { if (Objects.isNull(passengerFlowData)) {
log.debug(String.format("仿真[%s]没有客流数据,不初始化客流", simulation.debugStr())); log.debug(String.format("仿真[%s]没有客流数据,不初始化客流", simulation.debugStr()));
return; return false;
} }
// 初始化客流数据 // 初始化客流数据
Map<String, StandPassengerFlow> standPassengerFlowMap = this.loadStandPassengerFlow(simulation, passengerFlowData); Map<String, StandPassengerFlow> standPassengerFlowMap = this.loadStandPassengerFlow(simulation, passengerFlowData);
@ -133,16 +135,15 @@ public class PassengerFlowSimulateService {
standPassengerFlowMap, standPassengerFlowMap,
trainPassengerFlowMap); trainPassengerFlowMap);
passengerFlowSimulationDataMap.put(simulation.getId(), data); passengerFlowSimulationDataMap.put(simulation.getId(), data);
// 添加任务
largePassengerFlowStrategyService.addJobs(simulation);
addJobs(simulation);
// 发送初始化数据 // 发送初始化数据
this.sendStandPassengerFlowData(simulation, standPassengerFlowMap); this.sendStandPassengerFlowData(simulation, standPassengerFlowMap);
this.sendTrainPassengerFlowData(simulation, trainPassengerFlowMap); this.sendTrainPassengerFlowData(simulation, trainPassengerFlowMap);
// 发送列车pis数据 // 发送列车pis数据
this.sendTrainInitPisData(simulation, trainPassengerFlowMap); this.sendTrainInitPisData(simulation, trainPassengerFlowMap);
log.debug(String.format("客流初始化成功")); log.debug("客流初始化成功");
addJobs(simulation);
return true;
} }
private void sendTrainInitPisData(Simulation simulation, Map<String, TrainPassengerFlow> trainPassengerFlowMap) { private void sendTrainInitPisData(Simulation simulation, Map<String, TrainPassengerFlow> trainPassengerFlowMap) {

View File

@ -42,16 +42,12 @@ public class LargePassengerFlowStrategyService {
private StrategyCalculateAndRecommendService strategyCalculateAndRecommendService; private StrategyCalculateAndRecommendService strategyCalculateAndRecommendService;
// @Scheduled(fixedRate = 10*1000) // @Scheduled(fixedRate = 10*1000)
public void checkLpf() { public void checkLpf(Simulation simulation) {
List<PassengerFlowSimulationData> pfSimulationList = this.passengerFlowSimulateService.getPassengerFlowSimulations(); PassengerFlowSimulationData passengerFlowSimulationData = this.passengerFlowSimulateService.queryPassengerFlowSimulationData(simulation.getId());
if (CollectionUtils.isEmpty(pfSimulationList)) { if (passengerFlowSimulationData != null) {
return;
}
for (PassengerFlowSimulationData passengerFlowSimulationData : pfSimulationList) {
Simulation simulation = this.passengerFlowSimulateService.getSimulationByGroup(passengerFlowSimulationData.getGroup());
if (passengerFlowSimulationData.getNextRecommendTime() != null && if (passengerFlowSimulationData.getNextRecommendTime() != null &&
simulation.getSystemTime().isBefore(passengerFlowSimulationData.getNextRecommendTime())) { simulation.getSystemTime().isBefore(passengerFlowSimulationData.getNextRecommendTime())) {
continue; return;
} }
// 查询大客流站台 // 查询大客流站台
List<StandPassengerFlow> standPassengerFlowList = passengerFlowSimulationData.getAllStandPassengerFlow(); List<StandPassengerFlow> standPassengerFlowList = passengerFlowSimulationData.getAllStandPassengerFlow();
@ -67,9 +63,6 @@ public class LargePassengerFlowStrategyService {
StrategyCalculateData strategyCalculateData = this.buildStrategyCalculateData(passengerFlowSimulationData); StrategyCalculateData strategyCalculateData = this.buildStrategyCalculateData(passengerFlowSimulationData);
List<Strategy> recommendList = this.strategyCalculateAndRecommendService.calculateAndRecommend(strategyCalculateData.clone()); List<Strategy> recommendList = this.strategyCalculateAndRecommendService.calculateAndRecommend(strategyCalculateData.clone());
if (!CollectionUtils.isEmpty(recommendList)) { if (!CollectionUtils.isEmpty(recommendList)) {
if (simulation == null) {
return;
}
Set<String> users = simulation.getSimulationUserIds(); Set<String> users = simulation.getSimulationUserIds();
LpfStrategyRecommend recommend = new LpfStrategyRecommend(strategyCalculateData, recommendList); LpfStrategyRecommend recommend = new LpfStrategyRecommend(strategyCalculateData, recommendList);
String body = JsonUtils.writeValueAsString(recommend); String body = JsonUtils.writeValueAsString(recommend);
@ -84,6 +77,48 @@ public class LargePassengerFlowStrategyService {
passengerFlowSimulationData.updateNextRecommendTime(null); passengerFlowSimulationData.updateNextRecommendTime(null);
} }
} }
// List<PassengerFlowSimulationData> 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<StandPassengerFlow> standPassengerFlowList = passengerFlowSimulationData.getAllStandPassengerFlow();
// List<StandPassengerFlow> 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<Strategy> recommendList = this.strategyCalculateAndRecommendService.calculateAndRecommend(strategyCalculateData.clone());
// if (!CollectionUtils.isEmpty(recommendList)) {
// if (simulation == null) {
// return;
// }
// Set<String> users = simulation.getSimulationUserIds();
// LpfStrategyRecommend recommend = new LpfStrategyRecommend(strategyCalculateData, recommendList);
// String body = JsonUtils.writeValueAsString(recommend);
// SocketMessageVO<String> 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) { private StrategyCalculateData buildStrategyCalculateData(PassengerFlowSimulationData passengerFlowSimulationData) {
@ -102,7 +137,7 @@ public class LargePassengerFlowStrategyService {
} }
public void addJobs(Simulation simulation) { 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) { public void removeJobs(Simulation simulation) {