From 3aecd37d4b13881fa0b3cec544ef29e84787c66b Mon Sep 17 00:00:00 2001 From: simon <861719797@qq.com> Date: Thu, 21 Sep 2023 18:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A9=E6=B0=94=E3=80=81ip=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../third/ThirdWeatherInfoRequest.java | 44 +++++++ .../iot/data/resp/third/ThirdIpInfoResp.java | 26 +++++ .../iot/data/resp/third/ThirdRpcResp.java | 22 ++++ .../data/resp/third/weather/AirQuality.java | 11 ++ .../iot/data/resp/third/weather/Daily.java | 30 +++++ .../iot/data/resp/third/weather/Hourly.java | 22 ++++ .../data/resp/third/weather/LifeIndex.java | 20 ++++ .../resp/third/weather/LifeIndexDesc.java | 19 +++ .../resp/third/weather/Precipitation.java | 18 +++ .../iot/data/resp/third/weather/Result.java | 11 ++ .../data/resp/third/weather/Temperature.java | 22 ++++ .../iot/data/resp/third/weather/Value.java | 9 ++ .../data/resp/third/weather/Visibility.java | 14 +++ .../data/resp/third/weather/WeatherResp.java | 19 +++ .../resp/third/weather/WeatherTimeDouble.java | 14 +++ .../resp/third/weather/WeatherTimeInt.java | 18 +++ .../resp/third/weather/WeatherTimeString.java | 14 +++ .../resp/third/weather/WeatherTimeValue.java | 14 +++ .../iot/data/resp/third/weather/Wind.java | 19 +++ .../data/resp/third/weather/WindSpeed.java | 14 +++ iot-common/iot-third/.gitignore | 33 ++++++ iot-common/iot-third/pom.xml | 68 +++++++++++ .../qiuguo/iot/third/enums/WeatherEnum.java | 30 +++++ .../qiuguo/iot/third/service/IpService.java | 32 ++++++ .../iot/third/service/WeatherService.java | 108 ++++++++++++++++++ .../iot/third/IotThirdApplicationTests.java | 16 +++ .../iot/third/service/IpServiceTest.java | 46 ++++++++ .../iot/third/service/WeatherServiceTest.java | 47 ++++++++ 28 files changed, 760 insertions(+) create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/request/third/ThirdWeatherInfoRequest.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdIpInfoResp.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdRpcResp.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/AirQuality.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Daily.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Hourly.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndex.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndexDesc.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Precipitation.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Result.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Temperature.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Value.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Visibility.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherResp.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeDouble.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeInt.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeString.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeValue.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Wind.java create mode 100644 iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WindSpeed.java create mode 100644 iot-common/iot-third/.gitignore create mode 100644 iot-common/iot-third/pom.xml create mode 100644 iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/WeatherEnum.java create mode 100644 iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/IpService.java create mode 100644 iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/WeatherService.java create mode 100644 iot-common/iot-third/src/test/java/com/qiuguo/iot/third/IotThirdApplicationTests.java create mode 100644 iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/IpServiceTest.java create mode 100644 iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/WeatherServiceTest.java diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/request/third/ThirdWeatherInfoRequest.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/request/third/ThirdWeatherInfoRequest.java new file mode 100644 index 0000000..6d42bc3 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/request/third/ThirdWeatherInfoRequest.java @@ -0,0 +1,44 @@ +package com.qiuguo.iot.data.request.third; + +import lombok.Data; + +import javax.annotation.Nullable; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * @author simon + * @date 2023/9/21 + * @description 天气查询参数 + **/ + +@Data +public class ThirdWeatherInfoRequest { + + /* + * 1: 按小时查询 2:按天查询 + */ + @NotNull + private Integer type; + + /* + * 查询条数 + */ + @NotNull + private Integer size; + + /* + * 客户端ip + */ + private String ip; + + /* + * 纬度 + */ + private String lat; + + /* + * 经度 + */ + private String lng; +} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdIpInfoResp.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdIpInfoResp.java new file mode 100644 index 0000000..4af2c98 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdIpInfoResp.java @@ -0,0 +1,26 @@ +package com.qiuguo.iot.data.resp.third; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ + +@Data +public class ThirdIpInfoResp { + private Long id; + private String ip; + private Float lat; + private Float lng; + private String nation; + private String province; + private String city; + private String district; + private Integer adcode; +} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdRpcResp.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdRpcResp.java new file mode 100644 index 0000000..3770b3a --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/ThirdRpcResp.java @@ -0,0 +1,22 @@ +package com.qiuguo.iot.data.resp.third; + +import lombok.Data; +import org.springframework.core.ParameterizedTypeReference; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ +@Data +public class ThirdRpcResp { + private Integer code; + private String msg; + private Boolean success; + private Boolean error; + private T data; + + public static ParameterizedTypeReference> getResponseIpType() { + return new ParameterizedTypeReference>() {}; + } +} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/AirQuality.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/AirQuality.java new file mode 100644 index 0000000..a8ed30d --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/AirQuality.java @@ -0,0 +1,11 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +import java.util.List; + +@Data +public class AirQuality { + private List aqi; + private List pm25; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Daily.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Daily.java new file mode 100644 index 0000000..acbeddb --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Daily.java @@ -0,0 +1,30 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +import java.util.List; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ +@Data +public class Daily { + private String status; + private List precipitation_08h_20h; + private List precipitation_20h_32h; + private List precipitation; + private List temperature; + private List temperature_08h_20h; + private List temperature_20h_32h; + private List wind; + private List wind_08h_20h; + private List wind_20h_32h; + private List humidity; + private List cloudrate; + private List pressure; + private List visibility; + private List dswrf; + private LifeIndex life_index; +} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Hourly.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Hourly.java new file mode 100644 index 0000000..df50cf5 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Hourly.java @@ -0,0 +1,22 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +import java.util.List; + +@Data +public class Hourly { + private String status; + private String description; + private List precipitation; + private List temperature; + private List apparent_temperature; + private List wind; + private List humidity; + private List cloudrate; + private List skycon; + private List pressure; + private List visibility; + private List dswrf; + private AirQuality air_quality; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndex.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndex.java new file mode 100644 index 0000000..61a79b8 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndex.java @@ -0,0 +1,20 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +import java.util.List; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ + +@Data +public class LifeIndex { + private List ultraviolet; + private List carWashing; + private List dressing; + private List comfort; + private List coldRisk; +} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndexDesc.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndexDesc.java new file mode 100644 index 0000000..853e8d7 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/LifeIndexDesc.java @@ -0,0 +1,19 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; + +import java.util.Date; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ +@Data +public class LifeIndexDesc { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date date; + private String index; + private String desc; +} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Precipitation.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Precipitation.java new file mode 100644 index 0000000..0e3d5ed --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Precipitation.java @@ -0,0 +1,18 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +public class Precipitation { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + private Integer value; + private Integer probability; + private Integer max; + private Integer min; + private Double avg; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Result.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Result.java new file mode 100644 index 0000000..d1b46ec --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Result.java @@ -0,0 +1,11 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +@Data +public class Result { + private Hourly hourly; + private Daily daily; + private Integer primary; + private String forecast_keypoint; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Temperature.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Temperature.java new file mode 100644 index 0000000..9017780 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Temperature.java @@ -0,0 +1,22 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; + +import java.util.Date; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ +@Data +public class Temperature { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + //天气 + private Double max; + private Double min; + private Double avg; + private Double probability; +} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Value.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Value.java new file mode 100644 index 0000000..d3bd4bc --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Value.java @@ -0,0 +1,9 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +@Data +public class Value { + private Integer chn; + private Integer usa; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Visibility.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Visibility.java new file mode 100644 index 0000000..6d5888c --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Visibility.java @@ -0,0 +1,14 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +public class Visibility { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + private double value; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherResp.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherResp.java new file mode 100644 index 0000000..15a0a45 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherResp.java @@ -0,0 +1,19 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +import java.util.List; + +@Data +public class WeatherResp { + private String status; + private String api_version; + private String api_status; + private String lang; + private String unit; + private Integer tzshift; + private String timezone; + private Long server_time; + private List location; + private Result result; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeDouble.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeDouble.java new file mode 100644 index 0000000..508f28e --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeDouble.java @@ -0,0 +1,14 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +public class WeatherTimeDouble { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + private Double value; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeInt.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeInt.java new file mode 100644 index 0000000..8c6b0d2 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeInt.java @@ -0,0 +1,18 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +public class WeatherTimeInt { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + private Integer value; + //天气 + private Double max; + private Double min; + private Double avg; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeString.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeString.java new file mode 100644 index 0000000..fbdffbf --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeString.java @@ -0,0 +1,14 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +public class WeatherTimeString { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + private String value; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeValue.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeValue.java new file mode 100644 index 0000000..23c4d16 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WeatherTimeValue.java @@ -0,0 +1,14 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +public class WeatherTimeValue { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + private Value value; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Wind.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Wind.java new file mode 100644 index 0000000..68563f1 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/Wind.java @@ -0,0 +1,19 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +public class Wind { + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm", timezone = "GMT+8") + private Date datetime; + private Double speed; + private Double direction; + + private WindSpeed max; + private WindSpeed min; + private WindSpeed avg; +} \ No newline at end of file diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WindSpeed.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WindSpeed.java new file mode 100644 index 0000000..bbab749 --- /dev/null +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/resp/third/weather/WindSpeed.java @@ -0,0 +1,14 @@ +package com.qiuguo.iot.data.resp.third.weather; + +import lombok.Data; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ +@Data +public class WindSpeed { + private Double speed; + private Double direction; +} diff --git a/iot-common/iot-third/.gitignore b/iot-common/iot-third/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/iot-common/iot-third/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/iot-common/iot-third/pom.xml b/iot-common/iot-third/pom.xml new file mode 100644 index 0000000..85edf18 --- /dev/null +++ b/iot-common/iot-third/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.qiuguo.iot + iot-common + 0.0.1-SNAPSHOT + + iot-third + 0.0.1-SNAPSHOT + iot-third + iot-third + + UTF-8 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-webflux + + + com.qiuguo.iot + iot-base + 0.0.1-SNAPSHOT + compile + + + com.qiuguo.iot + iot-data + 0.0.1-SNAPSHOT + compile + + + io.projectreactor + reactor-test + test + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot.maven.plugin.version} + + + true + + + + + repackage + + + + + + + + diff --git a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/WeatherEnum.java b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/WeatherEnum.java new file mode 100644 index 0000000..6c24a36 --- /dev/null +++ b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/WeatherEnum.java @@ -0,0 +1,30 @@ +package com.qiuguo.iot.third.enums; + +/** + * @author simon + * @create 2023-09-21 10:57 + */ +public enum WeatherEnum { + //退款申请状态 0:待申请, 1:申请成功, 2:退款成功 + QUERY_TYPE_1(1, "按小时查询"), + QUERY_TYPE_2(2, "按天查询"), + QUERY_MIN_SIZE(1, "1"), + QUERY_HOUR_MAX_SIZE(360, "360小时"), + QUERY_DAY_MAX_SIZE(15, "15天"), + ; + + public final String name; + public final Integer code; + + WeatherEnum(Integer code, String name) { + this.code = code; + this.name = name; + } + + public Integer getCode() { + return this.code; + } + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/IpService.java b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/IpService.java new file mode 100644 index 0000000..f3e3782 --- /dev/null +++ b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/IpService.java @@ -0,0 +1,32 @@ +package com.qiuguo.iot.third.service; + +import com.qiuguo.iot.data.resp.third.ThirdIpInfoResp; +import com.qiuguo.iot.data.resp.third.ThirdRpcResp; +import org.springframework.beans.factory.annotation.Value; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +/** + * @author simon + * @date 2023/9/21 + * @description ip查询 + **/ + +@Service +@Slf4j +public class IpService { + @Value("https://qiuguo-app.qiuguojihua.com/prod-api/third/ip/info") + public String thirdIpInfoUrl; + + /** + * 查询ip信息 + * @param ip + * @return + */ + public Mono> getIpInfo(String ip) { + WebClient webClient = WebClient.builder().build(); + return webClient.get().uri(thirdIpInfoUrl + "?ip=" + ip).retrieve().bodyToMono(ThirdRpcResp.getResponseIpType()); + } +} diff --git a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/WeatherService.java b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/WeatherService.java new file mode 100644 index 0000000..8697eff --- /dev/null +++ b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/service/WeatherService.java @@ -0,0 +1,108 @@ +package com.qiuguo.iot.third.service; + +import cn.hutool.json.JSONObject; +import com.qiuguo.iot.data.request.third.ThirdWeatherInfoRequest; +import com.qiuguo.iot.data.resp.third.ThirdIpInfoResp; +import com.qiuguo.iot.data.resp.third.ThirdRpcResp; +import com.qiuguo.iot.data.resp.third.weather.WeatherResp; +import com.qiuguo.iot.third.enums.WeatherEnum; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.util.ObjectUtils; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.CoreSubscriber; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.util.annotation.Nullable; + +import javax.annotation.Resource; +import java.util.regex.Pattern; + +/** + * @author simon + * @date 2023/9/21 + * @description 天气服务 + **/ + +@Service +@Slf4j +@Validated +public class WeatherService { + //101.6656,39.2072/hourly?hourlysteps=1 + //101.6656,39.2072/daily?dailysteps=1 + @Value("https://api.caiyunapp.com/v2.6/ilUeAnf1vNkphxYS/") + private String queryWeatherUrl; + + @Resource + private IpService ipService; + + /** + * + * @return + */ + public Mono queryWeather(ThirdWeatherInfoRequest req) { + if (req.getType() < WeatherEnum.QUERY_TYPE_1.getCode() || req.getType() > WeatherEnum.QUERY_TYPE_2.getCode()) { + req.setType(WeatherEnum.QUERY_TYPE_1.getCode()); + } + if (req.getSize() < WeatherEnum.QUERY_MIN_SIZE.getCode()) { + req.setSize(WeatherEnum.QUERY_MIN_SIZE.getCode()); + } + //小时查询最大360小时 + if (req.getType().equals(WeatherEnum.QUERY_TYPE_1.getCode()) && req.getSize() > WeatherEnum.QUERY_HOUR_MAX_SIZE.getCode()) { + req.setSize(WeatherEnum.QUERY_HOUR_MAX_SIZE.getCode()); + } + //按天查询最多15天 + if (req.getType().equals(WeatherEnum.QUERY_TYPE_2.getCode()) && req.getSize() > WeatherEnum.QUERY_DAY_MAX_SIZE.getCode()) { + req.setSize(WeatherEnum.QUERY_DAY_MAX_SIZE.getCode()); + } + + + Mono lngLatMono = Mono.empty(); + + //如果经纬度为空,通过ip获取经纬度 + if (ObjectUtils.isEmpty(req.getLng()) || ObjectUtils.isEmpty(req.getLat())) { + if (ObjectUtils.isEmpty(req.getIp())) { + throw new RuntimeException("ip经纬度不能同时为空"); + } + boolean matches = Pattern.matches("^([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$", req.getIp()); + if (!matches) { + throw new RuntimeException("ip格式不正确"); + } + + lngLatMono = ipService.getIpInfo(req.getIp()).flatMap(resp -> { + if (!resp.getCode().equals(HttpStatus.OK.value())) { + return Mono.error(new RuntimeException("ip服务查询失败:" + resp.getMsg() + " " + resp.getCode())); + } + if (ObjectUtils.isEmpty(resp.getData().getLng()) || ObjectUtils.isEmpty(resp.getData().getLat())) { + return Mono.error(new RuntimeException("当前ip查询失败:" + req.getIp())); + } + req.setLng(resp.getData().getLng().toString()); + req.setLat(resp.getData().getLat().toString()); + return Mono.just(req); + }); + + } else { + lngLatMono = Mono.just(req); + } + + return thirdQueryWeather(lngLatMono, req.getType(), req.getSize()); + } + + public Mono thirdQueryWeather(Mono lngLatMono, Integer queryType, Integer querySize) { + WebClient webClient = WebClient.builder().build(); + + return lngLatMono.flatMap(r -> { + if (queryType.equals(WeatherEnum.QUERY_TYPE_1.getCode())) { + queryWeatherUrl = queryWeatherUrl + r.getLng().toString() + "," + r.getLat().toString() + "/hourly?hourlysteps=" + querySize; + } else { + queryWeatherUrl = queryWeatherUrl + r.getLng().toString() + "," + r.getLat().toString() + "/daily?dailysteps=" + querySize; + } + + + return webClient.get().uri(queryWeatherUrl).retrieve().bodyToMono(WeatherResp.class); + }); + } +} diff --git a/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/IotThirdApplicationTests.java b/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/IotThirdApplicationTests.java new file mode 100644 index 0000000..93a6c67 --- /dev/null +++ b/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/IotThirdApplicationTests.java @@ -0,0 +1,16 @@ +package com.qiuguo.iot.third; + +import com.qiuguo.iot.data.resp.third.ThirdIpInfoResp; +import com.qiuguo.iot.data.resp.third.ThirdRpcResp; +import com.qiuguo.iot.third.service.IpService; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import reactor.core.publisher.Mono; + +import javax.annotation.Resource; +import java.util.concurrent.atomic.AtomicReference; + +@SpringBootTest() +class IotThirdApplicationTests { + +} diff --git a/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/IpServiceTest.java b/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/IpServiceTest.java new file mode 100644 index 0000000..ee3284f --- /dev/null +++ b/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/IpServiceTest.java @@ -0,0 +1,46 @@ +package com.qiuguo.iot.third.service; + +import com.qiuguo.iot.data.resp.third.ThirdIpInfoResp; +import com.qiuguo.iot.data.resp.third.ThirdRpcResp; +import org.junit.jupiter.api.Test; +import org.springframework.beans.BeanUtils; +import org.springframework.boot.test.context.SpringBootTest; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import javax.annotation.Resource; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ + +@SpringBootTest(classes = IpService.class) +class IpServiceTest { + @Resource + private IpService ipService; + + @Test + void contextLoads() throws InterruptedException { + Mono> ipInfo = ipService.getIpInfo("60.186.105.204"); + System.out.println("contextLoads"); + + Mono> thirdRpcRespMono = ipInfo.flatMap(resp -> { + System.out.println(resp.getData().getCity()); + return Mono.just(resp); + }); + + thirdRpcRespMono.subscribe(System.out::println); + Thread.sleep(20000); + + // StepVerifier.create(ipInfo) + // //.expectNext("Hello, World!") // 验证预期的输出值 + // //.expectNext() + // .expectComplete() // 验证是否正常完成 + // .verify(); // 执行验证 + System.out.println("contextLoads"); + } +} \ No newline at end of file diff --git a/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/WeatherServiceTest.java b/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/WeatherServiceTest.java new file mode 100644 index 0000000..ef2ab9d --- /dev/null +++ b/iot-common/iot-third/src/test/java/com/qiuguo/iot/third/service/WeatherServiceTest.java @@ -0,0 +1,47 @@ +package com.qiuguo.iot.third.service; + +import cn.hutool.json.JSONObject; +import com.qiuguo.iot.data.request.third.ThirdWeatherInfoRequest; +import com.qiuguo.iot.data.resp.third.weather.WeatherResp; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import reactor.core.publisher.Mono; + +import javax.annotation.Resource; + + +/** + * @author simon + * @date 2023/9/21 + * @description + **/ + +@SpringBootTest(classes = {WeatherService.class, IpService.class}) +class WeatherServiceTest { + @Resource + private WeatherService weatherService; + + @Test + void queryWeather() throws InterruptedException { + ThirdWeatherInfoRequest thirdWeatherInfoRequest = new ThirdWeatherInfoRequest(); + thirdWeatherInfoRequest.setType(2); + thirdWeatherInfoRequest.setSize(1); + thirdWeatherInfoRequest.setIp("60.186.105.204"); + + System.out.println(thirdWeatherInfoRequest.getIp()); + Mono jsonObjectMono = weatherService.queryWeather(thirdWeatherInfoRequest); + + jsonObjectMono.flatMap(r -> { + System.out.println("flatMap"); + System.out.println(r.getApi_version()); + return Mono.just(r); + }).subscribe(System.out::println); + + Thread.sleep(20000); + System.out.println(thirdWeatherInfoRequest.getIp()); + } + + @Test + void thirdQueryWeather() { + } +} \ No newline at end of file