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

68 lines
2.4 KiB
Java
Raw Normal View History

2024-10-29 12:48:27 +01:00
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;
2024-10-29 13:09:28 +01:00
import java.net.URI;
import java.net.URISyntaxException;
2024-10-29 12:48:27 +01:00
import java.net.URL;
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 {
2024-10-29 13:09:28 +01:00
JSONArray journeys;
try {
journeys = getData(Station, Time);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
2024-10-29 12:48:27 +01:00
}
for (Object journey : journeys) {
JSONObject jour = (JSONObject) journey;
2024-10-29 13:09:28 +01:00
if(jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)){
if(jour.get("rt") instanceof JSONObject data) {
2024-10-29 12:48:27 +01:00
if (data.get("status").equals("Ausfall")) {
return 9999;
} else {
return data.getInt("dlm");
}
}else{
return 0;
}
}
}
return -1;
}
2024-10-29 13:09:28 +01:00
private static JSONArray getData(String Station, String Time) throws IOException, URISyntaxException {
URL url = new URI("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").toURL();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
String sub = content.substring(14);
JSONObject jo = new JSONObject(sub);
return jo.getJSONArray("journey");
}
2024-10-29 12:48:27 +01:00
}