simulation-backend/src/main/java/com/hivekion/room/bean/MoveTask.java

282 lines
9.7 KiB
Java
Raw Normal View History

2025-09-18 10:47:37 +08:00
package com.hivekion.room.bean;
import cn.hutool.extra.spring.SpringUtil;
2025-09-20 15:48:14 +08:00
import com.alibaba.fastjson2.JSON;
2025-09-20 12:07:36 +08:00
import com.hivekion.Global;
import com.hivekion.common.entity.ResponseCmdInfo;
2025-09-19 11:17:49 +08:00
import com.hivekion.common.redis.RedisUtil;
2025-09-19 14:56:12 +08:00
import com.hivekion.common.uuid.IdUtils;
import com.hivekion.room.RoomManager;
2025-09-18 10:47:37 +08:00
import com.hivekion.room.func.TaskAction;
2025-09-19 20:01:21 +08:00
import com.hivekion.scenario.entity.BattleConsume;
2025-09-19 14:56:12 +08:00
import com.hivekion.scenario.entity.ScenarioResource;
2025-09-18 10:47:37 +08:00
import com.hivekion.scenario.entity.ScenarioTask;
2025-09-19 20:01:21 +08:00
import com.hivekion.scenario.service.impl.BattleConsumeServiceImpl;
2025-09-19 14:56:12 +08:00
import com.hivekion.scenario.service.impl.BattleSupplierServiceImpl;
import com.hivekion.scenario.service.impl.ScenarioTaskServiceImpl;
2025-09-20 15:48:14 +08:00
import com.hivekion.statistic.bean.ScenarioInfo;
2025-09-19 11:17:49 +08:00
import com.hivekion.statistic.bean.StatisticBean;
import com.hivekion.statistic.service.impl.StatisticServiceImpl;
2025-09-19 14:56:12 +08:00
import com.hivekion.supplier.entity.SupplierRequest;
import com.hivekion.supplier.service.impl.SupplierRequestServiceImpl;
import java.time.LocalDateTime;
2025-09-20 12:07:36 +08:00
import java.util.HashMap;
2025-09-19 14:56:12 +08:00
import java.util.List;
2025-09-20 12:07:36 +08:00
import java.util.Map;
2025-09-18 11:41:46 +08:00
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
2025-09-18 13:51:58 +08:00
import java.util.concurrent.TimeUnit;
2025-09-19 14:56:12 +08:00
import java.util.concurrent.atomic.AtomicBoolean;
2025-09-18 10:47:37 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
/**
* [类的简要说明]
* <p>
* [详细描述可选]
* <p>
*
* @author LiDongYU
* @since 2025/7/22
*/
@Slf4j
2025-09-20 12:07:36 +08:00
public class MoveTask extends AbtParentTask implements TaskAction {
2025-09-18 10:47:37 +08:00
2025-09-18 23:33:24 +08:00
/**
* 速度 换算为100Km/小时
*/
private final double SPEED = 27;
2025-09-19 14:56:12 +08:00
/**
* 需求产生标志
*/
private final AtomicBoolean requestFlag = new AtomicBoolean(false);
2025-09-19 11:17:49 +08:00
/**
* 油料消耗速率
*/
private double fuelConsumption = 0;
2025-09-19 12:14:09 +08:00
private double fuelThreshold = 0;
2025-09-19 11:17:49 +08:00
/**
* 消耗任务间隔
*/
2025-09-19 20:01:21 +08:00
private final int consumptionTaskInterval = 10;
2025-09-19 11:17:49 +08:00
/**
* redis 服务类
*/
private final RedisUtil redis = SpringUtil.getBean(RedisUtil.class);
private StatisticBean statisticBean;
2025-09-18 13:51:58 +08:00
2025-09-18 10:47:37 +08:00
2025-09-20 12:07:36 +08:00
public MoveTask(ScenarioTask scenarioTask, String roomId) {
2025-09-18 10:47:37 +08:00
super(scenarioTask, roomId);
}
@Override
public void doSomeThing() {
2025-09-20 15:07:45 +08:00
log.info("move task running:{},fuel::{}", scenarioTask.getResourceId(), getCurrentFuel());
2025-09-19 11:17:49 +08:00
initEnv(); //初始化环境
2025-09-18 13:51:58 +08:00
initPath(); //初始化路径
2025-09-20 12:07:36 +08:00
updatePath(SPEED, new TaskAction() {
@Override
public void doSomeThing() {
2025-09-20 14:24:47 +08:00
2025-09-20 12:07:36 +08:00
//推送移动任务
Map<String, Object> map = new HashMap<>();
2025-09-20 15:07:45 +08:00
map.put("duringTime", getDuringTime());
map.put("id", scenarioTask.getResourceId());
map.put("roomStatus", true);
map.put("type", scenarioTask.getType());
2025-09-20 12:07:36 +08:00
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create("moveTask", roomId,
scenarioTask.getScenarioId(), map));
}
@Override
public String getId() {
return "";
}
@Override
public String getType() {
return "";
}
2025-09-20 15:07:45 +08:00
}, null); //更新路径
2025-09-19 11:17:49 +08:00
fuelConsumption();//油品消耗
}
/**
* 初始化环境
*/
private void initEnv() {
2025-09-19 20:01:21 +08:00
try {
2025-09-19 15:29:58 +08:00
//获取油品消耗规则
String fuelConsumptionStr = SpringUtil.getBean(Environment.class)
2025-09-20 16:12:21 +08:00
.getProperty("fuel.spreed");
2025-09-20 17:14:47 +08:00
2025-09-19 15:29:58 +08:00
fuelConsumption = Double.parseDouble(fuelConsumptionStr == null ? "0" : fuelConsumptionStr);
fuelThreshold = Double.parseDouble(SpringUtil.getBean(Environment.class)
2025-09-20 17:14:47 +08:00
.getProperty("fuel.warn", "0"));
log.info("初始化::{}-油料消耗速度::{},油料最低阈值::{}", this.scenarioTask.getResourceId(),
fuelConsumptionStr, fuelThreshold);
2025-09-19 15:29:58 +08:00
statisticBean = SpringUtil.getBean(StatisticServiceImpl.class)
.statistic(scenarioTask.getResourceId());
2025-09-19 20:01:21 +08:00
} catch (Exception e) {
log.error("init env exception", e);
2025-09-19 15:29:58 +08:00
}
2025-09-19 12:14:09 +08:00
2025-09-18 13:51:58 +08:00
}
2025-09-18 23:33:24 +08:00
2025-09-19 12:14:09 +08:00
private void fuelConsumption() {
2025-09-19 20:01:21 +08:00
try {
2025-09-19 15:29:58 +08:00
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
schedule.scheduleWithFixedDelay(() -> {
2025-09-20 15:07:45 +08:00
if (getRoomStatus() && this.canMoved.get()) {
2025-09-19 15:29:58 +08:00
double currentUseUp = consumptionTaskInterval * SPEED / 1000 * fuelConsumption;
2025-09-20 15:07:45 +08:00
double fuel = getCurrentFuel();
2025-09-20 17:14:47 +08:00
log.info("{}-当前消耗油料::{},当前剩余油料::{}", scenarioTask.getResourceId(),
currentUseUp, fuel);
fuel = fuel - currentUseUp;
if (fuel <= 0) {
log.error("{}-油料为空", scenarioTask.getResourceId());
2025-09-20 16:12:21 +08:00
this.canMoved.set(false);
2025-09-20 17:14:47 +08:00
log.info("{},can:{}", scenarioTask.getResourceId(), this.canMoved.get());
2025-09-20 15:07:45 +08:00
return;
}
2025-09-20 17:14:47 +08:00
setCurrentFuel(currentUseUp);
2025-09-20 15:07:45 +08:00
double totalFuel = statisticBean.getFuel().getTotal();
2025-09-20 17:14:47 +08:00
log.info("{}-当前比值{},阈值{}", scenarioTask.getResourceId(), fuel * 100 / totalFuel,
fuelThreshold);
2025-09-20 15:07:45 +08:00
if (fuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) {
2025-09-20 17:14:47 +08:00
log.info("{}-油料不足,需要补充,新建需求和任务", scenarioTask.getResourceId());
2025-09-20 15:07:45 +08:00
this.canMoved.set(false);
requestFlag.set(true);
//需要产生需求
produceFuelRequest();
//产生任务
produceTask();
2025-09-19 11:17:49 +08:00
}
2025-09-20 15:07:45 +08:00
2025-09-19 15:29:58 +08:00
//插入消耗表
2025-09-19 20:01:21 +08:00
insertConsumption(currentUseUp);
2025-09-20 11:15:10 +08:00
pushStatus(scenarioTask.getResourceId());
2025-09-19 11:17:49 +08:00
}
2025-09-20 15:07:45 +08:00
if (!this.canMoved.get()) {
//判断油料是否满足
double totalFuel = statisticBean.getFuel().getTotal();
if (Double.compare(this.getCurrentFuel(), totalFuel) >= 0) {
this.canMoved.set(true);
}
}
2025-09-19 11:17:49 +08:00
2025-09-18 23:33:24 +08:00
2025-09-19 15:29:58 +08:00
}, 0, consumptionTaskInterval, TimeUnit.SECONDS);
2025-09-19 20:01:21 +08:00
} catch (Exception e) {
log.error("fuel consumption exception", e);
2025-09-19 15:29:58 +08:00
}
2025-09-19 11:17:49 +08:00
}
2025-09-19 14:56:12 +08:00
private void produceFuelRequest() {
2025-09-20 17:14:47 +08:00
log.info("{}-产生油料保障需求", this.scenarioTask.getResourceId());
2025-09-19 14:56:12 +08:00
SupplierRequest supplierRequest = new SupplierRequest();
supplierRequest.setId(IdUtils.simpleUUID());
supplierRequest.setFromResourceId(scenarioTask.getResourceId());
supplierRequest.setSupplierNum(String.valueOf(statisticBean.getFuel().getTotal()));
supplierRequest.setSupplierType("fuel");
supplierRequest.setGeneralTime(LocalDateTime.now());
supplierRequest.setLat(scenarioTask.getToLat());
supplierRequest.setLng(scenarioTask.getToLng());
supplierRequest.setHandleFlag(1);
SpringUtil.getBean(SupplierRequestServiceImpl.class).save(supplierRequest);
}
2025-09-19 13:58:18 +08:00
2025-09-19 14:56:12 +08:00
private void produceTask() {
2025-09-20 17:14:47 +08:00
try {
log.info("{}-产生自动保障任务", this.scenarioTask.getResourceId());
List<ScenarioResource> resourceList = SpringUtil.getBean(BattleSupplierServiceImpl.class)
.selectSupplierResource(scenarioTask.getResourceId());
log.info("{}-可选保障分队长度{}", scenarioTask.getResourceId(), resourceList.size());
if (!resourceList.isEmpty()) {
ScenarioTask task = new ScenarioTask();
task.setId(IdUtils.simpleUUID());
task.setScenarioId(scenarioTask.getScenarioId());
task.setResourceId(scenarioTask.getResourceId());
task.setTaskType("6");
task.setSupplierNum(statisticBean.getFuel().getTotal());
task.setToLat(this.coordinateReference.get().getLat() + "");
task.setToLng(this.coordinateReference.get().getLng() + "");
task.setStartTime(LocalDateTime.now());
task.setFromLat(resourceList.get(0).getLat());
task.setFromLng(resourceList.get(0).getLng());
task.setFromSource("general");
log.info("{}-保障分队id::{},from::{},to::{}", this.scenarioTask.getResourceId(),
task.getSupplierResourceId(), task.getFromLat() + "," + task.getFromLng(),
task.getToLat() + "," + task.getToLng());
SpringUtil.getBean(ScenarioTaskServiceImpl.class).save(task);
//增加到房间任务
SupplierTask supplierTask = new SupplierTask(task, roomId);
//立即执行
RoomManager.addAction(roomId, 0, supplierTask);
} else {
log.error("{}-没有保障分队可以选择", scenarioTask.getResourceId());
}
} catch (Exception e) {
log.error("produceTask exception", e);
2025-09-19 14:56:12 +08:00
}
2025-09-20 17:14:47 +08:00
2025-09-19 14:56:12 +08:00
}
2025-09-19 20:01:21 +08:00
private void insertConsumption(double num) {
2025-09-20 17:14:47 +08:00
try{
log.info("{}-插入油料消耗::{}", this.scenarioTask.getResourceId(), num);
BattleConsume battleConsume = new BattleConsume();
battleConsume.setId(IdUtils.simpleUUID());
battleConsume.setResourceId(scenarioTask.getResourceId());
battleConsume.setFuel(num);
battleConsume.setConsumeDate(LocalDateTime.now());
SpringUtil.getBean(BattleConsumeServiceImpl.class).save(battleConsume);
}catch (Exception e){
log.error("insertConsumption exception", e);
}
2025-09-19 20:01:21 +08:00
}
2025-09-20 11:10:20 +08:00
2025-09-20 15:07:45 +08:00
private double getCurrentFuel() {
2025-09-20 15:48:14 +08:00
Object statisticObj = redis.hget(
2025-09-20 15:07:45 +08:00
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
2025-09-20 15:48:14 +08:00
"scenarioInfo");
if (statisticObj != null) {
ScenarioInfo scenarioInfo = JSON.parseObject(statisticObj.toString(), ScenarioInfo.class);
return scenarioInfo.getFuel().getCurrent();
2025-09-20 15:07:45 +08:00
}
return 0;
}
2025-09-20 17:14:47 +08:00
2025-09-20 15:48:14 +08:00
private void setCurrentFuel(double num) {
Object statisticObj = redis.hget(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"scenarioInfo");
if (statisticObj != null) {
ScenarioInfo scenarioInfo = JSON.parseObject(statisticObj.toString(), ScenarioInfo.class);
2025-09-20 17:14:47 +08:00
scenarioInfo.getFuel().setCurrent(scenarioInfo.getFuel().getCurrent() - num);
redis.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"scenarioInfo", JSON.toJSONString(scenarioInfo));
2025-09-20 15:48:14 +08:00
}
}
2025-09-18 23:33:24 +08:00
}