天气定时器

dev
zc 3 years ago
parent 650a3c8ba6
commit 2415bc7da4
  1. 19
      cjy-project/src/main/java/com/cjy/weather/domain/WeatherRealtime.java
  2. 4
      cjy-project/src/main/java/com/cjy/weather/mapper/WeatherForecastMapper.java
  3. 4
      cjy-project/src/main/java/com/cjy/weather/mapper/WeatherIndexMapper.java
  4. 2
      cjy-project/src/main/java/com/cjy/weather/mapper/WeatherQualityMapper.java
  5. 4
      cjy-project/src/main/java/com/cjy/weather/mapper/WeatherRealtimeMapper.java
  6. 7
      cjy-project/src/main/java/com/cjy/weather/service/IWeatherWarningService.java
  7. 11
      cjy-project/src/main/java/com/cjy/weather/service/impl/WeatherWarningServiceImpl.java
  8. 60
      cjy-project/src/main/resources/mapper/weatherforecast/WeatherForecastMapper.xml
  9. 28
      cjy-project/src/main/resources/mapper/weatherindex/WeatherIndexMapper.xml
  10. 37
      cjy-project/src/main/resources/mapper/weatherquality/WeatherQualityMapper.xml
  11. 39
      cjy-project/src/main/resources/mapper/weatherrealtime/WeatherRealtimeMapper.xml
  12. 11
      ruoyi-common/pom.xml
  13. 62
      ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
  14. 7
      ruoyi-quartz/pom.xml
  15. 308
      ruoyi-quartz/src/main/java/com/ruoyi/quartz/timer/WeatherTimer.java

@ -26,10 +26,6 @@ public class WeatherRealtime extends BaseEntity
/** 温度 */
private String temp;
/** 最高温度 */
private String maxTemp;
/** 最低温度 */
private String minTemp;
/** 体感温度 */
private String feelsLike;
@ -203,19 +199,4 @@ public class WeatherRealtime extends BaseEntity
return organCode;
}
public String getMaxTemp() {
return maxTemp;
}
public void setMaxTemp(String maxTemp) {
this.maxTemp = maxTemp;
}
public String getMinTemp() {
return minTemp;
}
public void setMinTemp(String minTemp) {
this.minTemp = minTemp;
}
}

@ -19,6 +19,8 @@ public interface WeatherForecastMapper
* @param weatherForecast 天气预报
* @return 天气预报 集合
*/
public List<WeatherForecast> selectWeatherForecastList(WeatherForecast weatherForecast);
List<WeatherForecast> selectWeatherForecastList(WeatherForecast weatherForecast);
int insertWeatherForecast(WeatherForecast weatherForecast);
}

@ -19,6 +19,8 @@ public interface WeatherIndexMapper
* @param weatherIndex 天气指数
* @return 天气指数 集合
*/
public List<WeatherIndex> selectWeatherIndexList(WeatherIndex weatherIndex);
List<WeatherIndex> selectWeatherIndexList(WeatherIndex weatherIndex);
int insertWeatherIndex(WeatherIndex weatherIndex);
}

@ -38,4 +38,6 @@ public interface WeatherQualityMapper
*/
List<Map<String,Object>> selectWeatherQualityByCategory(Long organCode);
int insertWeatherQuality(WeatherQuality weatherQuality);
}

