summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Helper/EFA.pm
blob: 0e7f7d77d2c7bb1e62d49656b17b72cade3c16e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package DBInfoscreen::Helper::EFA;

# Copyright (C) 2020-2022 Birte Kristina Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later

use strict;
use warnings;
use 5.020;

use DateTime;
use Encode     qw(decode encode);
use Mojo::JSON qw(decode_json);
use Mojo::Promise;
use Mojo::Util qw(url_escape);
use Travel::Status::DE::EFA;

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

	my $version = $opt{version};

	$opt{header}
	  = { 'User-Agent' =>
"dbf/${version} on $opt{root_url} +https://finalrewind.org/projects/db-fakedisplay"
	  };

	return bless( \%opt, $class );

}

sub get_polyline_p {
	my ( $self, %opt ) = @_;

	my $stopseq = $opt{stopseq};
	my $service = $opt{service};
	my $promise = Mojo::Promise->new;

	Travel::Status::DE::EFA->new_p(
		service    => $service,
		stopseq    => $stopseq,
		cache      => $self->{realtime_cache},
		promise    => 'Mojo::Promise',
		user_agent => $self->{user_agent}->request_timeout(10)
	)->then(
		sub {
			my ($efa) = @_;
			my $journey = $efa->result;

			$promise->resolve($journey);
			return;
		}
	)->catch(
		sub {
			my ($err) = @_;
			$self->{log}->debug("EFA->new_p($stopseq) error: $err");
			$promise->reject($err);
			return;
		}
	)->wait;

	return $promise;
}

sub get_coverage {
	my ( $self, $service ) = @_;

	my $service_definition = Travel::Status::DE::EFA::get_service($service);

	if ( not $service_definition ) {
		return {};
	}

	return $service_definition->{coverage}{area} // {};
}

sub get_json_p {
	my ( $self, $cache, $url ) = @_;

	my $promise = Mojo::Promise->new;

	if ( my $content = $cache->thaw($url) ) {
		$self->{log}->debug("efa->get_json_p($url): cached");
		if ( $content->{error} ) {
			return $promise->reject( $content->{error} );
		}
		return $promise->resolve($content);
	}

	$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
	  ->then(
		sub {
			my ($tx) = @_;

			if ( my $err = $tx->error ) {
				$self->{log}->debug(
					"efa->get_json_p($url): HTTP $err->{code} $err->{message}");
				$cache->freeze( $url, { error => $err->{message} } );
				$promise->reject(
					"GET $url returned HTTP $err->{code} $err->{message}");
				return;
			}

			my $res = $tx->res->json;

			if ( not $res ) {
				$self->{log}->debug("efa->get_json_p($url): empty response");
				$promise->reject("GET $url returned empty response");
				return;
			}

			$cache->freeze( $url, $res );

			$promise->resolve($res);

			return;
		}
	)->catch(
		sub {
			my ($err) = @_;
			$self->{log}->debug("efa->get_json_p($url): $err");
			$cache->freeze( $url, { error => $err } );
			$promise->reject($err);
			return;
		}
	)->wait;

	return $promise;
}

sub get_efa_occupancy {
	my ( $self, %opt ) = @_;

	my $eva      = $opt{eva};
	my $train_no = $opt{train_no};
	my $promise  = Mojo::Promise->new;

	$self->get_json_p( $self->{realtime_cache},
		"https://vrrf.finalrewind.org/_eva/occupancy-by-eva/${eva}.json" )
	  ->then(
		sub {
			my ($utilization_json) = @_;

			if ( $utilization_json->{train}{$train_no}{occupancy} ) {
				$promise->resolve(
					$utilization_json->{train}{$train_no}{occupancy} );
				return;
			}
			$promise->reject;
			return;
		}
	)->catch(
		sub {
			$promise->reject;
			return;
		}
	)->wait;

	return $promise;
}

1;