|
@@ -0,0 +1,190 @@
|
|
|
+package com.example.mdd.service;
|
|
|
+
|
|
|
+import cn.hutool.db.DbUtil;
|
|
|
+import cn.hutool.db.ds.simple.SimpleDataSource;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.example.mdd.entity.PowerStation;
|
|
|
+import com.example.mdd.entity.XMO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class XMODownload {
|
|
|
+
|
|
|
+ // 辅助方法:安全地获取列表中的值并转换为字符串,如果列表为空或索引越界则返回空字符串
|
|
|
+ private static String getValueSafelyAsString(List<?> list, int index) {
|
|
|
+ if (list == null || index < 0 || index >= list.size()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ Object value = list.get(index);
|
|
|
+ return value != null ? String.valueOf(value) : "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 辅助方法:安全地获取列表中的值,如果列表为空或索引越界则返回null
|
|
|
+ private static <T> T getValueSafely(List<T> list, int index) {
|
|
|
+ if (list == null || index < 0 || index >= list.size()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return list.get(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void download() {
|
|
|
+ String[] stationCodes = {"J00301", "J00557", "J00570", "J00117", "J01083", "J01084", "J00707", "J00371", "J00844", "J00811"};
|
|
|
+ try {
|
|
|
+ DataSource ds = new SimpleDataSource("jdbc:mysql://192.168.12.10:23306/mdd"
|
|
|
+ , "root", "la!yibei82nianxueB");
|
|
|
+ Connection conn = DbUtil.use(ds).getConnection();
|
|
|
+ for (String codes : stationCodes) {
|
|
|
+ String body = HttpUtil.get("http://itil.jiayuepowertech.com:9958/itil/api/power-station?paramStationCode=" + codes, 10000);
|
|
|
+ JSONObject json = JSONUtil.parseObj(body);
|
|
|
+ String code = json.get("code").toString();
|
|
|
+ String data = json.get("data").toString();
|
|
|
+ if (code.equals("0") && !data.isEmpty()) {
|
|
|
+ JSONArray array = JSONUtil.parseArray(data);
|
|
|
+ //itil所有的场站数据 根据场站数据的经纬度通过气象平台的api接口查询数据
|
|
|
+ List<PowerStation> list = array.toList(PowerStation.class);
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ if (!"".equals(list.get(0).getLongitude()) && !"".equals(list.get(0).getLatitude())) {
|
|
|
+ downloadHour(conn, list.get(0).getStationCode(), list.get(0).getLongitude(), list.get(0).getLatitude(), list.get(0).getStationType());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info(codes + "曦谋天气小时数据下载完成");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void downloadHour(Connection conn, String stationCode, String longitude, String latitude, String type) {
|
|
|
+ String url;
|
|
|
+ String startDate = "";
|
|
|
+ String endDate = "";
|
|
|
+ // 获取今日日期
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ // 获取16天后的日期
|
|
|
+ LocalDate sixteenDaysLater = today.plusDays(15);
|
|
|
+ // 定义日期格式
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+ // 格式化今日日期
|
|
|
+ startDate = today.format(formatter);
|
|
|
+ // 格式化16天后的日期
|
|
|
+ endDate = sixteenDaysLater.format(formatter);
|
|
|
+ //风
|
|
|
+ if ("2".equals(type)) {
|
|
|
+ url = "https://weather-api.xm-opt.com/v1/forecast15Minutes?latitude=" + latitude + "&longitude=" + longitude + "&minutely_15=temperature_2m,wind_speed_10m,wind_direction_10m,dew_point_2m,wind_gusts_10m,apparent_temperature,weather_code,precipitation,rain,snowfall&timezone=Asia%2FShanghai&start_date=" + startDate + "&end_date=" + endDate;
|
|
|
+ } else {
|
|
|
+ url = "https://weather-api.xm-opt.com/v1/forecast15Minutes?latitude=" + latitude + "&longitude=" + longitude + "&minutely_15=weather_code,apparent_temperature,relative_humidity_2m,dew_point_2m,precipitation,rain,snowfall,wind_speed_10m,wind_direction_10m,sunshine_duration,shortwave_radiation,direct_radiation,diffuse_radiation,direct_normal_irradiance,global_tilted_irradiance,terrestrial_radiation&timezone=Asia%2FShanghai&start_date=" + startDate + "&end_date=" + endDate;
|
|
|
+ }
|
|
|
+ String body = HttpUtil.createGet(url).header("apikey", "c615a8f6-f5dd-4a81-86fe-db2a42862c15").execute().charset("utf-8").body();
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
+ JSONObject result = JSONUtil.parseObj(jsonObject.get("minutely_15"));
|
|
|
+ List<XMO> xmoList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 获取各个数据数组
|
|
|
+ List<String> timeList = result.get("time", List.class);
|
|
|
+ List<Double> temperature2mList = result.get("temperature_2m", List.class);
|
|
|
+ List<Double> windSpeed10mList = result.get("wind_speed_10m", List.class);
|
|
|
+ List<Double> windDirection10mList = result.get("wind_direction_10m", List.class);
|
|
|
+ List<Double> dewPoint2mList = result.get("dew_point_2m", List.class);
|
|
|
+ List<Double> windGusts10mList = result.get("wind_gusts_10m", List.class);
|
|
|
+ List<Double> apparentTemperatureList = result.get("apparent_temperature", List.class);
|
|
|
+ List<Integer> weatherCodeList = result.get("weather_code", List.class);
|
|
|
+ List<Double> precipitationList = result.get("precipitation", List.class);
|
|
|
+ List<Double> rainList = result.get("rain", List.class);
|
|
|
+ List<Double> snowfallList = result.get("snowfall", List.class);
|
|
|
+ List<Double> sunshineDurationList = result.get("sunshine_duration", List.class);
|
|
|
+ List<Double> shortwaveRadiationList = result.get("shortwave_radiation", List.class);
|
|
|
+ List<Double> directRadiationList = result.get("direct_radiation", List.class);
|
|
|
+ List<Double> directNormalIrradianceList = result.get("direct_normal_irradiance", List.class);
|
|
|
+ List<Double> diffuseRadiationList = result.get("diffuse_radiation", List.class);
|
|
|
+ List<Double> globalTiltedIrradianceList = result.get("global_tilted_irradiance", List.class);
|
|
|
+ List<Double> terrestrialRadiationList = result.get("terrestrial_radiation", List.class);
|
|
|
+ List<Double> relativeHumidity2mList = result.get("relative_humidity_2m", List.class);
|
|
|
+
|
|
|
+ int length = timeList.size();
|
|
|
+ //获取当前时间 yyyy-mm-dd hh:mm
|
|
|
+ String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
|
|
|
+ // 遍历数组,填充XMO对象并添加到列表
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ XMO xmo = new XMO();
|
|
|
+ xmo.setStationCode(stationCode);
|
|
|
+ xmo.setTime(getValueSafely(timeList, i));
|
|
|
+ xmo.setWindSpeed10m(getValueSafelyAsString(windSpeed10mList, i));
|
|
|
+ xmo.setRain(getValueSafelyAsString(rainList, i));
|
|
|
+ xmo.setDewPoint2m(getValueSafelyAsString(dewPoint2mList, i));
|
|
|
+ xmo.setSnowfall(getValueSafelyAsString(snowfallList, i));
|
|
|
+ xmo.setWindDirection10m(getValueSafelyAsString(windDirection10mList, i));
|
|
|
+ xmo.setWindGusts10m(getValueSafelyAsString(windGusts10mList, i));
|
|
|
+ xmo.setTemperature2m(getValueSafelyAsString(temperature2mList, i));
|
|
|
+ xmo.setPrecipitation(getValueSafelyAsString(precipitationList, i));
|
|
|
+ xmo.setApparentTemperature(getValueSafelyAsString(apparentTemperatureList, i));
|
|
|
+ xmo.setWeatherCode(getValueSafelyAsString(weatherCodeList, i));
|
|
|
+ xmo.setSunshineDuration(getValueSafelyAsString(sunshineDurationList, i));
|
|
|
+ xmo.setShortwaveRadiation(getValueSafelyAsString(shortwaveRadiationList, i));
|
|
|
+ xmo.setDirectRadiation(getValueSafelyAsString(directRadiationList, i));
|
|
|
+ xmo.setDirectNormalIrradiance(getValueSafelyAsString(directNormalIrradianceList, i));
|
|
|
+ xmo.setDiffuseRadiation(getValueSafelyAsString(diffuseRadiationList, i));
|
|
|
+ xmo.setGlobalTiltedIrradiance(getValueSafelyAsString(globalTiltedIrradianceList, i));
|
|
|
+ xmo.setTerrestrialRadiation(getValueSafelyAsString(terrestrialRadiationList, i));
|
|
|
+ xmo.setRelativeHumidity2m(getValueSafelyAsString(relativeHumidity2mList, i));
|
|
|
+ // 假设createTime可以设置为当前时间,可根据实际情况调整
|
|
|
+ xmo.setCreateTime(currentTime);
|
|
|
+ xmoList.add(xmo);
|
|
|
+ }
|
|
|
+ if (!xmoList.isEmpty()) {
|
|
|
+ String tableSql = "INSERT INTO `mdd`.`xmo` (`station_code`, `time`, `temperature_2m`, `wind_speed_10m`, `wind_direction_10m`, `relative_humidity_2m`, `apparent_temperature`, `weather_code`, `precipitation`, `rain`, `snowfall`, `sunshine_duration`,`shortwave_radiation`, `direct_radiation`, `direct_normal_irradiance`, `diffuse_radiation`, `global_tilted_irradiance`, `terrestrial_radiation`, `create_time`) " +
|
|
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
|
|
|
+ try {
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(tableSql);
|
|
|
+ conn.setAutoCommit(false);
|
|
|
+ log.info("曦谋天气小时数据:共{}条记录", xmoList.size());
|
|
|
+ xmoList.forEach(item -> {
|
|
|
+ try {
|
|
|
+ pstmt.setString(1, item.getStationCode());
|
|
|
+ pstmt.setString(2, item.getTime());
|
|
|
+ pstmt.setString(3, item.getTemperature2m());
|
|
|
+ pstmt.setString(4, item.getWindSpeed10m());
|
|
|
+ pstmt.setString(5, item.getWindDirection10m());
|
|
|
+ pstmt.setString(6, item.getRelativeHumidity2m());
|
|
|
+ pstmt.setString(7, item.getApparentTemperature());
|
|
|
+ pstmt.setString(8, item.getWeatherCode());
|
|
|
+ pstmt.setString(9, item.getPrecipitation());
|
|
|
+ pstmt.setString(10, item.getRain());
|
|
|
+ pstmt.setString(11, item.getSnowfall());
|
|
|
+ pstmt.setString(12, item.getSunshineDuration());
|
|
|
+ pstmt.setString(13, item.getShortwaveRadiation());
|
|
|
+ pstmt.setString(14, item.getDirectRadiation());
|
|
|
+ pstmt.setString(15, item.getDirectNormalIrradiance());
|
|
|
+ pstmt.setString(16, item.getDiffuseRadiation());
|
|
|
+ pstmt.setString(17, item.getGlobalTiltedIrradiance());
|
|
|
+ pstmt.setString(18, item.getTerrestrialRadiation());
|
|
|
+ pstmt.setString(19, item.getCreateTime());
|
|
|
+ pstmt.addBatch();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ pstmt.executeBatch();
|
|
|
+ conn.commit();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|