This commit is contained in:
Minecon724 2024-05-30 13:16:56 +02:00
parent 21a3ed1563
commit f88d3f1023
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
6 changed files with 61 additions and 38 deletions

View file

@ -8,7 +8,7 @@ public class Coordinates {
public double latitude, longitude; public double latitude, longitude;
public Coordinates(double latitude, double longitude) { public Coordinates(double latitude, double longitude) {
this.latitude = latitude; this.latitude = (((latitude + 90) % 180) + 180) % 180 - 90;
this.longitude = longitude; this.longitude = (((longitude + 180) % 360) + 360) % 360 - 180;
} }
} }

View file

@ -4,11 +4,52 @@ package eu.m724.wtapi.object;
* contains weather conditions * contains weather conditions
*/ */
public class Weather { public class Weather {
public WeatherState weatherState; public Coordinates coordinates;
public float level = 1;
public Weather(WeatherState weatherState, float level) { public Severity rainSeverity, thunderstormSeverity, snowSeverity;
this.weatherState = weatherState;
this.level = level; // secondary conditions
public Severity sleetSeverity, drizzleSeverity;
public boolean shower;
/**
* in kelvin
*/
public float temperature, temperatureApparent;
/**
* in meters per second
*/
public float windSpeed, windGust;
/**
* 0.0 - 1.0
*/
public float humidity, cloudiness;
/**
* as unix timestamp
*/
public long sunrise, sunset;
public String city;
/**
* short name of weather
*/
public String description;
public boolean isRaining() {
return rainSeverity != null;
}
public boolean isThundering() {
return thunderstormSeverity != null;
}
public boolean isSnowing() {
return snowSeverity != null;
} }
} }

View file

