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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#!/usr/bin/env perl
use Mojolicious::Lite;
use Cache::File;
use DateTime;
use DateTime::Format::Strptime;
use App::VRR::Fakedisplay;
use Travel::Status::DE::VRR;
no warnings 'uninitialized';
our $VERSION = '0.02';
sub default_no_lines {
return 5;
}
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');
my $no_lines = $self->param('no_lines');
if ( $no_lines < 1 or $no_lines > 10 ) {
$no_lines = default_no_lines();
}
$self->stash( title => 'vrr-fakedisplay' );
$self->stash( version => $VERSION );
$self->stash( params => $self->req->params->to_string );
$self->stash( height => $no_lines * 10 );
$self->stash( width => 180 );
$self->render(
'main',
city => $city,
stop => $stop,
version => $VERSION,
title => "departures for ${city} ${stop}",
);
}
sub shorten_line {
my ($line) = @_;
$line =~ s{ ^ SB \K \s+ }{}x;
return $line;
}
sub shorten_destination {
my ( $dest, $city ) = @_;
if ( not( $dest =~ m{ Hbf $ }ix ) ) {
$dest =~ s{ ^ $city \s }{}ix;
}
if ( length($dest) > 20 ) {
$dest =~ s{^Dortmund}{DO}
or $dest =~ s{^Duisburg}{DU}
or $dest =~ s{^Düsseldorf}{D}
or $dest =~ s{^Essen}{E}
or $dest =~ s{^Gelsenkirchen}{GE}
or $dest =~ s{^Mülheim}{MH};
}
$dest = substr( $dest, 0, 20 );
return $dest;
}
sub render_image {
my $self = shift;
my $city = $self->stash('city');
my $stop = $self->stash('stop');
my $dt_now = DateTime->now( time_zone => 'Europe/Berlin' );
my $color = $self->param('color') || '255,208,0';
my $no_lines = $self->param('no_lines');
my ( @grep_line, @grep_platform );
my ( $results, $errstr ) = get_results_for( $city, $stop );
my $strp_simple = DateTime::Format::Strptime->new(
pattern => '%H:%M',
time_zone => 'floating',
);
my $strp_full = DateTime::Format::Strptime->new(
pattern => '%d.%m.%Y %H:%M',
time_zone => 'floating',
);
if ( $self->param('line') ) {
@grep_line = split( qr{,}, $self->param('line') );
}
if ( $self->param('platform') ) {
@grep_platform = split( qr{,}, $self->param('platform') );
}
if ( $no_lines < 1 or $no_lines > 10 ) {
$no_lines = default_no_lines();
}
my $png = App::VRR::Fakedisplay->new(
width => 180,
height => $no_lines * 10,
color => [ split( qr{,}, $color ) ]
);
$self->res->headers->content_type('image/png');
for my $d ( @{$results} ) {
my $line = $d->line;
my $platform = ( split( qr{ }, $d->platform ) )[-1];
my $destination = $d->destination;
my $time = $d->time;
my $etr;
my $dt_dep = $strp_full->parse_datetime($time)
// $strp_simple->parse_datetime($time);
my $dt;
if ( ( @grep_line and not( $line ~~ \@grep_line ) )
or ( @grep_platform and not( $platform ~~ \@grep_platform ) )
or ( $line =~ m{ ^ (RB | RE | IC | EC) }x ) )
{
next;
}
if ( $time =~ m{ ^ \d\d? : \d\d $ }x ) {
$dt = DateTime->new(
year => $dt_now->year,
month => $dt_now->month,
day => $dt_now->day,
hour => $dt_dep->hour,
minute => $dt_dep->minute,
second => $dt_dep->second,
time_zone => 'Europe/Berlin',
);
}
else {
$dt = $dt_dep;
}
my $duration = $dt->subtract_datetime($dt_now);
if ( $duration->is_negative ) {
next;
}
elsif ( $duration->in_units('minutes') == 0 ) {
$etr = 'sofort';
}
elsif ( $duration->in_units('hours') == 0 ) {
$etr = sprintf( ' %2d', $duration->in_units('minutes'), );
}
else {
last;
}
$destination = shorten_destination( $destination, $city );
$line = shorten_line($line);
$png->draw_at( 0, $line );
$png->draw_at( 25, $destination );
$png->draw_at( 144, $etr );
if ( $etr ne 'sofort' ) {
$png->draw_at( 161, 'min' );
}
$png->new_line();
}
$self->render( data => $png->png );
}
get '/_redirect' => sub {
my $self = shift;
my $city = $self->param('city');
my $stop = $self->param('stop');
my $params = $self->req->params;
$params->remove('city');
$params->remove('stop');
if ( not $params->param('no_lines')
or $params->param('no_lines') == default_no_lines() )
{
$params->remove('no_lines');
}
my $params_s = $params->to_string;
$self->redirect_to("/${city}/${stop}?${params_s}");
};
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;
}
div.break {
height: 1em;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
</head>
<body>
% if ($city and $stop) {
<img src="../<%= $city %>/<%= $stop %>.png?<%= $params %>" alt=""
id="display" height="<%= $height * 4 %>" width="<%= $width * 4 %>"/>
% }
% else {
<p>
VRR-Fakedisplay shows the next departures at a public transit stop, just like
the Lumino LED displays used at some stops.
</p>
<p>
It was written for the VRR (Verkehrsverbund Rhein-Ruhr), but supports most other
local transit networks as well.
</p>
% }
<div class="input-field">
<% if (my $error = stash 'error') { %>
<p>
Error: <%= $error %><br/>
</p>
<% } %>
<%= form_for _redirect => begin %>
<p>
City -> Stop:
<%= text_field 'city' %>
<%= text_field 'stop' %>
<%= submit_button 'Display' %>
<div class="break"></div>
(optional) number of lines (1 .. 10):
<%= text_field 'no_lines' %>
<br/>
</p>
<% end %>
</div>
<div class="about">
<a href="http://finalrewind.org/projects/vrr-fakedisplay/">vrr-fakedisplay</a>
v<%= $version %>
</div>
</body>
<script type="text/javascript">
function reloadDisplay() {
d = new Date();
$("#display").attr("src", "../<%= $city %>/<%= $stop %>.png?<%= $params %>&r="+d.getTime())
setTimeout('reloadDisplay()', 60000);
}
setTimeout('reloadDisplay()', 60000);
</script>
</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>
|