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 4.07
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, or provide details about a specific journey. It supports non-blocking operation via promises.
METHODS
- my $status = Travel::Status::DE::HAFAS->new(%opt)
-
Requests departures/arrivals/journey 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 or a journey 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
. - journey => { id => tripid [, name => line ] }
-
Request details about the journey identified by tripid and line. The result is available via
$status->result
.
The following optional flags may be set:
- arrivals => bool
-
Request arrivals (if bool is true) rather than departures (if bool is false or arrivals is not specified). Only relevant in station board mode.
- 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
-
Date and time to report for. Defaults to now. Only relevant in station board mode.
- excluded_mots => [mot1, mot2, ...]
-
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. Only relevant in station board mode.
- exclusive_mots => [mot1, mot2, ...]
-
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. Only relevant in station board mode.
- lookahead => int
-
Request arrivals/departures that occur up to int minutes after the specified datetime. Default: -1 (do not limit results by time). Only relevant in station board mode.
- 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
-
Request up to count results. Default: 30. Only relevant in station board mode.
- 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
-
Request a polyline (series of geo-coordinates) indicating the train's route. Only relevant in journey mode.
- my $status_p = Travel::Status::DE::HAFAS->new_p(%opt)
-
Return a promise that resolves into a Travel::Status::DE::HAFAS instance ($status) on success and rejects with an error message ($status->errstr) on failure. 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
-
Returns a list of arrivals/departures. Each list element is a Travel::Status::DE::HAFAS::Journey(3pm) object. Unavailable in journey mode.
If no matching results were found or the parser / http request failed, returns undef.
- $status->result
-
Returns a single Travel::Status::DE::HAFAS::Journey(3pm) object that describes the requested journey. Unavailable in station board mode.
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.
- $status->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.
Not available in journey mode.
- $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 'H730', 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->get_active_service
-
Returns a hashref describing the active service when a service is active and nothing otherwise. The hashref contains the keys url (URL to the station board service), stopfinder (URL to the stopfinder service, if supported), name, and productbits (arrayref describing the supported modes of transport, may contain duplicates).
- 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.
- 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
Travel::Status::DE::HAFAS::Journey(3pm), Travel::Status::DE::HAFAS::StopFinder(3pm).
AUTHOR
Copyright (C) 2015-2022 by Daniel Friesel <derf@finalrewind.org>
LICENSE
This module is licensed under the same terms as Perl itself.