From 77f38e2b9a21b300738aacf81e2d87db7f596bf2 Mon Sep 17 00:00:00 2001 From: Prominence Date: Thu, 5 Jul 2018 00:02:26 +0300 Subject: [PATCH] Basic raw implementation. --- .gitignore | 7 +- pom.xml | 28 +++ .../api/WeatherProviderManager.java | 40 ++++ .../openweather/api/model/Clouds.java | 63 ++++++ .../openweather/api/model/Coordinates.java | 80 ++++++++ .../openweather/api/model/Rain.java | 63 ++++++ .../openweather/api/model/Snow.java | 63 ++++++ .../openweather/api/model/Weather.java | 117 +++++++++++ .../openweather/api/model/WeatherInfo.java | 149 ++++++++++++++ .../api/model/WeatherResponse.java | 193 ++++++++++++++++++ .../api/model/WeatherSystemInfo.java | 134 ++++++++++++ .../openweather/api/model/Wind.java | 77 +++++++ .../api/provider/DefaultWeatherProvider.java | 91 +++++++++ .../api/provider/WeatherProvider.java | 36 ++++ .../openweather/api/utils/JsonUtils.java | 45 ++++ 15 files changed, 1183 insertions(+), 3 deletions(-) create mode 100644 pom.xml create mode 100644 src/main/java/by/prominence/openweather/api/WeatherProviderManager.java create mode 100644 src/main/java/by/prominence/openweather/api/model/Clouds.java create mode 100644 src/main/java/by/prominence/openweather/api/model/Coordinates.java create mode 100644 src/main/java/by/prominence/openweather/api/model/Rain.java create mode 100644 src/main/java/by/prominence/openweather/api/model/Snow.java create mode 100644 src/main/java/by/prominence/openweather/api/model/Weather.java create mode 100644 src/main/java/by/prominence/openweather/api/model/WeatherInfo.java create mode 100644 src/main/java/by/prominence/openweather/api/model/WeatherResponse.java create mode 100644 src/main/java/by/prominence/openweather/api/model/WeatherSystemInfo.java create mode 100644 src/main/java/by/prominence/openweather/api/model/Wind.java create mode 100644 src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java create mode 100644 src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java create mode 100644 src/main/java/by/prominence/openweather/api/utils/JsonUtils.java diff --git a/.gitignore b/.gitignore index a1c2a23..ddf57da 100644 --- a/.gitignore +++ b/.gitignore @@ -7,9 +7,6 @@ # BlueJ files *.ctxt -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - # Package Files # *.jar *.war @@ -21,3 +18,7 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +*.iml +.idea/ +**/ApplicationTest.java \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..542be16 --- /dev/null +++ b/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + openweather-api + by.prominence.openweater.api + 1.0-SNAPSHOT + + + + + + org.apache.httpcomponents + httpclient + 4.5.5 + + + + com.alibaba + fastjson + 1.2.44 + + + + + \ No newline at end of file diff --git a/src/main/java/by/prominence/openweather/api/WeatherProviderManager.java b/src/main/java/by/prominence/openweather/api/WeatherProviderManager.java new file mode 100644 index 0000000..64ffbf2 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/WeatherProviderManager.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api; + +import by.prominence.openweather.api.provider.DefaultWeatherProvider; +import by.prominence.openweather.api.provider.WeatherProvider; + +public class WeatherProviderManager { + + private String authToken; + + public WeatherProviderManager(String token) { + this.authToken = token; + } + + public WeatherProvider getProvider() { + return new DefaultWeatherProvider(authToken); + } + +} diff --git a/src/main/java/by/prominence/openweather/api/model/Clouds.java b/src/main/java/by/prominence/openweather/api/model/Clouds.java new file mode 100644 index 0000000..b29c568 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/Clouds.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.util.Objects; + +public class Clouds { + + @JSONField(name = "all") + // Cloudiness, % + private byte cloudiness; + + public byte getCloudiness() { + return cloudiness; + } + + public void setCloudiness(byte cloudiness) { + this.cloudiness = cloudiness; + } + + @Override + public String toString() { + return "Clouds{" + + "cloudiness=" + cloudiness + "%" + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Clouds clouds = (Clouds) o; + return cloudiness == clouds.cloudiness; + } + + @Override + public int hashCode() { + + return Objects.hash(cloudiness); + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/Coordinates.java b/src/main/java/by/prominence/openweather/api/model/Coordinates.java new file mode 100644 index 0000000..f5d5f4f --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/Coordinates.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.util.Objects; + +public class Coordinates { + + @JSONField(name = "lat") + // City geo location, latitude + private float latitude; + @JSONField(name = "lon") + // City geo location, longitude + private float longitude; + + public Coordinates(float latitude, float longitude) { + this.latitude = latitude; + this.longitude = longitude; + } + + public float getLatitude() { + return latitude; + } + + public void setLatitude(float latitude) { + this.latitude = latitude; + } + + public float getLongitude() { + return longitude; + } + + public void setLongitude(float longitude) { + this.longitude = longitude; + } + + @Override + public String toString() { + return "Coordinates{" + + "latitude=" + latitude + + ", longitude=" + longitude + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Coordinates that = (Coordinates) o; + return Double.compare(that.latitude, latitude) == 0 && + Double.compare(that.longitude, longitude) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(latitude, longitude); + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/Rain.java b/src/main/java/by/prominence/openweather/api/model/Rain.java new file mode 100644 index 0000000..9ad31ff --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/Rain.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.util.Objects; + +public class Rain { + + @JSONField(name = "3h") + // Rain volume for the last 3 hours + byte rainVolumeLast3Hrs; + + public byte getRainVolumeLast3Hrs() { + return rainVolumeLast3Hrs; + } + + public void setRainVolumeLast3Hrs(byte rainVolumeLast3Hrs) { + this.rainVolumeLast3Hrs = rainVolumeLast3Hrs; + } + + @Override + public String toString() { + return "Rain{" + + "rainVolumeLast3Hrs=" + rainVolumeLast3Hrs + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Rain rain = (Rain) o; + return rainVolumeLast3Hrs == rain.rainVolumeLast3Hrs; + } + + @Override + public int hashCode() { + + return Objects.hash(rainVolumeLast3Hrs); + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/Snow.java b/src/main/java/by/prominence/openweather/api/model/Snow.java new file mode 100644 index 0000000..fd8b787 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/Snow.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.util.Objects; + +public class Snow { + + @JSONField(name = "all") + // Snow volume for the last 3 hours + private byte snowVolumeLast3Hrs; + + public byte getSnowVolumeLast3Hrs() { + return snowVolumeLast3Hrs; + } + + public void setSnowVolumeLast3Hrs(byte snowVolumeLast3Hrs) { + this.snowVolumeLast3Hrs = snowVolumeLast3Hrs; + } + + @Override + public String toString() { + return "Snow{" + + "snowVolumeLast3Hrs=" + snowVolumeLast3Hrs + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Snow snow = (Snow) o; + return snowVolumeLast3Hrs == snow.snowVolumeLast3Hrs; + } + + @Override + public int hashCode() { + + return Objects.hash(snowVolumeLast3Hrs); + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/Weather.java b/src/main/java/by/prominence/openweather/api/model/Weather.java new file mode 100644 index 0000000..03214a7 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/Weather.java @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Objects; + +public class Weather { + + @JSONField(name = "id") + // Weather condition id + long conditionId; + + @JSONField(name = "main") + // Group of weather parameters (Rain, Snow, Extreme etc.) + String weatherGroup; + + @JSONField(name = "description") + // Weather condition within the group + String description; + + @JSONField(name = "icon") + // Weather icon id + String iconId; + + public long getConditionId() { + return conditionId; + } + + public void setConditionId(long conditionId) { + this.conditionId = conditionId; + } + + public String getWeatherGroup() { + return weatherGroup; + } + + public void setWeatherGroup(String weatherGroup) { + this.weatherGroup = weatherGroup; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getIconId() { + return iconId; + } + + public void setIconId(String iconId) { + this.iconId = iconId; + } + + public URL getWeatherIconUrl() { + URL iconUrl = null; + try { + iconUrl = new URL("http://openweathermap.org/img/w/" + iconId + ".png"); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return iconUrl; + } + + @Override + public String toString() { + return "Weather{" + + "conditionId=" + conditionId + + ", weatherGroup='" + weatherGroup + '\'' + + ", description='" + description + '\'' + + ", iconId='" + iconId + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Weather weather = (Weather) o; + return conditionId == weather.conditionId && + Objects.equals(weatherGroup, weather.weatherGroup) && + Objects.equals(description, weather.description) && + Objects.equals(iconId, weather.iconId); + } + + @Override + public int hashCode() { + + return Objects.hash(conditionId, weatherGroup, description, iconId); + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/WeatherInfo.java b/src/main/java/by/prominence/openweather/api/model/WeatherInfo.java new file mode 100644 index 0000000..90342a0 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/WeatherInfo.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.util.Objects; + +public class WeatherInfo { + + @JSONField(name = "temp") + // Temperature. Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. + private float temperature; + + @JSONField(name = "pressure") + // Atmospheric pressure (on the sea level, if there is no sea_level or grnd_level data), hPa + private short pressure; + + @JSONField(name = "humidity") + // Humidity, % + private byte humidity; + + @JSONField(name = "temp_min") + // Minimum temperature at the moment. This is deviation from current temp that is possible for large cities + // and megalopolises geographically expanded (use these parameter optionally). Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. + private float minimumTemperature; + + @JSONField(name = "temp_max") + // Maximum temperature at the moment. This is deviation from current temp that is possible for large cities + // and megalopolises geographically expanded (use these parameter optionally). Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. + private float maximumTemperature; + + @JSONField(name = "sea_level") + // Atmospheric pressure on the sea level, hPa + private short seaLevelPressure; + + @JSONField(name = "grnd_level") + // Atmospheric pressure on the ground level, hPa + private short groundLevelPressure; + + public float getTemperature() { + return temperature; + } + + public void setTemperature(float temperature) { + this.temperature = temperature; + } + + public short getPressure() { + return pressure; + } + + public void setPressure(short pressure) { + this.pressure = pressure; + } + + public byte getHumidity() { + return humidity; + } + + public void setHumidity(byte humidity) { + this.humidity = humidity; + } + + public float getMinimumTemperature() { + return minimumTemperature; + } + + public void setMinimumTemperature(float minimumTemperature) { + this.minimumTemperature = minimumTemperature; + } + + public float getMaximumTemperature() { + return maximumTemperature; + } + + public void setMaximumTemperature(float maximumTemperature) { + this.maximumTemperature = maximumTemperature; + } + + public short getSeaLevelPressure() { + return seaLevelPressure; + } + + public void setSeaLevelPressure(short seaLevelPressure) { + this.seaLevelPressure = seaLevelPressure; + } + + public short getGroundLevelPressure() { + return groundLevelPressure; + } + + public void setGroundLevelPressure(short groundLevelPressure) { + this.groundLevelPressure = groundLevelPressure; + } + + @Override + public String toString() { + return "WeatherInfo{" + + "temperature=" + temperature + + ", pressure=" + pressure + + ", humidity=" + humidity + "%" + + ", minimumTemperature=" + minimumTemperature + + ", maximumTemperature=" + maximumTemperature + + ", seaLevelPressure=" + seaLevelPressure + + ", groundLevelPressure=" + groundLevelPressure + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + WeatherInfo that = (WeatherInfo) o; + return Float.compare(that.temperature, temperature) == 0 && + pressure == that.pressure && + humidity == that.humidity && + Float.compare(that.minimumTemperature, minimumTemperature) == 0 && + Float.compare(that.maximumTemperature, maximumTemperature) == 0 && + seaLevelPressure == that.seaLevelPressure && + groundLevelPressure == that.groundLevelPressure; + } + + @Override + public int hashCode() { + + return Objects.hash(temperature, pressure, humidity, minimumTemperature, maximumTemperature, seaLevelPressure, groundLevelPressure); + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/WeatherResponse.java b/src/main/java/by/prominence/openweather/api/model/WeatherResponse.java new file mode 100644 index 0000000..ca47240 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/WeatherResponse.java @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.time.LocalTime; +import java.util.List; + +public class WeatherResponse { + + @JSONField(name = "id") + private long cityId; + + @JSONField(name = "name") + private String cityName; + + @JSONField(name = "coord") + private Coordinates coordinates; + + @JSONField(name = "weather") + private List weather; + + @JSONField(name = "base") + private String base; + + @JSONField(name = "main") + private WeatherInfo weatherInfo; + + @JSONField(name = "wind") + private Wind wind; + + @JSONField(name = "clouds") + private Clouds clouds; + + @JSONField(name = "rain") + private Rain rain; + + @JSONField(name = "snow") + private Snow snow; + + @JSONField(name = "dt") + private LocalTime dataCalculationTime; + + @JSONField(name = "sys") + private WeatherSystemInfo weatherSystemInfo; + + @JSONField(name = "cod") + private short responseCode; + + public long getCityId() { + return cityId; + } + + public void setCityId(long cityId) { + this.cityId = cityId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public Coordinates getCoordinates() { + return coordinates; + } + + public void setCoordinates(Coordinates coordinates) { + this.coordinates = coordinates; + } + + public List getWeather() { + return weather; + } + + public void setWeather(List weather) { + this.weather = weather; + } + + public String getBase() { + return base; + } + + public void setBase(String base) { + this.base = base; + } + + public WeatherInfo getWeatherInfo() { + return weatherInfo; + } + + public void setWeatherInfo(WeatherInfo weatherInfo) { + this.weatherInfo = weatherInfo; + } + + public Wind getWind() { + return wind; + } + + public void setWind(Wind wind) { + this.wind = wind; + } + + public Clouds getClouds() { + return clouds; + } + + public void setClouds(Clouds clouds) { + this.clouds = clouds; + } + + public Rain getRain() { + return rain; + } + + public void setRain(Rain rain) { + this.rain = rain; + } + + public Snow getSnow() { + return snow; + } + + public void setSnow(Snow snow) { + this.snow = snow; + } + + public LocalTime getDataCalculationTime() { + return dataCalculationTime; + } + + public void setDataCalculationTime(LocalTime dataCalculationTime) { + this.dataCalculationTime = dataCalculationTime; + } + + public WeatherSystemInfo getWeatherSystemInfo() { + return weatherSystemInfo; + } + + public void setWeatherSystemInfo(WeatherSystemInfo weatherSystemInfo) { + this.weatherSystemInfo = weatherSystemInfo; + } + + public short getResponseCode() { + return responseCode; + } + + public void setResponseCode(short responseCode) { + this.responseCode = responseCode; + } + + @Override + public String toString() { + return "WeatherResponse{" + + "cityId=" + cityId + + ",\n cityName='" + cityName + '\'' + + ",\n coordinates=" + coordinates + + ",\n weather=" + weather + + ",\n base='" + base + '\'' + + ",\n weatherInfo=" + weatherInfo + + ",\n wind=" + wind + + ",\n clouds=" + clouds + + ",\n rain=" + rain + + ",\n snow=" + snow + + ",\n dataCalculationTime=" + dataCalculationTime + + ",\n weatherSystemInfo=" + weatherSystemInfo + + ",\n responseCode=" + responseCode + + '}'; + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/WeatherSystemInfo.java b/src/main/java/by/prominence/openweather/api/model/WeatherSystemInfo.java new file mode 100644 index 0000000..6efd776 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/WeatherSystemInfo.java @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.time.LocalTime; +import java.util.Objects; + +public class WeatherSystemInfo { + + @JSONField(name = "type") + // Internal parameter + private short type; + + @JSONField(name = "id") + // Internal parameter + private long id; + + @JSONField(name = "message") + // Internal parameter + private double message; + + @JSONField(name = "country") + // Country code (GB, JP etc.) + private String country; + + @JSONField(name = "sunrise") + // Sunrise time, unix, UTC + private LocalTime sunrise; + + @JSONField(name = "sunset") + // Sunset time, unix, UTC + private LocalTime sunset; + + public short getType() { + return type; + } + + public void setType(short type) { + this.type = type; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public double getMessage() { + return message; + } + + public void setMessage(double message) { + this.message = message; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public LocalTime getSunrise() { + return sunrise; + } + + public void setSunrise(LocalTime sunrise) { + this.sunrise = sunrise; + } + + public LocalTime getSunset() { + return sunset; + } + + public void setSunset(LocalTime sunset) { + this.sunset = sunset; + } + + @Override + public String toString() { + return "WeatherSystemInfo{" + + "type=" + type + + ", id=" + id + + ", message=" + message + + ", country='" + country + '\'' + + ", sunrise=" + sunrise + + ", sunset=" + sunset + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + WeatherSystemInfo that = (WeatherSystemInfo) o; + return type == that.type && + id == that.id && + Double.compare(that.message, message) == 0 && + Objects.equals(country, that.country) && + Objects.equals(sunrise, that.sunrise) && + Objects.equals(sunset, that.sunset); + } + + @Override + public int hashCode() { + + return Objects.hash(type, id, message, country, sunrise, sunset); + } +} diff --git a/src/main/java/by/prominence/openweather/api/model/Wind.java b/src/main/java/by/prominence/openweather/api/model/Wind.java new file mode 100644 index 0000000..4ee7562 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/model/Wind.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.model; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.util.Objects; + +public class Wind { + + @JSONField(name = "speed") + // Wind speed. Unit Default: meter/sec, Metric: meter/sec, Imperial: miles/hour. + private float speed; + + @JSONField(name = "deg") + // Wind direction, degrees (meteorological) + private short degrees; + + public float getSpeed() { + return speed; + } + + public void setSpeed(float speed) { + this.speed = speed; + } + + public short getDegrees() { + return degrees; + } + + public void setDegrees(short degrees) { + this.degrees = degrees; + } + + @Override + public String toString() { + return "Wind{" + + "speed=" + speed + + ", degrees=" + degrees + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Wind wind = (Wind) o; + return Float.compare(wind.speed, speed) == 0 && + degrees == wind.degrees; + } + + @Override + public int hashCode() { + + return Objects.hash(speed, degrees); + } +} diff --git a/src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java b/src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java new file mode 100644 index 0000000..b62ec29 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.provider; + +import by.prominence.openweather.api.model.Coordinates; +import by.prominence.openweather.api.model.WeatherResponse; +import by.prominence.openweather.api.utils.JsonUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; + +import java.io.IOException; + +public class DefaultWeatherProvider implements WeatherProvider { + + private static final String OPEN_WEATHER_API_VERSION = "2.5"; + private static final String OPEN_WEATHER_API_URL = "http://api.openweathermap.org/data/" + OPEN_WEATHER_API_VERSION + "/weather"; + + private final String authToken; + + public DefaultWeatherProvider(String authToken) { + this.authToken = authToken; + } + + public WeatherResponse getByCityId(String id) { + return executeRequest("?id=" + id); + } + + public WeatherResponse getByCityName(String name) { + return executeRequest("?q=" + name); + } + + public WeatherResponse getByCoordinates(double latitude, double longitude) { + return executeRequest("?lat=" + latitude + "&lon=" + longitude); + } + + public WeatherResponse getByCoordinates(Coordinates coordinates) { + return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude()); + } + + public WeatherResponse getByZIPCode(String zipCode, String countryCode) { + return executeRequest("?zip=" + zipCode + "," + countryCode); + } + + private WeatherResponse executeRequest(String parameterString) { + + String url = OPEN_WEATHER_API_URL + parameterString + "&appid=" + authToken; + + HttpClient httpClient = HttpClientBuilder.create().build(); + HttpGet request = new HttpGet(url); + HttpResponse response = null; + + try { + response = httpClient.execute(request); + } catch (IOException e) { + e.printStackTrace(); + } + + WeatherResponse weatherResponse = null; + if (response != null) { + try { + weatherResponse = (WeatherResponse)JsonUtils.parseJson(response.getEntity().getContent(), WeatherResponse.class); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return weatherResponse; + } +} diff --git a/src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java b/src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java new file mode 100644 index 0000000..1e66303 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.provider; + +import by.prominence.openweather.api.model.Coordinates; +import by.prominence.openweather.api.model.WeatherResponse; + +public interface WeatherProvider { + + public WeatherResponse getByCityId(String id); + public WeatherResponse getByCityName(String name); + public WeatherResponse getByCoordinates(double latitude, double longitude); + public WeatherResponse getByCoordinates(Coordinates coordinates); + public WeatherResponse getByZIPCode(String zipCode, String countryCode); + +} diff --git a/src/main/java/by/prominence/openweather/api/utils/JsonUtils.java b/src/main/java/by/prominence/openweather/api/utils/JsonUtils.java new file mode 100644 index 0000000..c015cf6 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/utils/JsonUtils.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.utils; + +import com.alibaba.fastjson.JSON; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class JsonUtils { + + public static Object parseJson(InputStream inputStream, Class clazz) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + + StringBuffer result = new StringBuffer(); + String line = ""; + while ((line = reader.readLine()) != null) { + result.append(line); + } + + return JSON.parseObject(result.toString(), clazz); + } +}