Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IVB moved to mobile HAFAS and fahrplan.ivb.at #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions enabler/src/de/schildbach/pte/AbstractHafasMobileProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected AbstractHafasMobileProvider setApiClient(final String apiClient) {
public NearbyLocationsResult queryNearbyLocations(final EnumSet<LocationType> types, final Location location,
final int maxDistance, final int maxLocations) throws IOException {
if (location.hasLocation())
return jsonLocGeoPos(types, location.lat, location.lon);
return jsonLocGeoPos(types, location.lat, location.lon, maxDistance, maxLocations);
else
throw new IllegalArgumentException("cannot handle: " + location);
}
Expand Down Expand Up @@ -133,23 +133,21 @@ public QueryTripsResult queryMoreTrips(final QueryTripsContext context, final bo
jsonContext.products, later ? jsonContext.laterContext : jsonContext.earlierContext);
}

protected final NearbyLocationsResult jsonLocGeoPos(final EnumSet<LocationType> types, final int lat, final int lon)
protected final NearbyLocationsResult jsonLocGeoPos(final EnumSet<LocationType> types, final int lat, final int lon, int maxDistance, int maxLocations)
throws IOException {
final boolean getPOIs = types.contains(LocationType.POI);
final String request = wrapJsonApiRequest("LocGeoPos",
"{\"ring\":" //
+ "{\"cCrd\":{\"x\":" + lon + ",\"y\":" + lat + "}}," //
+ "{\"cCrd\":{\"x\":" + lon + ",\"y\":" + lat + "}" + (maxDistance > 0 ? ",\"maxDist\":" + maxDistance : "") + "}," //
+ (maxLocations > 0 ? "\"maxLoc\":" + maxLocations + "," : "") //
+ "\"getPOIs\":" + getPOIs + "}", //
false);

final HttpUrl url = checkNotNull(mgateEndpoint);
final CharSequence page = httpClient.get(url, request, "application/json");

try {
final JSONObject head = new JSONObject(page.toString());
final String headErr = head.optString("err", null);
if (headErr != null)
throw new RuntimeException(headErr);
final JSONObject head = parseJsonPage(page);
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT, head.getString("ver"), null, 0, null);

final JSONArray svcResList = head.getJSONArray("svcResL");
Expand Down Expand Up @@ -203,18 +201,15 @@ protected final QueryDeparturesResult jsonStationBoard(final String stationId, f
+ "\"time\":\"" + jsonTime + "\"," //
+ "\"stbLoc\":{\"type\":\"S\"," + "\"state\":\"F\"," // F/M
+ "\"extId\":" + JSONObject.quote(normalizedStationId.toString()) + "}," //
+ "\"stbFltrEquiv\":" + stbFltrEquiv + ",\"maxJny\":" + maxJny + ",\"getPasslist\":"
+ getPasslist + "}",
+ ("1.20".equals(apiVersion) ? "" : "\"stbFltrEquiv\":" + stbFltrEquiv + ",\"getPasslist\":" + getPasslist + ",") //
+ "\"maxJny\":" + maxJny + "}",
false);

final HttpUrl url = checkNotNull(mgateEndpoint);
final CharSequence page = httpClient.get(url, request, "application/json");

try {
final JSONObject head = new JSONObject(page.toString());
final String headErr = head.optString("err", null);
if (headErr != null)
throw new RuntimeException(headErr);
final JSONObject head = parseJsonPage(page);
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT, head.getString("ver"), null, 0, null);
final QueryDeparturesResult result = new QueryDeparturesResult(header);

Expand Down Expand Up @@ -323,10 +318,7 @@ protected final SuggestLocationsResult jsonLocMatch(final CharSequence constrain
final CharSequence page = httpClient.get(url, request, "application/json");

try {
final JSONObject head = new JSONObject(page.toString());
final String headErr = head.optString("err", null);
if (headErr != null)
throw new RuntimeException(headErr);
final JSONObject head = parseJsonPage(page);
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT, head.getString("ver"), null, 0, null);

final JSONArray svcResList = head.getJSONArray("svcResL");
Expand Down Expand Up @@ -358,6 +350,20 @@ protected final SuggestLocationsResult jsonLocMatch(final CharSequence constrain

private static final Joiner JOINER = Joiner.on(' ').skipNulls();

private static JSONObject parseJsonPage(CharSequence page) throws JSONException {
final JSONObject head = new JSONObject(page.toString());
final String headErr = head.optString("err", null);
final String headErrText = head.optString("errTxt", null);
if ("OK".equals(headErr))
return head;
if (headErr != null && headErrText != null)
throw new RuntimeException(headErr + ": " + headErrText);
else if (headErr != null)
throw new RuntimeException(headErr);
else
return head;
}

private Location jsonTripSearchIdentify(final Location location) throws IOException {
if (location.hasName()) {
final List<Location> locations = jsonLocMatch(JOINER.join(location.place, location.name)).getLocations();
Expand All @@ -366,7 +372,7 @@ private Location jsonTripSearchIdentify(final Location location) throws IOExcept
}
if (location.hasLocation()) {
final List<Location> locations = jsonLocGeoPos(EnumSet.allOf(LocationType.class), location.lat,
location.lon).locations;
location.lon, 0, 0).locations;
if (!locations.isEmpty())
return locations.get(0);
}
Expand Down Expand Up @@ -400,7 +406,7 @@ protected final QueryTripsResult jsonTripSearch(Location from, @Nullable Locatio
c.setTime(time);
final CharSequence outDate = jsonDate(c);
final CharSequence outTime = jsonTime(c);
final CharSequence outFrwdKey = "1.11".equals(apiVersion) ? "outFrwd" : "frwd";
final CharSequence outFrwdKey = "1.10".equals(apiVersion) ? "frwd" : "outFrwd";
final CharSequence outFrwd = Boolean.toString(dep);
final CharSequence jnyFltr = productsString(products);
final CharSequence jsonContext = moreContext != null ? "\"ctxScr\":" + JSONObject.quote(moreContext) + "," : "";
Expand All @@ -421,10 +427,7 @@ protected final QueryTripsResult jsonTripSearch(Location from, @Nullable Locatio
final CharSequence page = httpClient.get(url, request, "application/json");

try {
final JSONObject head = new JSONObject(page.toString());
final String headErr = head.optString("err", null);
if (headErr != null)
throw new RuntimeException(headErr);
final JSONObject head = parseJsonPage(page);
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT, head.getString("ver"), null, 0, null);

final JSONArray svcResList = head.getJSONArray("svcResL");
Expand Down
18 changes: 12 additions & 6 deletions enabler/src/de/schildbach/pte/IvbProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@

package de.schildbach.pte;

import de.schildbach.pte.dto.Product;
import okhttp3.HttpUrl;

/**
* @author Andreas Schildbach
*/
public class IvbProvider extends AbstractEfaProvider {
private static final HttpUrl API_BASE = HttpUrl.parse("http://efa.ivb.at/ivb/");
public class IvbProvider extends AbstractHafasMobileProvider {
private static final HttpUrl API_BASE = HttpUrl.parse("https://fahrplan.ivb.at/bin/");
// TODO review
private static final Product[] PRODUCTS_MAP = { Product.HIGH_SPEED_TRAIN, Product.SUBURBAN_TRAIN, Product.SUBWAY,
null, Product.TRAM, Product.REGIONAL_TRAIN, Product.BUS, Product.BUS, Product.TRAM, Product.FERRY,
Product.ON_DEMAND, Product.BUS, Product.REGIONAL_TRAIN, null, null, null };

public IvbProvider() {
super(NetworkId.IVB, API_BASE);

setUseRouteIndexAsTripId(false);
public IvbProvider(final String apiAuthorization) {
super(NetworkId.IVB, API_BASE, PRODUCTS_MAP);
setApiVersion("1.20");
setApiAuthorization(apiAuthorization);
setApiClient("{\"id\":\"VAO\",\"l\":\"vs_ivb\",\"type\":\"AND\"}");
}
}
23 changes: 18 additions & 5 deletions enabler/test/de/schildbach/pte/live/IvbProviderLiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

package de.schildbach.pte.live;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.util.EnumSet;

import org.junit.Ignore;
import org.junit.Test;

import de.schildbach.pte.IvbProvider;
Expand All @@ -40,27 +44,36 @@
*/
public class IvbProviderLiveTest extends AbstractProviderLiveTest {
public IvbProviderLiveTest() {
super(new IvbProvider());
super(new IvbProvider(secretProperty("ivb.api_authorization")));
}

@Test
@Ignore(value = "does not work")
public void nearbyStations() throws Exception {
final NearbyLocationsResult result = queryNearbyStations(new Location(LocationType.STATION, "60401187"));
print(result);
}

@Test
public void nearbyStationsByCoordinate() throws Exception {
final NearbyLocationsResult result = queryNearbyStations(Location.coord(47271228, 11402063));
final NearbyLocationsResult result = queryNearbyLocations(EnumSet.of(LocationType.STATION), Location.coord(47271228, 11402063), 10000, 7);
print(result);
assertEquals(result.locations.size(), 7);
}

@Test
public void queryDepartures() throws Exception {
final QueryDeparturesResult result = queryDepartures("60401187", false);
final QueryDeparturesResult result = queryDepartures("476640200", false);
print(result);
}

@Test
public void suggestLocations() throws Exception {
final SuggestLocationsResult result = suggestLocations("Innsbruck Hauptbahnhof");
print(result);
assertThat(result.getLocations(), hasItem(new Location(LocationType.STATION, "470118700")));
}

@Test
public void suggestLocationsIncomplete() throws Exception {
final SuggestLocationsResult result = suggestLocations("Kur");
Expand All @@ -75,8 +88,8 @@ public void suggestLocationsWithUmlaut() throws Exception {

@Test
public void shortTrip() throws Exception {
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "60466402", null, "Kochstraße"),
null, new Location(LocationType.STATION, "60461679", null, "Messe/Zeughaus"), new Date(), true,
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "476640200", null, "Kochstraße"),
null, new Location(LocationType.STATION, "476167900", null, "Messe/Zeughaus"), new Date(), true,
Product.ALL, WalkSpeed.NORMAL, Accessibility.NEUTRAL);
print(result);
assertEquals(QueryTripsResult.Status.OK, result.status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vor.api_authorization =
ooevv.api_authorization =
svv.api_authorization =
vvt.api_authorization =
ivb.api_authorization =
vmobil.api_authorization =
vao.api_authorization =
hsl.usertoken =
Expand Down