From ae6fb6a1aa12a52d94bf1f220d75111dff6a54ee Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 29 Oct 2011 02:29:33 +0200 Subject: initial commit --- cgi/index.pl | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 cgi/index.pl (limited to 'cgi/index.pl') diff --git a/cgi/index.pl b/cgi/index.pl new file mode 100644 index 0000000..90ef4db --- /dev/null +++ b/cgi/index.pl @@ -0,0 +1,156 @@ +#!/usr/bin/env perl +use Mojolicious::Lite; +use Cache::File; + +use App::VRR::Fakedisplay; +use Travel::Status::DE::VRR; + +our $VERSION = '0.00'; + +sub get_results_for { + my ($city, $stop) = @_; + + my $cache = Cache::File->new( + cache_root => '/tmp/vrr-fake', + default_expires => '900 sec', + ); + + my $results = $cache->thaw("${city} _ ${stop}"); + + if ( not $results ) { + my $status + = Travel::Status::DE::VRR->new(place => $city, name => $stop); + $results = [ [$status->results], $status->errstr ]; + $cache->freeze( "${city} _ ${stop}", $results ); + } + + return @{$results}; +} + +sub handle_request { + my $self = shift; + my $city = $self->stash('city'); + my $stop = $self->stash('stop'); + + $self->stash( title => 'vrr-fakedisplay' ); + $self->stash( version => $VERSION ); + + $self->render( + 'main', + city => $city, + stop => $stop, + version => $VERSION, + title => "departures for ${city} ${stop}", + ); +} + +sub render_image { + my $self = shift; + my $city = $self->stash('city'); + my $stop = $self->stash('stop'); + + $self->res->headers->content_type('image/png'); + + my ($results, $errstr) = get_results_for($city, $stop); + + my $png = App::VRR::Fakedisplay->new(); + for my $d (@{$results}[0 .. 5]) { + $png->draw_at(0, $d->line); + $png->draw_at(30, $d->destination); + $png->draw_at(180, $d->time); + $png->new_line(); + } + + $self->render(data => $png->png); +} + +get '/_redirect' => sub { + my $self = shift; + my $city = $self->param('city'); + my $stop = $self->param('stop'); + + $self->redirect_to("/${city}/${stop}"); +}; + +get '/' => \&handle_request; +get '/:city/:stop.png' => \&render_image; +get '/:city/:stop' => \&handle_request; + +app->start(); + +__DATA__ + +@@ main.html.ep + + + + <%= $title %> + + + + + +% if ($city and $stop) { + +% } +% else { + +

+DB-Fakedisplay displays the next departures at a DB station, just like the big +LC display in the station itself. +

+ +% } + +
+ +<% if (my $error = stash 'error') { %> +

+ Error: <%= $error %>
+

+<% } %> + +<%= form_for _redirect => begin %> +

+ Station name: + <%= text_field 'city' %> + <%= text_field 'stop' %> + <%= submit_button 'Display' %> +

+<% end %> + +
+ +
+db-fakedisplay +v<%= $version %> +
+ + + + +@@ not_found.html.ep + + + + page not found + + + +
+page not found +
+ + -- cgit v1.2.3