NAME

Travel::Status::DE::HAFAS - Interface to HAFAS-based online arrival/departure monitors

SYNOPSIS

use Travel::Status::DE::HAFAS;

my $status = Travel::Status::DE::HAFAS->new(
	station => 'Essen Hbf',
);

if (my $err = $status->errstr) {
	die("Request error: ${err}\n");
}

for my $departure ($status->results) {
	printf(
		"At %s: %s to %s from platform %s\n",
		$departure->time,
		$departure->line,
		$departure->destination,
		$departure->platform,
	);
}

VERSION

version 6.15

DESCRIPTION

Travel::Status::DE::HAFAS is an interface to HAFAS-based arrival/departure monitors using the mgate.exe interface.

It can report departures/arrivals at a specific station, search for stations, or provide details about a specific journey. It supports non-blocking operation via promises.

METHODS

my $status = Travel::Status::DE::HAFAS->new(%opt)

Requests item(s) as specified by opt and returns a new Travel::Status::DE::HAFAS element with the results. Dies if the wrong opt were passed.

opt must contain either a station, geoSearch, locationSearch, journey, or journeyMatch flag:

station => station

Request station board (arrivals or departures) for station, e.g. "Essen HBf" or "Alfredusbad, Essen (Ruhr)". The station must be specified either by name or by EVA ID (e.g. 8000080 for Dortmund Hbf). Results are available via $status->results.

geoSearch => { lat => latitude, lon => longitude }

Search for stations near latitude, longitude. Results are available via $status->results.

locationSearch => query

Search for stations whose name is similar to query. Results are available via $status->results.

journey => { id => tripid [, name => line ] }

Request details about the journey identified by tripid and line. The result is available via $status->result.

journeyMatch => query

Request journeys that match query (e.g. "ICE 205" or "S 31111"). Results are available via $status->results. In contrast to journey, the results typically only contain a minimal amount of information: trip ID, train/line identifier, and first and last stop. There is no real-time data.

The following optional flags may be set. Values in brackets indicate flags that are only relevant in certain request modes, e.g. geoSearch or journey.

arrivals => bool (station)

Request arrivals (if bool is true) rather than departures (if bool is false or arrivals is not specified).

cache => Cache::File object

Store HAFAS replies in the provided cache object. This module works with real-time data, so the object should be configured for an expiry of one to two minutes.

datetime => DateTime object (station)

Date and time to report for. Defaults to now.

excluded_mots => [mot1, mot2, ...] (geoSearch, station, journeyMatch)

By default, all modes of transport (trains, trams, buses etc.) are returned. If this option is set, all modes appearing in mot1, mot2, ... will be excluded. The supported modes depend on service, use get_services or get_service to get the supported values.

exclusive_mots => [mot1, mot2, ...] (geoSearch, station, journeyMatch)

If this option is set, only the modes of transport appearing in mot1, mot2, ... will be returned. The supported modes depend on service, use get_services or get_service to get the supported values.

language => language

Request text messages to be provided in language. Supported languages depend on service, use get_services or get_service to get the supported values. Providing an unsupported or invalid value may lead to garbage output.

lookahead => int (station)

Request arrivals/departures that occur up to int minutes after the specified datetime. Default: -1 (do not limit results by time).

lwp_options => \%hashref

Passed on to LWP::UserAgent->new. Defaults to { timeout => 10 }, pass an empty hashref to call the LWP::UserAgent constructor without arguments.

results => count (geoSearch, locationSearch, station)

Request up to count results. Default: 30.

service => service

Request results from service, defaults to "DB". See get_services (and hafas-m --list) for a list of supported services.

with_polyline => bool (journey)

Request a polyline (series of geo-coordinates) indicating the train's route.

my $status_p = Travel::Status::DE::HAFAS->new_p(%opt)

Returns a promise that resolves into a Travel::Status::DE::HAFAS instance ($status) on success and rejects with an error message on failure. If the failure occured after receiving a response from the HAFAS backend, the rejected promise contains a Travel::Status::DE::HAFAS instance as a second argument. This instance can be used e.g. to call similar_stops_p in case of an ambiguous location specifier. In addition to the arguments of new, the following mandatory arguments must be set.

promise => promises module

Promises implementation to use for internal promises as well as new_p return value. Recommended: Mojo::Promise(3pm).

user_agent => user agent

User agent instance to use for asynchronous requests. The object must implement a post_p function. Recommended: Mojo::UserAgent(3pm).

$status->errcode

In case of an error in the HAFAS backend, returns the corresponding error code as string. If no backend error occurred, returns undef.

