Parcourir la source

新增彩云天气小时数据接入

xiaowang il y a 7 mois
Parent
commit
f0e4014b97

+ 2 - 0
src/main/java/com/example/mdd/entity/CaiYun.java

@@ -27,4 +27,6 @@ public class CaiYun {
     String dswrf;
     //地表水平能见度
     String visibility;
+    //降水数据
+    String precipitation;
 }

+ 25 - 0
src/main/java/com/example/mdd/job/DownloadHour.java

@@ -0,0 +1,25 @@
+package com.example.mdd.job;
+
+import com.example.mdd.service.MeteorologicalDataDownload;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+
+@Slf4j
+@Service
+@EnableScheduling
+public class DownloadHour {
+    @Resource
+    MeteorologicalDataDownload md;
+
+    //每小时下载数据
+    //@Scheduled(fixedDelay = 1000)
+    @Scheduled(cron = "0 0 0/1 * * ?")
+    public void download() {
+        md.downloadHour();
+    }
+}

+ 84 - 0
src/main/java/com/example/mdd/service/CaiYunDownload.java

@@ -92,4 +92,88 @@ public class CaiYunDownload {
         }
         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);
+    }
 }

+ 35 - 0
src/main/java/com/example/mdd/service/MeteorologicalDataDownload.java

@@ -1,6 +1,8 @@
 package com.example.mdd.service;
 
+import cn.hutool.db.Db;
 import cn.hutool.db.DbUtil;
+import cn.hutool.db.Entity;
 import cn.hutool.db.ds.simple.SimpleDataSource;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONArray;
@@ -16,6 +18,7 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
 @Service
 @Slf4j
@@ -59,4 +62,36 @@ public class MeteorologicalDataDownload {
 
         }
     }
+
+    public void downloadHour() {
+        String body = HttpUtil.get("http://itil.jiayuepowertech.com:9958/itil/api/power-station?paramStationState=01,10,20", 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();
+                List<Entity> saveList = Db.use().findAll("save_station");
+                log.info("开始下载彩云天气小时数据,需要下载的场站数量为" + saveList.size());
+                for (Entity entity : saveList) {
+                    String stationCode = entity.getStr("station_code");
+                    List<PowerStation> collect = list.stream().filter(item -> item.getStationCode().equals(stationCode)).collect(Collectors.toList());
+                    if (!collect.isEmpty()) {
+                        if (!"".equals(collect.get(0).getLongitude()) && !"".equals(collect.get(0).getLatitude())) {
+                            caiYunDownload.downloadHour(conn, collect.get(0).getStationCode(), collect.get(0).getLongitude(), collect.get(0).getLatitude());
+                        }
+                    }
+                }
+                log.info("彩云天气小时数据下载完成");
+            } catch (SQLException e) {
+                throw new RuntimeException(e);
+            }
+
+        }
+    }
 }