Hafas: Support via for JSON trips query.

This commit is contained in:
Andreas Schildbach 2016-10-12 11:00:42 +02:00
parent 5daefdbc56
commit 879de41cf7
5 changed files with 36 additions and 15 deletions

View file

@ -149,15 +149,17 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider {
@SuppressWarnings("serial")
public static class JsonContext implements QueryTripsContext {
public final Location from, to;
public final Location from, via, to;
public final Date date;
public final boolean dep;
public final Set<Product> products;
public final String laterContext, earlierContext;
public JsonContext(final Location from, final Location to, final Date date, final boolean dep,
final Set<Product> products, final String laterContext, final String earlierContext) {
public JsonContext(final Location from, final @Nullable Location via, final Location to, final Date date,
final boolean dep, final Set<Product> products, final String laterContext,
final String earlierContext) {
this.from = from;
this.via = via;
this.to = to;
this.date = date;
this.dep = dep;
@ -1029,8 +1031,8 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider {
private static final Joiner JOINER = Joiner.on(' ').skipNulls();
protected final QueryTripsResult jsonTripSearch(Location from, Location to, final Date time, final boolean dep,
final @Nullable Set<Product> products, final String moreContext) throws IOException {
protected final QueryTripsResult jsonTripSearch(Location from, @Nullable Location via, Location to, final Date time,
final boolean dep, final @Nullable Set<Product> products, final String moreContext) throws IOException {
if (!from.hasId() && from.hasName()) {
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT);
final List<Location> locations = suggestLocations(JOINER.join(from.place, from.name)).getLocations();
@ -1041,6 +1043,16 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider {
from = locations.get(0);
}
if (via != null && !via.hasId() && via.hasName()) {
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT);
final List<Location> locations = suggestLocations(JOINER.join(via.place, via.name)).getLocations();
if (locations.isEmpty())
return new QueryTripsResult(header, QueryTripsResult.Status.UNKNOWN_VIA);
if (locations.size() > 1)
return new QueryTripsResult(header, locations, null, null);
via = locations.get(0);
}
if (!to.hasId() && to.hasName()) {
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT);
final List<Location> locations = suggestLocations(JOINER.join(to.place, to.name)).getLocations();
@ -1063,6 +1075,7 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider {
+ jsonContext //
+ "\"depLocL\":[" + jsonLocation(from) + "]," //
+ "\"arrLocL\":[" + jsonLocation(to) + "]," //
+ (via != null ? "\"viaLocL\":[{\"loc\":" + jsonLocation(via) + "}]," : "") //
+ "\"outDate\":\"" + outDate + "\"," //
+ "\"outTime\":\"" + outTime + "\"," //
+ "\"" + outFrwdKey + "\":" + outFrwd + "," //
@ -1195,7 +1208,7 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider {
trips.add(trip);
}
final JsonContext context = new JsonContext(from, to, time, dep, products, res.optString("outCtxScrF"),
final JsonContext context = new JsonContext(from, via, to, time, dep, products, res.optString("outCtxScrF"),
res.optString("outCtxScrB"));
return new QueryTripsResult(header, null, from, null, to, context, trips);
} catch (final JSONException x) {

View file

@ -116,14 +116,14 @@ public class ShProvider extends AbstractHafasProvider {
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, to, date, dep, products, null);
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.to, jsonContext.date, jsonContext.dep, jsonContext.products,
later ? jsonContext.laterContext : jsonContext.earlierContext);
return jsonTripSearch(jsonContext.from, jsonContext.via, jsonContext.to, jsonContext.date, jsonContext.dep,
jsonContext.products, later ? jsonContext.laterContext : jsonContext.earlierContext);
}
@Override

View file

@ -118,14 +118,14 @@ public class VaoProvider extends AbstractHafasProvider {
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, to, date, dep, products, null);
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.to, jsonContext.date, jsonContext.dep, jsonContext.products,
later ? jsonContext.laterContext : jsonContext.earlierContext);
return jsonTripSearch(jsonContext.from, jsonContext.via, jsonContext.to, jsonContext.date, jsonContext.dep,
jsonContext.products, later ? jsonContext.laterContext : jsonContext.earlierContext);
}
private static final Map<String, Style> STYLES = new HashMap<String, Style>();

View file

@ -115,14 +115,14 @@ public class VbnProvider extends AbstractHafasProvider {
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, to, date, dep, products, null);
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.to, jsonContext.date, jsonContext.dep, jsonContext.products,
later ? jsonContext.laterContext : jsonContext.earlierContext);
return jsonTripSearch(jsonContext.from, jsonContext.via, jsonContext.to, jsonContext.date, jsonContext.dep,
jsonContext.products, later ? jsonContext.laterContext : jsonContext.earlierContext);
}
private static final Map<String, Style> STYLES = new HashMap<String, Style>();

View file

@ -115,6 +115,14 @@ public class ShProviderLiveTest extends AbstractProviderLiveTest {
print(result);
}
@Test
public void tripKielVia() throws Exception {
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "3490015"),
new Location(LocationType.STATION, "3490020"), new Location(LocationType.STATION, "706923"), new Date(),
true, Product.ALL, WalkSpeed.NORMAL, Accessibility.NEUTRAL);
print(result);
}
@Test
public void trip_errorTooClose() throws Exception {
final Location station = new Location(LocationType.STATION, "003665026");