发送天气资源消息

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("是不是小堆")
private Byte isXd;
@TableField(value = "start_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime;
@TableField(value = "end_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

View File

@ -1,7 +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;
@ -19,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 +58,10 @@ public class ScenarioTaskServiceImpl extends
@Resource
private TaskLogicService taskLogicService;
@PostConstruct
public void initTest(){
this.start(2746,"1");
}
@Override
public void start(Integer id, String 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));
// 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();
queryTask.setScenarioId(id);
redisUtil.hset(roomId + "_" + id, "taskList", queryTaskList(queryTask).get(0).getTaskType());
// redisUtil.hset(roomId + "_" + id, "taskList", queryTaskList(queryTask));
new Thread(() -> {
springGlobalTaskManager.startPerSecondTask(roomId + "_" + id + "_task", () -> {
@ -87,7 +106,7 @@ public class ScenarioTaskServiceImpl extends
//天气触发
weatherTrigger(currentScenario, roomId);
//任务触发
taskTrigger(currentScenario, roomId);
// taskTrigger(currentScenario, roomId);
});
}).start();
@ -137,26 +156,51 @@ public class ScenarioTaskServiceImpl extends
*/
private void weatherTrigger(Scenario currentScenario, String roomId) {
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());
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);
}
}
responseCmdInfo.setData(weatherMsgArray);
System.out.println(responseCmdInfo.toString());
Global.sendCmdInfoQueue.add(responseCmdInfo);
} catch (Exception ex) {
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage());
}