VVT: Switch to JSON API.

This commit is contained in:
Andreas Schildbach 2016-10-23 19:46:43 +02:00
parent 7c30711dcf
commit 0dabc3da75
2 changed files with 120 additions and 10 deletions

View file

@ -17,15 +17,110 @@
package de.schildbach.pte;
import java.io.IOException;
import java.util.Date;
import java.util.EnumSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import com.google.common.base.Charsets;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyLocationsResult;
import de.schildbach.pte.dto.Product;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.QueryTripsContext;
import de.schildbach.pte.dto.QueryTripsResult;
import de.schildbach.pte.dto.SuggestLocationsResult;
/**
* @author Andreas Schildbach
*/
public class VvtProvider extends AbstractEfaProvider {
private final static String API_BASE = "http://efa.vvt.at/vvtadr/";
public class VvtProvider extends AbstractHafasProvider {
private static final String API_BASE = "http://fahrplan.vvt.at/bin/";
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 VvtProvider() {
super(NetworkId.VVT, API_BASE);
public VvtProvider(final String jsonApiAuthorization) {
super(NetworkId.VVT, API_BASE, "dn", PRODUCTS_MAP);
setUseRouteIndexAsTripId(false);
setJsonApiVersion("1.11");
setJsonApiClient("{\"id\":\"VAO\",\"l\":\"vs_vvt\",\"type\":\"AND\"}");
setJsonApiAuthorization(jsonApiAuthorization);
setJsonNearbyLocationsEncoding(Charsets.UTF_8);
}
@Override
public Set<Product> defaultProducts() {
return Product.ALL;
}
private static final Pattern P_SPLIT_NAME_ONE_COMMA = Pattern.compile("([^,]*), ([^,]{3,64})");
@Override
protected String[] splitStationName(final String name) {
final Matcher m = P_SPLIT_NAME_ONE_COMMA.matcher(name);
if (m.matches())
return new String[] { m.group(2), m.group(1) };
return super.splitStationName(name);
}
@Override
protected String[] splitPOI(final String poi) {
final Matcher m = P_SPLIT_NAME_ONE_COMMA.matcher(poi);
if (m.matches())
return new String[] { m.group(2), m.group(1) };
return super.splitPOI(poi);
}
@Override
protected String[] splitAddress(final String address) {
final Matcher m = P_SPLIT_NAME_FIRST_COMMA.matcher(address);
if (m.matches())
return new String[] { m.group(1), m.group(2) };
return super.splitAddress(address);
}
@Override
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);
else
throw new IllegalArgumentException("cannot handle: " + location);
}
@Override
public QueryDeparturesResult queryDepartures(final String stationId, final @Nullable Date time,
final int maxDepartures, final boolean equivs) throws IOException {
return jsonStationBoard(stationId, time, maxDepartures, equivs);
}
@Override
public SuggestLocationsResult suggestLocations(final CharSequence constraint) throws IOException {
return jsonLocMatch(constraint);
}
@Override
public QueryTripsResult queryTrips(final Location from, final @Nullable Location via, final Location to,
final Date date, final boolean dep, final @Nullable Set<Product> products,
final @Nullable Optimize optimize, final @Nullable WalkSpeed walkSpeed,
final @Nullable Accessibility accessibility, final @Nullable Set<Option> options) throws IOException {
return jsonTripSearch(from, via, to, date, dep, products, null);
}
@Override
public QueryTripsResult queryMoreTrips(final QueryTripsContext context, final boolean later) throws IOException {
final JsonContext jsonContext = (JsonContext) context;
return jsonTripSearch(jsonContext.from, jsonContext.via, jsonContext.to, jsonContext.date, jsonContext.dep,
jsonContext.products, later ? jsonContext.laterContext : jsonContext.earlierContext);
}
}

View file

@ -17,7 +17,9 @@
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;
@ -40,12 +42,12 @@ import de.schildbach.pte.dto.SuggestLocationsResult;
*/
public class VvtProviderLiveTest extends AbstractProviderLiveTest {
public VvtProviderLiveTest() {
super(new VvtProvider());
super(new VvtProvider(secretProperty("vvt.json_api_authorization")));
}
@Test
public void nearbyStations() throws Exception {
final NearbyLocationsResult result = queryNearbyStations(new Location(LocationType.STATION, "60101187"));
final NearbyLocationsResult result = queryNearbyStations(new Location(LocationType.STATION, "470118700"));
print(result);
}
@ -57,10 +59,23 @@ public class VvtProviderLiveTest extends AbstractProviderLiveTest {
@Test
public void queryDepartures() throws Exception {
final QueryDeparturesResult result = queryDepartures("60101187", false);
final QueryDeparturesResult result = queryDepartures("470118700", false);
print(result);
}
@Test
public void queryDeparturesInvalidStation() throws Exception {
final QueryDeparturesResult result = queryDepartures("999999", 0, false);
assertEquals(QueryDeparturesResult.Status.INVALID_STATION, result.status);
}
@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");
@ -76,8 +91,8 @@ public class VvtProviderLiveTest extends AbstractProviderLiveTest {
@Test
public void shortTrip() throws Exception {
final QueryTripsResult result = queryTrips(
new Location(LocationType.STATION, "60161512", 47268336, 11355532, "Innsbruck", "Allerheiligen"), null,
new Location(LocationType.STATION, "60161510", 47267272, 11350938, "Innsbruck", "Tschiggfreystraße"),
new Location(LocationType.STATION, "476151200", 47268248, 11355560, "Innsbruck", "Allerheiligen"), null,
new Location(LocationType.STATION, "476151000", 47267241, 11351003, "Innsbruck", "Tschiggfreystraße"),
new Date(), true, Product.ALL, WalkSpeed.NORMAL, Accessibility.NEUTRAL);
print(result);
assertEquals(QueryTripsResult.Status.OK, result.status);