summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2024-02-03 16:09:55 +0000
committerdakkar <dakkar@thenautilus.net>2024-02-03 16:09:55 +0000
commit0894f68085a8ef9bfd3c673339416c014be02f85 (patch)
treeb839a26128d69438198a24ae098e30892278e1f2
parentmore cleaning (diff)
downloadHomePanel-0894f68085a8ef9bfd3c673339416c014be02f85.tar.gz
HomePanel-0894f68085a8ef9bfd3c673339416c014be02f85.tar.bz2
HomePanel-0894f68085a8ef9bfd3c673339416c014be02f85.zip
show more than 1 bus stop
-rwxr-xr-xdriver-async.pl2
-rw-r--r--forecast.html.tt2
-rw-r--r--lib/HomePanel/Driver.pm24
-rw-r--r--lib/WebService/TFL/Bus/Prediction.pm2
-rw-r--r--lib/WebService/TFL/Bus/Response.pm20
5 files changed, 42 insertions, 8 deletions
diff --git a/driver-async.pl b/driver-async.pl
index 701a8cd..c158315 100755
--- a/driver-async.pl
+++ b/driver-async.pl
@@ -21,7 +21,7 @@ my $hp = HomePanel::Driver->new({
forecast_key => $config{forecast_key},
forecast_latitude => $config{forecast_latitude},
forecast_longitude => $config{forecast_longitude},
- bus_stop_id => $config{bus_stop_id},
+ bus_stop_ids => $config{bus_stop_id},
});
$hp->start;
diff --git a/forecast.html.tt b/forecast.html.tt
index 8d02648..7b90714 100644
--- a/forecast.html.tt
+++ b/forecast.html.tt
@@ -153,7 +153,7 @@
[% FOREACH p IN b.predictions %]
<tr>
<td class="line">[% p.lineName %]</td>
- <td class="destination">[% p.towards %]</td>
+ <td class="destination">[% p.destinationName %]</td>
<td class="eta">[% minsec_until(p.expectedArrival) %]</td>
</tr>
[% END %]
diff --git a/lib/HomePanel/Driver.pm b/lib/HomePanel/Driver.pm
index ce3026a..e9ea31f 100644
--- a/lib/HomePanel/Driver.pm
+++ b/lib/HomePanel/Driver.pm
@@ -7,9 +7,11 @@ use Net::Async::HTTP;
use Future::AsyncAwait;
use WebService::ForecastIo;
use WebService::TFL::Bus;
+use WebService::TFL::Bus::Response;
use WebService::TFL::TubeStatus;
use HomePanel::Render;
use Types::Path::Tiny qw(AbsFile AbsPath);
+use Types::Standard qw(ArrayRef Str StrictNum);
use Try::Tiny;
use curry::weak;
use namespace::clean;
@@ -31,10 +33,12 @@ sub _build_user_agent {
}
has [qw(forecast_latitude forecast_longitude)] => (
+ isa => StrictNum,
is => 'ro',
required => 1,
);
has forecast_key => (
+ isa => Str,
is => 'ro',
required => 1,
);
@@ -65,7 +69,12 @@ sub forecast_timer_cb {
})->then(sub { $self->forecast_response(shift) })->retain;
}
-has bus_stop_id => ( is => 'ro', required => 1 );
+has bus_stop_ids => (
+ is => 'ro',
+ required => 1,
+ isa => ArrayRef->of(Str)->plus_coercions(Str, sub { [ $_ ] }),
+ coerce => 1,
+);
has bus => (
is => 'lazy',
);
@@ -86,9 +95,16 @@ sub _build_bus_timer {
sub bus_timer_cb {
my ($self) = @_;
- $self->bus->request(
- $self->bus_stop_id
- )->then(sub { $self->bus_response(shift) })->retain;
+ Future->wait_all(
+ map { $self->bus->request($_) } $self->bus_stop_ids->@*
+ )->then(
+ sub {
+ my @done_results = map { $_->result } grep { $_->is_done } @_;
+ $self->bus_response(
+ WebService::TFL::Bus::Response->new_merged(@done_results)
+ );
+ }
+ )->retain;
};
has tube => (
diff --git a/lib/WebService/TFL/Bus/Prediction.pm b/lib/WebService/TFL/Bus/Prediction.pm
index 670baf7..8873b02 100644
--- a/lib/WebService/TFL/Bus/Prediction.pm
+++ b/lib/WebService/TFL/Bus/Prediction.pm
@@ -4,7 +4,7 @@ use Types::Standard -all;
use Types::DateTime -all;
use namespace::clean;
-has [qw(stationName lineName towards)] => (
+has [qw(stationName destinationName lineName towards)] => (
is => 'ro',
isa => Str,
required => 1,
diff --git a/lib/WebService/TFL/Bus/Response.pm b/lib/WebService/TFL/Bus/Response.pm
index 0f811fc..59582be 100644
--- a/lib/WebService/TFL/Bus/Response.pm
+++ b/lib/WebService/TFL/Bus/Response.pm
@@ -9,12 +9,20 @@ has predictions => (
isa => ArrayRef[InstanceOf['WebService::TFL::Bus::Prediction']],
);
+sub _sort {
+ return sort {
+ $a->destinationName cmp $b->destinationName
+ ||
+ $a->expectedArrival <=> $b->expectedArrival
+ } @_;
+}
+
sub new_from_response {
my ($class,$response_data) = @_;
return $class->new({
predictions => [
- sort { $a->expectedArrival <=> $b->expectedArrival }
+ _sort
map { WebService::TFL::Bus::Prediction->new_from_response($_) }
grep { $_->{'$type'} =~ /\bPrediction\b/ }
$response_data->@*
@@ -22,4 +30,14 @@ sub new_from_response {
});
}
+sub new_merged {
+ my ($class, @responses) = @_;
+
+ return $class->new({
+ predictions => [
+ _sort map { $_->predictions->@* } @responses
+ ],
+ });
+}
+
1;