2024-10-29 12:48:27 +01:00
|
|
|
package be.jaud;
|
|
|
|
|
2024-11-12 15:06:41 +01:00
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.Logger;
|
2024-10-29 12:48:27 +01:00
|
|
|
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;
|
2024-10-30 19:09:02 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
2024-10-29 12:48:27 +01:00
|
|
|
import java.util.Collections;
|
2024-10-30 19:09:02 +01:00
|
|
|
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-11-12 18:03:17 +01:00
|
|
|
private static final String version = "0.3";
|
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();
|
2024-10-30 19:09:02 +01:00
|
|
|
static long time;
|
|
|
|
static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
|
|
|
|
static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
|
2024-11-12 15:06:41 +01:00
|
|
|
public static final Logger LOG = LoggerFactory.getLogger(Main.class);
|
2024-10-29 12:48:27 +01:00
|
|
|
|
2024-11-12 15:06:41 +01:00
|
|
|
public static void main(String[] args) {
|
2024-10-30 10:43:44 +01:00
|
|
|
conf = Settings.getInstance();
|
2024-11-12 15:06:41 +01:00
|
|
|
LOG.info("OebbInfo Version " + version + " starting.");
|
|
|
|
try {
|
|
|
|
refresh();
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.error("Error at refresh: {}", e.toString());
|
|
|
|
}
|
|
|
|
HttpServer server = null;
|
|
|
|
try {
|
|
|
|
server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0);
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.error("Error at HttpServer: {}", e.toString());
|
|
|
|
}
|
2024-10-29 13:09:28 +01:00
|
|
|
Runnable task = () -> {
|
|
|
|
try {
|
|
|
|
refresh();
|
|
|
|
} catch (IOException e) {
|
2024-11-12 15:06:41 +01:00
|
|
|
LOG.error("Error at Task: {}", e.toString());
|
2024-10-29 12:48:27 +01:00
|
|
|
}
|
|
|
|
};
|
2024-10-30 19:09:02 +01:00
|
|
|
service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS);
|
2024-11-12 15:06:41 +01:00
|
|
|
if (server != null) {
|
|
|
|
server.createContext("/oebbapi", new OebbHandler());
|
|
|
|
server.setExecutor(null); // creates a default executor
|
|
|
|
server.start();
|
|
|
|
}
|
2024-10-29 12:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void refresh() throws IOException {
|
2024-11-04 08:47:56 +01:00
|
|
|
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"))));
|
2024-10-30 19:09:02 +01:00
|
|
|
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) {
|
2024-11-12 15:06:41 +01:00
|
|
|
LOG.error("Error at HttpRequest: {}", e.toString());
|
2024-10-29 12:48:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String createOutput(){
|
2024-11-04 10:00:41 +01:00
|
|
|
JSONObject dat = new JSONObject();
|
2024-10-30 19:09:02 +01:00
|
|
|
Date updatetime = new Date(time);
|
2024-11-04 10:00:41 +01:00
|
|
|
dat.put("interval",conf.getInt("refreshTime")*1000);
|
2024-11-04 08:47:56 +01:00
|
|
|
JSONArray display = new JSONArray();
|
2024-10-29 12:48:27 +01:00
|
|
|
JSONObject monitor1 = new JSONObject();
|
|
|
|
monitor1.put("label","Zug");
|
2024-10-30 19:09:02 +01:00
|
|
|
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);
|
2024-11-12 18:02:16 +01:00
|
|
|
text1.put("color",conf.getInt("textcolormain"));
|
|
|
|
text1.put("background",conf.getInt("bgcolormain"));
|
2024-10-30 19:09:02 +01:00
|
|
|
JSONObject text2 = new JSONObject();
|
|
|
|
text2.put("type","text");
|
|
|
|
text2.put("x",120);
|
|
|
|
text2.put("y",170);
|
|
|
|
text2.put("text", dateFormat.format(updatetime));
|
2024-11-12 18:02:16 +01:00
|
|
|
text2.put("color",conf.getInt("textcolorupdate"));
|
2024-10-30 19:09:02 +01:00
|
|
|
text2.put("font",2);
|
2024-11-12 18:02:16 +01:00
|
|
|
text2.put("background",conf.getInt("bgcolormain"));
|
2024-10-30 19:09:02 +01:00
|
|
|
layout.put(text2);
|
|
|
|
layout.put(text1);
|
|
|
|
|
|
|
|
monitor1.put("data",layout);
|
2024-11-12 18:02:16 +01:00
|
|
|
monitor1.put("labelColor",conf.getInt("textcolormain"));
|
|
|
|
monitor1.put("color",conf.getInt("textcolormain"));
|
|
|
|
monitor1.put("background",conf.getInt("bgcolormain"));
|
2024-11-04 08:47:56 +01:00
|
|
|
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-11-12 18:02:16 +01:00
|
|
|
monitor2.put("labelColor",conf.getInt("labelcolor"));
|
2024-11-04 08:47:56 +01:00
|
|
|
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);
|
2024-11-04 08:47:56 +01:00
|
|
|
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-11-12 18:02:16 +01:00
|
|
|
monitor3.put("labelColor",conf.getInt("labelcolor"));
|
2024-11-04 08:47:56 +01:00
|
|
|
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);
|
2024-11-04 08:47:56 +01:00
|
|
|
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-11-12 18:02:16 +01:00
|
|
|
monitor4.put("labelColor",conf.getInt("labelcolor"));
|
2024-11-04 08:47:56 +01:00
|
|
|
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);
|
2024-11-04 08:47:56 +01:00
|
|
|
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-11-12 18:02:16 +01:00
|
|
|
monitor5.put("labelColor",conf.getInt("labelcolor"));
|
2024-11-04 08:47:56 +01:00
|
|
|
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);
|
2024-11-04 08:47:56 +01:00
|
|
|
display.put(monitor5);
|
2024-11-04 10:00:41 +01:00
|
|
|
dat.put("displays",display);
|
2024-10-29 12:48:27 +01:00
|
|
|
return dat.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void makeLabel(JSONObject Monitor, int zeit){
|
|
|
|
if(zeit != 0){
|
2024-11-12 18:02:16 +01:00
|
|
|
Monitor.put("color",conf.getInt("textcolordelay"));
|
2024-10-29 12:48:27 +01:00
|
|
|
if(zeit > 0 && zeit < 9999){
|
|
|
|
Monitor.put("data",zeit);
|
|
|
|
}else if(zeit == 9999){
|
|
|
|
Monitor.put("data","AUSFALL!");
|
|
|
|
}else{
|
|
|
|
Monitor.put("data","Fehler!");
|
|
|
|
}
|
2024-11-12 18:02:16 +01:00
|
|
|
Monitor.put("background",conf.getInt("bgcolordelay"));
|
2024-10-29 12:48:27 +01:00
|
|
|
} else {
|
2024-11-12 18:02:16 +01:00
|
|
|
Monitor.put("color",conf.getInt("textcolor"));
|
2024-10-29 12:48:27 +01:00
|
|
|
Monitor.put("data","On time");
|
2024-11-12 18:02:16 +01:00
|
|
|
Monitor.put("background",conf.getInt("bgcolor"));
|
2024-10-29 12:48:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|