|
@@ -92,4 +92,88 @@ public class CaiYunDownload {
|
|
}
|
|
}
|
|
log.info("场站编号:{} 彩云天气数据下载完成", stationCode);
|
|
log.info("场站编号:{} 彩云天气数据下载完成", stationCode);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public void downloadHour(Connection conn, String stationCode, String longitude, String latitude) {
|
|
|
|
+ //彩云天气api接口
|
|
|
|
+ String url = "https://api.caiyunapp.com/v2.6/SRQIFijX5MGtdmhe/" + longitude + "," + latitude + "/hourly?hourlysteps=1 ";
|
|
|
|
+ try {
|
|
|
|
+ String body = HttpUtil.createGet(url).execute().charset("utf-8").body();
|
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
|
+ JSONObject result = JSONUtil.parseObj(jsonObject.get("result"));
|
|
|
|
+ JSONObject hourly = JSONUtil.parseObj(result.get("hourly"));
|
|
|
|
+ // 降水数据
|
|
|
|
+ JSONArray precipitation = JSONUtil.parseArray(hourly.get("precipitation"));
|
|
|
|
+ // 地表 2 米气温
|
|
|
|
+ JSONArray temperature = JSONUtil.parseArray(hourly.get("temperature"));
|
|
|
|
+ // 地表水平能见度
|
|
|
|
+ JSONArray visibility = JSONUtil.parseArray(hourly.get("visibility"));
|
|
|
|
+ // 地面气压
|
|
|
|
+ JSONArray pressure = JSONUtil.parseArray(hourly.get("pressure"));
|
|
|
|
+ // 地表 2 米相对湿度
|
|
|
|
+ JSONArray humidity = JSONUtil.parseArray(hourly.get("humidity"));
|
|
|
|
+ // 向下短波辐射通量
|
|
|
|
+ JSONArray dswrf = JSONUtil.parseArray(hourly.get("dswrf"));
|
|
|
|
+ // 地表 10 米风向和风速
|
|
|
|
+ JSONArray wind = JSONUtil.parseArray(hourly.get("wind"));
|
|
|
|
+ List<CaiYun> list = new ArrayList<>();
|
|
|
|
+ if (!visibility.isEmpty()) {
|
|
|
|
+ for (int i = 0; i < 1; i++) {
|
|
|
|
+ CaiYun caiYun = new CaiYun();
|
|
|
|
+ Map vis = (Map) visibility.get(i);
|
|
|
|
+ Map temp = (Map) temperature.get(i);
|
|
|
|
+ Map pre = (Map) pressure.get(i);
|
|
|
|
+ Map hum = (Map) humidity.get(i);
|
|
|
|
+ Map ds = (Map) dswrf.get(i);
|
|
|
|
+ Map w = (Map) wind.get(i);
|
|
|
|
+ Map p = (Map) precipitation.get(i);
|
|
|
|
+ caiYun.setVisibility(vis.get("value").toString());
|
|
|
|
+ caiYun.setDatetime(vis.get("datetime").toString());
|
|
|
|
+ caiYun.setTemperature(temp.get("value").toString());
|
|
|
|
+ caiYun.setPressure(pre.get("value").toString());
|
|
|
|
+ caiYun.setHumidity(hum.get("value").toString());
|
|
|
|
+ caiYun.setDswrf(ds.get("value").toString());
|
|
|
|
+ caiYun.setDirection(w.get("direction").toString());
|
|
|
|
+ caiYun.setPrecipitation(p.get("value").toString());
|
|
|
|
+ caiYun.setSpeed(new BigDecimal(String.valueOf(w.get("speed"))).divide(new BigDecimal("3.6"), 2, RoundingMode.HALF_UP).toString());
|
|
|
|
+ list.add(caiYun);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!list.isEmpty()) {
|
|
|
|
+ String tableSql = "INSERT ignore INTO `caiyun_hour` ( `stationCode`, `longitude`, `latitude`, `datetime`, `temperature`, `pressure`, `humidity`, `direction`, `speed`, `dswrf`, `visibility`,`precipitation`) " +
|
|
|
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
|
|
|
|
+ try {
|
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(tableSql);
|
|
|
|
+ conn.setAutoCommit(false);
|
|
|
|
+ log.info("彩云天气小时数据:共{}条记录", list.size());
|
|
|
|
+ list.forEach(item -> {
|
|
|
|
+ try {
|
|
|
|
+ pstmt.setString(1, stationCode);
|
|
|
|
+ pstmt.setString(2, longitude);
|
|
|
|
+ pstmt.setString(3, latitude);
|
|
|
|
+ pstmt.setString(4, item.getDatetime());
|
|
|
|
+ pstmt.setString(5, item.getTemperature());
|
|
|
|
+ pstmt.setString(6, item.getPressure());
|
|
|
|
+ pstmt.setString(7, item.getHumidity());
|
|
|
|
+ pstmt.setString(8, item.getDirection());
|
|
|
|
+ pstmt.setString(9, item.getSpeed());
|
|
|
|
+ pstmt.setString(10, item.getDswrf());
|
|
|
|
+ pstmt.setString(11, item.getVisibility());
|
|
|
|
+ pstmt.setString(12, item.getPrecipitation());
|
|
|
|
+ pstmt.addBatch();
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ pstmt.executeBatch();
|
|
|
|
+ conn.commit();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (RuntimeException e) {
|
|
|
|
+// throw new RuntimeException(e);
|
|
|
|
+ log.error("场站编号:{} 彩云天气小时数据下载失败JSON格式化错误", stationCode);
|
|
|
|
+ }
|
|
|
|
+ log.info("场站编号:{} 彩云天气小时数据下载完成", stationCode);
|
|
|
|
+ }
|
|
}
|
|
}
|