summaryrefslogtreecommitdiff
path: root/cgi/index.pl
blob: 90ef4dbbdca47c5188cb837c18080c665b12a1b5 (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
#!/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
<!DOCTYPE html>
<html>
<head>
	<title><%= $title %></title>
	<meta charset="utf-8">
	<style type="text/css">

	div.about {
		font-family: Sans-Serif;
		color: #666666;
	}

	div.about a {
		color: #000066;
	}

	</style>
</head>
<body>

% if ($city and $stop) {
<img src="../../<%= $city %>/<%= $stop %>.png" alt=""/>
% }
% else {

<p>
DB-Fakedisplay displays the next departures at a DB station, just like the big
LC display in the station itself.
</p>

% }

<div class="input-field">

<% if (my $error = stash 'error') { %>
<p>
  Error: <%= $error %><br/>
</p>
<% } %>

<%= form_for _redirect => begin %>
<p>
  Station name:
  <%= text_field 'city' %>
  <%= text_field 'stop' %>
  <%= submit_button 'Display' %>
</p>
<% end %>

</div>

<div class="about">
<a href="http://finalrewind.org/projects/db-fakedisplay/">db-fakedisplay</a>
v<%= $version %>
</div>

</body>
</html>

@@ not_found.html.ep
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>page not found</title>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<div>
page not found
</div>
</body>
</html>