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.util.Collections; import java.util.HashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; 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(conf.getInt("port")), 0); server.createContext("/oebbapi", new OebbHandler()); server.setExecutor(null); // creates a default executor server.start(); Runnable task = () -> { try { refresh(); } catch (IOException e) { throw new RuntimeException(e); } }; try (ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor()) { service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS); } } 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"))); 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 { @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(); JSONObject monitor1 = new JSONObject(); monitor1.put("label","Zug"); monitor1.put("data","Abfahrten"); monitor1.put("labelColor","red"); monitor1.put("color","red"); monitor1.put("background","silver"); dat.put(monitor1); JSONObject monitor2 = new JSONObject(); monitor2.put("label",conf.getString("monitors.monitor1.Title")); monitor2.put("labelColor","green"); int zug1 = data.get(conf.getString("monitors.monitor1.DepartureTime")); makeLabel(monitor2, zug1); dat.put(monitor2); JSONObject monitor3 = new JSONObject(); monitor3.put("label",conf.getString("monitors.monitor2.Title")); monitor3.put("labelColor","green"); int zug2 = data.get(conf.getString("monitors.monitor2.DepartureTime")); makeLabel(monitor3, zug2); dat.put(monitor3); JSONObject monitor4 = new JSONObject(); monitor4.put("label",conf.getString("monitors.monitor3.Title")); monitor4.put("labelColor","green"); int zug3 = data.get(conf.getString("monitors.monitor3.DepartureTime")); makeLabel(monitor4, zug3); dat.put(monitor4); JSONObject monitor5 = new JSONObject(); monitor5.put("label",conf.getString("monitors.monitor4.Title")); monitor5.put("labelColor","green"); int zug4 = data.get(conf.getString("monitors.monitor4.DepartureTime")); makeLabel(monitor5, zug4); dat.put(monitor5); 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"); } } }