Unverified Commit 87637c4f authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

add preliminary departure board support

parent 091f9fa0
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use DateTime;
use Encode qw(decode);
use JSON;
use Getopt::Long qw(:config no_ignore_case);
use List::Util   qw(max);
use Travel::Status::DE::DBRIS;

my $developer_mode;
@@ -146,7 +147,17 @@ if ($json_output) {
}

if ( $opt{station} ) {
	die("Unimplemented");
	my $max_line = max map { length( $_->line ) } $status->results;
	my $max_dest = max map { length( $_->dest_name ) } $status->results;
	for my $result ( $status->results ) {
		printf(
			"%s  %${max_line}s  %${max_dest}s  %s\n",
			$result->is_cancelled ? '--:--' : $result->dep->strftime('%H:%M'),
			$result->line,
			$result->dest_name,
			$result->platform // $result->sched_platform
		);
	}
}
elsif ( $opt{geoSearch} ) {
	for my $result ( $status->results ) {
+20 −10
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use DateTime::Format::Strptime;
use Encode qw(decode encode);
use JSON;
use LWP::UserAgent;
use Travel::Status::DE::DBRIS::Journey;
use Travel::Status::DE::DBRIS::Location;

our $VERSION = '0.01';
@@ -67,10 +68,10 @@ sub new {
		confess('station or geoSearch must be specified');
	}

	#$self->{strptime_obj} //= DateTime::Format::Strptime->new(
	#	pattern   => '%Y%m%dT%H%M%S',
	#	time_zone => $hafas_instance->{$service}{time_zone} // 'Europe/Berlin',
	#);
	$self->{strptime_obj} //= DateTime::Format::Strptime->new(
		pattern   => '%Y-%m-%dT%H:%M:%S%Z',
		time_zone => 'Europe/Berlin',
	);

	my $json = $self->{json} = JSON->new->utf8;

@@ -244,15 +245,24 @@ sub get_with_cache_p {
sub parse_search {
	my ($self) = @_;

	$self->{results} = [];
	@{ $self->{results} }
	  = map { Travel::Status::DE::DBRIS::Location->new( json => $_ ) }
	  @{ $self->{raw_json} // [] };

	if ( $self->{errstr} ) {
	return $self;
}

	@{ $self->{results} }
	  = map { Travel::Status::DE::DBRIS::Location->new( json => $_ ) }
	  @{ $self->{raw_json} // [] };
sub parse_stationboard {
	my ($self) = @_;

	# @{$self->{messages}} = map { Travel::Status::DE::DBRIS::Message->new(...) } @{$self->{raw_json}{globalMessages}/[]};

	@{ $self->{results} } = map {
		Travel::Status::DE::DBRIS::Journey->new(
			json         => $_,
			strptime_obj => $self->{strptime_obj}
		)
	} @{ $self->{raw_json}{entries} // [] };

	return $self;
}
+69 −0
Original line number Diff line number Diff line
package Travel::Status::DE::DBRIS::Journey;

use strict;
use warnings;
use 5.020;

use parent 'Class::Accessor';

our $VERSION = '0.01';

Travel::Status::DE::DBRIS::Journey->mk_ro_accessors(
	qw(type dep sched_dep rt_dep is_cancelled line stop_name stop_eva id admin_id journey_id sched_platform platform dest_name dest_eva route)
);

sub new {
	my ( $obj, %opt ) = @_;

	my $json     = $opt{json}->[0];
	my $strptime = $opt{strptime_obj};

	my $ref = {
		type                => $json->{type},
		line                => $json->{lineName},
		is_cancelled        => $json->{canceled},
		dest_name           => $json->{destination}{name},
		platform            => $json->{platform},
		sched_platform      => $json->{platformSchedule},
		dest_eva            => $json->{destination}{evaNumber},
		raw_route           => $json->{viaStops},
		raw_cancelled_route => $json->{canceledStopsAfterActualDestination},
	};

	bless( $ref, $obj );

	if ( $json->{timeSchedule} ) {
		$ref->{sched_dep} = $strptime->parse_datetime( $json->{timeSchedule} );
	}
	if ( $json->{timeDelayed} ) {
		$ref->{rt_dep} = $strptime->parse_datetime( $json->{timeDelayed} );
	}
	$ref->{dep} = $ref->{rt_dep} // $ref->{schd_dep};

	return $ref;
}

sub route {
	my ($self) = @_;

	if ( $self->{route} ) {
		return @{ $self->{route} };
	}

	@{ $self->{route} }
	  = map { Travel::Status::DE::DBRIS::Location->new( json => $_ ) }
	  ( @{ $self->{raw_route} // [] },
		@{ $self->{raw_cancelled_route} // [] } );

	return @{ $self->{route} };
}

sub TO_JSON {
	my ($self) = @_;

	my $ret = { %{$self} };

	return $ret;
}

1;
+12 −8
Original line number Diff line number Diff line
@@ -9,7 +9,8 @@ use parent 'Class::Accessor';
our $VERSION = '0.01';

Travel::Status::DE::DBRIS::Location->mk_ro_accessors(
	qw(eva id lat lon name products type));
	qw(eva id lat lon name products type is_cancelled is_additional is_separation display_priority)
);

sub new {
	my ( $obj, %opt ) = @_;
@@ -17,13 +18,16 @@ sub new {
	my $json = $opt{json};

	my $ref = {
		eva      => $json->{extId},
		eva           => $json->{extId} // $json->{evaNumber},
		id            => $json->{id},
		lat           => $json->{lat},
		lon           => $json->{lon},
		name          => $json->{name},
		products      => $json->{products},
		type          => $json->{type},
		is_cancelled  => $json->{canceled},
		is_additional => $json->{additional},

	};

	bless( $ref, $obj );