Added refresh time and did some layouting

This commit is contained in:
genuineparts 2024-10-30 19:09:02 +01:00
parent 342a6c6db3
commit f72afb66e5

View file

@ -9,7 +9,9 @@ import org.json.JSONObject;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -19,6 +21,9 @@ public class Main {
private static final HashMap<String, Integer> data = new HashMap<>();
public static Settings conf;
static OebbCheck check = new OebbCheck();
static long time;
static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
public static void main(String[] args) throws IOException {
conf = Settings.getInstance();
@ -26,8 +31,6 @@ public class Main {
HttpServer server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0);
server.createContext("/oebbapi", new OebbHandler());
server.setExecutor(null); // creates a default executor
server.start();
Runnable task = () -> {
try {
refresh();
@ -35,9 +38,8 @@ public class Main {
throw new RuntimeException(e);
}
};
try (ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor()) {
service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS);
}
service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS);
server.start();
}
private static void refresh() throws IOException {
@ -45,6 +47,7 @@ public class Main {
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")));
time = System.currentTimeMillis();
}
static class OebbHandler implements HttpHandler {
@ -65,10 +68,30 @@ public class Main {
private static String createOutput(){
JSONArray dat = new JSONArray();
Date updatetime = new Date(time);
JSONObject monitor1 = new JSONObject();
monitor1.put("label","Zug");
monitor1.put("data","Abfahrten");
JSONArray layout = new JSONArray();
JSONObject text1 = new JSONObject();
text1.put("type","text");
text1.put("x",120);
text1.put("y",120);
text1.put("text","Abfahrten");
text1.put("font",2);
text1.put("color","red");
text1.put("background","silver");
JSONObject text2 = new JSONObject();
text2.put("type","text");
text2.put("x",120);
text2.put("y",170);
text2.put("text", dateFormat.format(updatetime));
text2.put("color","black");
text2.put("font",2);
text2.put("background","silver");
layout.put(text2);
layout.put(text1);
monitor1.put("data",layout);
monitor1.put("labelColor","red");
monitor1.put("color","red");
monitor1.put("background","silver");