发送天气资源消息

This commit is contained in:
wangwenhua 2025-09-14 23:10:15 +08:00
parent 1525954f1a
commit 3adfc9d456
2 changed files with 69 additions and 26 deletions

View File

@ -67,7 +67,6 @@ public class Scenario extends SearchInputVo {
@ApiModelProperty("是不是小堆") @ApiModelProperty("是不是小堆")
private Byte isXd; private Byte isXd;
@TableField(value = "start_time") @TableField(value = "start_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime; private LocalDateTime startTime;
@TableField(value = "end_time") @TableField(value = "end_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

View File

@ -1,7 +1,8 @@
package com.hivekion.scenario.service.impl; package com.hivekion.scenario.service.impl;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; 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.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hivekion.Global; import com.hivekion.Global;
@ -19,6 +20,8 @@ import com.hivekion.scenario.service.ScenarioTaskService;
import com.hivekion.scenario.service.TaskLogicService; import com.hivekion.scenario.service.TaskLogicService;
import com.hivekion.statistic.service.StatisticService; import com.hivekion.statistic.service.StatisticService;
import com.hivekion.thread.SpringGlobalTaskManager; import com.hivekion.thread.SpringGlobalTaskManager;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -55,7 +58,10 @@ public class ScenarioTaskServiceImpl extends
@Resource @Resource
private TaskLogicService taskLogicService; private TaskLogicService taskLogicService;
@PostConstruct
public void initTest(){
this.start(2746,"1");
}
@Override @Override
public void start(Integer id, String roomId) { public void start(Integer id, String roomId) {
log.info("id::{},roomId::{}",id,roomId); log.info("id::{},roomId::{}",id,roomId);
@ -69,15 +75,28 @@ public class ScenarioTaskServiceImpl extends
//查询天气数据 //查询天气数据
WeatherResource weatherList = weatherResourceService.getOne(new QueryWrapper<WeatherResource>() List<WeatherResource> weatherList = weatherResourceService.list(new QueryWrapper<WeatherResource>()
.eq("scenario_id",id)); .eq("scenario_id",id));
// currentScenario.getGuid()); 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 + "_" + id, "weather", weatherList.getWeatherType()); redisUtil.hset(roomId + "_" + id, "weather", JSON.toJSONString(jsonArray));
//查询任务 //查询任务
ScenarioTask queryTask = new ScenarioTask(); ScenarioTask queryTask = new ScenarioTask();
queryTask.setScenarioId(id); queryTask.setScenarioId(id);
redisUtil.hset(roomId + "_" + id, "taskList", queryTaskList(queryTask).get(0).getTaskType()); // redisUtil.hset(roomId + "_" + id, "taskList", queryTaskList(queryTask));
new Thread(() -> { new Thread(() -> {
springGlobalTaskManager.startPerSecondTask(roomId + "_" + id + "_task", () -> { springGlobalTaskManager.startPerSecondTask(roomId + "_" + id + "_task", () -> {
@ -87,7 +106,7 @@ public class ScenarioTaskServiceImpl extends
//天气触发 //天气触发
weatherTrigger(currentScenario, roomId); weatherTrigger(currentScenario, roomId);
//任务触发 //任务触发
taskTrigger(currentScenario, roomId); // taskTrigger(currentScenario, roomId);
}); });
}).start(); }).start();
@ -137,26 +156,51 @@ public class ScenarioTaskServiceImpl extends
*/ */
private void weatherTrigger(Scenario currentScenario, String roomId) { private void weatherTrigger(Scenario currentScenario, String roomId) {
try { try {
QueryWrapper<WeatherResource> weatherResourceQueryWrapper = new QueryWrapper<>(); String weatherResources = (String) redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather");
weatherResourceQueryWrapper.eq("scenario_id", currentScenario.getId()); JSONArray weatherArray = JSONArray.parseArray(weatherResources);
List<WeatherResource> weatherResourceList = this.weatherResourceService.list(weatherResourceQueryWrapper); String weatherStatus = redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather-status") !=null?(String) redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather-status"):null;
ResponseCmdInfo<JSONArray> responseCmdInfo = new ResponseCmdInfo(); 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.setScenarioId(currentScenario.getId());
responseCmdInfo.setRoom(roomId); responseCmdInfo.setRoom(roomId);
JSONArray weatherMsgArray = new JSONArray(); responseCmdInfo.setCmdType("start-" + weatherObj.getString("weatherType"));
for (WeatherResource weatherResource : weatherResourceList) { responseCmdInfo.setScenarioId(currentScenario.getId());
JSONObject jsonObject = new JSONObject(); responseCmdInfo.setRoom(roomId);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(responseCmdInfo.toString());
jsonObject.putOnce("begTime", dtf.format(weatherResource.getLastBegTime())); redisUtil.hset(roomId + "_" + currentScenario.getId(), "weather-status","start");
jsonObject.putOnce("endTime", dtf.format(weatherResource.getLastEndTime())); Global.sendCmdInfoQueue.add(responseCmdInfo);
weatherMsgArray.add(jsonObject);
responseCmdInfo.setCmdType("66-" + weatherResource.getWeatherType());
} }
responseCmdInfo.setData(weatherMsgArray); 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()); System.out.println(responseCmdInfo.toString());
Global.sendCmdInfoQueue.add(responseCmdInfo); Global.sendCmdInfoQueue.add(responseCmdInfo);
} catch (Exception ex) { }
}
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage()); log.error(ex.getMessage());
} }