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.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

View file

@ -7,6 +7,10 @@ import eu.m724.wtapi.provider.exception.ProviderException;
public abstract class ThunderProvider {
/**
* initialize and test provider
* @throws ProviderException
*/
public abstract void init() throws ProviderException;
/**
@ -32,4 +36,10 @@ public abstract class ThunderProvider {
* @return delay in ms
*/
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 final int delay = 10000;
private long latency;
private int latencySamplesCount;
@Override
public void init() throws ProviderException {
@ -75,7 +78,17 @@ public class BlitzortungProvider extends ThunderProvider {
}
void submitStrike(Coordinates coordinates, long timestamp) {
long now = System.currentTimeMillis();
long delay = now - timestamp;
latency += (delay - latency) / ++latencySamplesCount;
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 {
@Test
public void lightningMapsTest() throws InterruptedException {
public void blitzortungTest() throws InterruptedException {
ArrayList<Coordinates> coordinatesList = new ArrayList<>();
ThunderProvider provider = new BlitzortungProvider();
provider.registerStrikeHandler(coordinates -> {
coordinatesList.add(coordinates);
System.out.printf("%f %f\n", coordinates.latitude, coordinates.longitude);
});
provider.init();
@ -25,12 +24,14 @@ public class BlitzortungTest {
for (int i=0; i < 100; i++) {
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);
}
System.out.printf("Strikes in the last 10s: %d\n", coordinatesList.size());
if (coordinatesList.size() > 0) // lightningmaps can be unreliable
System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude);
System.out.printf("Latency: %dms\n", provider.getLatency());
}
}

View file

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

View file

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