添加仿真延时任务对象和逻辑
This commit is contained in:
parent
fa376fdfa3
commit
546f558397
@ -25,6 +25,8 @@ public abstract class Simulation<U extends SimulationUser> {
|
||||
* 仿真系统时间
|
||||
*/
|
||||
private LocalDateTime systemTime;
|
||||
|
||||
private static final int SYSTEM_TIME_RATE = 10;
|
||||
/**
|
||||
* 仿真状态
|
||||
*/
|
||||
@ -45,6 +47,8 @@ public abstract class Simulation<U extends SimulationUser> {
|
||||
*/
|
||||
public static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(4);
|
||||
|
||||
private Map<String, SimulationDelayJob> delayJobMap = new ConcurrentHashMap<>();
|
||||
|
||||
private SimulationPublisher publisher;
|
||||
|
||||
private Map<String, U> simulationUserMap = new ConcurrentHashMap<>();
|
||||
@ -62,7 +66,16 @@ public abstract class Simulation<U extends SimulationUser> {
|
||||
this.systemTime = LocalDateTime.now();
|
||||
this.state.set(PAUSE);
|
||||
this.addJob("systemTime", () ->
|
||||
this.systemTime = this.systemTime.plusNanos(TimeUnit.MILLISECONDS.toNanos(1)), 10);
|
||||
this.systemTime = this.systemTime.plusNanos(TimeUnit.MILLISECONDS.toNanos(SYSTEM_TIME_RATE)), SYSTEM_TIME_RATE);
|
||||
this.addJob("delayJobExecute", ()->{
|
||||
LocalDateTime systemTime = this.systemTime;
|
||||
for (SimulationDelayJob delayJob : this.delayJobMap.values()) {
|
||||
if (delayJob.isTimeToRun(systemTime)) {
|
||||
EXECUTOR.execute(delayJob);
|
||||
this.delayJobMap.remove(delayJob.getId());
|
||||
}
|
||||
}
|
||||
}, SYSTEM_TIME_RATE);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
@ -115,12 +128,12 @@ public abstract class Simulation<U extends SimulationUser> {
|
||||
|
||||
/**
|
||||
* 延时执行逻辑,只执行一次
|
||||
* PS:暂时不处理速度变化对延迟执行的逻辑产生影响
|
||||
* @param logic
|
||||
* @param job
|
||||
* @param delay
|
||||
*/
|
||||
public void delayExecute(Runnable logic, long delay) {
|
||||
EXECUTOR.schedule(logic, delay, TimeUnit.MILLISECONDS);
|
||||
public void delayExecute(SimulationJob job, long delay) {
|
||||
SimulationDelayJob delayJob = new SimulationDelayJob(this, job, delay);
|
||||
this.delayJobMap.put(delayJob.getId(), delayJob);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,55 @@
|
||||
package club.joylink.rtss.simulation;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Slf4j
|
||||
@Getter
|
||||
public final class SimulationDelayJob implements Runnable {
|
||||
private static final AtomicLong ID_SEED = new AtomicLong(0);
|
||||
|
||||
private Simulation simulation;
|
||||
private String id;
|
||||
/**
|
||||
* 运行逻辑
|
||||
*/
|
||||
private SimulationJob job;
|
||||
/**
|
||||
* 执行时间
|
||||
*/
|
||||
private LocalDateTime time;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param simulation
|
||||
* @param job
|
||||
* @param delay - 单位:毫秒
|
||||
*/
|
||||
public SimulationDelayJob(Simulation simulation, SimulationJob job, long delay) {
|
||||
this.simulation = simulation;
|
||||
this.id = generateId();
|
||||
this.job = job;
|
||||
this.time = simulation.getSystemTime().plusNanos(TimeUnit.MILLISECONDS.toNanos(delay));
|
||||
}
|
||||
|
||||
public static String generateId() {
|
||||
return String.valueOf(ID_SEED.incrementAndGet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
this.job.run();
|
||||
} catch (Throwable e) {
|
||||
log.error("仿真延时任务执行异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isTimeToRun(LocalDateTime systemTime) {
|
||||
return systemTime.isAfter(this.time);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package club.joylink.rtss.simulation.rt;
|
||||
|
||||
import club.joylink.rtss.simulation.SimulationIdGenerator;
|
||||
import club.joylink.rtss.simulation.SimulationManager;
|
||||
import club.joylink.rtss.simulation.rt.srd.SimulateRealDeviceLogic;
|
||||
import club.joylink.rtss.vo.UserVO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -11,10 +12,15 @@ public class RtSimulationService {
|
||||
@Autowired
|
||||
private SimulationManager simulationManager;
|
||||
|
||||
@Autowired
|
||||
private SimulateRealDeviceLogic simulateRealDeviceLogic;
|
||||
|
||||
public RtSimulation create(UserVO userVO) {
|
||||
RtSimulation rtSimulation = new RtSimulation(SimulationIdGenerator.buildId());
|
||||
this.simulationManager.save(rtSimulation);
|
||||
this.load(rtSimulation);
|
||||
this.simulateRealDeviceLogic.addJobs(rtSimulation);
|
||||
this.simulationManager.start(rtSimulation.getId());
|
||||
return rtSimulation;
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,11 @@
|
||||
package club.joylink.rtss.simulation.rt.srd;
|
||||
|
||||
import club.joylink.rtss.simulation.rt.RtSimulation;
|
||||
import club.joylink.rtss.simulation.rt.srd.bo.SrTrain;
|
||||
import club.joylink.rtss.simulation.rt.srd.bo.SrdRepository;
|
||||
import club.joylink.rtss.simulation.rt.srd.bo.TrackPosition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SimulateRealDeviceLogic {
|
||||
public static final int TRAIN_RUN_RATE = 2000;
|
||||
@ -15,16 +13,21 @@ public class SimulateRealDeviceLogic {
|
||||
public void addJobs(RtSimulation rtSimulation) {
|
||||
rtSimulation.addJob("srTrainRun", () -> this.srTrainRun(rtSimulation.getSrdRepository()), TRAIN_RUN_RATE);
|
||||
|
||||
log.debug("add delay job");
|
||||
rtSimulation.delayExecute(()->{
|
||||
log.debug("delay 1000 job");
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
public void srTrainRun(SrdRepository repository) {
|
||||
List<SrTrain> trainList = repository.getTrainList();
|
||||
for (SrTrain srTrain : trainList) {
|
||||
TrackPosition position = srTrain.getPosition();
|
||||
if (position == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
log.debug(String.format("train run logic in 2000ms rate"));
|
||||
// List<SrTrain> trainList = repository.getTrainList();
|
||||
// for (SrTrain srTrain : trainList) {
|
||||
// TrackPosition position = srTrain.getPosition();
|
||||
// if (position == null) {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user