OEBBInfoOrbApp/src/main/java/be/jaud/Main.java

159 lines
6.8 KiB
Java
Raw Normal View History

2024-10-29 12:48:27 +01:00
package be.jaud;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
2024-10-29 12:48:27 +01:00
import java.util.Collections;
import java.util.Date;
2024-10-29 12:48:27 +01:00
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
2024-10-29 13:09:28 +01:00
private static final HashMap<String, Integer> data = new HashMap<>();
2024-10-30 10:43:44 +01:00
public static Settings conf;
2024-10-29 12:48:27 +01:00
static OebbCheck check = new OebbCheck();
static long time;
static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
2024-10-29 12:48:27 +01:00
public static void main(String[] args) throws IOException {
2024-10-30 10:43:44 +01:00
conf = Settings.getInstance();
2024-10-29 12:48:27 +01:00
refresh();
2024-10-30 10:43:44 +01:00
HttpServer server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0);
2024-10-29 12:48:27 +01:00
server.createContext("/oebbapi", new OebbHandler());
server.setExecutor(null); // creates a default executor
2024-10-29 13:09:28 +01:00
Runnable task = () -> {
try {
refresh();
} catch (IOException e) {
throw new RuntimeException(e);
2024-10-29 12:48:27 +01:00
}
};
service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS);
server.start();
2024-10-29 12:48:27 +01:00
}
private static void refresh() throws IOException {
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"),String.valueOf(conf.getInt("monitors.monitor1.maxjourneys"))));
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"),String.valueOf(conf.getInt("monitors.monitor2.maxjourneys"))));
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"),String.valueOf(conf.getInt("monitors.monitor3.maxjourneys"))));
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"),String.valueOf(conf.getInt("monitors.monitor4.maxjourneys"))));
time = System.currentTimeMillis();
2024-10-29 12:48:27 +01:00
}
static class OebbHandler implements HttpHandler {
@Override
public void handle(HttpExchange t){
String response = createOutput();
t.getResponseHeaders().put("Content-Type", Collections.singletonList("application/json"));
try {
t.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static String createOutput(){
JSONArray dat = new JSONArray();
Date updatetime = new Date(time);
JSONObject displays = new JSONObject();
JSONArray display = new JSONArray();
JSONObject refresh = new JSONObject();
refresh.put("interval",conf.getInt("refreshTime"));
dat.put(refresh);
2024-10-29 12:48:27 +01:00
JSONObject monitor1 = new JSONObject();
monitor1.put("label","Zug");
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);
2024-10-29 12:48:27 +01:00
monitor1.put("labelColor","red");
monitor1.put("color","red");
monitor1.put("background","silver");
display.put(monitor1);
2024-10-29 12:48:27 +01:00
JSONObject monitor2 = new JSONObject();
2024-10-30 10:43:44 +01:00
monitor2.put("label",conf.getString("monitors.monitor1.Title"));
2024-10-29 12:48:27 +01:00
monitor2.put("labelColor","green");
monitor2.put("fullDraw",true);
2024-10-30 10:43:44 +01:00
int zug1 = data.get(conf.getString("monitors.monitor1.DepartureTime"));
2024-10-29 12:48:27 +01:00
makeLabel(monitor2, zug1);
display.put(monitor2);
2024-10-29 12:48:27 +01:00
JSONObject monitor3 = new JSONObject();
2024-10-30 10:43:44 +01:00
monitor3.put("label",conf.getString("monitors.monitor2.Title"));
2024-10-29 12:48:27 +01:00
monitor3.put("labelColor","green");
monitor3.put("fullDraw",true);
2024-10-30 10:43:44 +01:00
int zug2 = data.get(conf.getString("monitors.monitor2.DepartureTime"));
2024-10-29 12:48:27 +01:00
makeLabel(monitor3, zug2);
display.put(monitor3);
2024-10-29 12:48:27 +01:00
JSONObject monitor4 = new JSONObject();
2024-10-30 10:43:44 +01:00
monitor4.put("label",conf.getString("monitors.monitor3.Title"));
2024-10-29 12:48:27 +01:00
monitor4.put("labelColor","green");
monitor4.put("fullDraw",true);
2024-10-30 10:43:44 +01:00
int zug3 = data.get(conf.getString("monitors.monitor3.DepartureTime"));
2024-10-29 12:48:27 +01:00
makeLabel(monitor4, zug3);
display.put(monitor4);
2024-10-29 12:48:27 +01:00
JSONObject monitor5 = new JSONObject();
2024-10-30 10:43:44 +01:00
monitor5.put("label",conf.getString("monitors.monitor4.Title"));
2024-10-29 12:48:27 +01:00
monitor5.put("labelColor","green");
monitor5.put("fullDraw",true);
2024-10-30 10:43:44 +01:00
int zug4 = data.get(conf.getString("monitors.monitor4.DepartureTime"));
2024-10-29 12:48:27 +01:00
makeLabel(monitor5, zug4);
display.put(monitor5);
displays.put("displays", display);
dat.put(displays);
2024-10-29 12:48:27 +01:00
return dat.toString();
}
private static void makeLabel(JSONObject Monitor, int zeit){
if(zeit != 0){
Monitor.put("color","red");
if(zeit > 0 && zeit < 9999){
Monitor.put("data",zeit);
}else if(zeit == 9999){
Monitor.put("data","AUSFALL!");
}else{
Monitor.put("data","Fehler!");
}
Monitor.put("background","yellow");
} else {
Monitor.put("color","green");
Monitor.put("data","On time");
Monitor.put("background","silver");
}
}
}