Added configuration

This commit is contained in:
genuineparts 2024-10-30 10:43:44 +01:00
parent 67866265ba
commit 342a6c6db3
6 changed files with 146 additions and 15 deletions

3
.gitignore vendored
View file

@ -35,4 +35,5 @@ build/
.vscode/
### Mac OS ###
.DS_Store
.DS_Store
/config/apiserver.conf

View file

@ -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 = ""
}
}
}

View file

@ -12,6 +12,12 @@
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.4.3</version>
</dependency>
</dependencies>
<build>
<finalName>OebbApi</finalName>

View file

@ -17,11 +17,13 @@ import java.util.concurrent.TimeUnit;
public class Main {
private static final HashMap<String, Integer> 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);

View file

@ -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<Entry<String, ConfigValue>> 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<String> getStringList(String key){
return config.getStringList(key);
}
}

View file

@ -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 = ""
}
}
}