This commit is contained in:
genuineparts 2024-10-29 13:09:28 +01:00
parent 24c4e622e1
commit 5565fd2201
2 changed files with 32 additions and 30 deletions

View file

@ -11,14 +11,12 @@ 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>();
private static final HashMap<String, Integer> data = new HashMap<>();
static OebbCheck check = new OebbCheck();
public static void main(String[] args) throws IOException {
@ -28,15 +26,12 @@ public class Main {
server.setExecutor(null); // creates a default executor
server.start();
Runnable task = new Runnable() {
@Override
public void run() {
Runnable task = () -> {
try {
refresh();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
try (ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor()) {
service.scheduleAtFixedRate(task, 60, 60, TimeUnit.SECONDS);

View file

@ -7,9 +7,9 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
public class OebbCheck {
@ -25,26 +25,16 @@ public class OebbCheck {
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);
JSONArray journeys;
try {
journeys = getData(Station, Time);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
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(jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)){
if(jour.get("rt") instanceof JSONObject data) {
if (data.get("status").equals("Ausfall")) {
return 9999;
} else {
@ -57,4 +47,21 @@ public class OebbCheck {
}
return -1;
}
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");
}
}