This commit is contained in:
Minecon724 2024-06-10 16:02:43 +02:00
parent 1cf35bc45d
commit eb09e65f92
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
6 changed files with 38 additions and 7 deletions

View file

@ -9,7 +9,6 @@ import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;

View file

@ -7,6 +7,10 @@ import eu.m724.wtapi.provider.exception.ProviderException;
public abstract class ThunderProvider { public abstract class ThunderProvider {
/**
* initialize and test provider
* @throws ProviderException
*/
public abstract void init() throws ProviderException; public abstract void init() throws ProviderException;
/** /**
@ -32,4 +36,10 @@ public abstract class ThunderProvider {
* @return delay in ms * @return delay in ms
*/ */
public abstract int getDelay(); public abstract int getDelay();
/**
* this is not {@link/getDelay}
* @return latency to api in ms
*/
public abstract long getLatency();
} }

View file

@ -15,6 +15,9 @@ public class BlitzortungProvider extends ThunderProvider {
private long reconnectPending; private long reconnectPending;
private final int delay = 10000; private final int delay = 10000;
private long latency;
private int latencySamplesCount;
@Override @Override
public void init() throws ProviderException { public void init() throws ProviderException {
@ -75,7 +78,17 @@ public class BlitzortungProvider extends ThunderProvider {
} }
void submitStrike(Coordinates coordinates, long timestamp) { void submitStrike(Coordinates coordinates, long timestamp) {
long now = System.currentTimeMillis();
long delay = now - timestamp;
latency += (delay - latency) / ++latencySamplesCount;
strikes.add(new TimedStrike(timestamp, coordinates)); strikes.add(new TimedStrike(timestamp, coordinates));
} }
@Override
public long getLatency() {
return latency;
}
} }

View file

@ -9,14 +9,13 @@ import eu.m724.wtapi.thunder.impl.blitzortung.BlitzortungProvider;
public class BlitzortungTest { public class BlitzortungTest {
@Test @Test
public void lightningMapsTest() throws InterruptedException { public void blitzortungTest() throws InterruptedException {
ArrayList<Coordinates> coordinatesList = new ArrayList<>(); ArrayList<Coordinates> coordinatesList = new ArrayList<>();
ThunderProvider provider = new BlitzortungProvider(); ThunderProvider provider = new BlitzortungProvider();
provider.registerStrikeHandler(coordinates -> { provider.registerStrikeHandler(coordinates -> {
coordinatesList.add(coordinates); coordinatesList.add(coordinates);
System.out.printf("%f %f\n", coordinates.latitude, coordinates.longitude);
}); });
provider.init(); provider.init();
@ -25,12 +24,14 @@ public class BlitzortungTest {
for (int i=0; i < 100; i++) { for (int i=0; i < 100; i++) {
provider.tick(); provider.tick();
int size = coordinatesList.size();
if (size > 0)
System.out.printf("Last from tick: %f %f (total %d)\n", coordinatesList.get(size-1).latitude, coordinatesList.get(size-1).longitude, size);
Thread.sleep(100); Thread.sleep(100);
} }
System.out.printf("Strikes in the last 10s: %d\n", coordinatesList.size()); System.out.printf("Strikes in the last 10s: %d\n", coordinatesList.size());
if (coordinatesList.size() > 0) // lightningmaps can be unreliable System.out.printf("Latency: %dms\n", provider.getLatency());
System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude);
} }
} }

View file

@ -69,4 +69,9 @@ public class MockThunderProvider extends ThunderProvider {
return 10000; return 10000;
} }
@Override
public long getLatency() {
return 690;
}
} }

View file

@ -22,13 +22,16 @@ public class ThunderProviderTest {
for (int i=0; i < 50; i++) { for (int i=0; i < 50; i++) {
provider.tick(); provider.tick();
int size = coordinatesList.size();
if (size > 0)
System.out.printf("Last from tick: %f %f (total %d)\n", coordinatesList.get(size-1).latitude, coordinatesList.get(size-1).longitude, size);
Thread.sleep(50); Thread.sleep(50);
} }
provider.stop(); provider.stop();
System.out.printf("Strikes in the last 5s: %d\n", coordinatesList.size()); System.out.printf("Strikes in the last 2.5s: %d\n", coordinatesList.size());
System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude); System.out.printf("Latency: %dms\n", provider.getLatency());
assert coordinatesList.size() == 25; assert coordinatesList.size() == 25;
} }