From 342a6c6db3699a1f55836d3b128a1fca6ca44fec Mon Sep 17 00:00:00 2001 From: genuineparts Date: Wed, 30 Oct 2024 10:43:44 +0100 Subject: [PATCH] Added configuration --- .gitignore | 3 +- config/apiserver.conf.default | 35 +++++++++++++++++++ pom.xml | 6 ++++ src/main/java/be/jaud/Main.java | 30 +++++++++-------- src/main/java/be/jaud/Settings.java | 52 +++++++++++++++++++++++++++++ src/main/resources/reference.conf | 35 +++++++++++++++++++ 6 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 config/apiserver.conf.default create mode 100644 src/main/java/be/jaud/Settings.java create mode 100644 src/main/resources/reference.conf diff --git a/.gitignore b/.gitignore index 5ff6309..1098639 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ build/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store +/config/apiserver.conf diff --git a/config/apiserver.conf.default b/config/apiserver.conf.default new file mode 100644 index 0000000..1946560 --- /dev/null +++ b/config/apiserver.conf.default @@ -0,0 +1,35 @@ +apiserver { + port = 8111 //Port to listen to + refreshTime = 60 //Refresh Time in Seconds + + monitors { + monitor1 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + monitor2 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + monitor3 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + monitor4 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 40f8992..6cf0936 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,12 @@ json 20240303 + + com.typesafe + config + 1.4.3 + + OebbApi diff --git a/src/main/java/be/jaud/Main.java b/src/main/java/be/jaud/Main.java index 3a56995..705b6df 100644 --- a/src/main/java/be/jaud/Main.java +++ b/src/main/java/be/jaud/Main.java @@ -17,11 +17,13 @@ import java.util.concurrent.TimeUnit; public class Main { private static final HashMap data = new HashMap<>(); + public static Settings conf; static OebbCheck check = new OebbCheck(); public static void main(String[] args) throws IOException { + conf = Settings.getInstance(); refresh(); - HttpServer server = HttpServer.create(new InetSocketAddress(8111), 0); + HttpServer server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0); server.createContext("/oebbapi", new OebbHandler()); server.setExecutor(null); // creates a default executor server.start(); @@ -34,15 +36,15 @@ public class Main { } }; try (ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor()) { - service.scheduleAtFixedRate(task, 60, 60, TimeUnit.SECONDS); + service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS); } } private static void refresh() throws IOException { - data.put("06:16",check.getStatus("1250201","6:16", "Freilassing (Oberbayern)","S 3")); - data.put("06:29",check.getStatus("1250201","6:29", "Salzburg Hbf","REX 3")); - data.put("06:50",check.getStatus("8100002","6:50", "Freilassing (Oberbayern)","REX 5052")); - data.put("06:39",check.getStatus("1250201","6:39", "Freilassing (Oberbayern)","R 3")); + data.put(conf.getString("monitors.monitor1.DepartureTime"),check.getStatus(conf.getString("monitors.monitor1.Station"),conf.getString("monitors.monitor1.DepartureTime"), conf.getString("monitors.monitor1.FinalStop"),conf.getString("monitors.monitor1.TrainType"))); + data.put(conf.getString("monitors.monitor2.DepartureTime"),check.getStatus(conf.getString("monitors.monitor2.Station"),conf.getString("monitors.monitor2.DepartureTime"), conf.getString("monitors.monitor2.FinalStop"),conf.getString("monitors.monitor2.TrainType"))); + data.put(conf.getString("monitors.monitor3.DepartureTime"),check.getStatus(conf.getString("monitors.monitor3.Station"),conf.getString("monitors.monitor3.DepartureTime"), conf.getString("monitors.monitor3.FinalStop"),conf.getString("monitors.monitor3.TrainType"))); + data.put(conf.getString("monitors.monitor4.DepartureTime"),check.getStatus(conf.getString("monitors.monitor4.Station"),conf.getString("monitors.monitor4.DepartureTime"), conf.getString("monitors.monitor4.FinalStop"),conf.getString("monitors.monitor4.TrainType"))); } static class OebbHandler implements HttpHandler { @@ -73,30 +75,30 @@ public class Main { dat.put(monitor1); JSONObject monitor2 = new JSONObject(); - monitor2.put("label","S3 - 06:16"); + monitor2.put("label",conf.getString("monitors.monitor1.Title")); monitor2.put("labelColor","green"); - int zug1 = data.get("06:16"); + int zug1 = data.get(conf.getString("monitors.monitor1.DepartureTime")); makeLabel(monitor2, zug1); dat.put(monitor2); JSONObject monitor3 = new JSONObject(); - monitor3.put("label","REX3 - 06:29"); + monitor3.put("label",conf.getString("monitors.monitor2.Title")); monitor3.put("labelColor","green"); - int zug2 = data.get("06:29"); + int zug2 = data.get(conf.getString("monitors.monitor2.DepartureTime")); makeLabel(monitor3, zug2); dat.put(monitor3); JSONObject monitor4 = new JSONObject(); - monitor4.put("label","REX5052 - 06:50"); + monitor4.put("label",conf.getString("monitors.monitor3.Title")); monitor4.put("labelColor","green"); - int zug3 = data.get("06:50"); + int zug3 = data.get(conf.getString("monitors.monitor3.DepartureTime")); makeLabel(monitor4, zug3); dat.put(monitor4); JSONObject monitor5 = new JSONObject(); - monitor5.put("label","R3 - 06:39"); + monitor5.put("label",conf.getString("monitors.monitor4.Title")); monitor5.put("labelColor","green"); - int zug4 = data.get("06:39"); + int zug4 = data.get(conf.getString("monitors.monitor4.DepartureTime")); makeLabel(monitor5, zug4); dat.put(monitor5); diff --git a/src/main/java/be/jaud/Settings.java b/src/main/java/be/jaud/Settings.java new file mode 100644 index 0000000..ed83079 --- /dev/null +++ b/src/main/java/be/jaud/Settings.java @@ -0,0 +1,52 @@ +package be.jaud; + +import java.io.File; +import java.nio.file.Paths; +import java.util.Iterator; +import java.util.List; +import java.util.Map.Entry; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigException; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigValue; + +public class Settings { + private static Settings instance = null; + private final Config config; + public static Settings getInstance() { + if (instance == null) { + instance = new Settings(); + } + return instance; + } + + private Settings(){ + config = ConfigFactory.load(ConfigFactory.parseFile(new File(Paths.get("").toAbsolutePath() +"/config/apiserver.conf"))).getConfig("apiserver"); + try { + config.checkValid(ConfigFactory.defaultReference().getConfig("apiserver")); + } catch (ConfigException.ValidationFailed ex) { + System.out.println("Error loading Config! "+ ex); + } + } + + public Iterator> getall(){ + return config.entrySet().iterator(); + } + + public String getString(String key){ + return config.getString(key); + } + + public Boolean getBoolean(String key){ + return config.getBoolean(key); + } + + public int getInt(String key){ + return config.getInt(key); + } + + public List getStringList(String key){ + return config.getStringList(key); + } +} diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf new file mode 100644 index 0000000..1946560 --- /dev/null +++ b/src/main/resources/reference.conf @@ -0,0 +1,35 @@ +apiserver { + port = 8111 //Port to listen to + refreshTime = 60 //Refresh Time in Seconds + + monitors { + monitor1 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + monitor2 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + monitor3 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + monitor4 { + Title = "" + DepartureTime = "" + TrainType = "" + Station = "" + FinalStop = "" + } + } +} \ No newline at end of file