# Conflicts:
#	src/main/resources/mapper/tbl/ScenarioMapper.xml
This commit is contained in:
李玉东 2025-09-14 23:51:56 +08:00
commit 69c1797c1f
2 changed files with 72 additions and 36 deletions

View File

@ -1,8 +1,8 @@
package com.hivekion.scenario.service.impl;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hivekion.Global;
@ -20,6 +20,8 @@ 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;
@ -55,7 +57,10 @@ 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);
@ -69,13 +74,24 @@ public class ScenarioTaskServiceImpl extends
//查询天气数据
WeatherResource weatherList = weatherResourceService.getOne(new QueryWrapper<WeatherResource>()
List<WeatherResource> weatherList = weatherResourceService.list(new QueryWrapper<WeatherResource>()
.eq("scenario_id",scenarioId));
if(weatherList!=null){
//放入天气数据
redisUtil.hset(roomId + "_" + scenarioId, "weather", JSON.toJSONString(weatherList));
}
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);
}
//放入天气数据
redisUtil.hset(roomId + "_" + scenarioId, "weather", JSON.toJSONString(jsonArray));
//查询任务
ScenarioTask queryTask = new ScenarioTask();
queryTask.setScenarioId(scenarioId);
@ -139,34 +155,54 @@ public class ScenarioTaskServiceImpl extends
* @param roomId 房间ID
*/
private void weatherTrigger(Scenario currentScenario, String roomId) {
//获取到 reids中天气数据
//每个天气开始遍历
//获取天气开始的时间 ,最好加一个状态提示天气任务的开始运行
//想定的开始时间+想定目前持续的时间
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);
}
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);
}
}
// try {
// QueryWrapper<WeatherResource> weatherResourceQueryWrapper = new QueryWrapper<>();
// weatherResourceQueryWrapper.eq("scenario_id", currentScenario.getId());
// List<WeatherResource> weatherResourceList = this.weatherResourceService.list(weatherResourceQueryWrapper);
// ResponseCmdInfo<JSONArray> responseCmdInfo = new ResponseCmdInfo();
// responseCmdInfo.setScenarioId(currentScenario.getId());
// responseCmdInfo.setRoom(roomId);
// JSONArray weatherMsgArray = new JSONArray();
// for (WeatherResource weatherResource : weatherResourceList) {
// JSONObject jsonObject = new JSONObject();
// DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// jsonObject.putOnce("begTime", dtf.format(weatherResource.getLastBegTime()));
// jsonObject.putOnce("endTime", dtf.format(weatherResource.getLastEndTime()));
// weatherMsgArray.add(jsonObject);
// responseCmdInfo.setCmdType("66-" + weatherResource.getWeatherType());
// }
// responseCmdInfo.setData(weatherMsgArray);
// System.out.println(responseCmdInfo.toString());
// Global.sendCmdInfoQueue.add(responseCmdInfo);
// } catch (Exception ex) {
//
// log.error(ex.getMessage());
// }
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage());
}
}

View File

@ -63,7 +63,7 @@
Author,
CreateUserId,
CreateTime,
start_time ,
start_time As startTime,
end_time,
id,
left_up_lng AS leftUpLng,