Merge remote-tracking branch 'origin/main'

This commit is contained in:
wangwenhua 2025-09-19 16:30:36 +08:00
commit 1c10bd9660
5 changed files with 74 additions and 48 deletions

View File

@ -127,7 +127,7 @@ public abstract class AbtParentTask implements TaskAction {
protected void initPath() { protected void initPath() {
try { try {
log.info("init path");
String url = SpringUtil.getBean(Environment.class).getProperty("path.planning.url"); String url = SpringUtil.getBean(Environment.class).getProperty("path.planning.url");
String params = url + "?" String params = url + "?"
+ "profile=car" + "profile=car"
@ -137,12 +137,13 @@ public abstract class AbtParentTask implements TaskAction {
+ scenarioTask.getToLng() + scenarioTask.getToLng()
+ "&points_encoded=false" + "&points_encoded=false"
+ "&algorithm=alternative_route&alternative_route.max_paths=3"; + "&algorithm=alternative_route&alternative_route.max_paths=3";
log.info("params:;{}", params);
//获取路线信息 //获取路线信息
String result = webClient.get().uri(params) String result = webClient.get().uri(params)
.retrieve() .retrieve()
.bodyToMono(String.class) .bodyToMono(String.class)
.block(); .block();
log.info("init path finished ::{}", result);
JSONObject pointJson = JSON.parseObject(result); JSONObject pointJson = JSON.parseObject(result);
//获取路径点 //获取路径点
if (pointJson != null) { if (pointJson != null) {
@ -204,14 +205,24 @@ public abstract class AbtParentTask implements TaskAction {
try { try {
if (this.getRoomStatus()) { if (this.getRoomStatus()) {
if (distanceInfoMap.isEmpty()) {
return;
}
long duringTime = getDuringTime() - taskRelativeTime; long duringTime = getDuringTime() - taskRelativeTime;
if (duringTime <= 0) {
return;
}
log.info("duringTime::{}", duringTime);
//跑动距离 //跑动距离
double distance = duringTime * speed; double distance = duringTime * speed;
//获取大与此距离的第一个路线点key //获取大与此距离的第一个路线点key
Entry<Double, Coordinate> endPoint = distanceInfoMap.ceilingEntry(distance); Entry<Double, Coordinate> endPoint = distanceInfoMap.ceilingEntry(distance);
if (endPoint == null) {
endPoint = distanceInfoMap.lastEntry();
}
// log.info("enPoint::{}",endPoint);
//ws数据 //ws数据
List<double[]> dataList = new ArrayList<>(); List<double[]> dataList = new ArrayList<>();
HashMap<Object, Object> dataMap = new HashMap<>(); HashMap<Object, Object> dataMap = new HashMap<>();
@ -221,8 +232,10 @@ public abstract class AbtParentTask implements TaskAction {
if (Double.compare(distance, endPoint.getKey()) < 0) { if (Double.compare(distance, endPoint.getKey()) < 0) {
//获取小于最大值的第一个key //获取小于最大值的第一个key
Double lowerKey = distanceInfoMap.lowerKey(endPoint.getKey()); Double lowerKey = distanceInfoMap.lowerKey(endPoint.getKey());
// log.info("distance::{},lowerKey::{},endPoint{}",distance,lowerKey,endPoint.getKey()); if(lowerKey==null){
//获取从上一个开始节点到lowKey的数据 lowerKey = endPoint.getKey();
}
NavigableMap<Double, Coordinate> subPathMap = distanceInfoMap.subMap(startPoint.get(), NavigableMap<Double, Coordinate> subPathMap = distanceInfoMap.subMap(startPoint.get(),
true, lowerKey, true); true, lowerKey, true);
for (Double key : subPathMap.keySet()) { for (Double key : subPathMap.keySet()) {
@ -272,6 +285,8 @@ public abstract class AbtParentTask implements TaskAction {
Global.sendCmdInfoQueue.add( Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_FINISHED.getCode(), roomId, ResponseCmdInfo.create(WsCmdTypeEnum.PATH_FINISHED.getCode(), roomId,
scenarioTask.getScenarioId(), dataMap)); scenarioTask.getScenarioId(), dataMap));
//任务终止
schedule.shutdown();
} }

View File

@ -65,7 +65,7 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
@Override @Override
public void doSomeThing() { public void doSomeThing() {
log.info("move task running"); log.info("move task running:{}",scenarioTask.getResourceId());
initEnv(); //初始化环境 initEnv(); //初始化环境
initPath(); //初始化路径 initPath(); //初始化路径
@ -77,53 +77,61 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
* 初始化环境 * 初始化环境
*/ */
private void initEnv() { private void initEnv() {
try{
//获取油品消耗规则
String fuelConsumptionStr = SpringUtil.getBean(Environment.class)
.getProperty("fuel_spreed");
fuelConsumption = Double.parseDouble(fuelConsumptionStr == null ? "0" : fuelConsumptionStr);
fuelThreshold = Double.parseDouble(SpringUtil.getBean(Environment.class)
.getProperty("fuel.warn ", "0"));
statisticBean = SpringUtil.getBean(StatisticServiceImpl.class)
.statistic(scenarioTask.getResourceId());
}catch (Exception e){
log.error("init env exception",e);
}
//获取油品消耗规则
String fuelConsumptionStr = SpringUtil.getBean(Environment.class)
.getProperty("fuel_spreed");
fuelConsumption = Double.parseDouble(fuelConsumptionStr == null ? "0" : fuelConsumptionStr);
fuelThreshold = Double.parseDouble(SpringUtil.getBean(Environment.class)
.getProperty("fuel.warn ", "0"));
statisticBean = SpringUtil.getBean(StatisticServiceImpl.class)
.statistic(scenarioTask.getResourceId());
} }
private void fuelConsumption() { private void fuelConsumption() {
try{
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
schedule.scheduleWithFixedDelay(() -> {
if (getRoomStatus()) {
double currentUseUp = consumptionTaskInterval * SPEED / 1000 * fuelConsumption;
ScheduledExecutorService schedule = Executors.newScheduledThreadPool( //更新redis中油品的消耗
1); Object currentFuelObj = redis.hget(
schedule.scheduleWithFixedDelay(() -> {
if (getRoomStatus()) {
double currentUseUp = consumptionTaskInterval * SPEED / 1000 * fuelConsumption;
//更新redis中油品的消耗
Object currentFuelObj = redis.hget(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"fuel");
if (currentFuelObj != null) {
double fuel = Double.parseDouble(currentFuelObj.toString());
fuel = fuel - currentUseUp;
redis.hset(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"fuelConsume", fuel); "fuel");
if (currentFuelObj != null) {
double fuel = Double.parseDouble(currentFuelObj.toString());
fuel = fuel - currentUseUp;
double totalFuel = statisticBean.getFuel().getTotal(); redis.hset(
if (fuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) { scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
requestFlag.set(true); "fuelConsume", fuel);
//需要产生需求
produceFuelRequest();
//产生任务
produceTask();
double totalFuel = statisticBean.getFuel().getTotal();
if (fuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) {
requestFlag.set(true);
//需要产生需求
produceFuelRequest();
//产生任务
produceTask();
}
} }
//插入消耗表
} }
//插入消耗表
}
}, 0, consumptionTaskInterval, TimeUnit.SECONDS); }, 0, consumptionTaskInterval, TimeUnit.SECONDS);
}catch (Exception e){
log.error("fuel consumption exception",e);
}
} }

View File

@ -63,7 +63,7 @@ public class Room implements AutoCloseable {
//线程池 //线程池
private final ExecutorService actionExecutor = private final ExecutorService actionExecutor =
new ThreadPoolExecutor( new ThreadPoolExecutor(
5, 5, // corePoolSize, maximumPoolSize 5, 100, // corePoolSize, maximumPoolSize
0L, TimeUnit.MILLISECONDS, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1000), // 有界队列只允许100个待执行任务 new LinkedBlockingQueue<>(1000), // 有界队列只允许100个待执行任务
new ThreadPoolExecutor.AbortPolicy() // 超出直接抛异常 new ThreadPoolExecutor.AbortPolicy() // 超出直接抛异常
@ -130,7 +130,7 @@ public class Room implements AutoCloseable {
if (magValue != null) { if (magValue != null) {
this.mag = magValue.getMag(); this.mag = magValue.getMag();
} }
log.info("mag:{}", mag);
long curTime = duringTime.addAndGet(this.mag); long curTime = duringTime.addAndGet(this.mag);
sendRemainTime((totalTime.get() - curTime)); sendRemainTime((totalTime.get() - curTime));

View File

@ -70,7 +70,7 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
SpringUtil.getBean(RedisUtil.class) SpringUtil.getBean(RedisUtil.class)
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), .hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"medical", statistic.getMedical().getTotal()); "medical", statistic.getMedical().getTotal()+"");
} }
@ -79,7 +79,7 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
SpringUtil.getBean(RedisUtil.class) SpringUtil.getBean(RedisUtil.class)
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), .hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"fuel", statistic.getFuel().getTotal()); "fuel", statistic.getFuel().getTotal()+"");
} }
@ -87,7 +87,7 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
SpringUtil.getBean(RedisUtil.class) SpringUtil.getBean(RedisUtil.class)
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), .hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"ammunition", statistic.getAmmunition().getTotal()); "ammunition", statistic.getAmmunition().getTotal()+"");
} }
@ -95,7 +95,7 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
SpringUtil.getBean(RedisUtil.class) SpringUtil.getBean(RedisUtil.class)
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), .hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"water", statistic.getWater().getTotal()); "water", statistic.getWater().getTotal()+"");
} }
@ -103,7 +103,7 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
SpringUtil.getBean(RedisUtil.class) SpringUtil.getBean(RedisUtil.class)
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), .hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"food", statistic.getFood().getTotal()); "food", statistic.getFood().getTotal()+"");
} }

