Initial commit
This commit is contained in:
commit
24c4e622e1
9 changed files with 410 additions and 0 deletions
128
src/main/java/be/jaud/Main.java
Normal file
128
src/main/java/be/jaud/Main.java
Normal file
|
@ -0,0 +1,128 @@
|
|||
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.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Main {
|
||||
private static final HashMap<String, Integer> data = new HashMap<String, Integer>();
|
||||
static OebbCheck check = new OebbCheck();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
refresh();
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
||||
server.createContext("/oebbapi", new OebbHandler());
|
||||
server.setExecutor(null); // creates a default executor
|
||||
server.start();
|
||||
|
||||
Runnable task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
refresh();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
try (ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor()) {
|
||||
service.scheduleAtFixedRate(task, 60, 60, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
private static void refresh() throws IOException {
|
||||
data.put("06:16",check.getStatus("1250201","6:16", "Freilassing (Oberbayern)","S 3"));
|
||||
data.put("06:29",check.getStatus("1250201","6:29", "Salzburg Hbf","REX 3"));
|
||||
data.put("06:50",check.getStatus("8100002","6:50", "Freilassing (Oberbayern)","REX 5052"));
|
||||
data.put("06:39",check.getStatus("1250201","6:39", "Freilassing (Oberbayern)","R 3"));
|
||||
}
|
||||
|
||||
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","S3 - 06:16");
|
||||
monitor2.put("labelColor","green");
|
||||
int zug1 = data.get("06:16");
|
||||
makeLabel(monitor2, zug1);
|
||||
dat.put(monitor2);
|
||||
|
||||
JSONObject monitor3 = new JSONObject();
|
||||
monitor3.put("label","REX3 - 06:29");
|
||||
monitor3.put("labelColor","green");
|
||||
int zug2 = data.get("06:29");
|
||||
makeLabel(monitor3, zug2);
|
||||
dat.put(monitor3);
|
||||
|
||||
JSONObject monitor4 = new JSONObject();
|
||||
monitor4.put("label","REX5052 - 06:50");
|
||||
monitor4.put("labelColor","green");
|
||||
int zug3 = data.get("06:50");
|
||||
makeLabel(monitor4, zug3);
|
||||
dat.put(monitor4);
|
||||
|
||||
JSONObject monitor5 = new JSONObject();
|
||||
monitor5.put("label","R3 - 06:39");
|
||||
monitor5.put("labelColor","green");
|
||||
int zug4 = data.get("06:39");
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
60
src/main/java/be/jaud/OebbCheck.java
Normal file
60
src/main/java/be/jaud/OebbCheck.java
Normal file
|
@ -0,0 +1,60 @@
|
|||
package be.jaud;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class OebbCheck {
|
||||
|
||||
public OebbCheck(){
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the Delay in Minutes for a given StationID, Departure Time, final stop and Train type(i.e. REX 3, S 2....)
|
||||
Returns -1 if no connection was found
|
||||
Returns 0 for no delay
|
||||
>0 is the delay in Minutes
|
||||
9999 means the Train has been cancelled
|
||||
*/
|
||||
public int getStatus(String Station, String Time, String FinalStop, String TrainType) throws IOException {
|
||||
URL url = new URL("https://fahrplan.oebb.at/bin/stboard.exe/dn?L=vs_scotty.vs_liveticker&evaId=" + Station + "&boardType=dep&time="+Time+"&productsFilter=1011110111011&additionalTime=0&maxJourneys=1&outputMode=tickerDataOnly&start=yes&selectDate=today");
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer content = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
String sub = content.substring(14);
|
||||
JSONObject jo = new JSONObject(sub);
|
||||
JSONArray journeys = jo.getJSONArray("journey");
|
||||
for (Object journey : journeys) {
|
||||
JSONObject jour = (JSONObject) journey;
|
||||
if(jour.get("st").equals(FinalStop)){
|
||||
|
||||
if(jour.get("rt") instanceof JSONObject) {
|
||||
JSONObject data = (JSONObject) jour.get("rt");
|
||||
if (data.get("status").equals("Ausfall")) {
|
||||
return 9999;
|
||||
} else {
|
||||
return data.getInt("dlm");
|
||||
}
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue