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);
}
}