View File

@ -144,6 +144,7 @@ public class ScenarioTaskServiceImpl extends
ScenarioTask scenarioTask = new ScenarioTask(); ScenarioTask scenarioTask = new ScenarioTask();
scenarioTask.setScenarioId(scenario.getId()); scenarioTask.setScenarioId(scenario.getId());
List<ScenarioTask> taskList = this.queryTaskList(scenarioTask); List<ScenarioTask> taskList = this.queryTaskList(scenarioTask);
log.info("taskList.size ::{}", taskList.size());
for (ScenarioTask task : taskList) { for (ScenarioTask task : taskList) {
try { try {
long diff = Duration.between(scenario.getStartTime(),task.getStartTime()) long diff = Duration.between(scenario.getStartTime(),task.getStartTime())
@ -152,6 +153,7 @@ public class ScenarioTaskServiceImpl extends
switch (task.getTaskType()) { switch (task.getTaskType()) {
//移动任务 //移动任务
case "1": case "1":
log.info("move task::{}",diff);
MoveRootTask moveRootTask = new MoveRootTask(task, roomId); MoveRootTask moveRootTask = new MoveRootTask(task, roomId);
RoomManager.addAction(roomId, diff, moveRootTask); RoomManager.addAction(roomId, diff, moveRootTask);
break; break;
@ -166,6 +168,7 @@ public class ScenarioTaskServiceImpl extends
case "6": case "6":
case "7": case "7":
case "8": case "8":
log.info("supplier task::{}",diff);
SupplierTask supplierTask = new SupplierTask(task, roomId); SupplierTask supplierTask = new SupplierTask(task, roomId);
RoomManager.addAction(roomId, diff, supplierTask); RoomManager.addAction(roomId, diff, supplierTask);
break; break;