feat: documentation

This commit is contained in:
Martin Eyben 2024-11-19 17:59:45 +01:00
parent d263a3b68f
commit decbd83316
9 changed files with 152 additions and 8 deletions

View File

@ -13,6 +13,10 @@ import org.json.JSONObject;
import eirb.pg203.utils.Coords; import eirb.pg203.utils.Coords;
/**
* Representation of a city
* Possibility to get city coordinates based on the name
*/
public class City { public class City {
private String cityName; private String cityName;
private Coords cityCoords; private Coords cityCoords;
@ -49,6 +53,10 @@ public class City {
this.cityName = cityName; this.cityName = cityName;
} }
/**
* Get city name
* @return city name string
*/
public String getCityName() { public String getCityName() {
return cityName; return cityName;
} }

View File

@ -1,12 +1,19 @@
package eirb.pg203; package eirb.pg203;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
/**
* Main class
*/
public class Main { public class Main {
/**
* Default constructor (private)
*/
private Main() {};
private static class Option { private static class Option {
String flag, value; String flag, value;
public Option(String flag, String value){ public Option(String flag, String value){
@ -27,7 +34,13 @@ public class Main {
return value; return value;
} }
} }
public static void main(String[] args) throws IOException, IllegalArgumentException{
/**
* Main loop
* @param args list of arguments
* @throws IllegalArgumentException if arguments are not provided or in a wrong way
*/
public static void main(String[] args) throws IllegalArgumentException{
HashMap<String, Option> options = new HashMap<>(); HashMap<String, Option> options = new HashMap<>();
List<String> argsList = new ArrayList<>(); List<String> argsList = new ArrayList<>();

View File

@ -14,10 +14,19 @@ import eirb.pg203.WeatherData.Condition;
// https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m // https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m
/**
* OpenMeteo implementation
*/
public class OpenMeteo implements WeatherDataAPI { public class OpenMeteo implements WeatherDataAPI {
private static final String forecastBaseURL = "https://api.open-meteo.com/v1/forecast"; private static final String forecastBaseURL = "https://api.open-meteo.com/v1/forecast";
private static final String dailyQuery = "weather_code,temperature_2m_max,temperature_2m_min,wind_speed_10m_max,wind_direction_10m_dominant"; private static final String dailyQuery = "weather_code,temperature_2m_max,temperature_2m_min,wind_speed_10m_max,wind_direction_10m_dominant";
/**
* Default constructor
*/
public OpenMeteo() {}
// https://www.nodc.noaa.gov/archive/arc0021/0002199/1.1/data/0-data/HTML/WMO-CODE/WMO4677.HTM // https://www.nodc.noaa.gov/archive/arc0021/0002199/1.1/data/0-data/HTML/WMO-CODE/WMO4677.HTM
private JSONObject fetchWeather(int days, City city) throws IOException { private JSONObject fetchWeather(int days, City city) throws IOException {
URL url = URI.create( URL url = URI.create(

View File

@ -14,8 +14,9 @@ import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import eirb.pg203.WeatherData.Condition; import eirb.pg203.WeatherData.Condition;
// https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m /**
* OpenWeatherMap api implementation
*/
public class OpenWeatherMap implements WeatherDataAPI { public class OpenWeatherMap implements WeatherDataAPI {
private static final String forecastBaseURL = "https://api.openweathermap.org/data/2.5/forecast"; private static final String forecastBaseURL = "https://api.openweathermap.org/data/2.5/forecast";
private String APIKey; private String APIKey;

View File

@ -3,16 +3,36 @@ package eirb.pg203;
import java.time.Instant; import java.time.Instant;
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Condition;
/**
* Weather Data representation
* A weather data is a Temperature, on a date, in a city, with a weather condition and wind speed + direction
* The representation of the data is managed in this class.
*/
public class WeatherData { public class WeatherData {
/** /**
* Representation of a weather condition (with a smiley for String representation) * Representation of a weather condition (with a smiley for String representation)
*/ */
public enum Condition { public enum Condition {
/**
* Sunny condition
*/
SUNNY("☀️"), SUNNY("☀️"),
/**
* A little a bit of sun and a little bit of clouds
*/
PARTIAL("🌤"), PARTIAL("🌤"),
/**
* Cloudy weather
*/
CLOUDY("☁️"), CLOUDY("☁️"),
/**
* Rainy weather -> like most of the time in Bordeaux
*/
RAINY("🌧"), RAINY("🌧"),
/**
* Impossible to determine a Weather Condition
*/
ERROR("E"); ERROR("E");
private final String desc; private final String desc;
@ -28,14 +48,42 @@ public class WeatherData {
* Representation of the wind direction with an arrow * Representation of the wind direction with an arrow
*/ */
public enum WindDirection { public enum WindDirection {
/**
* North direction
*/
N("🡩"), N("🡩"),
/**
* North East direction
*/
NE("🡭"), NE("🡭"),
/**
* East direction
*/
E("🡪"), E("🡪"),
/**
* South East direction
*/
SE("🡮"), SE("🡮"),
/**
* South direction
*/
S("🡫"), S("🡫"),
/**
* South West direction
*/
SW("🡯"), SW("🡯"),
/**
* West direction
*/
W("🡨"), W("🡨"),
/**
* North West direction
*/
NW("🡬"), NW("🡬"),
/**
* Error wind direction
*/
ERROR("E"); ERROR("E");
private final String desc; private final String desc;
@ -93,6 +141,7 @@ public class WeatherData {
} }
/** /**
* Get city from where the weather data come from
* @return city * @return city
*/ */
public City getCity() { public City getCity() {
@ -100,7 +149,7 @@ public class WeatherData {
} }
/** /**
* * Get date of the weather data
* @return date of the Weather data * @return date of the Weather data
*/ */
public Instant getDate() { public Instant getDate() {
@ -108,6 +157,7 @@ public class WeatherData {
} }
/** /**
* Get weather condition representation
* @return Weather Condition representation * @return Weather Condition representation
*/ */
public Condition getCondition() { public Condition getCondition() {
@ -115,6 +165,7 @@ public class WeatherData {
} }
/** /**
* Get temperature value
* @return float temperature * @return float temperature
*/ */
public float getTemp() { public float getTemp() {
@ -122,6 +173,7 @@ public class WeatherData {
} }
/** /**
* Get wind speed value
* @return wind speed * @return wind speed
*/ */
public float getWindSpeed() { public float getWindSpeed() {
@ -129,11 +181,13 @@ public class WeatherData {
} }
/** /**
* Get wind direction angle value
* @return Wind direction angle * @return Wind direction angle
*/ */
public float getWindDirectionAngle() {return this.windDirectionAngle;} public float getWindDirectionAngle() {return this.windDirectionAngle;}
/** /**
* Get wind direction representation
* @return wind direction representation * @return wind direction representation
*/ */
public WindDirection getWindDirection() { public WindDirection getWindDirection() {
@ -165,6 +219,7 @@ public class WeatherData {
} }
/** /**
* Set temperature value
* @param temp Weather data temperature * @param temp Weather data temperature
*/ */
public void setTemp(float temp) { public void setTemp(float temp) {
@ -172,6 +227,7 @@ public class WeatherData {
} }
/** /**
* Set wind speed value
* @param windSpeed Wind speed * @param windSpeed Wind speed
*/ */
public void setWindSpeed(float windSpeed) { public void setWindSpeed(float windSpeed) {
@ -179,6 +235,7 @@ public class WeatherData {
} }
/** /**
* Set wind direction angle value
* @param windDirectionAngle wind direction angle * @param windDirectionAngle wind direction angle
*/ */
public void setWindDirectionAngle(float windDirectionAngle) { public void setWindDirectionAngle(float windDirectionAngle) {

View File

@ -18,7 +18,7 @@ public interface WeatherDataAPI {
WeatherData getTemperature(int day, String city) throws IOException; WeatherData getTemperature(int day, String city) throws IOException;
/** /**
* * Get WeatherData for a specific day
* @param day Since D+0 * @param day Since D+0
* @param hour Since H+0 * @param hour Since H+0
* @param city Localisation * @param city Localisation

View File

@ -1,6 +1,13 @@
package eirb.pg203.exceptions; package eirb.pg203.exceptions;
/**
* Exception when an error during the api call
*/
public class WeatherFetchingException extends Exception { public class WeatherFetchingException extends Exception {
/**
* Weather Fetching exception
* @param message message of the exception
*/
public WeatherFetchingException(String message) { public WeatherFetchingException(String message) {
super(message); super(message);
} }

View File

@ -1,26 +1,50 @@
package eirb.pg203.utils; package eirb.pg203.utils;
/**
* Coordinates representation
*/
public class Coords { public class Coords {
private float lat; private float lat;
private float lon; private float lon;
/**
* Coordinates representation
* @param lat latitude
* @param lon longitude
*/
public Coords(float lat, float lon) { public Coords(float lat, float lon) {
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
} }
/**
* Get latitude
* @return latitude
*/
public float getLat() { public float getLat() {
return lat; return lat;
} }
/**
* Get longitude
* @return longitude
*/
public float getLon() { public float getLon() {
return lon; return lon;
} }
/**
* Set latitude
* @param lat latitude
*/
public void setLat(float lat) { public void setLat(float lat) {
this.lat = lat; this.lat = lat;
} }
/**
* Set longitude
* @param lon longitude
*/
public void setLon(float lon) { public void setLon(float lon) {
this.lon = lon; this.lon = lon;
} }

View File

@ -9,8 +9,21 @@ import java.net.URL;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
/**
* Util for http calls
*/
public class JSONFetcher { public class JSONFetcher {
/**
* No need for constructor
*/
private JSONFetcher() {};
/**
* Make the request
* @param url url to fetch
* @return String of the response
* @throws IOException if the request failed
*/
private static String fetchString(URL url) throws IOException{ private static String fetchString(URL url) throws IOException{
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
@ -24,13 +37,25 @@ public class JSONFetcher {
return result.toString(); return result.toString();
} }
/**
* Fetch an url
* @param url url
* @return Json object of the response
* @throws IOException if the request failed
*/
public static JSONObject fetch(URL url) throws IOException { public static JSONObject fetch(URL url) throws IOException {
String result = fetchString(url); String result = fetchString(url);
return new JSONObject(result); return new JSONObject(result);
} }
/**
* Fetch a Json array from an url
* @param url url
* @return JSON array
* @throws IOException when request failed
*/
public static JSONArray fetchArray(URL url) throws IOException { public static JSONArray fetchArray(URL url) throws IOException {
String result = fetchString(url); String result = fetchString(url);