mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-17 08:29:50 +00:00
DB: Migrate to Hafas client interface.
This commit is contained in:
parent
5ff1d2bd35
commit
88f0e12fcb
3 changed files with 52 additions and 65 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 the original author or authors.
|
* Copyright the original author or authors.
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -21,6 +21,10 @@ import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
|
import de.schildbach.pte.dto.Line;
|
||||||
import de.schildbach.pte.dto.Product;
|
import de.schildbach.pte.dto.Product;
|
||||||
|
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
|
@ -28,18 +32,18 @@ import okhttp3.HttpUrl;
|
||||||
/**
|
/**
|
||||||
* @author Andreas Schildbach
|
* @author Andreas Schildbach
|
||||||
*/
|
*/
|
||||||
public final class BahnProvider extends AbstractHafasLegacyProvider {
|
public final class BahnProvider extends AbstractHafasClientInterfaceProvider {
|
||||||
private static final HttpUrl API_BASE = HttpUrl.parse("https://reiseauskunft.bahn.de/bin/");
|
private static final HttpUrl API_BASE = HttpUrl.parse("https://reiseauskunft.bahn.de/bin/");
|
||||||
private static final Product[] PRODUCTS_MAP = { Product.HIGH_SPEED_TRAIN, Product.HIGH_SPEED_TRAIN,
|
private static final Product[] PRODUCTS_MAP = { Product.HIGH_SPEED_TRAIN, Product.HIGH_SPEED_TRAIN,
|
||||||
Product.REGIONAL_TRAIN, Product.REGIONAL_TRAIN, Product.SUBURBAN_TRAIN, Product.BUS, Product.FERRY,
|
Product.REGIONAL_TRAIN, Product.REGIONAL_TRAIN, Product.SUBURBAN_TRAIN, Product.BUS, Product.FERRY,
|
||||||
Product.SUBWAY, Product.TRAM, Product.ON_DEMAND, null, null, null, null };
|
Product.SUBWAY, Product.TRAM, Product.ON_DEMAND, null, null, null, null };
|
||||||
|
|
||||||
public BahnProvider() {
|
public BahnProvider(final String apiAuthorization) {
|
||||||
super(NetworkId.DB, API_BASE, "dn", PRODUCTS_MAP);
|
super(NetworkId.DB, API_BASE, PRODUCTS_MAP);
|
||||||
|
setApiVersion("1.14");
|
||||||
setStationBoardEndpoint(HttpUrl.parse("https://mobile.bahn.de/bin/mobil/bhftafel.exe"));
|
setApiClient("{\"id\":\"DB\",\"v\":\"16040000\",\"type\":\"AND\",\"name\":\"DB Navigator\"}");
|
||||||
setStationBoardHasStationTable(false);
|
setApiAuthorization(apiAuthorization);
|
||||||
setJsonGetStopsUseWeight(false);
|
setRequestChecksumSalt("bdI8UVj40K5fvxwf".getBytes(Charsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -54,7 +58,6 @@ public final class BahnProvider extends AbstractHafasLegacyProvider {
|
||||||
final Matcher m = P_SPLIT_NAME_ONE_COMMA.matcher(name);
|
final Matcher m = P_SPLIT_NAME_ONE_COMMA.matcher(name);
|
||||||
if (m.matches())
|
if (m.matches())
|
||||||
return new String[] { m.group(2), m.group(1) };
|
return new String[] { m.group(2), m.group(1) };
|
||||||
|
|
||||||
return super.splitStationName(name);
|
return super.splitStationName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +66,6 @@ public final class BahnProvider extends AbstractHafasLegacyProvider {
|
||||||
final Matcher m = P_SPLIT_NAME_FIRST_COMMA.matcher(poi);
|
final Matcher m = P_SPLIT_NAME_FIRST_COMMA.matcher(poi);
|
||||||
if (m.matches())
|
if (m.matches())
|
||||||
return new String[] { m.group(1), m.group(2) };
|
return new String[] { m.group(1), m.group(2) };
|
||||||
|
|
||||||
return super.splitStationName(poi);
|
return super.splitStationName(poi);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,28 +74,17 @@ public final class BahnProvider extends AbstractHafasLegacyProvider {
|
||||||
final Matcher m = P_SPLIT_NAME_FIRST_COMMA.matcher(address);
|
final Matcher m = P_SPLIT_NAME_FIRST_COMMA.matcher(address);
|
||||||
if (m.matches())
|
if (m.matches())
|
||||||
return new String[] { m.group(1), m.group(2) };
|
return new String[] { m.group(1), m.group(2) };
|
||||||
|
|
||||||
return super.splitStationName(address);
|
return super.splitStationName(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Pattern P_NORMALIZE_LINE_NAME_TRAM = Pattern.compile("str\\s+(.*)", Pattern.CASE_INSENSITIVE);
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String normalizeLineName(final String lineName) {
|
protected Line newLine(final String operator, final Product product, final String name, final String number) {
|
||||||
final Matcher mTram = P_NORMALIZE_LINE_NAME_TRAM.matcher(lineName);
|
if (product == Product.SUBURBAN_TRAIN && name != null && name.startsWith("S")
|
||||||
if (mTram.matches())
|
&& !(number != null && number.startsWith("S")))
|
||||||
return mTram.group(1);
|
return super.newLine(operator, product, name, "S" + Strings.nullToEmpty(number));
|
||||||
|
if (product == Product.SUBWAY && name != null && name.startsWith("U")
|
||||||
return super.normalizeLineName(lineName);
|
&& !(number != null && number.startsWith("U")))
|
||||||
}
|
return super.newLine(operator, product, name, "U" + Strings.nullToEmpty(number));
|
||||||
|
return super.newLine(operator, product, name, number);
|
||||||
@Override
|
|
||||||
protected Product normalizeType(final String type) {
|
|
||||||
final String ucType = type.toUpperCase();
|
|
||||||
|
|
||||||
if ("N".equals(ucType))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return super.normalizeType(type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,13 +44,7 @@ import de.schildbach.pte.dto.TripOptions;
|
||||||
*/
|
*/
|
||||||
public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
public BahnProviderLiveTest() {
|
public BahnProviderLiveTest() {
|
||||||
super(new BahnProvider());
|
super(new BahnProvider(secretProperty("db.api_authorization")));
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void nearbyStations() throws Exception {
|
|
||||||
final NearbyLocationsResult result = queryNearbyStations(new Location(LocationType.STATION, "692991"));
|
|
||||||
print(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -62,10 +56,11 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
@Test
|
@Test
|
||||||
public void nearbyPOIsByCoordinate() throws Exception {
|
public void nearbyPOIsByCoordinate() throws Exception {
|
||||||
final NearbyLocationsResult result = queryNearbyLocations(EnumSet.of(LocationType.POI),
|
final NearbyLocationsResult result = queryNearbyLocations(EnumSet.of(LocationType.POI),
|
||||||
Location.coord(52525589, 13369548));
|
Location.coord(Point.fromDouble(52.5304903, 13.3791152)));
|
||||||
print(result);
|
print(result);
|
||||||
assertThat(result.locations,
|
assertThat(result.locations, hasItem(new Location(LocationType.POI,
|
||||||
hasItem(new Location(LocationType.POI, "990416076", "Berlin", "Museum für Naturkunde")));
|
"A=4@O=Berlin, Museum für Naturkunde (Kultur und Unterhal@X=13380003@Y=52529724@u=0@U=104@L=991597061@",
|
||||||
|
"Berlin", "Museum für Naturkunde")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -84,14 +79,16 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
public void suggestLocationsUmlaut() throws Exception {
|
public void suggestLocationsUmlaut() throws Exception {
|
||||||
final SuggestLocationsResult result = suggestLocations("Güntzelstr. (U)");
|
final SuggestLocationsResult result = suggestLocations("Güntzelstr. (U)");
|
||||||
print(result);
|
print(result);
|
||||||
assertEquals("Güntzelstr. (U)", result.getLocations().get(0).name);
|
assertThat(result.getLocations(),
|
||||||
|
hasItem(new Location(LocationType.STATION, "731371", "Berlin", "Güntzelstr. (U)")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void suggestLocationsIncomplete() throws Exception {
|
public void suggestLocationsIncomplete() throws Exception {
|
||||||
final SuggestLocationsResult result = suggestLocations("Dammt");
|
final SuggestLocationsResult result = suggestLocations("Dammt");
|
||||||
print(result);
|
print(result);
|
||||||
assertEquals("Hamburg Dammtor", result.getLocations().get(0).name);
|
assertThat(result.getLocations(),
|
||||||
|
hasItem(new Location(LocationType.STATION, "8002548", null, "Hamburg Dammtor")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -104,14 +101,16 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
public void suggestLocationsAddress() throws Exception {
|
public void suggestLocationsAddress() throws Exception {
|
||||||
final SuggestLocationsResult result = suggestLocations("München, Friedenstraße 2");
|
final SuggestLocationsResult result = suggestLocations("München, Friedenstraße 2");
|
||||||
print(result);
|
print(result);
|
||||||
assertEquals(LocationType.ADDRESS, result.getLocations().get(0).type);
|
assertThat(result.getLocations(), hasItem(new Location(LocationType.ADDRESS,
|
||||||
assertEquals("Friedenstraße 2", result.getLocations().get(0).name);
|
"A=2@O=München - Berg am Laim, Friedenstraße 2@X=11602251@Y=48123949@U=103@L=980857648@B=1@p=1378873973@",
|
||||||
|
"München - Berg am Laim", "Friedenstraße 2")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shortTrip() throws Exception {
|
public void shortTrip() throws Exception {
|
||||||
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8011160", null, "Berlin Hbf"),
|
final Location from = new Location(LocationType.STATION, "8011160", null, "Berlin Hbf");
|
||||||
null, new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"), new Date(), true, null);
|
final Location to = new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf");
|
||||||
|
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
||||||
print(result);
|
print(result);
|
||||||
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
||||||
print(laterResult);
|
print(laterResult);
|
||||||
|
@ -131,19 +130,15 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
"Tutzinger-Hof-Platz, Starnberg");
|
"Tutzinger-Hof-Platz, Starnberg");
|
||||||
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
||||||
print(result);
|
print(result);
|
||||||
|
|
||||||
if (!result.context.canQueryLater())
|
|
||||||
return;
|
|
||||||
|
|
||||||
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
||||||
print(laterResult);
|
print(laterResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void noTrips() throws Exception {
|
public void noTrips() throws Exception {
|
||||||
final QueryTripsResult result = queryTrips(
|
final Location from = new Location(LocationType.STATION, "513729", null, "Schillerplatz, Kaiserslautern");
|
||||||
new Location(LocationType.STATION, "513729", null, "Schillerplatz, Kaiserslautern"), null,
|
final Location to = new Location(LocationType.STATION, "403631", null, "Trippstadt Grundschule");
|
||||||
new Location(LocationType.STATION, "403631", null, "Trippstadt Grundschule"), new Date(), true, null);
|
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
||||||
print(result);
|
print(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,10 +150,6 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
"Starnberg, Possenhofener Straße 13");
|
"Starnberg, Possenhofener Straße 13");
|
||||||
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
||||||
print(result);
|
print(result);
|
||||||
|
|
||||||
if (!result.context.canQueryLater())
|
|
||||||
return;
|
|
||||||
|
|
||||||
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
||||||
print(laterResult);
|
print(laterResult);
|
||||||
}
|
}
|
||||||
|
@ -167,31 +158,35 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
public void tripsAcrossBorder() throws Exception {
|
public void tripsAcrossBorder() throws Exception {
|
||||||
final TripOptions options = new TripOptions(EnumSet.of(Product.BUS), null, WalkSpeed.NORMAL,
|
final TripOptions options = new TripOptions(EnumSet.of(Product.BUS), null, WalkSpeed.NORMAL,
|
||||||
Accessibility.NEUTRAL, null);
|
Accessibility.NEUTRAL, null);
|
||||||
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8506131", null, "Kreuzlingen"),
|
final Location from = new Location(LocationType.STATION, "8506131", null, "Kreuzlingen");
|
||||||
null, new Location(LocationType.STATION, "8003400", null, "Konstanz"), new Date(), true, options);
|
final Location to = new Location(LocationType.STATION, "8003400", null, "Konstanz");
|
||||||
|
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, options);
|
||||||
print(result);
|
print(result);
|
||||||
assertEquals(QueryTripsResult.Status.OK, result.status);
|
assertEquals(QueryTripsResult.Status.OK, result.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tripsByCoordinate() throws Exception {
|
public void tripBetweenCoordinates() throws Exception {
|
||||||
final QueryTripsResult result = queryTrips(Location.coord(52535576, 13422171), null,
|
final Location from = Location.coord(52535576, 13422171); // Berlin Marienburger Str.
|
||||||
Location.coord(52525589, 13369548), new Date(), true, null);
|
final Location to = Location.coord(52525589, 13369548); // Berlin Hbf
|
||||||
|
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
||||||
print(result);
|
print(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tripsTooClose() throws Exception {
|
public void tripsTooClose() throws Exception {
|
||||||
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"),
|
final Location location = new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf");
|
||||||
null, new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"), new Date(), true, null);
|
final QueryTripsResult result = queryTrips(location, null, location, new Date(), true, null);
|
||||||
print(result);
|
print(result);
|
||||||
assertEquals(QueryTripsResult.Status.TOO_CLOSE, result.status);
|
assertEquals(QueryTripsResult.Status.TOO_CLOSE, result.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tripsInvalidDate() throws Exception {
|
public void tripsInvalidDate() throws Exception {
|
||||||
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8011160", null, "Berlin Hbf"),
|
final Location from = new Location(LocationType.STATION, "8011160", null, "Berlin Hbf");
|
||||||
null, new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"), new Date(0), true, null);
|
final Location to = new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf");
|
||||||
|
final Date date = new Date(System.currentTimeMillis() - 2 * 365 * 24 * 3600 * 1000l); // 2 years ago
|
||||||
|
final QueryTripsResult result = queryTrips(from, null, to, date, true, null);
|
||||||
print(result);
|
print(result);
|
||||||
assertEquals(QueryTripsResult.Status.INVALID_DATE, result.status);
|
assertEquals(QueryTripsResult.Status.INVALID_DATE, result.status);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# Secrets are needed to run some of the live tests.
|
# Secrets are needed to run some of the live tests.
|
||||||
navitia.authorization =
|
navitia.authorization =
|
||||||
|
db.api_authorization =
|
||||||
bvg.api_authorization =
|
bvg.api_authorization =
|
||||||
nvv.api_authorization =
|
nvv.api_authorization =
|
||||||
vbn.api_authorization =
|
vbn.api_authorization =
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue