|
@@ -1,15 +1,21 @@
|
|
|
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.BaiDu;
|
|
|
+import com.example.mdd.entity.PowerStation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.PreparedStatement;
|
|
|
+import java.sql.SQLException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
|
|
@@ -438,4 +444,230 @@ public class BaiDuDownload {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void complements() {
|
|
|
+ String body = HttpUtil.get("http://itil.jiayuepowertech.com:9958/itil/api/power-station", 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);
|
|
|
+ try {
|
|
|
+ DataSource ds = new SimpleDataSource("jdbc:mysql://192.168.12.10:23306/mdd"
|
|
|
+ , "root", "la!yibei82nianxueB");
|
|
|
+ Connection conn = DbUtil.use(ds).getConnection();
|
|
|
+ long time = 1722700800000L;
|
|
|
+ for (long i = time; i <= time; i = i - 86400000) {
|
|
|
+ for (PowerStation powerStation : list) {
|
|
|
+ if (!"".equals(powerStation.getLongitude()) && !"".equals(powerStation.getLatitude())) {
|
|
|
+ //heFengDownload.download(conn, powerStation.getStationCode(), powerStation.getLongitude(), powerStation.getLatitude());
|
|
|
+ //caiYunDownload.download(conn, powerStation.getStationCode(), powerStation.getLongitude(), powerStation.getLatitude());
|
|
|
+ //xinZhiDownload.download(conn, powerStation.getStationCode(), powerStation.getLongitude(), powerStation.getLatitude());
|
|
|
+ download(conn, powerStation.getStationCode(), powerStation.getLongitude(), powerStation.getLatitude(), new Date(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //去预测15天的数据 8月5号取8月6号到8月20号的数据
|
|
|
+ public void download(Connection conn, String stationCode, String longitude, String latitude, Date date) {
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ // 设置时间为明天
|
|
|
+ String day = sdf.format(date.getTime() + 86400000);
|
|
|
+ //第15天
|
|
|
+ String day14 = sdf.format(date.getTime() + 86400000 * 15);
|
|
|
+ //当天
|
|
|
+ String day0 = sdf.format(date.getTime());
|
|
|
+ //String[] fieldNames = getFieldName();
|
|
|
+ String fieldNames = "temperature_2m,relative_humidity_2m,dew_point_2m,apparent_temperature,precipitation_probability,precipitation,rain,showers,snowfall,snow_depth,weather_code,pressure_msl,surface_pressure,cloud_cover,cloud_cover_low,cloud_cover_mid,cloud_cover_high,visibility,evapotranspiration,et0_fao_evapotranspiration,vapour_pressure_deficit,wind_speed_10m,wind_speed_80m,wind_speed_120m,wind_speed_180m,wind_direction_10m,wind_direction_80m,wind_direction_120m,wind_direction_180m,wind_gusts_10m,temperature_80m,temperature_120m,temperature_180m,soil_temperature_0cm,soil_temperature_6cm,soil_temperature_18cm,soil_temperature_54cm,soil_moisture_0_to_1cm,soil_moisture_1_to_3cm,soil_moisture_3_to_9cm,soil_moisture_9_to_27cm,soil_moisture_27_to_81cm,uv_index,uv_index_clear_sky,is_day,cape,freezing_level_height,sunshine_duration,shortwave_radiation,direct_radiation,diffuse_radiation,direct_normal_irradiance,global_tilted_irradiance,terrestrial_radiation,shortwave_radiation_instant,direct_radiation_instant,diffuse_radiation_instant,direct_normal_irradiance_instant,global_tilted_irradiance_instant,terrestrial_radiation_instant";
|
|
|
+
|
|
|
+ //彩云天气api接口
|
|
|
+ String url = "http://weather-api.xm-opt.com/v1/forecast15Minutes?longitude=" + longitude + "&latitude=" + latitude + "&start_date=" + day + "&end_date=" + day14 + "&timezone=Asia/Shanghai&minutely_15=" + fieldNames + ",&apikey=FG9yxRdv14HTwYOl";
|
|
|
+ String body = HttpUtil.createGet(url).execute().charset("utf-8").body();
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
+ JSONObject result = JSONUtil.parseObj(jsonObject.get("minutely_15"));
|
|
|
+ // 将 JSONObject 转换为 Map
|
|
|
+ Map<String, Object> map = result.toBean(LinkedHashMap.class);
|
|
|
+ //map里的key都是BaiDu实体类的属性 怎么整合转换成一个list
|
|
|
+ // 创建一个 List 来存储 BaiDu 实例
|
|
|
+ List<BaiDu> baiduList = new ArrayList<>();
|
|
|
+ // 遍历 Map 的条目
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
+ // 获取当前属性名
|
|
|
+ String propertyName = entry.getKey();
|
|
|
+
|
|
|
+ // 获取当前属性的值列表
|
|
|
+ List<String> propertyValues = (List<String>) entry.getValue();
|
|
|
+
|
|
|
+ // 确保所有属性都有相同的值数量
|
|
|
+ if (baiduList.isEmpty()) {
|
|
|
+ // 第一次遍历时初始化列表大小
|
|
|
+ for (int i = 0; i < propertyValues.size(); i++) {
|
|
|
+ baiduList.add(new BaiDu());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 验证当前属性值的数量与之前的一致
|
|
|
+ if (propertyValues.size() != baiduList.size()) {
|
|
|
+ throw new IllegalArgumentException("Inconsistent number of values for property: " + propertyName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ extracted(propertyValues, propertyName, baiduList);
|
|
|
+ }
|
|
|
+ if (!baiduList.isEmpty()) {
|
|
|
+ String sql = "INSERT INTO baidu (" +
|
|
|
+ " time," +
|
|
|
+ " temperature_2m," +
|
|
|
+ " relative_humidity_2m," +
|
|
|
+ " dew_point_2m," +
|
|
|
+ " apparent_temperature," +
|
|
|
+ " precipitation_probability," +
|
|
|
+ " precipitation," +
|
|
|
+ " rain," +
|
|
|
+ " showers," +
|
|
|
+ " snowfall," +
|
|
|
+ " snow_depth," +
|
|
|
+ " weather_code," +
|
|
|
+ " pressure_msl," +
|
|
|
+ " surface_pressure," +
|
|
|
+ " cloud_cover," +
|
|
|
+ " cloud_cover_low," +
|
|
|
+ " cloud_cover_mid," +
|
|
|
+ " cloud_cover_high," +
|
|
|
+ " visibility," +
|
|
|
+ " evapotranspiration," +
|
|
|
+ " et0_fao_evapotranspiration," +
|
|
|
+ " vapour_pressure_deficit," +
|
|
|
+ " wind_speed_10m," +
|
|
|
+ " wind_speed_80m," +
|
|
|
+ " wind_speed_120m," +
|
|
|
+ " wind_speed_180m," +
|
|
|
+ " wind_direction_10m," +
|
|
|
+ " wind_direction_80m," +
|
|
|
+ " wind_direction_120m," +
|
|
|
+ " wind_direction_180m," +
|
|
|
+ " wind_gusts_10m," +
|
|
|
+ " temperature_80m," +
|
|
|
+ " temperature_120m," +
|
|
|
+ " temperature_180m," +
|
|
|
+ " soil_temperature_0cm," +
|
|
|
+ " soil_temperature_6cm," +
|
|
|
+ " soil_temperature_18cm," +
|
|
|
+ " soil_temperature_54cm," +
|
|
|
+ " soil_moisture_0_to_1cm," +
|
|
|
+ " soil_moisture_1_to_3cm," +
|
|
|
+ " soil_moisture_3_to_9cm," +
|
|
|
+ " soil_moisture_9_to_27cm," +
|
|
|
+ " soil_moisture_27_to_81cm," +
|
|
|
+ " uv_index," +
|
|
|
+ " uv_index_clear_sky," +
|
|
|
+ " is_day," +
|
|
|
+ " cape," +
|
|
|
+ " freezing_level_height," +
|
|
|
+ " sunshine_duration," +
|
|
|
+ " shortwave_radiation," +
|
|
|
+ " direct_radiation," +
|
|
|
+ " diffuse_radiation," +
|
|
|
+ " direct_normal_irradiance," +
|
|
|
+ " global_tilted_irradiance," +
|
|
|
+ " terrestrial_radiation," +
|
|
|
+ " shortwave_radiation_instant," +
|
|
|
+ " direct_radiation_instant," +
|
|
|
+ " diffuse_radiation_instant," +
|
|
|
+ " direct_normal_irradiance_instant," +
|
|
|
+ " global_tilted_irradiance_instant," +
|
|
|
+ " terrestrial_radiation_instant," +
|
|
|
+ " station_code," +
|
|
|
+ " insert_time" +
|
|
|
+ ")" +
|
|
|
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
|
|
+
|
|
|
+ try {
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
|
+ conn.setAutoCommit(false);
|
|
|
+ log.info("百度天气:共{}条记录", baiduList.size());
|
|
|
+ for (BaiDu b : baiduList) {
|
|
|
+ // 设置参数
|
|
|
+ pstmt.setString(1, b.getTime());
|
|
|
+ pstmt.setString(2, b.getTemperature_2m());
|
|
|
+ pstmt.setString(3, b.getRelative_humidity_2m());
|
|
|
+ pstmt.setString(4, b.getDew_point_2m());
|
|
|
+ pstmt.setString(5, b.getApparent_temperature());
|
|
|
+ pstmt.setString(6, b.getPrecipitation_probability());
|
|
|
+ pstmt.setString(7, b.getPrecipitation());
|
|
|
+ pstmt.setString(8, b.getRain());
|
|
|
+ pstmt.setString(9, b.getShowers());
|
|
|
+ pstmt.setString(10, b.getSnowfall());
|
|
|
+ pstmt.setString(11, b.getSnow_depth());
|
|
|
+ pstmt.setString(12, b.getWeather_code());
|
|
|
+ pstmt.setString(13, b.getPressure_msl());
|
|
|
+ pstmt.setString(14, b.getSurface_pressure());
|
|
|
+ pstmt.setString(15, b.getCloud_cover());
|
|
|
+ pstmt.setString(16, b.getCloud_cover_low());
|
|
|
+ pstmt.setString(17, b.getCloud_cover_mid());
|
|
|
+ pstmt.setString(18, b.getCloud_cover_high());
|
|
|
+ pstmt.setString(19, b.getVisibility());
|
|
|
+ pstmt.setString(20, b.getEvapotranspiration());
|
|
|
+ pstmt.setString(21, b.getEt0_fao_evapotranspiration());
|
|
|
+ pstmt.setString(22, b.getVapour_pressure_deficit());
|
|
|
+ pstmt.setString(23, b.getWind_speed_10m());
|
|
|
+ pstmt.setString(24, b.getWind_speed_80m());
|
|
|
+ pstmt.setString(25, b.getWind_speed_120m());
|
|
|
+ pstmt.setString(26, b.getWind_speed_180m());
|
|
|
+ pstmt.setString(27, b.getWind_direction_10m());
|
|
|
+ pstmt.setString(28, b.getWind_direction_80m());
|
|
|
+ pstmt.setString(29, b.getWind_direction_120m());
|
|
|
+ pstmt.setString(30, b.getWind_direction_180m());
|
|
|
+ pstmt.setString(31, b.getWind_gusts_10m());
|
|
|
+ pstmt.setString(32, b.getTemperature_80m());
|
|
|
+ pstmt.setString(33, b.getTemperature_120m());
|
|
|
+ pstmt.setString(34, b.getTemperature_180m());
|
|
|
+ pstmt.setString(35, b.getSoil_temperature_0cm());
|
|
|
+ pstmt.setString(36, b.getSoil_temperature_6cm());
|
|
|
+ pstmt.setString(37, b.getSoil_temperature_18cm());
|
|
|
+ pstmt.setString(38, b.getSoil_temperature_54cm());
|
|
|
+ pstmt.setString(39, b.getSoil_moisture_0_to_1cm());
|
|
|
+ pstmt.setString(40, b.getSoil_moisture_1_to_3cm());
|
|
|
+ pstmt.setString(41, b.getSoil_moisture_3_to_9cm());
|
|
|
+ pstmt.setString(42, b.getSoil_moisture_9_to_27cm());
|
|
|
+ pstmt.setString(43, b.getSoil_moisture_27_to_81cm());
|
|
|
+ pstmt.setString(44, b.getUv_index());
|
|
|
+ pstmt.setString(45, b.getUv_index_clear_sky());
|
|
|
+ pstmt.setString(46, b.getIs_day());
|
|
|
+ pstmt.setString(47, b.getCape());
|
|
|
+ pstmt.setString(48, b.getFreezing_level_height());
|
|
|
+ pstmt.setString(49, b.getSunshine_duration());
|
|
|
+ pstmt.setString(50, b.getShortwave_radiation());
|
|
|
+ pstmt.setString(51, b.getDirect_radiation());
|
|
|
+ pstmt.setString(52, b.getDiffuse_radiation());
|
|
|
+ pstmt.setString(53, b.getDirect_normal_irradiance());
|
|
|
+ pstmt.setString(54, b.getGlobal_tilted_irradiance());
|
|
|
+ pstmt.setString(55, b.getTerrestrial_radiation());
|
|
|
+ pstmt.setString(56, b.getShortwave_radiation_instant());
|
|
|
+ pstmt.setString(57, b.getDirect_radiation_instant());
|
|
|
+ pstmt.setString(58, b.getDiffuse_radiation_instant());
|
|
|
+ pstmt.setString(59, b.getDirect_normal_irradiance_instant());
|
|
|
+ pstmt.setString(60, b.getGlobal_tilted_irradiance_instant());
|
|
|
+ pstmt.setString(61, b.getTerrestrial_radiation_instant());
|
|
|
+ pstmt.setString(62, stationCode);
|
|
|
+ pstmt.setString(63, day0);
|
|
|
+ // 添加当前记录到批处理中
|
|
|
+ pstmt.addBatch();
|
|
|
+ }
|
|
|
+ pstmt.executeBatch();
|
|
|
+ conn.commit();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("场站编号:{} ,{}下载:{}-{} 百度天气预测数据下载完成", stationCode, day0, day, day14);
|
|
|
+ }
|
|
|
}
|