@ -1,6 +1,5 @@
package eu.m724.wtapi.provider; package eu.m724.wtapi.provider;
import java.time.Duration;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Coordinates;
@ -39,10 +38,11 @@ public abstract class WeatherProvider {
public abstract int getQuotaHourly(); public abstract int getQuotaHourly();
/** /**
* how many requests actually took place last time you called a method * how many coordinates in one bulk request
* @return amount of requests, 0 if no calls happened * this is because some apis don't support bulk or limit it
* @return amount of coordinates per bulk request
*/ */
public abstract int getLastRequestQuota(); public abstract int getBulkLimit();
/** /**
* estimates minimum delay between calls given last request * estimates minimum delay between calls given last request

View file

@ -1,8 +1,8 @@
package eu.m724.wtapi.provider.exception; package eu.m724.wtapi.provider.exception;
import java.util.concurrent.ExecutionException; import java.util.concurrent.CompletionException;
public class ProviderException extends ExecutionException { public class ProviderException extends CompletionException {
private static final long serialVersionUID = -841882181122537157L; private static final long serialVersionUID = -841882181122537157L;

View file

@ -22,8 +22,9 @@ public class ProviderTest {
public void testProvider() throws InterruptedException, ExecutionException { public void testProvider() throws InterruptedException, ExecutionException {
WeatherProvider provider = new MockWeatherProvider(false); WeatherProvider provider = new MockWeatherProvider(false);
provider.init(); provider.init();
assert provider.getQuotaHourly() == 5; assert provider.getQuotaHourly() == 5;
assert provider.getLastRequestQuota() == 0; assert provider.getBulkLimit() == 1;
CompletableFuture<Weather> weatherFuture = CompletableFuture<Weather> weatherFuture =
provider.getWeather(new Coordinates(0, 0)); provider.getWeather(new Coordinates(0, 0));
@ -31,9 +32,6 @@ public class ProviderTest {
Weather weather = weatherFuture.get(); Weather weather = weatherFuture.get();
assertNotNull(weather); assertNotNull(weather);
assert provider.getLastRequestQuota() == 1;
CompletableFuture<Weather[]> weathersFuture = CompletableFuture<Weather[]> weathersFuture =
provider.getWeatherBulk(new Coordinates[] { provider.getWeatherBulk(new Coordinates[] {
new Coordinates(0, 0), new Coordinates(0, 0),
@ -45,14 +43,11 @@ public class ProviderTest {
assert weathers.length == 2; assert weathers.length == 2;
assertNotNull(weathers[0]); assertNotNull(weathers[0]);
assertNotNull(weathers[1]); assertNotNull(weathers[1]);
assert provider.getLastRequestQuota() == 2;
} }
@Test @Test
public void testFaultyProvider() throws InterruptedException { public void testFaultyProvider() throws InterruptedException {
WeatherProvider provider = new MockWeatherProvider(true); WeatherProvider provider = new MockWeatherProvider(true);
assert provider.getLastRequestQuota() == 0;
CompletableFuture<Weather> weatherFuture = CompletableFuture<Weather> weatherFuture =
provider.getWeather(new Coordinates(0, 0)); provider.getWeather(new Coordinates(0, 0));
@ -68,9 +63,6 @@ public class ProviderTest {
assertNull(weather); assertNull(weather);
assert provider.getLastRequestQuota() == 1;
Weather[] weathers = null; Weather[] weathers = null;
CompletableFuture<Weather[]> weathersFuture = CompletableFuture<Weather[]> weathersFuture =
@ -87,7 +79,6 @@ public class ProviderTest {
assert qee.getRetryIn() == 60; assert qee.getRetryIn() == 60;
} }
assert provider.getLastRequestQuota() == 2;
assertNull(weathers); assertNull(weathers);
} }

View file

@ -1,20 +1,14 @@
package eu.m724.wtapi.impl; package eu.m724.wtapi.impl;
import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Weather; import eu.m724.wtapi.object.Weather;
import eu.m724.wtapi.object.WeatherState;
import eu.m724.wtapi.provider.WeatherProvider; import eu.m724.wtapi.provider.WeatherProvider;
import eu.m724.wtapi.provider.exception.ProviderException; import eu.m724.wtapi.provider.exception.ProviderException;
import eu.m724.wtapi.provider.exception.QuotaExceededException; import eu.m724.wtapi.provider.exception.QuotaExceededException;
import eu.m724.wtapi.provider.exception.ServerProviderException; import eu.m724.wtapi.provider.exception.ServerProviderException;
public class MockWeatherProvider extends WeatherProvider { public class MockWeatherProvider extends WeatherProvider {
private int req; // THIS IS NOT HOW IT SHOULD BE DONE
private boolean faulty; private boolean faulty;
public MockWeatherProvider(boolean faulty) { public MockWeatherProvider(boolean faulty) {
@ -28,8 +22,6 @@ public class MockWeatherProvider extends WeatherProvider {
@Override @Override
public CompletableFuture<Weather> getWeather(Coordinates coordinates) { public CompletableFuture<Weather> getWeather(Coordinates coordinates) {
req++;
if (coordinates == null) if (coordinates == null)
throw new NullPointerException("no coordinates passed"); throw new NullPointerException("no coordinates passed");
@ -42,7 +34,7 @@ public class MockWeatherProvider extends WeatherProvider {
if (faulty) if (faulty)
completableFuture.completeExceptionally(new ServerProviderException("server is on vacation rn")); completableFuture.completeExceptionally(new ServerProviderException("server is on vacation rn"));
else else
completableFuture.complete(new Weather(WeatherState.CLEAR, 0)); completableFuture.complete(new Weather());
return completableFuture; return completableFuture;
} }
@ -50,7 +42,6 @@ public class MockWeatherProvider extends WeatherProvider {
@Override @Override
public CompletableFuture<Weather[]> getWeatherBulk(Coordinates[] coordinateses) { public CompletableFuture<Weather[]> getWeatherBulk(Coordinates[] coordinateses) {
int len = coordinateses.length; int len = coordinateses.length;
req++;
if (len == 0) if (len == 0)
throw new IllegalArgumentException("no coordinates passed"); throw new IllegalArgumentException("no coordinates passed");
@ -67,7 +58,7 @@ public class MockWeatherProvider extends WeatherProvider {
if (coordinateses[i] == null) if (coordinateses[i] == null)
throw new IllegalArgumentException("a coordinate is null"); throw new IllegalArgumentException("a coordinate is null");
weathers[i] = new Weather(WeatherState.CLEAR, 1); weathers[i] = new Weather();
} }
if (faulty) if (faulty)
@ -84,8 +75,8 @@ public class MockWeatherProvider extends WeatherProvider {
} }
@Override @Override
public int getLastRequestQuota() { public int getBulkLimit() {
return this.req; // ONCE AGAIN TJHIS IS NOT HOW IT SHOULD BE DONE return 1;
} }
} }