$status->errstr

In case of an error in the HTTP request or HAFAS backend, returns a string describing it. If no error occurred, returns undef.

$status->results (geoSearch, locationSearch)

Returns a list of stop locations. Each list element is a Travel::Status::DE::HAFAS::Location(3pm) object.

If no matching results were found or the parser / http request failed, returns an empty list.

$status->results (station)

Returns a list of arrivals/departures. Each list element is a Travel::Status::DE::HAFAS::Journey(3pm) object.

If no matching results were found or the parser / http request failed, returns undef.

$status->results (journeyMatch)

Returns a list of Travel::Status::DE::HAFAS::Journey(3pm) object that describe matching journeys. In general, these objects lack real-time data, intermediate stops, and more.

$status->result (journey)

Returns a single Travel::Status::DE::HAFAS::Journey(3pm) object that describes the requested journey.

If no result was found or the parser / http request failed, returns undef.

$status->messages

Returns a list of Travel::Status::DE::HAFAS::Message(3pm) objects with service messages. Each message belongs to at least one arrival/departure (station, journey) or to at least stop alongside its route (journey).

$status->station (station)

Returns a hashref describing the departure stations in all requested journeys. The hashref contains four entries: names (station names), name (most common name), evas (UIC / EVA IDs), and eva (most common UIC / EVA ID). These are subject to change.

Note that the most common name and ID may be different from the station for which departures were requested, as HAFAS uses different identifiers for train stations, bus stops, and other modes of transit even if they are interlinked.

$status->similar_stops

Returns a list of hashrefs describing stops whose name is similar to the one requested in the constructor's station parameter. Returns nothing if the active service does not support this feature. This is most useful if errcode returns 'LOCATION', which means that the HAFAS backend could not identify the stop.

See Travel::Status::DE::HAFAS::StopFinder(3pm)'s results method for details on the return value.

$status->similar_stops_p(%opt)

Returns a promise resolving to a list of hashrefs describing stops whose name is similar to the one requested in the constructor's station parameter. Returns nothing if the active service does not support this feature. This is most useful if errcode returns 'LOCATION', which means that the HAFAS backend could not identify the stop.

See Travel::Status::DE::HAFAS::StopFinder(3pm)'s results method for details on the resolved values.

If $status has been created using new_p, this function does not require arguments. Otherwise, the caller must specify promise and user_agent (see new_p above).

$status->get_active_service

Returns a hashref describing the active service when a service is active and nothing otherwise. The hashref contains the following keys.

coverage => hashref

Area in which the service provides near-optimal coverage. Typically, this means a (nearly) complete list of departures and real-time data. The hashref contains two optional keys: area (GeoJSON) and regions (list of strings, e.g. "DE" or "CH-BE").

homepage => string

Homepage URL of the service provider.

languages => arrayref

Languages supported by the backend; see the constructor's language argument.

name => string

Service name, e.g. Bay Area Rapid Transit or Deutsche Bahn.

mgate => string

HAFAS backend URL

productbits => arrayref

MOT bits supported by the backend. arrayref contains either strings (one string per mode of transit) or arrayrefs (one string pair per mode of transit, with the first entry referring to the MOT identifier and the second one containing a slightly longer description of it).

time_zone => string (optional)

The time zone this service reports arrival/departure times in. If this key is not present, it is safe to assume that it uses Europe/Berlin.

Travel::Status::DE::HAFAS::get_services()

Returns an array containing all supported HAFAS services. Each element is a hashref and contains all keys mentioned in get_active_service. It also contains a shortname key, which is the service name used by the constructor's service parameter, e.g. BART or DB.

Travel::Status::DE::HAFAS::get_service($service)

Returns a hashref describing the service $service. Returns nothing if $service is not supported. See get_active_service for the hashref layout.

DIAGNOSTICS

None.

DEPENDENCIES

  • Class::Accessor(3pm)

  • DateTime(3pm)

  • DateTime::Format::Strptime(3pm)

  • LWP::UserAgent(3pm)

BUGS AND LIMITATIONS

The non-default services (anything other than DB) are not well tested.

SEE ALSO

  • https://dbf.finalrewind.org?hafas=1 provides a web frontend to most of this module's features. Set hafas=service to use a specific service.

  • Travel::Routing::DE::HAFAS(3pm) for itineraries.

AUTHOR

Copyright (C) 2015-2024 by Birte Kristina Friesel <derf@finalrewind.org>

LICENSE

This module is licensed under the same terms as Perl itself.