From 7bce063c7906829ede2d55772293253414e31025 Mon Sep 17 00:00:00 2001 From: wenfei Date: Sat, 21 Jun 2025 16:07:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=B0=E9=A3=8E=E8=AF=A6=E6=83=85=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ZjsltStormController.java | 13 +- .../jpahefengweather/dto/TyphoonInfoDto.java | 393 ++++++++++++++++++ .../ScheduledHefengWeatherService.java | 6 +- .../service/ScheduledZjsltStormService.java | 154 ++++++- 4 files changed, 544 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/rehome/jpahefengweather/dto/TyphoonInfoDto.java diff --git a/src/main/java/com/rehome/jpahefengweather/controller/ZjsltStormController.java b/src/main/java/com/rehome/jpahefengweather/controller/ZjsltStormController.java index e816c06..a46919f 100644 --- a/src/main/java/com/rehome/jpahefengweather/controller/ZjsltStormController.java +++ b/src/main/java/com/rehome/jpahefengweather/controller/ZjsltStormController.java @@ -1,10 +1,13 @@ package com.rehome.jpahefengweather.controller; +import com.google.gson.Gson; +import com.rehome.jpahefengweather.dto.TyphoonInfoDto; import com.rehome.jpahefengweather.entity.TyphoonInfo; import com.rehome.jpahefengweather.entity.ZjsltStorm; import com.rehome.jpahefengweather.service.ZjsltStormService; import jakarta.annotation.Resource; +import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.*; import java.text.SimpleDateFormat; import java.util.Date; @@ -56,7 +59,13 @@ public class ZjsltStormController { @CrossOrigin @ResponseBody @RequestMapping(value = "/getTyhoonInfo",method = RequestMethod.GET, produces = "application/json;charset=UTF-8") - public TyphoonInfo getTyhoonInfo(@RequestParam("tfid") String tfid) { - return this.zjsltStormService.findTyphoonInfoByTfid(tfid); + public TyphoonInfoDto getTyhoonInfo(@RequestParam("tfid") String tfid) { + Gson gson = new Gson(); + TyphoonInfo typhoonInfo = this.zjsltStormService.findTyphoonInfoByTfid(tfid); + if(!ObjectUtils.isEmpty(typhoonInfo)){ + TyphoonInfoDto typhoonInfoDto = gson.fromJson(typhoonInfo.getStormData(),TyphoonInfoDto.class); + return typhoonInfoDto; + } + return null; } } \ No newline at end of file diff --git a/src/main/java/com/rehome/jpahefengweather/dto/TyphoonInfoDto.java b/src/main/java/com/rehome/jpahefengweather/dto/TyphoonInfoDto.java new file mode 100644 index 0000000..f60b1f8 --- /dev/null +++ b/src/main/java/com/rehome/jpahefengweather/dto/TyphoonInfoDto.java @@ -0,0 +1,393 @@ +package com.rehome.jpahefengweather.dto; + +import java.util.List; + +/** + * @author huangwenfei + * @version v1.0.0.0 + * Created DateTime 2021-04-26 14:35 + * @description: 浙江省水利厅 台风台风详情 + */ +public class TyphoonInfoDto { + private String tfid;//台风编号 + private String name;//台风中文名称 + private String enname;//台风英文名称 + private String isactive;//台风是否处于活跃状态,1:活跃,2:不活跃 + private String starttime;//台风开始时间 + private String endtime;//台风结束时间 + private String warnlevel;//台风预警等级 + private String centerlng;//台风中心经度 + private String centerlat;//台风中心纬度 + private List land;//台风登录地点列表,活跃状态可能为空,没有登录也可能为空 + private List points;//台风路径 + + public String getTfid() { + return tfid; + } + + public void setTfid(String tfid) { + this.tfid = tfid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEnname() { + return enname; + } + + public void setEnname(String enname) { + this.enname = enname; + } + + public String getIsactive() { + return isactive; + } + + public void setIsactive(String isactive) { + this.isactive = isactive; + } + + public String getStarttime() { + return starttime; + } + + public void setStarttime(String starttime) { + this.starttime = starttime; + } + + public String getEndtime() { + return endtime; + } + + public void setEndtime(String endtime) { + this.endtime = endtime; + } + + public String getWarnlevel() { + return warnlevel; + } + + public void setWarnlevel(String warnlevel) { + this.warnlevel = warnlevel; + } + + public String getCenterlng() { + return centerlng; + } + + public void setCenterlng(String centerlng) { + this.centerlng = centerlng; + } + + public String getCenterlat() { + return centerlat; + } + + public void setCenterlat(String centerlat) { + this.centerlat = centerlat; + } + + public List getLand() { + return land; + } + + public void setLand(List land) { + this.land = land; + } + + public List getPoints() { + return points; + } + + public void setPoints(List points) { + this.points = points; + } + + public static class Land{ + private String landaddress;//台风登录地址 + private String landtime;//台风登录时间 + private String lng;//台风登录经度 + private String lat;//台风登录纬度 + private String info;//台风登录描述 + private String strong;//台风等级中文描述 + + public String getLandaddress() { + return landaddress; + } + + public void setLandaddress(String landaddress) { + this.landaddress = landaddress; + } + + public String getLandtime() { + return landtime; + } + + public void setLandtime(String landtime) { + this.landtime = landtime; + } + + public String getLng() { + return lng; + } + + public void setLng(String lng) { + this.lng = lng; + } + + public String getLat() { + return lat; + } + + public void setLat(String lat) { + this.lat = lat; + } + + public String getInfo() { + return info; + } + + public void setInfo(String info) { + this.info = info; + } + + public String getStrong() { + return strong; + } + + public void setStrong(String strong) { + this.strong = strong; + } + } + + public static class Point{ + private String time;//台风中心点记录时间 + private String lng;//台风中心经茺 + private String lat;//台风中心续度 + private String strong;//台风等级中文描述 + private String power;//台风等级,例如 "7" 表示7级 + private String speed;//台风风速,单位 米/秒 + private String pressure;//台风中心气压,单位:帕 + private String movespeed;//台风移动速度,单位 公里/小时 + private String movedirection;//台风移动方向中文描述 + private String radius7;//台风7级风圈,单位 公里 + private String radius10;//台风10级风圈,单位 公里 + private String radius12;//台风12级风圈,单位 公里 + private List forecast;//未来台风路径预测 + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getLng() { + return lng; + } + + public void setLng(String lng) { + this.lng = lng; + } + + public String getLat() { + return lat; + } + + public void setLat(String lat) { + this.lat = lat; + } + + public String getStrong() { + return strong; + } + + public void setStrong(String strong) { + this.strong = strong; + } + + public String getPower() { + return power; + } + + public void setPower(String power) { + this.power = power; + } + + public String getSpeed() { + return speed; + } + + public void setSpeed(String speed) { + this.speed = speed; + } + + public String getPressure() { + return pressure; + } + + public void setPressure(String pressure) { + this.pressure = pressure; + } + + public String getMovespeed() { + return movespeed; + } + + public void setMovespeed(String movespeed) { + this.movespeed = movespeed; + } + + public String getMovedirection() { + return movedirection; + } + + public void setMovedirection(String movedirection) { + this.movedirection = movedirection; + } + + public String getRadius7() { + return radius7; + } + + public void setRadius7(String radius7) { + this.radius7 = radius7; + } + + public String getRadius10() { + return radius10; + } + + public void setRadius10(String radius10) { + this.radius10 = radius10; + } + + public String getRadius12() { + return radius12; + } + + public void setRadius12(String radius12) { + this.radius12 = radius12; + } + + public List getForecast() { + return forecast; + } + + public void setForecast(List forecast) { + this.forecast = forecast; + } + } + + public static class Forecast{ + private String tm;//台风预测国家 + private List forecastpoints;//未来台风路径预测 + + public String getTm() { + return tm; + } + + public void setTm(String tm) { + this.tm = tm; + } + + public List getForecastpoints() { + return forecastpoints; + } + + public void setForecastpoints(List forecastpoints) { + this.forecastpoints = forecastpoints; + } + } + + public static class Forecastpoint{ + private String time;//台风路线预测中心点时间 + private String lng;//台风路线预测中心点经茺 + private String lat;//台风路线预测中心点续度 + private String strong;//台风路线预测中心点等级中文描述 + private String power;//台风路线预测中心点等级,例如 "7" 表示7级 + private String speed;//台风路线预测中心点风速,单位 米/秒 + private String pressure;//台风路线预测中心点气压,单位:帕 + private String tm;//台风预测国家 + private String ybsj;//台风预报时间 + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getLng() { + return lng; + } + + public void setLng(String lng) { + this.lng = lng; + } + + public String getLat() { + return lat; + } + + public void setLat(String lat) { + this.lat = lat; + } + + public String getStrong() { + return strong; + } + + public void setStrong(String strong) { + this.strong = strong; + } + + public String getPower() { + return power; + } + + public void setPower(String power) { + this.power = power; + } + + public String getSpeed() { + return speed; + } + + public void setSpeed(String speed) { + this.speed = speed; + } + + public String getPressure() { + return pressure; + } + + public void setPressure(String pressure) { + this.pressure = pressure; + } + + public String getTm() { + return tm; + } + + public void setTm(String tm) { + this.tm = tm; + } + + public String getYbsj() { + return ybsj; + } + + public void setYbsj(String ybsj) { + this.ybsj = ybsj; + } + } +} diff --git a/src/main/java/com/rehome/jpahefengweather/service/ScheduledHefengWeatherService.java b/src/main/java/com/rehome/jpahefengweather/service/ScheduledHefengWeatherService.java index dd03c40..1f32bd6 100644 --- a/src/main/java/com/rehome/jpahefengweather/service/ScheduledHefengWeatherService.java +++ b/src/main/java/com/rehome/jpahefengweather/service/ScheduledHefengWeatherService.java @@ -53,7 +53,7 @@ public class ScheduledHefengWeatherService { * //天气图标用法: https://icons.qweather.com/usage/ * @Param: null */ - @Scheduled(cron = "0 44 17 * * *")//每个小时执行一次 + //@Scheduled(cron = "0 44 17 * * *")//每个小时执行一次 public void scheduled() { log.info("scheduled"); log.info("=====>>>>>使用cron:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); @@ -125,7 +125,7 @@ public class ScheduledHefengWeatherService { */ //@Scheduled(cron = "0 0 10,14,18,20,22 * * *")//每天10,14,18,20,22点各执行一次,一天总共执行五次 //@Scheduled(cron = "0 0 0,8,12,16 * * *")//每天0,8,12,16点各执行一次,一天总共执行四次 - //@Scheduled(cron = "0 15 * * * *")//每个小时执行一次 + @Scheduled(cron = "0 15 * * * *")//每个小时执行一次 public void scheduledGetRealtimeWeather() { log.info("scheduledGetRealtimeWeather"); log.info("=====>>>>>使用cron:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); @@ -141,7 +141,7 @@ public class ScheduledHefengWeatherService { * @Param: null */ //@Scheduled(cron = "0 0 9 * * *")//每天9点执行一次 - //@Scheduled(cron = "0 15 * * * *")//每个小时执行一次 + @Scheduled(cron = "0 15 * * * *")//每个小时执行一次 public void scheduledGetForecastWeather() { log.info("scheduledGetForecastWeather"); log.info("=====>>>>>使用cron:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); diff --git a/src/main/java/com/rehome/jpahefengweather/service/ScheduledZjsltStormService.java b/src/main/java/com/rehome/jpahefengweather/service/ScheduledZjsltStormService.java index 8892cdf..92c2932 100644 --- a/src/main/java/com/rehome/jpahefengweather/service/ScheduledZjsltStormService.java +++ b/src/main/java/com/rehome/jpahefengweather/service/ScheduledZjsltStormService.java @@ -3,18 +3,22 @@ package com.rehome.jpahefengweather.service; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.rehome.jpahefengweather.dto.NmcBaseDto; +import com.rehome.jpahefengweather.dto.TyphoonInfoDto; import com.rehome.jpahefengweather.entity.*; import com.rehome.jpahefengweather.service.excel.*; import com.rehome.jpahefengweather.utils.JwtUtils; import com.rehome.jpahefengweather.utils.OkHttpUtil; import jakarta.annotation.Resource; import okhttp3.*; +import org.aspectj.util.FileUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; + import java.io.File; import java.io.IOException; import java.lang.reflect.Type; @@ -41,19 +45,20 @@ public class ScheduledZjsltStormService { * @description: 从浙江省水利厅获取历史台风列表数据和对应的台风详情 * @Param: null */ - @Scheduled(cron = "0 45 17 * * *") - public void getZjsltHistoryStormList() { - for(int i=1944;i<2026;i++){ - getZjsltStormList(String.valueOf(i)); - } - } +// @Scheduled(cron = "0 32 11 * * *") +// public void getZjsltHistoryStormList() { +// for(int i=1944;i<2026;i++){ +// getZjsltStormList(String.valueOf(i)); +// } +// } + /** * @date 2022-03-16 09:41 * @description: 从浙江省水利厅获取台风列表 * @Param: null */ - //@Scheduled(cron = "0 23 * * * *") - public void getZjsltStormList(String nowYear) { + @Scheduled(cron = "0 15 * * * *") + public void getZjsltStormList() { log.info("getZjsltStormList"); String strNow = String.valueOf(System.currentTimeMillis()); @@ -68,7 +73,7 @@ public class ScheduledZjsltStormService { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy"); String nowDate = sdf.format(new Date()); - //String nowYear = sdfYear.format(new Date()); + String nowYear = sdfYear.format(new Date()); Random random = new Random();//以系统自身时间为种子数 int iRandom = random.nextInt(); @@ -107,7 +112,7 @@ public class ScheduledZjsltStormService { if (stormDB == null) { //数据库无数据,第一次插入,同时从第三方平台拉取台风路径数据入库 this.zjsltStormService.saveZjsltStorm(storm); - this.saveOrUpdateTyphoonInfo(storm); + this.saveOrUpdateTyphoonInfoByHistorySearch(storm); } else { //数据库有数据 if (stormDB.getIsactive().equals("1")) { @@ -115,7 +120,7 @@ public class ScheduledZjsltStormService { //数据库里面台风数据是活跃状态,第三方平台台风数据已变成不活跃状态,更新数据库台风状态,同时更新台风路径 stormDB.setIsactive(storm.getIsactive()); this.zjsltStormService.saveZjsltStorm(stormDB); - this.saveOrUpdateTyphoonInfo(stormDB); + this.saveOrUpdateTyphoonInfoByHistorySearch(stormDB); } else { //数据库里面台风数据是活跃状态,第三方平台台风数据也是活跃状态 if ((!storm.getEndtime().equals(stormDB.getEndtime())) || (!storm.getEnname().equals(stormDB.getEnname())) || (!storm.getName().equals(stormDB.getName()))) { @@ -126,14 +131,18 @@ public class ScheduledZjsltStormService { this.zjsltStormService.saveZjsltStorm(stormDB); } //更新台风路径 - this.saveOrUpdateTyphoonInfo(stormDB); + this.saveOrUpdateTyphoonInfoByHistorySearch(stormDB); } } else { //数据库里面台风数据是不活跃状态,无需做任何事情 - //this.saveOrUpdateTyphoonInfo(storm); + this.saveOrUpdateTyphoonInfoByHistorySearch(storm); } } } + } else { +// ZjsltStorm storm = new ZjsltStorm(); +// storm.setTfid("202501"); +// this.saveOrUpdateTyphoonInfoByHistorySearch(storm); } } } @@ -186,14 +195,25 @@ public class ScheduledZjsltStormService { // log.info(body.indexOf("[")); // log.info(body.lastIndexOf("]")); String typhoonInfoArryStr = body.substring(startIndex, endIndex + 1); - //log.info(typhoonInfoArryStr); if (typhoonInfoArryStr != null && typhoonInfoArryStr.length() > 2) { int startIndex0 = typhoonInfoArryStr.indexOf("{"); int endIndex0 = typhoonInfoArryStr.lastIndexOf("}"); // log.info(startIndex0); // log.info(endIndex0); String typhoonInfoStr = typhoonInfoArryStr.substring(startIndex0, endIndex0 + 1); - //log.info(typhoonInfoStr); + log.info(typhoonInfoStr); + + log.info("--------浙江省水利厅台风详情---------"); + log.info(typhoonInfoArryStr); + try { + String currentSecond = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); + String typhoonInfoFileName = "typhoonInfo_" + currentSecond + ".json"; + String bodyFile = "typhoonInfo_" + currentSecond + "_body.json"; + FileUtil.writeAsString(new File("/Users/admin/typhoonInfo/" + typhoonInfoFileName), typhoonInfoStr); + FileUtil.writeAsString(new File("/Users/admin/typhoonInfo/" + bodyFile), body); + } catch (Exception e) { + e.printStackTrace(); + } TyphoonInfo typhoonInfoDB = this.zjsltStormService.findTyphoonInfoByTfid(storm.getTfid()); TyphoonInfo typhoonInfo = new TyphoonInfo(); @@ -203,12 +223,12 @@ public class ScheduledZjsltStormService { typhoonInfo.setStormData(typhoonInfoStr); typhoonInfo.setCreateDate(new Date()); typhoonInfo.setLastUpdateDate(new Date()); - this.zjsltStormService.saveTyphoonInfo(typhoonInfo); + //this.zjsltStormService.saveTyphoonInfo(typhoonInfo); } else { //数据库有数据,更新台风路径数据 typhoonInfoDB.setStormData(typhoonInfoStr); typhoonInfoDB.setLastUpdateDate(new Date()); - this.zjsltStormService.saveTyphoonInfo(typhoonInfoDB); + //this.zjsltStormService.saveTyphoonInfo(typhoonInfoDB); } } } @@ -218,6 +238,88 @@ public class ScheduledZjsltStormService { } } + /** + * @date 2022-03-16 09:41 + * @description: 从浙江省水利厅获取台风详情数据,保存或更新台风路径 + * @Param: null + */ + public void saveOrUpdateTyphoonInfoByHistorySearch(ZjsltStorm storm) { + log.info("saveOrUpdateTyphoonInfoByHistorySearch"); + String strNow = String.valueOf(System.currentTimeMillis()); + Gson gson = new Gson(); + + // 初始化 OkHttpClient + OkHttpClient client = new OkHttpClient.Builder() + .connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .build(); + + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy"); + String nowDate = sdf.format(new Date()); + String nowYear = sdfYear.format(new Date()); + Random random = new Random();//以系统自身时间为种子数 + int iRandom = random.nextInt(); + Long nowLong = 25528353350362976L; + Long nowLongTemp = nowLong + iRandom; + String url = "https://typhoon.slt.zj.gov.cn/Api/TyphoonInfo/" + storm.getTfid()+ "?callback=jQuery" + "1830" + String.valueOf(nowLongTemp) + "_" + strNow + "&_=" + String.valueOf(System.currentTimeMillis()); + log.info(url); + String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"; + // 初始化请求体 + Request request = new Request.Builder() + .get() + .url(url) + .addHeader("User-Agent", userAgent) + .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7") + .addHeader("Accept-language", "zh-CN,zh;q=0.9") + .addHeader("Connection", "keep-alive") + .build(); +// log.info("---------OkHttpClient Request Header-----------------"); +// log.info(gson.toJson(request.headers())); + + // 得到返回Response + Response response = client.newCall(request).execute(); + String body = response.body().string(); + //log.info(body); + if (StringUtils.hasText(body)) { + if (body != null && body.length() > 2) { + log.info("--------浙江省水利厅台风详情---------"); + log.info(body); + + try { + String currentSecond = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); + String typhoonInfoFileName = "typhoonInfo_" + currentSecond + ".json"; + String bodyFile = "typhoonInfo_" + currentSecond + "_body.json"; + FileUtil.writeAsString(new File("/Users/admin/typhoonInfo/" + typhoonInfoFileName), body); + FileUtil.writeAsString(new File("/Users/admin/typhoonInfo/" + bodyFile), body); + } catch (Exception e) { + e.printStackTrace(); + } + + TyphoonInfo typhoonInfoDB = this.zjsltStormService.findTyphoonInfoByTfid(storm.getTfid()); + TyphoonInfo typhoonInfo = new TyphoonInfo(); + if (typhoonInfoDB == null) { + //数据库无数据,保存风路径数据入库 + typhoonInfo.setTfid(storm.getTfid()); + typhoonInfo.setStormData(body); + typhoonInfo.setCreateDate(new Date()); + typhoonInfo.setLastUpdateDate(new Date()); + this.zjsltStormService.saveTyphoonInfo(typhoonInfo); + } else { + //数据库有数据,更新台风路径数据 + typhoonInfoDB.setStormData(body); + typhoonInfoDB.setLastUpdateDate(new Date()); + this.zjsltStormService.saveTyphoonInfo(typhoonInfoDB); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + /** * @date 2022-03-16 09:41 * @description: 从本地数据库获取活跃台风列表 @@ -248,4 +350,22 @@ public class ScheduledZjsltStormService { log.info(String.valueOf(list.size())); log.info(new Gson().toJson(list)); } + + /** + * @date 2026-06-21 15:41 + * @description: 根据年份查台风列表 + * @Param: null + */ + //@Scheduled(cron = "0 0/1 * * * *") + public void getTyphoonInfoDto() { + log.info("getTyphoonInfoDto"); + Gson gson = new Gson(); + String tfid = "202501"; + TyphoonInfo typhoonInfo = this.zjsltStormService.findTyphoonInfoByTfid(tfid); + if(!ObjectUtils.isEmpty(typhoonInfo)){ + TyphoonInfoDto typhoonInfoDto = gson.fromJson(typhoonInfo.getStormData(),TyphoonInfoDto.class); + log.info("-----------------"); + log.info(gson.toJson(typhoonInfoDto)); + } + } }