package be.jaud; import org.slf4j.LoggerFactory; import org.slf4j.Logger; 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; import java.util.Collections; import java.util.Date; 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 String version = "0.2"; private static final HashMap 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 final Logger LOG = LoggerFactory.getLogger(Main.class); public static void main(String[] args) { conf = Settings.getInstance(); 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()); } Runnable task = () -> { try { refresh(); } catch (IOException e) { LOG.error("Error at Task: {}", e.toString()); } }; service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS); if (server != null) { server.createContext("/oebbapi", new OebbHandler()); server.setExecutor(null); // creates a default executor server.start(); } } 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(); } 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) { LOG.error("Error at HttpRequest: {}", e.toString()); } } } private static String createOutput(){ JSONObject dat = new JSONObject(); Date updatetime = new Date(time); dat.put("interval",conf.getInt("refreshTime")*1000); JSONArray display = new JSONArray(); 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); monitor1.put("labelColor","red"); monitor1.put("color","red"); monitor1.put("background","silver"); display.put(monitor1); JSONObject monitor2 = new JSONObject(); monitor2.put("label",conf.getString("monitors.monitor1.Title")); monitor2.put("labelColor","green"); monitor2.put("fullDraw",true); int zug1 = data.get(conf.getString("monitors.monitor1.DepartureTime")); makeLabel(monitor2, zug1); display.put(monitor2); JSONObject monitor3 = new JSONObject(); monitor3.put("label",conf.getString("monitors.monitor2.Title")); monitor3.put("labelColor","green"); monitor3.put("fullDraw",true); int zug2 = data.get(conf.getString("monitors.monitor2.DepartureTime")); makeLabel(monitor3, zug2); display.put(monitor3); JSONObject monitor4 = new JSONObject(); monitor4.put("label",conf.getString("monitors.monitor3.Title")); monitor4.put("labelColor","green"); monitor4.put("fullDraw",true); int zug3 = data.get(conf.getString("monitors.monitor3.DepartureTime")); makeLabel(monitor4, zug3); display.put(monitor4); JSONObject monitor5 = new JSONObject(); monitor5.put("label",conf.getString("monitors.monitor4.Title")); monitor5.put("labelColor","green"); monitor5.put("fullDraw",true); int zug4 = data.get(conf.getString("monitors.monitor4.DepartureTime")); makeLabel(monitor5, zug4); display.put(monitor5); dat.put("displays",display); 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"); } } }