Skip to content

Commit

Permalink
Merge pull request #151 from xspanger3770/develop
Browse files Browse the repository at this point in the history
Release 0.9.8
  • Loading branch information
xspanger3770 committed Oct 10, 2023
2 parents 017ed50 + 12302bf commit 8ac764c
Show file tree
Hide file tree
Showing 96 changed files with 2,871 additions and 1,111 deletions.
57 changes: 0 additions & 57 deletions src/main/java/globalquake/core/AlertManager.java

This file was deleted.

23 changes: 19 additions & 4 deletions src/main/java/globalquake/core/GlobalQuake.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package globalquake.core;

import globalquake.core.alert.AlertManager;
import globalquake.core.earthquake.ClusterAnalysis;
import globalquake.core.earthquake.Earthquake;
import globalquake.core.earthquake.data.Earthquake;
import globalquake.core.earthquake.EarthquakeAnalysis;
import globalquake.core.earthquake.EarthquakeArchive;
import globalquake.core.archive.EarthquakeArchive;
import globalquake.core.station.GlobalStationManager;
import globalquake.database.StationDatabaseManager;
import globalquake.events.GlobalQuakeEventHandler;
import globalquake.main.Main;
import globalquake.ui.globalquake.GlobalQuakeFrame;

Expand All @@ -23,6 +25,9 @@ public class GlobalQuake {
private final EarthquakeAnalysis earthquakeAnalysis;
private final AlertManager alertManager;
private final EarthquakeArchive archive;

private final GlobalQuakeEventHandler eventHandler;

public static GlobalQuake instance;

private final GlobalStationManager globalStationManager;
Expand All @@ -31,14 +36,16 @@ public GlobalQuake(StationDatabaseManager stationDatabaseManager) {
instance = this;
this.stationDatabaseManager = stationDatabaseManager;

eventHandler = new GlobalQuakeEventHandler().runHandler();

globalStationManager = new GlobalStationManager();
globalStationManager.initStations(stationDatabaseManager);

earthquakeAnalysis = new EarthquakeAnalysis();
clusterAnalysis = new ClusterAnalysis();

alertManager = new AlertManager();
archive = new EarthquakeArchive();
archive = new EarthquakeArchive().loadArchive();

globalQuakeRuntime = new GlobalQuakeRuntime();
seedlinkNetworksReader = new SeedlinkNetworksReader();
Expand Down Expand Up @@ -66,7 +73,7 @@ public void windowClosing(WindowEvent e) {
getArchive().saveArchive();
}
});
});
});
return this;
}

Expand Down Expand Up @@ -105,4 +112,12 @@ public SeedlinkNetworksReader getSeedlinkReader() {
public StationDatabaseManager getStationDatabaseManager() {
return stationDatabaseManager;
}

public GlobalQuakeEventHandler getEventHandler() {
return eventHandler;
}

public GlobalQuakeFrame getGlobalQuakeFrame() {
return globalQuakeFrame;
}
}
9 changes: 0 additions & 9 deletions src/main/java/globalquake/core/GlobalQuakeRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import globalquake.core.station.AbstractStation;
import globalquake.main.Main;
import globalquake.ui.settings.Settings;
import globalquake.utils.NamedThreadFactory;
import org.tinylog.Logger;

Expand All @@ -19,8 +18,6 @@ public class GlobalQuakeRuntime {
private long clusterAnalysisT;
private long lastQuakesT;

private int lastSettings = Settings.changes;

public void runThreads() {
ScheduledExecutorService execAnalysis = Executors
.newSingleThreadScheduledExecutor(new NamedThreadFactory("Station Analysis Thread"));
Expand Down Expand Up @@ -50,12 +47,6 @@ public void runThreads() {
GlobalQuake.instance.getEarthquakeAnalysis().second();
}
lastSecond = System.currentTimeMillis() - a;

if(lastSettings != Settings.changes){
System.gc();
}

lastSettings = Settings.changes;
} catch (Exception e) {
Logger.error("Exception occurred in 1-second loop");
Main.getErrorHandler().handleException(e);
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/globalquake/core/alert/AlertManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package globalquake.core.alert;

import java.util.*;

import globalquake.core.GlobalQuake;
import globalquake.core.earthquake.data.Earthquake;
import globalquake.events.GlobalQuakeEventAdapter;
import globalquake.events.specific.AlertIssuedEvent;
import globalquake.events.specific.ClusterCreateEvent;
import globalquake.events.specific.QuakeCreateEvent;
import globalquake.events.specific.QuakeUpdateEvent;
import globalquake.ui.settings.Settings;
import globalquake.geo.GeoUtils;

public class AlertManager {
public static final int STORE_TIME_MINUTES = 2 * 60;
private final Map<Warnable, Warning> warnings;

public AlertManager() {
this.warnings = new HashMap<>();

GlobalQuake.instance.getEventHandler().registerEventListener(new GlobalQuakeEventAdapter(){
@Override
public void onQuakeCreate(QuakeCreateEvent event) {
tick();
}

@Override
public void onClusterCreate(ClusterCreateEvent event) {
tick();
}

@SuppressWarnings("unused")
@Override
public void onQuakeUpdate(QuakeUpdateEvent event) {
tick();
}
});
}

public synchronized void tick() {
GlobalQuake.instance.getEarthquakeAnalysis().getEarthquakes().forEach(earthquake -> warnings.putIfAbsent(earthquake, new Warning()));

for (Iterator<Map.Entry<Warnable, Warning>> iterator = warnings.entrySet().iterator(); iterator.hasNext(); ) {
var kv = iterator.next();
Warnable warnable = kv.getKey();
Warning warning = kv.getValue();

long age = System.currentTimeMillis() - warning.createdAt;
if(age > 1000 * 60 * STORE_TIME_MINUTES){
iterator.remove();
continue;
}

if (meetsConditions(warnable) && !warning.metConditions) {
warning.metConditions = true;
conditionsSatisfied(warnable, warning);
}
}
}

private void conditionsSatisfied(Warnable warnable, Warning warning) {
if(GlobalQuake.instance != null){
GlobalQuake.instance.getEventHandler().fireEvent(new AlertIssuedEvent(warnable, warning));
}
}

private boolean meetsConditions(Warnable warnable) {
if(warnable instanceof Earthquake){
return meetsConditions((Earthquake) warnable);
}

// TODO cluster warnings

return false;
}

public static boolean meetsConditions(Earthquake quake) {
double distGC = GeoUtils.greatCircleDistance(quake.getLat(), quake.getLon(), Settings.homeLat,
Settings.homeLon);

if (Settings.alertLocal && distGC < Settings.alertLocalDist) {
return true;
}

if (Settings.alertRegion && distGC < Settings.alertRegionDist && quake.getMag() >= Settings.alertRegionMag) {
return true;
}

return Settings.alertGlobal && quake.getMag() > Settings.alertGlobalMag;
}
}

11 changes: 11 additions & 0 deletions src/main/java/globalquake/core/alert/Warnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package globalquake.core.alert;

public interface Warnable {

@SuppressWarnings("unused")
double getWarningLat();

@SuppressWarnings("unused")
double getWarningLon();

}
13 changes: 13 additions & 0 deletions src/main/java/globalquake/core/alert/Warning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package globalquake.core.alert;

public class Warning {

public Warning() {
createdAt = System.currentTimeMillis();
metConditions = false;
}

public final long createdAt;
public boolean metConditions;

}
Loading

0 comments on commit 8ac764c

Please sign in to comment.