diff --git a/src/main/java/com/hivekion/scenario/service/impl/ScenarioTaskServiceImpl.java b/src/main/java/com/hivekion/scenario/service/impl/ScenarioTaskServiceImpl.java index b8ceafb..ad2cc57 100644 --- a/src/main/java/com/hivekion/scenario/service/impl/ScenarioTaskServiceImpl.java +++ b/src/main/java/com/hivekion/scenario/service/impl/ScenarioTaskServiceImpl.java @@ -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() + List weatherList = weatherResourceService.list(new QueryWrapper() .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= timeBegstamp && StringUtils.isEmpty(weatherStatus)) { + ResponseCmdInfo 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 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 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 weatherResourceQueryWrapper = new QueryWrapper<>(); -// weatherResourceQueryWrapper.eq("scenario_id", currentScenario.getId()); -// List weatherResourceList = this.weatherResourceService.list(weatherResourceQueryWrapper); -// ResponseCmdInfo 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()); + } } diff --git a/src/main/resources/mapper/tbl/ScenarioMapper.xml b/src/main/resources/mapper/tbl/ScenarioMapper.xml index eea426d..feae4f9 100644 --- a/src/main/resources/mapper/tbl/ScenarioMapper.xml +++ b/src/main/resources/mapper/tbl/ScenarioMapper.xml @@ -63,7 +63,7 @@ Author, CreateUserId, CreateTime, - start_time , + start_time As startTime, end_time, id, left_up_lng AS leftUpLng,