/*
* Copyright 2010-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 java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.schildbach.pte.dto.Departure;
import de.schildbach.pte.dto.Line;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.Position;
import de.schildbach.pte.dto.Product;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.QueryDeparturesResult.Status;
import de.schildbach.pte.dto.QueryTripsContext;
import de.schildbach.pte.dto.QueryTripsResult;
import de.schildbach.pte.dto.ResultHeader;
import de.schildbach.pte.dto.StationDepartures;
import de.schildbach.pte.util.ParserUtils;
/**
* @author Andreas Schildbach
*/
public class SeptaProvider extends AbstractHafasProvider
{
public static final NetworkId NETWORK_ID = NetworkId.SEPTA;
private static final String API_BASE = "http://airs1.septa.org/bin/";
private static final long PARSER_DAY_ROLLOVER_THRESHOLD_MS = 12 * 60 * 60 * 1000;
public SeptaProvider()
{
super(API_BASE + "stboard.exe/en", API_BASE + "ajax-getstop.exe/dny", API_BASE + "query.exe/en", 4);
}
public NetworkId id()
{
return NETWORK_ID;
}
@Override
protected TimeZone timeZone()
{
return TimeZone.getTimeZone("EST");
}
public boolean hasCapabilities(final Capability... capabilities)
{
for (final Capability capability : capabilities)
if (capability == Capability.AUTOCOMPLETE_ONE_LINE || capability == Capability.DEPARTURES || capability == Capability.TRIPS)
return true;
return false;
}
@Override
protected void setProductBits(final StringBuilder productBits, final Product product)
{
if (product == Product.HIGH_SPEED_TRAIN)
{
}
else if (product == Product.REGIONAL_TRAIN || product == Product.SUBURBAN_TRAIN)
{
productBits.setCharAt(3, '1'); // Regional Rail
}
else if (product == Product.SUBWAY)
{
productBits.setCharAt(0, '1'); // Subway
}
else if (product == Product.TRAM)
{
productBits.setCharAt(1, '1'); // Trolley
}
else if (product == Product.BUS)
{
productBits.setCharAt(2, '1'); // Bus
}
else if (product == Product.ON_DEMAND || product == Product.FERRY || product == Product.CABLECAR)
{
}
else
{
throw new IllegalArgumentException("cannot handle: " + product);
}
}
private static final Pattern P_SPLIT_ADDRESS = Pattern.compile("(.*),\\s+([^,]+\\s+\\d{4,5})");
@Override
protected String[] splitPlaceAndName(final String name)
{
final Matcher matcher = P_SPLIT_ADDRESS.matcher(name);
if (matcher.matches())
return new String[] { matcher.group(2), matcher.group(1) };
else
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);
}
}
private String departuresQueryUri(final String stationId, final int maxDepartures)
{
final Calendar now = new GregorianCalendar(timeZone());
final StringBuilder uri = new StringBuilder(stationBoardEndpoint);
uri.append("?input=").append(normalizeStationId(stationId));
uri.append("&boardType=dep");
uri.append("&time=");
uri.append(ParserUtils.urlEncode(String.format(Locale.ENGLISH, "%02d:%02d %s", now.get(Calendar.HOUR), now.get(Calendar.MINUTE),
now.get(Calendar.AM_PM) == Calendar.AM ? "am" : "pm")));
uri.append("&date=");
uri.append(String.format(Locale.ENGLISH, "%02d%02d%04d", now.get(Calendar.MONTH) + 1, now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.YEAR)));
uri.append("&productsFilter=").append(allProductsString());
if (maxDepartures != 0)
uri.append("&maxJourneys=").append(maxDepartures);
uri.append("&disableEquivs=yes"); // don't use nearby stations
uri.append("&start=yes");
return uri.toString();
}
private static final Pattern P_DEPARTURES_PAGE_COARSE = Pattern
.compile(
".*?" //
+ "(?:" //
+ "
([^<]*)<.*?" // location
+ "\n(\\d{2}/\\d{2}/\\d{4})[^\n]*\n" // date
+ "Departure (\\d{1,2}:\\d{2} [AP]M)\n.*?" // time
+ "(?:
]*>(.+?)
|(No trains in this space of time))" //
+ "|(input cannot be interpreted)|(Verbindung zum Server konnte leider nicht hergestellt werden|kann vom Server derzeit leider nicht bearbeitet werden))" //
+ ".*?" //
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("