@ -21,6 +21,8 @@ public interface WeatherRealtimeMapper
*/
WeatherRealtime selectWeatherRealtime(Long organCode);
/**
* 查询实时天气 列表
*
@ -30,4 +32,6 @@ public interface WeatherRealtimeMapper
List<WeatherRealtime> selectWeatherRealtimeList(WeatherRealtime weatherRealtime);
int insertWeatherRealtime(WeatherRealtime weatherRealtime);
}

@ -13,13 +13,6 @@ import java.util.Map;
*/
public interface IWeatherWarningService
{
/**
* 查询天气灾害预警
*
* @param id 天气灾害预警 主键
* @return 天气灾害预警
*/
public WeatherWarning selectWeatherWarningById(Long id);
/**
* 查询天气灾害预警 列表

@ -22,17 +22,6 @@ public class WeatherWarningServiceImpl implements IWeatherWarningService
@Autowired
private WeatherWarningMapper weatherWarningMapper;
/**
* 查询天气灾害预警
*
* @param id 天气灾害预警 主键
* @return 天气灾害预警
*/
@Override
public WeatherWarning selectWeatherWarningById(Long id)
{
return weatherWarningMapper.selectWeatherWarningById(id);
}
/**
* 查询天气灾害预警 列表

@ -66,5 +66,63 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</where>
ORDER BY forecast_date ASC
</select>
<insert id="insertWeatherForecast" parameterType="WeatherForecast" useGeneratedKeys="true" keyProperty="id">
insert into sys_weather_forecast
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="forecastDate != null">forecast_date,</if>
<if test="sunrise != null">sunrise,</if>
<if test="sunset != null">sunset,</if>
<if test="moonrise != null">moonrise,</if>
<if test="moonset != null">moonset,</if>
<if test="moonPhase != null">moon_phase,</if>
<if test="tempMax != null">temp_max,</if>
<if test="tempMin != null">temp_min,</if>
<if test="textDay != null">text_day,</if>
<if test="textNight != null">text_night,</if>
<if test="windDirDay != null">wind_dir_day,</if>
<if test="windScaleDay != null">wind_scale_day,</if>
<if test="windSpeedDay != null">wind_speed_day,</if>
<if test="windDirNight != null">wind_dir_night,</if>
<if test="windScaleNight != null">wind_scale_night,</if>
<if test="windSpeedNight != null">wind_speed_night,</if>
<if test="humidity != null">humidity,</if>
<if test="precip != null">precip,</if>
<if test="pressure != null">pressure,</if>
<if test="vis != null">vis,</if>
<if test="cloud != null">cloud,</if>
<if test="uvIndex != null">uv_index,</if>
<if test="updateTime != null">update_time,</if>
<if test="createTime != null">create_time,</if>
<if test="organCode != null">organ_code,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="forecastDate != null">#{forecastDate},</if>
<if test="sunrise != null">#{sunrise},</if>
<if test="sunset != null">#{sunset},</if>
<if test="moonrise != null">#{moonrise},</if>
<if test="moonset != null">#{moonset},</if>
<if test="moonPhase != null">#{moonPhase},</if>
<if test="tempMax != null">#{tempMax},</if>
<if test="tempMin != null">#{tempMin},</if>
<if test="textDay != null">#{textDay},</if>
<if test="textNight != null">#{textNight},</if>
<if test="windDirDay != null">#{windDirDay},</if>
<if test="windScaleDay != null">#{windScaleDay},</if>
<if test="windSpeedDay != null">#{windSpeedDay},</if>
<if test="windDirNight != null">#{windDirNight},</if>
<if test="windScaleNight != null">#{windScaleNight},</if>
<if test="windSpeedNight != null">#{windSpeedNight},</if>
<if test="humidity != null">#{humidity},</if>
<if test="precip != null">#{precip},</if>
<if test="pressure != null">#{pressure},</if>
<if test="vis != null">#{vis},</if>
<if test="cloud != null">#{cloud},</if>
<if test="uvIndex != null">#{uvIndex},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createTime != null">#{createTime},</if>
<if test="organCode != null">#{organCode},</if>
</trim>
</insert>
</mapper>

@ -34,5 +34,31 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</where>
ORDER BY forecast_date ASC
</select>
<insert id="insertWeatherIndex" parameterType="WeatherIndex" useGeneratedKeys="true" keyProperty="id">
insert into sys_weather_index
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="forecastDate != null">forecast_date,</if>
<if test="type != null">type,</if>
<if test="indexName != null">index_name,</if>
<if test="indexLevel != null">index_level,</if>
<if test="category != null">category,</if>
<if test="text != null">text,</if>
<if test="updateTime != null">update_time,</if>
<if test="createTime != null">create_time,</if>
<if test="organCode != null">organ_code,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="forecastDate != null">#{forecastDate},</if>
<if test="type != null">#{type},</if>
<if test="indexName != null">#{indexName},</if>
<if test="indexLevel != null">#{indexLevel},</if>
<if test="category != null">#{category},</if>
<if test="text != null">#{text},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createTime != null">#{createTime},</if>
<if test="organCode != null">#{organCode},</if>
</trim>
</insert>
</mapper>

@ -53,4 +53,41 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
SELECT category as name,COUNT(*) as value FROM sys_weather_quality where organ_code = #{organCode} GROUP BY category
</select>
<insert id="insertWeatherQuality" parameterType="WeatherQuality" useGeneratedKeys="true" keyProperty="id">
insert into sys_weather_quality
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="pubTime != null">pub_time,</if>
<if test="aqi != null">aqi,</if>
<if test="qualityLevel != null">quality_level,</if>
<if test="category != null">category,</if>
<if test="pollutant != null">pollutant,</if>
<if test="pm10 != null">pm10,</if>
<if test="pm2p5 != null">pm2p5,</if>
<if test="nitrogenDioxide != null">nitrogen_dioxide,</if>
<if test="sulfurDioxide != null">sulfur_dioxide,</if>
<if test="carbonMonoxide != null">carbon_monoxide,</if>
<if test="ozone != null">ozone,</if>
<if test="updateTime != null">update_time,</if>
<if test="createTime != null">create_time,</if>
<if test="organCode != null">organ_code,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="pubTime != null">#{pubTime},</if>
<if test="aqi != null">#{aqi},</if>
<if test="qualityLevel != null">#{qualityLevel},</if>
<if test="category != null">#{category},</if>
<if test="pollutant != null">#{pollutant},</if>
<if test="pm10 != null">#{pm10},</if>
<if test="pm2p5 != null">#{pm2p5},</if>
<if test="nitrogenDioxide != null">#{nitrogenDioxide},</if>
<if test="sulfurDioxide != null">#{sulfurDioxide},</if>
<if test="carbonMonoxide != null">#{carbonMonoxide},</if>
<if test="ozone != null">#{ozone},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createTime != null">#{createTime},</if>
<if test="organCode != null">#{organCode},</if>
</trim>
</insert>
</mapper>

@ -55,5 +55,44 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where organ_code = #{organCode} ORDER BY create_time DESC LIMIT 0,1
</select>
<insert id="insertWeatherRealtime" parameterType="WeatherRealtime" useGeneratedKeys="true" keyProperty="id">
insert into sys_weather_realtime
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="obsTime != null">obs_time,</if>
<if test="temp != null">temp,</if>
<if test="feelsLike != null">feels_like,</if>
<if test="text != null">text,</if>
<if test="windDir != null">wind_dir,</if>
<if test="windScale != null">wind_scale,</if>
<if test="windSpeed != null">wind_speed,</if>
<if test="humidity != null">humidity,</if>
<if test="precip != null">precip,</if>
<if test="pressure != null">pressure,</if>
<if test="vis != null">vis,</if>
<if test="cloud != null">cloud,</if>
<if test="dew != null">dew,</if>
<if test="updateTime != null">update_time,</if>
<if test="createTime != null">create_time,</if>
<if test="organCode != null">organ_code,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="obsTime != null">#{obsTime},</if>
<if test="temp != null">#{temp},</if>
<if test="feelsLike != null">#{feelsLike},</if>
<if test="text != null">#{text},</if>
<if test="windDir != null">#{windDir},</if>
<if test="windScale != null">#{windScale},</if>
<if test="windSpeed != null">#{windSpeed},</if>
<if test="humidity != null">#{humidity},</if>
<if test="precip != null">#{precip},</if>
<if test="pressure != null">#{pressure},</if>
<if test="vis != null">#{vis},</if>
<if test="cloud != null">#{cloud},</if>
<if test="dew != null">#{dew},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createTime != null">#{createTime},</if>
<if test="organCode != null">#{organCode},</if>
</trim>
</insert>
</mapper>

@ -132,6 +132,17 @@
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
</dependencies>
</project>

@ -17,6 +17,15 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
@ -117,6 +126,59 @@ public class HttpUtils
return result.toString();
}
public static String doGet(String url){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String result = "";
try{
//通过默认配置创建一个httpClient实例
httpClient = HttpClients.createDefault();
//创建httpGet远程连接实例
HttpGet httpGet = new HttpGet(url);
//httpGet.addHeader("Connection", "keep-alive");
//设置请求头信息
httpGet.addHeader("Accept", "application/json");
//配置请求参数
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(35000) //设置连接主机服务超时时间
.setConnectionRequestTimeout(35000)//设置请求超时时间
.setSocketTimeout(60000)//设置数据读取超时时间
.build();
//为httpGet实例设置配置
httpGet.setConfig(requestConfig);
//执行get请求得到返回对象
response = httpClient.execute(httpGet);
//通过返回对象获取返回数据
HttpEntity entity = response.getEntity();
//通过EntityUtils中的toString方法将结果转换为字符串,后续根据需要处理对应的reponse code
result = EntityUtils.toString(entity);
System.out.println(result);
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException ioe){
ioe.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭资源
if(response != null){
try {
response.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
if(httpClient != null){
try{
httpClient.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
}
return result;
}
/**
* 向指定 URL 发送POST方法的请求
*

@ -35,6 +35,13 @@
<artifactId>ruoyi-common</artifactId>
</dependency>
<dependency>
<groupId>com.cjy</groupId>
<artifactId>cjy-project</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

@ -0,0 +1,308 @@
package com.ruoyi.quartz.timer;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cjy.enterprise.domain.EnterpriseInfo;
import com.cjy.enterprise.domain.vo.EnterpriseInfoExtend;
import com.cjy.enterprise.mapper.EnterpriseInfoMapper;
import com.cjy.weather.domain.*;
import com.cjy.weather.mapper.*;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
/**
* @ClassName: WeatherTimer
* @Author: zc
* @Date: 2023/3/21 13:37
* @Description: TODO
*/
@Component("weather")
public class WeatherTimer {
private static final String HF_WEATHER_KEY = "5773d9af03544ef59a6bad2a72325aeb";
@Autowired
private EnterpriseInfoMapper enterpriseInfoMapper;
@Autowired
private WeatherRealtimeMapper weatherRealtimeMapper;
@Autowired
private WeatherForecastMapper weatherForecastMapper;
@Autowired
private WeatherMeteorologicalMapper weatherMeteorologicalMapper;
@Autowired
private WeatherQualityMapper weatherQualityMapper;
@Autowired
private WeatherIndexMapper weatherIndexMapper;
@Autowired
private WeatherWarningMapper weatherWarningMapper;
/**
* 功能描述:获取实时天气
* @param:
* @return:
* @auther: zc
* @date: 2023/3/21 16:06
*/
public void getWeatherRealtime(){
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList();
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) {
Integer location = enterpriseInfoExtend.getLocation();
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location;
String hfLoncationData = HttpUtils.doGet(locationUrl);
String code = JSONObject.parseObject(hfLoncationData).getString("code");
if("200".equals(code)){
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location"));
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString());
String locationId = jsonObject.getString("id");
String url = "https://devapi.qweather.com/v7/weather/now?key="+HF_WEATHER_KEY+"&location="+locationId;
String weatherStr = HttpUtils.doGet(url);
JSONObject weatherJson = JSONObject.parseObject(weatherStr);
if("200".equals(weatherJson.get("code").toString())){
JSONObject now = JSONObject.parseObject(weatherJson.getString("now"));
WeatherRealtime weatherRealtime = new WeatherRealtime();
weatherRealtime.setObsTime(now.getDate("obsTime"));
weatherRealtime.setTemp(now.getString("temp"));
weatherRealtime.setFeelsLike(now.getString("feelsLike"));
weatherRealtime.setText(now.getString("text"));
weatherRealtime.setWindDir(now.getString("windDir"));
weatherRealtime.setWindScale(now.getString("windScale"));
weatherRealtime.setWindSpeed(now.getString("windSpeed"));
weatherRealtime.setHumidity(now.getString("humidity"));
weatherRealtime.setPrecip(now.getString("precip"));
weatherRealtime.setPressure(now.getString("pressure"));
weatherRealtime.setVis(now.getString("vis"));
weatherRealtime.setCloud(now.getString("cloud"));
weatherRealtime.setDew(now.getString("dew"));
weatherRealtime.setCreateTime(new Date());
weatherRealtime.setOrganCode(enterpriseInfoExtend.getOrganCode());
weatherRealtimeMapper.insertWeatherRealtime(weatherRealtime);
}
}
}
}
/**
* 功能描述:获取天气预报
* @param:
* @return:
* @auther: zc
* @date: 2023/3/21 16:07
*/
public void getWeatherForecast(){
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList();
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) {
Integer location = enterpriseInfoExtend.getLocation();
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location;
String hfLoncationData = HttpUtils.doGet(locationUrl);
String code = JSONObject.parseObject(hfLoncationData).getString("code");
if("200".equals(code)){
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location"));
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString());
String locationId = jsonObject.getString("id");
String url = "https://devapi.qweather.com/v7/weather/7d?key="+HF_WEATHER_KEY+"&location="+locationId;
String weatherStr = HttpUtils.doGet(url);
JSONObject weatherJson = JSONObject.parseObject(weatherStr);
if("200".equals(weatherJson.get("code").toString())){
JSONArray daily = JSONArray.parseArray(weatherJson.getString("daily"));
for (Object obj : daily) {
//天气预报表
JSONObject dailyObj = JSONObject.parseObject(obj.toString());
WeatherForecast weatherForecast = new WeatherForecast();
weatherForecast.setOrganCode(enterpriseInfoExtend.getOrganCode());
weatherForecast.setForecastDate(dailyObj.getDate("fxDate"));
weatherForecast.setSunrise(dailyObj.getString("sunrise"));
weatherForecast.setSunset(dailyObj.getString("sunset"));
weatherForecast.setMoonrise(dailyObj.getString("moonrise"));
weatherForecast.setMoonset(dailyObj.getString("moonset"));
weatherForecast.setMoonPhase(dailyObj.getString("moonPhase"));
weatherForecast.setTempMax(dailyObj.getString("tempMax"));
weatherForecast.setTempMin(dailyObj.getString("tempMin"));
weatherForecast.setTextDay(dailyObj.getString("textDay"));
weatherForecast.setTextNight(dailyObj.getString("textNight"));
weatherForecast.setWindDirDay(dailyObj.getString("windDirDay"));
weatherForecast.setWindScaleDay(dailyObj.getString("windScaleDay"));
weatherForecast.setWindSpeedDay(dailyObj.getString("windSpeedDay"));
weatherForecast.setWindDirNight(dailyObj.getString("windDirNight"));
weatherForecast.setWindScaleNight(dailyObj.getString("windScaleNight"));
weatherForecast.setWindSpeedNight(dailyObj.getString("windSpeedNight"));
weatherForecast.setHumidity(dailyObj.getString("humidity"));
weatherForecast.setPrecip(dailyObj.getString("precip"));
weatherForecast.setPressure(dailyObj.getString("pressure"));
weatherForecast.setVis(dailyObj.getString("vis"));
weatherForecast.setCloud(dailyObj.getString("cloud"));
weatherForecast.setUvIndex(dailyObj.getString("uvIndex"));
weatherForecast.setCreateTime(new Date());
weatherForecastMapper.insertWeatherForecast(weatherForecast);
//用于统计的表
WeatherMeteorological weatherMeteorological = new WeatherMeteorological();
weatherMeteorological.setForecastDate(dailyObj.getDate("fxDate"));
weatherMeteorological.setTempMax(dailyObj.getString("tempMax"));
weatherMeteorological.setTempMin(dailyObj.getString("tempMin"));
weatherMeteorological.setTextDay(dailyObj.getString("textDay"));
weatherMeteorological.setOrganCode(enterpriseInfoExtend.getOrganCode());
weatherMeteorological.setCreateTime(new Date());
weatherMeteorologicalMapper.insertWeatherMeteorological(weatherMeteorological);
}
}
}
}
}
/**
* 功能描述:获取空气质量
* @param:
* @return:
* @auther: zc
* @date: 2023/3/21 17:05
*/
public void getWeatherQuality(){
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList();
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) {
Integer location = enterpriseInfoExtend.getLocation();
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location;
String hfLoncationData = HttpUtils.doGet(locationUrl);
String code = JSONObject.parseObject(hfLoncationData).getString("code");
if("200".equals(code)){
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location"));
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString());
String locationId = jsonObject.getString("id");
String url = "https://devapi.qweather.com/v7/air/now?key="+HF_WEATHER_KEY+"&location="+locationId;
String weatherStr = HttpUtils.doGet(url);
JSONObject weatherJson = JSONObject.parseObject(weatherStr);
if("200".equals(weatherJson.get("code").toString())){
JSONObject now = JSONObject.parseObject(weatherJson.getString("now"));
WeatherQuality weatherQuality = new WeatherQuality();
weatherQuality.setPubTime(now.getDate("pubTime"));
weatherQuality.setAqi(now.getString("aqi"));
weatherQuality.setQualityLevel(now.getString("level"));
weatherQuality.setCategory(now.getString("category"));
weatherQuality.setPollutant(now.getString("primary"));
weatherQuality.setPm2p5(now.getString("pm2p5"));
weatherQuality.setPm10(now.getString("pm10"));
weatherQuality.setNitrogenDioxide(now.getString("no2"));
weatherQuality.setSulfurDioxide(now.getString("so2"));
weatherQuality.setCarbonMonoxide(now.getString("co"));
weatherQuality.setOzone(now.getString("o3"));
weatherQuality.setOrganCode(enterpriseInfoExtend.getOrganCode());
weatherQuality.setCreateTime(new Date());
weatherQualityMapper.insertWeatherQuality(weatherQuality);
}
}
}
}
/**
* 功能描述:获取天气指数
* @param:
* @return:
* @auther: zc
* @date: 2023/3/21 17:30
*/
public void getWeatherIndex(){
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList();
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) {
Integer location = enterpriseInfoExtend.getLocation();
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location;
String hfLoncationData = HttpUtils.doGet(locationUrl);
String code = JSONObject.parseObject(hfLoncationData).getString("code");
if("200".equals(code)){
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location"));
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString());
String locationId = jsonObject.getString("id");
String url = "https://devapi.qweather.com/v7/indices/1d?key="+HF_WEATHER_KEY+"&location="+locationId+"&type=0";
String weatherStr = HttpUtils.doGet(url);
JSONObject weatherJson = JSONObject.parseObject(weatherStr);
if("200".equals(weatherJson.get("code").toString())){
JSONArray daily = JSONArray.parseArray(weatherJson.getString("daily"));
for (Object obj : daily) {
JSONObject dailyObj = JSONObject.parseObject(obj.toString());
WeatherIndex weatherIndex = new WeatherIndex();
weatherIndex.setForecastDate(dailyObj.getDate("date"));
weatherIndex.setType(dailyObj.getString("type"));
weatherIndex.setIndexName(dailyObj.getString("name"));
weatherIndex.setIndexLevel(dailyObj.getString("level"));
weatherIndex.setCategory(dailyObj.getString("category"));
weatherIndex.setText(dailyObj.getString("text"));
weatherIndex.setOrganCode(enterpriseInfoExtend.getOrganCode());
weatherIndex.setCreateTime(new Date());
weatherIndexMapper.insertWeatherIndex(weatherIndex);
}
}
}
}
}
/**
* 功能描述:获取预警信息
* @param:
* @return:
* @auther: zc
* @date: 2023/3/21 17:44
*/
public void getWeatherWarning(){
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList();
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) {
Integer location = enterpriseInfoExtend.getLocation();
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location;
String hfLoncationData = HttpUtils.doGet(locationUrl);
String code = JSONObject.parseObject(hfLoncationData).getString("code");
if("200".equals(code)){
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location"));
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString());
String locationId = jsonObject.getString("id");
String url = "https://devapi.qweather.com/v7/warning/now?key="+HF_WEATHER_KEY+"&location="+locationId;
String weatherStr = HttpUtils.doGet(url);
JSONObject weatherJson = JSONObject.parseObject(weatherStr);
if("200".equals(weatherJson.get("code").toString())){
JSONArray daily = JSONArray.parseArray(weatherJson.getString("daily"));
for (Object obj : daily) {
JSONObject dailyObj = JSONObject.parseObject(obj.toString());
WeatherWarning weatherWarningData = new WeatherWarning();
weatherWarningData.setWarningId(dailyObj.getString("id"));
List<WeatherWarning> weatherWarnings = weatherWarningMapper.selectWeatherWarningList(weatherWarningData);
if(!weatherWarnings.isEmpty()){
WeatherWarning weatherWarning = new WeatherWarning();
weatherWarning.setWarningId(dailyObj.getString("id"));
weatherWarning.setSender(dailyObj.getString("sender"));
weatherWarning.setPubTime(dailyObj.getDate("pubTime"));
weatherWarning.setTitle(dailyObj.getString("title"));
weatherWarning.setStartTime(dailyObj.getDate("startTime"));
weatherWarning.setEndTime(dailyObj.getDate("endTime"));
weatherWarning.setWarningStatus(dailyObj.getString("status"));
weatherWarning.setSeverity(dailyObj.getString("severity"));
weatherWarning.setSeverityColor(dailyObj.getString("severityColor"));
weatherWarning.setType(dailyObj.getString("type"));
weatherWarning.setTypeName(dailyObj.getString("typeName"));
weatherWarning.setText(dailyObj.getString("text"));
weatherWarning.setRelated(dailyObj.getString("related"));
weatherWarning.setOrganCode(enterpriseInfoExtend.getOrganCode());
weatherWarning.setCreateTime(new Date());
weatherWarningMapper.insertWeatherWarning(weatherWarning);
}
}
}
}
}
}
private List<EnterpriseInfoExtend> getEntitiesInfoList(){
List<EnterpriseInfoExtend> enterpriseInfoExtends = enterpriseInfoMapper.selectEnterpriseInfoList(new EnterpriseInfo());
return enterpriseInfoExtends;
}
public static void main(String[] args) {
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location=130110";
String hfLoncationData = HttpUtils.doGet(locationUrl);
String location = JSONObject.parseObject(hfLoncationData).getString("location");
JSONArray objects = JSONArray.parseArray(location);
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString());
System.out.println(jsonObject.getString("id"));
}
}
Loading…
Cancel
Save