Compare commits

...

6 Commits

Author SHA1 Message Date
李玉东
08ee625021 初次提交 2025-09-15 00:45:00 +08:00
李玉东
3dab4b367f Merge branch 'main' of http://git.hivekion.com:3000/liyudong/simulation-backend 2025-09-15 00:36:25 +08:00
李玉东
59b129c064 初次提交 2025-09-15 00:36:13 +08:00
李玉东
69c1797c1f Merge branch 'main' of http://git.hivekion.com:3000/liyudong/simulation-backend
# Conflicts:
#	src/main/resources/mapper/tbl/ScenarioMapper.xml
2025-09-14 23:51:56 +08:00
李玉东
4fb146c25c 初次提交 2025-09-14 23:51:40 +08:00
李玉东
11ced2acf4 初次提交 2025-09-14 23:32:48 +08:00
5 changed files with 125 additions and 200 deletions

View File

@ -4,10 +4,11 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.Data;
/**
* <p>
@ -19,91 +20,36 @@ import io.swagger.annotations.ApiModelProperty;
*/
@TableName("TBL_WEATHER_RESOURCE")
@ApiModel(value = "WeatherResource对象", description = "气像资源信息")
@Data
public class WeatherResource implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
@ApiModelProperty("物理主键")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty("物理主键")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty("想定房间编号")
@TableField(value="room_id")
private String roomId;
@ApiModelProperty("想定房间编号")
@TableField(value = "room_id")
private String roomId;
@ApiModelProperty("想定场景编号")
@TableField(value="scenario_id")
private Integer scenarioId;
@ApiModelProperty("想定场景编号")
@TableField(value = "scenario_id")
private Integer scenarioId;
@ApiModelProperty("天气类型")
@TableField(value = "weather_type")
private String weatherType;
@ApiModelProperty("天气类型")
@TableField(value = "weather_type")
private String weatherType;
@ApiModelProperty("持续开始时间")
@TableField(value = "last_beg_time")
private LocalDateTime lastBegTime;
@ApiModelProperty("持续开始时间")
@TableField(value = "last_beg_time")
private LocalDateTime lastBegTime;
@ApiModelProperty("持续结束时间")
@TableField(value = "last_end_time")
private LocalDateTime lastEndTime;
@ApiModelProperty("持续结束时间")
@TableField(value = "last_end_time")
private LocalDateTime lastEndTime;
@TableField(exist = false)
private String status = "init";
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRoomId() {
return roomId;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
public Integer getScenarioId() {
return scenarioId;
}
public void setScenarioId(Integer scenarioId) {
this.scenarioId = scenarioId;
}
public String getWeatherType() {
return weatherType;
}
public void setWeatherType(String weatherType) {
this.weatherType = weatherType;
}
public LocalDateTime getLastBegTime() {
return lastBegTime;
}
public void setLastBegTime(LocalDateTime lastBegTime) {
this.lastBegTime = lastBegTime;
}
public LocalDateTime getLastEndTime() {
return lastEndTime;
}
public void setLastEndTime(LocalDateTime lastEndTime) {
this.lastEndTime = lastEndTime;
}
@Override
public String toString() {
return "WeatherResource{" +
"id = " + id +
", roomId = " + roomId +
", scenarioId = " + scenarioId +
", weatherType = " + weatherType +
", lastBegTime = " + lastBegTime +
", lastEndTime = " + lastEndTime +
"}";
}
}

View File

