From c39a49eed2af14781f13679970250b8d255f90e1 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Fri, 4 Jul 2014 21:38:12 +0200 Subject: [PATCH] =?UTF-8?q?G=C3=B6ttingen=20&=20S=C3=BCdniedersachsen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- enabler/src/de/schildbach/pte/NetworkId.java | 2 +- .../src/de/schildbach/pte/VsnProvider.java | 187 ++++++++++++++++++ .../pte/live/VsnProviderLiveTest.java | 113 +++++++++++ 3 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 enabler/src/de/schildbach/pte/VsnProvider.java create mode 100644 enabler/test/de/schildbach/pte/live/VsnProviderLiveTest.java diff --git a/enabler/src/de/schildbach/pte/NetworkId.java b/enabler/src/de/schildbach/pte/NetworkId.java index 879f8cde..a4a706e9 100644 --- a/enabler/src/de/schildbach/pte/NetworkId.java +++ b/enabler/src/de/schildbach/pte/NetworkId.java @@ -26,7 +26,7 @@ public enum NetworkId RT, // Germany - DB, BVG, VBB, NVV, BAYERN, MVV, INVG, AVV, VGN, VVM, VMV, HVV, SH, GVH, BSVAG, VBN, NASA, VVO, VMS, VGS, VRR, VRS, MVG, NPH, VRN, VRT, VVS, NALDO, DING, KVV, VAGFR, NVBW, VVV, + DB, BVG, VBB, NVV, BAYERN, MVV, INVG, AVV, VGN, VVM, VMV, HVV, SH, GVH, VSN, BSVAG, VBN, NASA, VVO, VMS, VGS, VRR, VRS, MVG, NPH, VRN, VRT, VVS, NALDO, DING, KVV, VAGFR, NVBW, VVV, // Austria OEBB, VOR, WIEN, LINZ, SVV, VVT, VMOBIL, IVB, STV, diff --git a/enabler/src/de/schildbach/pte/VsnProvider.java b/enabler/src/de/schildbach/pte/VsnProvider.java new file mode 100644 index 00000000..b9a8623c --- /dev/null +++ b/enabler/src/de/schildbach/pte/VsnProvider.java @@ -0,0 +1,187 @@ +/* + * Copyright 2014 the original author or authors. + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.schildbach.pte; + +import java.io.IOException; + +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.Product; +import de.schildbach.pte.dto.QueryDeparturesResult; + +/** + * @author Andreas Schildbach + */ +public class VsnProvider extends AbstractHafasProvider +{ + public static final NetworkId NETWORK_ID = NetworkId.VSN; + private static final String API_BASE = "http://fahrplaner.vsninfo.de/hafas/"; + + public VsnProvider() + { + super(API_BASE + "stboard.exe/dn", API_BASE + "ajax-getstop.exe/dn", API_BASE + "query.exe/dn", 10, UTF_8); + } + + public NetworkId id() + { + return NETWORK_ID; + } + + public boolean hasCapabilities(final Capability... capabilities) + { + for (final Capability capability : capabilities) + if (capability == Capability.SUGGEST_LOCATIONS || capability == Capability.DEPARTURES || capability == Capability.TRIPS) + return true; + + return false; + } + + @Override + protected char intToProduct(final int value) + { + if (value == 1) + return 'I'; + if (value == 2) + return 'I'; + if (value == 4) + return 'R'; + if (value == 8) + return 'R'; + if (value == 16) + return 'S'; + if (value == 32) + return 'B'; + if (value == 64) + return 'F'; + if (value == 128) + return 'U'; + if (value == 256) + return 'T'; + if (value == 512) + return 'P'; + + throw new IllegalArgumentException("cannot handle: " + value); + } + + @Override + protected void setProductBits(final StringBuilder productBits, final Product product) + { + if (product == Product.HIGH_SPEED_TRAIN) + { + productBits.setCharAt(0, '1'); // Hochgeschwindigkeitszug + productBits.setCharAt(1, '1'); // IC/EC + } + else if (product == Product.REGIONAL_TRAIN) + { + productBits.setCharAt(2, '1'); // sonstiger Schnellzug + productBits.setCharAt(3, '1'); // Regionalzug + } + else if (product == Product.SUBURBAN_TRAIN) + { + productBits.setCharAt(4, '1'); // S-Bahn + } + else if (product == Product.SUBWAY) + { + productBits.setCharAt(7, '1'); // U-Bahn + } + else if (product == Product.TRAM) + { + productBits.setCharAt(8, '1'); // Straßenbahn + } + else if (product == Product.BUS) + { + productBits.setCharAt(5, '1'); // Bus + } + else if (product == Product.ON_DEMAND) + { + productBits.setCharAt(9, '1'); // Anrufverkehre + } + else if (product == Product.FERRY) + { + productBits.setCharAt(6, '1'); // Schiff + } + else if (product == Product.CABLECAR) + { + } + else + { + throw new IllegalArgumentException("cannot handle: " + product); + } + } + + private static final String[] PLACES = { "Göttingen" }; + + @Override + protected String[] splitPlaceAndName(final String name) + { + for (final String place : PLACES) + { + if (name.startsWith(place + " ") || name.startsWith(place + "-")) + return new String[] { place, name.substring(place.length() + 1) }; + } + + return super.splitPlaceAndName(name); + } + + public NearbyStationsResult queryNearbyStations(final Location location, final int maxDistance, final int maxStations) throws IOException + { + if (location.type == LocationType.STATION && location.hasId()) + { + final StringBuilder uri = new StringBuilder(stationBoardEndpoint); + uri.append("?near=Anzeigen"); + uri.append("&distance=").append(maxDistance != 0 ? maxDistance / 1000 : 50); + uri.append("&input=").append(normalizeStationId(location.id)); + + return htmlNearbyStations(uri.toString()); + } + else + { + throw new IllegalArgumentException("cannot handle: " + location); + } + } + + public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures, final boolean equivs) throws IOException + { + final StringBuilder uri = new StringBuilder(stationBoardEndpoint); + uri.append(xmlQueryDeparturesParameters(stationId)); + + return xmlQueryDepartures(uri.toString(), stationId); + } + + @Override + protected char normalizeType(final String type) + { + final String ucType = type.toUpperCase(); + + if ("E".equals(ucType)) + return 'R'; + + if ("TB".equals(ucType)) // Hamburg-Koeln-Express + return 'P'; + + if ("T84".equals(ucType)) + return '?'; + + final char t = super.normalizeType(type); + if (t != 0) + return t; + + return 0; + } +} diff --git a/enabler/test/de/schildbach/pte/live/VsnProviderLiveTest.java b/enabler/test/de/schildbach/pte/live/VsnProviderLiveTest.java new file mode 100644 index 00000000..f0d1a87c --- /dev/null +++ b/enabler/test/de/schildbach/pte/live/VsnProviderLiveTest.java @@ -0,0 +1,113 @@ +/* + * Copyright 2014 the original author or authors. + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.schildbach.pte.live; + +import static org.junit.Assert.assertEquals; + +import java.util.Date; + +import org.junit.Test; + +import de.schildbach.pte.NetworkProvider.Accessibility; +import de.schildbach.pte.NetworkProvider.WalkSpeed; +import de.schildbach.pte.VsnProvider; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.Product; +import de.schildbach.pte.dto.QueryDeparturesResult; +import de.schildbach.pte.dto.QueryTripsResult; +import de.schildbach.pte.dto.SuggestLocationsResult; + +/** + * @author Andreas Schildbach + */ +public class VsnProviderLiveTest extends AbstractProviderLiveTest +{ + public VsnProviderLiveTest() + { + super(new VsnProvider()); + } + + @Test + public void nearbyStations() throws Exception + { + final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.STATION, "8000128"), 0, 0); + + print(result); + } + + @Test + public void nearbyStationsByCoordinate() throws Exception + { + final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.ADDRESS, 51536614, 9925673), 0, 0); + + print(result); + } + + @Test + public void queryDepartures() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures("8000128", 0, false); + + print(result); + } + + @Test + public void queryDeparturesInvalidStation() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures("999999", 0, false); + + assertEquals(QueryDeparturesResult.Status.INVALID_STATION, result.status); + } + + @Test + public void suggestLocations() throws Exception + { + final SuggestLocationsResult result = provider.suggestLocations("Hannover"); + + print(result); + } + + @Test + public void suggestLocationsUmlaut() throws Exception + { + final SuggestLocationsResult result = provider.suggestLocations("Göttingen"); + + print(result); + } + + @Test + public void suggestLocationsWithoutCoordinatesInResult() throws Exception + { + final SuggestLocationsResult result = provider.suggestLocations("aachen"); + + print(result); + } + + @Test + public void shortTrip() throws Exception + { + final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8000128", null, "Göttingen"), null, new Location( + LocationType.STATION, "1140061", null, "Göttingen Nikolausberger Weg"), new Date(), true, Product.ALL, WalkSpeed.NORMAL, + Accessibility.NEUTRAL); + System.out.println(result); + final QueryTripsResult laterResult = queryMoreTrips(result.context, true); + System.out.println(laterResult); + } +}