|
@@ -0,0 +1,269 @@
|
|
|
|
+package com.gyee.runeconomy.service.Weather;
|
|
|
|
+
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
|
|
|
|
+import com.gyee.runeconomy.service.auto.IProBasicPowerstationService;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author hlf
|
|
|
|
+ * @date 2022/9/21 10:52
|
|
|
|
+ * 文件说明:定时任务工具类
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+//@EnableAsync
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class WertherForecast {
|
|
|
|
+
|
|
|
|
+ private RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private IProBasicPowerstationService powerstationService;
|
|
|
|
+//
|
|
|
|
+// @Resource
|
|
|
|
+// private IWeatherDataHourService weatherDataHourService;
|
|
|
|
+//
|
|
|
|
+// @Resource
|
|
|
|
+// private IWeatherDataFutureService weatherDataFutureService;
|
|
|
|
+//
|
|
|
|
+// @Resource
|
|
|
|
+// private IWeatherDataFutureHistoryService historyService;
|
|
|
|
+//
|
|
|
|
+// @Resource
|
|
|
|
+// private IWeather24HourService weather24HourService;
|
|
|
|
+//
|
|
|
|
+// @Resource
|
|
|
|
+// private IWeather24HourHistoryService weather24HourHistoryService;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 天气预报
|
|
|
|
+ * 一小时执行一次
|
|
|
|
+ */
|
|
|
|
+// @Scheduled(cron = "0 0 */1 * * ?")
|
|
|
|
+// @Async
|
|
|
|
+ public void doTaskWeatherForecast() {
|
|
|
|
+ System.out.println("开始执行天气预报异步任务,当前线程:" + Thread.currentThread().getName());
|
|
|
|
+
|
|
|
|
+ List<ProBasicPowerstation> powerstations = powerstationService.selectList1();
|
|
|
|
+ if (powerstations.isEmpty()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (ProBasicPowerstation windplant : powerstations) {
|
|
|
|
+ String url = "http://weathernew.pae.baidu.com/weathernew/pc?query=" + windplant.getAddress() + "天气&srcid=4982";
|
|
|
|
+ ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
|
|
|
|
+
|
|
|
|
+ if (entity.getBody() == null) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ String jsonString = extractJsonString(entity.getBody());
|
|
|
|
+ JsonNode root = new ObjectMapper().readTree(jsonString);
|
|
|
|
+ System.out.println(root.toString());
|
|
|
|
+//
|
|
|
|
+// //未来40天天气-包含当天
|
|
|
|
+// JsonNode infoArray = root.path("long_day_forecast").path("info");
|
|
|
|
+// //当天紫外线-湿度
|
|
|
|
+// JsonNode infoArrayzwx = root.path("feature");
|
|
|
|
+// //气压-能见度
|
|
|
|
+// JsonNode infoArrayqy = root.path("weather");
|
|
|
|
+// //24小时天气
|
|
|
|
+// JsonNode infoArray24 = root.path("24_hour_forecast").path("info");
|
|
|
|
+//
|
|
|
|
+// //存储当天天气
|
|
|
|
+// List<WeatherDataHour> weatherDataHours = new ArrayList<>();
|
|
|
|
+// //存储24小时天气
|
|
|
|
+// List<Weather24Hour> weatherDataHours24 = new ArrayList<>();
|
|
|
|
+// //存储24小时天气历史
|
|
|
|
+// List<Weather24HourHistory> weatherDataHours24history= new ArrayList<>();
|
|
|
|
+// //存储未来40天天气
|
|
|
|
+// List<WeatherDataFuture> weatherDataFutures = new ArrayList<>();
|
|
|
|
+// //存储未来40天天气-历史
|
|
|
|
+// List<WeatherDataFutureHistory> futureHistories = new ArrayList<>();
|
|
|
|
+//
|
|
|
|
+// for (JsonNode info : infoArray24) {
|
|
|
|
+// Weather24Hour weather24Hour = parseWeather24Hour(info, windplant);
|
|
|
|
+// weatherDataHours24.add(weather24Hour);
|
|
|
|
+// weather24HourService.remove(windplant.getId());
|
|
|
|
+// weather24HourService.batchAdd(weatherDataHours24);
|
|
|
|
+//
|
|
|
|
+// Weather24HourHistory weather24HourHistory = parseWeather24HourHistory(info, windplant);
|
|
|
|
+// weatherDataHours24history.add(weather24HourHistory);
|
|
|
|
+// weather24HourHistoryService.batchAdd(weatherDataHours24history);
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// for (JsonNode info : infoArray) {
|
|
|
|
+// WeatherDataHour dataHour = parseWeatherDataHour(info, infoArrayzwx,infoArrayqy, windplant);
|
|
|
|
+// if (dataHour.getRecordDate().equals(LocalDate.now())) {
|
|
|
|
+// weatherDataHours.add(dataHour);
|
|
|
|
+// weatherDataHourService.remove(windplant.getId());
|
|
|
|
+// weatherDataHourService.batchAdd(weatherDataHours);
|
|
|
|
+// weatherDataHours.clear();
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// WeatherDataFuture dataFuture = parseWeatherDataFuture(info, windplant);
|
|
|
|
+// weatherDataFutures.add(dataFuture);
|
|
|
|
+// weatherDataFutureService.remove(windplant.getId());
|
|
|
|
+// weatherDataFutureService.batchAdd(weatherDataFutures);
|
|
|
|
+// weatherDataFutures.clear();
|
|
|
|
+//
|
|
|
|
+// WeatherDataFutureHistory history = parseWeatherDataFutureHistory(info, windplant);
|
|
|
|
+// futureHistories.add(history);
|
|
|
|
+// historyService.batchAdd(futureHistories);
|
|
|
|
+// futureHistories.clear();
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ System.out.println("天气预报异步任务执行完毕,当前线程:" + Thread.currentThread().getName());
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private String extractJsonString(String body) {
|
|
|
|
+ int index = body.indexOf("<!DOC");
|
|
|
|
+ String a = body.substring(0, index);
|
|
|
|
+ String b = a.replaceAll("<script>", "");
|
|
|
|
+ int index1 = b.lastIndexOf(";<");
|
|
|
|
+ String c = b.substring(0, index1);
|
|
|
|
+ int index2 = c.indexOf(" {");
|
|
|
|
+ String d = c.substring(index2);
|
|
|
|
+ String e = d.substring(1);
|
|
|
|
+ return e.substring(0, e.indexOf(";</"));
|
|
|
|
+ }
|
|
|
|
+//
|
|
|
|
+// //当天24小时天气
|
|
|
|
+// private Weather24Hour parseWeather24Hour(JsonNode info,ProBasicPowerstation windplant) throws ParseException {
|
|
|
|
+// Weather24Hour Hour24 = new Weather24Hour();
|
|
|
|
+// Hour24.setStationId(windplant.getId());
|
|
|
|
+// Hour24.setStationName(windplant.getAname());
|
|
|
|
+// String hour = info.path("hour").asText();
|
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
|
|
|
|
+// Hour24.setRecordDate(sdf.parse(hour));
|
|
|
|
+// Hour24.setWeather(info.path("weather").asText());
|
|
|
|
+// Hour24.setPrecipitation(info.path("precipitation").asText());
|
|
|
|
+// Hour24.setTemperature(info.path("temperature").asText());
|
|
|
|
+// Hour24.setWindDirection(info.path("wind_direction").asText());
|
|
|
|
+// Hour24.setWindPower(parseWindPower(info.path("wind_power").asText()));
|
|
|
|
+// Hour24.setListvalue(info.path("pm25").path("listValue").asText());
|
|
|
|
+// Hour24.setListaqival(info.path("pm25").path("listAqiVal").asText());
|
|
|
|
+// return Hour24;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// //当天24小时天气
|
|
|
|
+// private Weather24HourHistory parseWeather24HourHistory(JsonNode info, ProBasicPowerstation windplant) throws ParseException {
|
|
|
|
+// Weather24HourHistory Hour24History = new Weather24HourHistory();
|
|
|
|
+// Hour24History.setStationId(windplant.getId());
|
|
|
|
+// Hour24History.setStationName(windplant.getAname());
|
|
|
|
+// String hour = info.path("hour").asText();
|
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
|
|
|
|
+// Hour24History.setRecordDate(sdf.parse(hour));
|
|
|
|
+// Hour24History.setWeather(info.path("weather").asText());
|
|
|
|
+// Hour24History.setTemperature(info.path("temperature").asText());
|
|
|
|
+// Hour24History.setPrecipitation(info.path("precipitation").asText());
|
|
|
|
+// Hour24History.setWindDirection(info.path("wind_direction").asText());
|
|
|
|
+// Hour24History.setWindPower(parseWindPower(info.path("wind_power").asText()));
|
|
|
|
+// Hour24History.setListvalue(info.path("pm25").path("listValue").asText());
|
|
|
|
+// Hour24History.setListaqival(info.path("pm25").path("listAqiVal").asText());
|
|
|
|
+// return Hour24History;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// //当天天气
|
|
|
|
+// private WeatherDataHour parseWeatherDataHour(JsonNode info,JsonNode infoArrayzwx,JsonNode infoArrayqy,ProBasicPowerstation windplant) {
|
|
|
|
+// WeatherDataHour data = new WeatherDataHour();
|
|
|
|
+// data.setStationId(windplant.getId());
|
|
|
|
+// data.setStationName(windplant.getAname());
|
|
|
|
+// data.setRecordDate(LocalDate.parse(info.path("date").asText()));
|
|
|
|
+// data.setWeather(info.path("weather_day").asText());
|
|
|
|
+// data.setHumidity(infoArrayzwx.path("humidity").asText());
|
|
|
|
+// data.setUltraviolet(infoArrayzwx.path("ultraviolet").asText());
|
|
|
|
+// data.setPressure(infoArrayqy.path("pressure").asText());
|
|
|
|
+// data.setVisibility(infoArrayqy.path("visibility").asText());
|
|
|
|
+// data.setTemperatureNight(new BigDecimal(info.path("temperature_night").asText()));
|
|
|
|
+// data.setTemperatureDay(new BigDecimal(info.path("temperature_day").asText()));
|
|
|
|
+// data.setWindDirection(info.path("wind_direction_day").asText());
|
|
|
|
+// data.setWindPower(parseWindPower(info.path("wind_power_night").asText()));
|
|
|
|
+// data.setSunrisetime(info.path("sunriseTime").asText());
|
|
|
|
+// data.setSunsettime(info.path("sunsetTime").asText());
|
|
|
|
+// return data;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// //未来40天天气
|
|
|
|
+// private WeatherDataFuture parseWeatherDataFuture(JsonNode info, ProBasicPowerstation windplant) {
|
|
|
|
+// WeatherDataFuture data = new WeatherDataFuture();
|
|
|
|
+// data.setStationId(windplant.getId());
|
|
|
|
+// data.setStationName(windplant.getAname());
|
|
|
|
+// data.setRecordDate(LocalDate.parse(info.path("date").asText()));
|
|
|
|
+// data.setWeather(info.path("weather_day").asText());
|
|
|
|
+// data.setWeatherNight(info.path("weather_night").asText());
|
|
|
|
+// data.setTemperatureNight(new BigDecimal(info.path("temperature_night").asText()));
|
|
|
|
+// data.setTemperatureDay(new BigDecimal(info.path("temperature_day").asText()));
|
|
|
|
+// data.setWindDirection(info.path("wind_direction_day").asText());
|
|
|
|
+// data.setWindPower(parseWindPower(info.path("wind_power_night").asText()));
|
|
|
|
+// data.setWindPowerDay(parseWindPower(info.path("wind_power_day").asText()));
|
|
|
|
+// data.setSunrisetime(info.path("sunriseTime").asText());
|
|
|
|
+// data.setSunsettime(info.path("sunsetTime").asText());
|
|
|
|
+// data.setListvalue(info.path("pm25").path("listQuality").path("listValue").asText());
|
|
|
|
+// return data;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// //未来40天天气-历史
|
|
|
|
+// private WeatherDataFutureHistory parseWeatherDataFutureHistory(JsonNode info, ProBasicPowerstation windplant) {
|
|
|
|
+// WeatherDataFutureHistory data = new WeatherDataFutureHistory();
|
|
|
|
+// data.setStationId(windplant.getId());
|
|
|
|
+// data.setStationName(windplant.getAname());
|
|
|
|
+// data.setRecordDate(LocalDate.parse(info.path("date").asText()));
|
|
|
|
+// data.setWeather(info.path("weather_day").asText());
|
|
|
|
+// data.setWeatherNight(info.path("weather_night").asText());
|
|
|
|
+// data.setTemperatureNight(new BigDecimal(info.path("temperature_night").asText()));
|
|
|
|
+// data.setTemperatureDay(new BigDecimal(info.path("temperature_day").asText()));
|
|
|
|
+// data.setWindDirection(info.path("wind_direction_day").asText());
|
|
|
|
+// data.setWindPower(parseWindPower(info.path("wind_power_night").asText()));
|
|
|
|
+// data.setWindPowerDay(parseWindPower(info.path("wind_power_day").asText()));
|
|
|
|
+// data.setSunrisetime(info.path("sunriseTime").asText());
|
|
|
|
+// data.setSunsettime(info.path("sunsetTime").asText());
|
|
|
|
+// data.setListvalue(info.path("pm25").path("listQuality").path("listValue").asText());
|
|
|
|
+// return data;
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 解析风速字符串,例如 "5-6级" 转换为 BigDecimal
|
|
|
|
+ private static BigDecimal parseWindPower(String windPower) {
|
|
|
|
+ if (windPower.contains("级")) {
|
|
|
|
+ String[] parts = windPower.split("级")[0].split("-");
|
|
|
|
+ if (parts.length == 2) {
|
|
|
|
+ try {
|
|
|
|
+ BigDecimal min = new BigDecimal(parts[0].replace("<", "").trim());
|
|
|
|
+ BigDecimal max = new BigDecimal(parts[1].replace("<", "").trim());
|
|
|
|
+ return min.add(max).divide(BigDecimal.valueOf(2)); // 取风速的平均值
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ try {
|
|
|
|
+ return new BigDecimal(parts[0].replace("<", "").trim());
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return BigDecimal.ZERO; // 默认返回 0 风速
|
|
|
|
+ }
|
|
|
|
+}
|