@ -1,6 +1,5 @@
package com.hivekion.scenario.service.impl;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -12,17 +11,11 @@ import com.hivekion.baseData.service.IWeatherResourceService;
import com.hivekion.baseData.service.ScenarioService;
import com.hivekion.common.entity.ResponseCmdInfo;
import com.hivekion.common.redis.RedisUtil;
import com.hivekion.environment.entity.SimtoolWeather;
import com.hivekion.environment.service.SimtoolWeatherService;
import com.hivekion.scenario.entity.ScenarioTask;
import com.hivekion.scenario.mapper.ScenarioTaskMapper;
import com.hivekion.scenario.service.ScenarioTaskService;
import com.hivekion.scenario.service.TaskLogicService;
import com.hivekion.statistic.service.StatisticService;
import com.hivekion.thread.SpringGlobalTaskManager;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@ -46,8 +39,7 @@ public class ScenarioTaskServiceImpl extends
@Resource
private SpringGlobalTaskManager springGlobalTaskManager;
@Resource
private SimtoolWeatherService weatherService;
@Resource
private RedisUtil redisUtil;
@Resource
@ -58,45 +50,29 @@ public class ScenarioTaskServiceImpl extends
@Resource
private TaskLogicService taskLogicService;
@PostConstruct
public void initTest(){
this.start(2746,"1");
}
@Override
public void start(Integer scenarioId, String roomId) {
log.info("scenarioId::{},roomId::{}",scenarioId,roomId);
log.info("scenarioId::{},roomId::{}", scenarioId, roomId);
Scenario currentScenario = scenarioService.getScenarioById(scenarioId);
//想定当前持续时间
redisUtil.hset(roomId + "_" + scenarioId, "duringTime", "0");
//想定当前状态
redisUtil.hset(roomId + "_" + scenarioId, "states", "running");
redisUtil.hset(roomId + "_" + scenarioId, "status", "running");
//查询天气数据
List<WeatherResource> weatherList = weatherResourceService.list(new QueryWrapper<WeatherResource>()
.eq("scenario_id",scenarioId));
JSONArray jsonArray = new JSONArray();
for(WeatherResource weatherResource: weatherList) {
String weaherStr = JSON.toJSONString(weatherResource);
Long timeBegstamp = weatherResource.getLastBegTime().atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
Long timeEndstamp = weatherResource.getLastEndTime().atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
com.alibaba.fastjson.JSONObject weatherObj = JSON.parseObject(weaherStr);
weatherObj.put("weatherBegTime",timeBegstamp);
weatherObj.put("weatherEndTime",timeEndstamp);
jsonArray.add(weatherObj);
}
List<WeatherResource> weatherList = weatherResourceService.list(
new QueryWrapper<WeatherResource>()
.eq("scenario_id", scenarioId));
//放入天气数据
redisUtil.hset(roomId + "_" + scenarioId, "weather", JSON.toJSONString(jsonArray));
redisUtil.hset(roomId + "_" + scenarioId, "weather", JSONArray.toJSONString(weatherList));
//查询任务
ScenarioTask queryTask = new ScenarioTask();
queryTask.setScenarioId(scenarioId);
redisUtil.hset(roomId + "_" + scenarioId, "taskList", JSON.toJSONString(queryTaskList(queryTask)));
redisUtil.hset(roomId + "_" + scenarioId, "taskList",
JSON.toJSONString(queryTaskList(queryTask)));
new Thread(() -> {
springGlobalTaskManager.startPerSecondTask(roomId + "_" + scenarioId + "_task", () -> {
@ -104,9 +80,9 @@ public class ScenarioTaskServiceImpl extends
//时间累计
increaseTime(currentScenario, roomId);
//天气触发
weatherTrigger(currentScenario, roomId);
weatherTrigger(currentScenario, roomId);
//任务触发
taskTrigger(currentScenario, roomId);
// taskTrigger(currentScenario, roomId);
});
}).start();
@ -128,23 +104,25 @@ public class ScenarioTaskServiceImpl extends
}
private void increaseTime(Scenario currentScenario, String roomId) {
try{
log.info("currentScenario:;{}",currentScenario);
try {
int mag = Global.roomParamMap.get(currentScenario.getId() + "_" + roomId) == null ? 1
: Global.roomParamMap.get(currentScenario.getId() + "_" + roomId).getMag();
//获取当前状态
Object statusObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "status");
log.info("scenario_id:{},status::{}", currentScenario.getId(), statusObj);
if (statusObj != null && statusObj.toString().equals("running")) {
Object duringObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "duringTime");
if (duringObj != null) {
int oldValue = duringObj instanceof Integer ? (Integer) duringObj : 0;
oldValue = oldValue + mag;
redisUtil.hset(roomId + "_" + currentScenario.getId(), "duringTime", oldValue);
}
int duringTime = getCurrentDuringTime(currentScenario, roomId);
duringTime = duringTime + mag;
log.info("duringTime::{}",duringTime);
redisUtil.hset(roomId + "_" + currentScenario.getId(), "duringTime", duringTime + "");
}
}catch (Exception e){
log.error("error::",e);
} catch (
Exception e) {
log.error("error::", e);
}
}
@ -157,58 +135,61 @@ public class ScenarioTaskServiceImpl extends
*/
private void weatherTrigger(Scenario currentScenario, String roomId) {
try {
String weatherResources = (String) redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather");
JSONArray weatherArray = JSONArray.parseArray(weatherResources);
String weatherStatus = redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather-status") !=null?(String) redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather-status"):null;
for(int i=0;i<weatherArray.size();i++){
com.alibaba.fastjson.JSONObject weatherObj = (com.alibaba.fastjson.JSONObject) weatherArray.get(i);
Long timeBegstamp =Long.valueOf(weatherObj.getString("weatherBegTime"));
Long timeEndstamp =Long.valueOf(weatherObj.getString("weatherEndTime"));
Long duringTime = Long.valueOf(redisUtil.hget(roomId + "_" + currentScenario.getId(), "duringTime").toString());
Long scenarioBegtime = currentScenario.getStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
if(scenarioBegtime+duringTime >= timeBegstamp && StringUtils.isEmpty(weatherStatus)) {
ResponseCmdInfo<JSONObject> responseCmdInfo = new ResponseCmdInfo();
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
responseCmdInfo.setCmdType("start-" + weatherObj.getString("weatherType"));
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
System.out.println(responseCmdInfo.toString());
redisUtil.hset(roomId + "_" + currentScenario.getId(), "weather-status","start");
Global.sendCmdInfoQueue.add(responseCmdInfo);
String weatherResources = (String) redisUtil.hget(roomId + "_" + currentScenario.getId(),
"weather");
List<WeatherResource> weatherList = JSONArray.parseArray(weatherResources,
WeatherResource.class);
int duringTime = getCurrentDuringTime(currentScenario, roomId);
for (WeatherResource resource : weatherList) {
if ("init".equals(resource.getStatus())) {
if (currentScenario.getStartTime().plusSeconds(duringTime)
.isAfter(resource.getLastBegTime())) {
log.info("{}","start_"+resource.getWeatherType());
resource.setStatus("running");
ResponseCmdInfo<Void> responseCmdInfo = new ResponseCmdInfo<>();
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
responseCmdInfo.setCmdType("start_" + resource.getWeatherType());
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
Global.sendCmdInfoQueue.add(responseCmdInfo);
}
}
else if(timeBegstamp+duringTime >= timeEndstamp && StringUtils.isNotEmpty(weatherStatus)){
ResponseCmdInfo<JSONObject> responseCmdInfo = new ResponseCmdInfo();
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
responseCmdInfo.setCmdType("end-" + weatherObj.getString("weatherType"));
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
System.out.println(responseCmdInfo.toString());
redisUtil.hset(roomId + "_" + currentScenario.getId(), "weather-status","end");
Global.sendCmdInfoQueue.add(responseCmdInfo);
}else{
ResponseCmdInfo<JSONObject> responseCmdInfo = new ResponseCmdInfo();
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
responseCmdInfo.setCmdType("remain-" + weatherObj.getString("weatherType"));
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
System.out.println(responseCmdInfo.toString());
Global.sendCmdInfoQueue.add(responseCmdInfo);
if ("running".equals(resource.getStatus())) {
if (currentScenario.getStartTime().plusSeconds(duringTime)
.isAfter(resource.getLastEndTime())) {
log.info("{}","stopped_"+resource.getWeatherType());
resource.setStatus("stopped");
ResponseCmdInfo<Void> responseCmdInfo = new ResponseCmdInfo<>();
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
responseCmdInfo.setCmdType("stop_" + resource.getWeatherType());
responseCmdInfo.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId);
Global.sendCmdInfoQueue.add(responseCmdInfo);
}
}
}
redisUtil.hset(roomId + "_" + currentScenario.getId(), "weather",
JSONArray.toJSONString(weatherList));
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage());
}
}
/**
* 获取当前想定从开始到现在时间
*
* @param scenario
* @param roomId
* @return
@ -216,13 +197,14 @@ public class ScenarioTaskServiceImpl extends
private int getCurrentDuringTime(Scenario scenario, String roomId) {
Object duringTime = redisUtil.hget(roomId + "_" + scenario.getId(), "duringTime");
if (duringTime != null) {
return (Integer) duringTime;
return Integer.parseInt(duringTime.toString());
}
return 0;
}
private void taskTrigger(Scenario currentScenario, String roomId) {
try{
log.info("{}",currentScenario);
try {
log.info("{}", JSON.toJSONString(currentScenario));
Object statusObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "status");
if (statusObj != null && statusObj.toString().equals("running")) {
Object taskListObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "taskList");
@ -235,7 +217,8 @@ public class ScenarioTaskServiceImpl extends
ScenarioTask scenarioTask = (ScenarioTask) task;
switch (scenarioTask.getTaskType()) {
case "1":
taskLogicService.handleMoveTask(scenarioTask, currentScenario, roomId,18.0,null);
taskLogicService.handleMoveTask(scenarioTask, currentScenario, roomId, 18.0,
null);
break;
case "2":
taskLogicService.handleBattleTask(scenarioTask, currentScenario, roomId);
@ -252,8 +235,8 @@ public class ScenarioTaskServiceImpl extends
}
}
}
}catch (Exception e){
log.error("error::",e);
} catch (Exception e) {
log.error("error::", e);
}
}

View File

@ -1,8 +1,9 @@
package com.hivekion;
package com.hivekion.startup;
import com.hivekion.thread.HandleReceiveThread;
import com.hivekion.thread.HandleSendThread;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
@ -15,9 +16,12 @@ import org.springframework.stereotype.Component;
* @since 2025/7/22
*/
@Component
@Slf4j
public class Init {
@PostConstruct
public void init() {
public void appInit() {
log.info("Init start");
new Thread(new HandleSendThread()).start();
new Thread(new HandleReceiveThread()).start();
}

View File

@ -20,9 +20,12 @@ public class HandleSendThread implements Runnable {
@Override
public void run() {
log.info("HandleSendThread start .....................");
while (!Thread.currentThread().isInterrupted()) {
try {
log.info("waiting message");
ResponseCmdInfo<?> response = Global.sendCmdInfoQueue.take();
log.info("send response");
WsServer.sendMessage(response.getScenarioId(), response.getRoom(), JSON.toJSONString(response));
} catch (Exception e) {
log.error("error::", e);

View File

@ -60,13 +60,7 @@ public class WsServer {
}
try{
session.getBasicRemote().sendText(testWeatherJson("start_rain"));
Thread.sleep(10000);
session.getBasicRemote().sendText(testWeatherJson("start_snow"));
}catch (Exception e){
log.error("error::",e);
}
}
}
@ -116,30 +110,25 @@ public class WsServer {
}
public static void sendMessage(Integer scenarioId, String room, String message) {
log.info("send {},{},{}",message,scenarioId,room);
Object lock = lockMap.computeIfAbsent(scenarioId, k -> new Object());
synchronized (lock) {
Map<String, Map<String, Session>> roomMap = SESSION_MAP.get(String.valueOf(scenarioId));
if (roomMap.containsKey(room)) {
Map<String, Session> singleRoomMap = roomMap.get(room);
singleRoomMap.forEach((sessionId, session) -> {
try{
session.getBasicRemote().sendText(message);
}catch (Exception e){
log.error("error::",e);
}
if(roomMap!=null){
if (roomMap.containsKey(room)) {
Map<String, Session> singleRoomMap = roomMap.get(room);
singleRoomMap.forEach((sessionId, session) -> {
try{
session.getBasicRemote().sendText(message);
}catch (Exception e){
log.error("error::",e);
}
});
});
}
}
}
}
private String testWeatherJson(String tag){
ResponseCmdInfo responseCmdInfo = new ResponseCmdInfo();
responseCmdInfo.setCmdType(tag);
responseCmdInfo.setRoom("123");
responseCmdInfo.setScenarioId(2746);
Map<String,Object> data = new HashMap<>();
data.put("begTime","2028-03-09 10:00:00");
data.put("endTime","2028-03-19 00:20:00");
return JSON.toJSONString(responseCmdInfo);
}
}