mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-13 08:10:46 +00:00
ZVV: Migrate to Hafas client interface.
This commit is contained in:
parent
52e6fc3aab
commit
d0bcedc0e8
3 changed files with 22 additions and 105 deletions
|
@ -21,12 +21,7 @@ import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
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 de.schildbach.pte.dto.Style;
|
import de.schildbach.pte.dto.Style;
|
||||||
import de.schildbach.pte.dto.Style.Shape;
|
import de.schildbach.pte.dto.Style.Shape;
|
||||||
|
@ -36,25 +31,20 @@ import okhttp3.HttpUrl;
|
||||||
/**
|
/**
|
||||||
* @author Andreas Schildbach
|
* @author Andreas Schildbach
|
||||||
*/
|
*/
|
||||||
public class ZvvProvider extends AbstractHafasLegacyProvider {
|
public class ZvvProvider extends AbstractHafasClientInterfaceProvider {
|
||||||
private static final HttpUrl API_BASE = HttpUrl.parse("https://online.fahrplan.zvv.ch/bin/");
|
private static final HttpUrl API_BASE = HttpUrl.parse("https://online.fahrplan.zvv.ch/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.FERRY, Product.SUBURBAN_TRAIN, Product.BUS,
|
Product.REGIONAL_TRAIN, Product.REGIONAL_TRAIN, Product.FERRY, Product.SUBURBAN_TRAIN, Product.BUS,
|
||||||
Product.CABLECAR, Product.SUBWAY, Product.TRAM };
|
Product.CABLECAR, Product.BUS, Product.TRAM };
|
||||||
|
|
||||||
public ZvvProvider() {
|
public ZvvProvider(final String jsonApiAuthorization) {
|
||||||
super(NetworkId.ZVV, API_BASE, "dn", PRODUCTS_MAP);
|
super(NetworkId.ZVV, API_BASE, PRODUCTS_MAP);
|
||||||
|
setApiVersion("1.15");
|
||||||
setRequestUrlEncoding(Charsets.UTF_8);
|
setApiClient("{\"id\":\"ZVV\",\"type\":\"AND\"}");
|
||||||
setJsonNearbyLocationsEncoding(Charsets.UTF_8);
|
setApiAuthorization(jsonApiAuthorization);
|
||||||
setStyles(STYLES);
|
setStyles(STYLES);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Product> defaultProducts() {
|
|
||||||
return Product.ALL;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String[] OPERATORS = { "SBB", "SZU" };
|
private static final String[] OPERATORS = { "SBB", "SZU" };
|
||||||
private static final String[] PLACES = { "Zürich", "Winterthur" };
|
private static final String[] PLACES = { "Zürich", "Winterthur" };
|
||||||
|
|
||||||
|
@ -79,7 +69,6 @@ public class ZvvProvider extends AbstractHafasLegacyProvider {
|
||||||
for (final String place : PLACES)
|
for (final String place : PLACES)
|
||||||
if (name.startsWith(place + " ") || name.startsWith(place + ","))
|
if (name.startsWith(place + " ") || name.startsWith(place + ","))
|
||||||
return new String[] { place, name.substring(place.length() + 1) };
|
return new String[] { place, name.substring(place.length() + 1) };
|
||||||
|
|
||||||
return super.splitStationName(name);
|
return super.splitStationName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +77,6 @@ public class ZvvProvider 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,86 +85,12 @@ public class ZvvProvider 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_NUMBER = Pattern.compile("\\d{2,5}");
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Line parseLineAndType(final String lineAndType) {
|
public Set<Product> defaultProducts() {
|
||||||
final Matcher m = P_NORMALIZE_LINE_AND_TYPE.matcher(lineAndType);
|
return Product.ALL;
|
||||||
if (m.matches()) {
|
|
||||||
final String number = Strings.emptyToNull(m.group(1).replaceAll("\\s+", ""));
|
|
||||||
final String type = Strings.emptyToNull(m.group(2));
|
|
||||||
|
|
||||||
if ("Bus".equals(type))
|
|
||||||
return newLine(Product.BUS, stripPrefix(number, "Bus"), null);
|
|
||||||
if ("Bus-NF".equals(type))
|
|
||||||
return newLine(Product.BUS, stripPrefix(number, "Bus", "Bus-NF"), null, Line.Attr.WHEEL_CHAIR_ACCESS);
|
|
||||||
if ("Tro".equals(type) || "Trolley".equals(type))
|
|
||||||
return newLine(Product.BUS, stripPrefix(number, "Tro"), null);
|
|
||||||
if ("Tro-NF".equals(type))
|
|
||||||
return newLine(Product.BUS, stripPrefix(number, "Tro", "Tro-NF"), null, Line.Attr.WHEEL_CHAIR_ACCESS);
|
|
||||||
if ("Trm".equals(type))
|
|
||||||
return newLine(Product.TRAM, stripPrefix(number, "Trm"), null);
|
|
||||||
if ("Trm-NF".equals(type))
|
|
||||||
return newLine(Product.TRAM, stripPrefix(number, "Trm", "Trm-NF"), null, Line.Attr.WHEEL_CHAIR_ACCESS);
|
|
||||||
|
|
||||||
if ("S18".equals(number))
|
|
||||||
return newLine(Product.SUBURBAN_TRAIN, "S18", null);
|
|
||||||
|
|
||||||
if (number != null && P_NUMBER.matcher(number).matches())
|
|
||||||
return newLine(null, number, null);
|
|
||||||
|
|
||||||
if (type != null) {
|
|
||||||
final Product product = normalizeType(type);
|
|
||||||
if (product != null)
|
|
||||||
return newLine(product, number, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalStateException(
|
|
||||||
"cannot normalize type " + type + " number " + number + " line#type " + lineAndType);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalStateException("cannot normalize line#type " + lineAndType);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String stripPrefix(final String str, final String... prefixes) {
|
|
||||||
for (final String prefix : prefixes)
|
|
||||||
if (str.startsWith(prefix))
|
|
||||||
return str.substring(prefix.length());
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Product normalizeType(final String type) {
|
|
||||||
final String ucType = type.toUpperCase();
|
|
||||||
|
|
||||||
if ("N".equals(ucType)) // Nachtbus
|
|
||||||
return Product.BUS;
|
|
||||||
if ("TX".equals(ucType))
|
|
||||||
return Product.BUS;
|
|
||||||
if ("KB".equals(ucType)) // Kleinbus?
|
|
||||||
return Product.BUS;
|
|
||||||
|
|
||||||
if ("TE2".equals(ucType)) // Basel - Strasbourg
|
|
||||||
return Product.REGIONAL_TRAIN;
|
|
||||||
|
|
||||||
if ("D-SCHIFF".equals(ucType))
|
|
||||||
return Product.FERRY;
|
|
||||||
if ("DAMPFSCH".equals(ucType))
|
|
||||||
return Product.FERRY;
|
|
||||||
|
|
||||||
if ("BERGBAHN".equals(ucType))
|
|
||||||
return Product.CABLECAR;
|
|
||||||
if ("LSB".equals(ucType)) // Luftseilbahn
|
|
||||||
return Product.CABLECAR;
|
|
||||||
if ("SLB".equals(ucType)) // Sesselliftbahn
|
|
||||||
return Product.CABLECAR;
|
|
||||||
|
|
||||||
return super.normalizeType(type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Map<String, Style> STYLES = new HashMap<>();
|
private static final Map<String, Style> STYLES = new HashMap<>();
|
||||||
|
|
|
@ -37,13 +37,7 @@ import de.schildbach.pte.dto.SuggestLocationsResult;
|
||||||
*/
|
*/
|
||||||
public class ZvvProviderLiveTest extends AbstractProviderLiveTest {
|
public class ZvvProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
public ZvvProviderLiveTest() {
|
public ZvvProviderLiveTest() {
|
||||||
super(new ZvvProvider());
|
super(new ZvvProvider(secretProperty("zvv.api_authorization")));
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void nearbyStationsByStation() throws Exception {
|
|
||||||
final NearbyLocationsResult result = queryNearbyStations(new Location(LocationType.STATION, "8503000"));
|
|
||||||
print(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -72,9 +66,9 @@ public class ZvvProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shortTrip() throws Exception {
|
public void shortTrip() throws Exception {
|
||||||
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8503000", null, "Zürich HB"),
|
final Location from = new Location(LocationType.STATION, "8503000", "Zürich", "Hauptbahnhof");
|
||||||
null, new Location(LocationType.STATION, "8507785", null, "Bern, Hauptbahnhof"), new Date(), true,
|
final Location to = new Location(LocationType.STATION, "8507785", "Bern", "Hauptbahnhof");
|
||||||
null);
|
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);
|
||||||
|
@ -91,4 +85,12 @@ public class ZvvProviderLiveTest extends AbstractProviderLiveTest {
|
||||||
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
|
||||||
print(laterResult);
|
print(laterResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tripBetweenCoordinates() throws Exception {
|
||||||
|
final Location from = Location.coord(Point.fromDouble(47.3782535, 8.5392280)); // Zürich Hauptbahnhof
|
||||||
|
final Location to = Location.coord(Point.fromDouble(47.3852910, 8.5172170)); // Bahnhof Hardbrücke
|
||||||
|
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
|
||||||
|
print(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ svv.api_authorization =
|
||||||
vvt.api_authorization =
|
vvt.api_authorization =
|
||||||
vmobil.api_authorization =
|
vmobil.api_authorization =
|
||||||
vao.api_authorization =
|
vao.api_authorization =
|
||||||
|
zvv.api_authorization =
|
||||||
se.api_authorization =
|
se.api_authorization =
|
||||||
hsl.usertoken =
|
hsl.usertoken =
|
||||||
hsl.passphrase =
|
hsl.passphrase =
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue