forked from apparentlymart/led-alpha-sign-msg-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignipserver.pl
134 lines (99 loc) · 2.63 KB
/
signipserver.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl;
use Data::Dumper;
use lib "/opt/led-alpha-sign-msg-server/lib";
use Sign;
use Sign::TextFile;
use Sign::StringFile;
use Sign::MessageUtil qw(mode fancylines lines string flash_text show_time show_day_of_week show_date);
use AnyEvent;
use AnyEvent::HTTPD;
use IO::Socket::INET;
our %alert_status = ();
my @hosts = @ARGV;
my $http_port = 8081;
my @signs = ();
my @fhs = ();
my $fh = "";
die "You didn't give me any ip:port combinations!\n" unless @hosts;
foreach my $fn (@hosts) {
my ( $host, $port ) = split ":", $fn;
$fh = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $host,
PeerPort => $port
)
or die "can't connect to port $port on $host: $!";
push @signs, Sign->new($fh,"ip");
push @fhs, $fh;
}
bootstrap_signs();
run_service($http_port);
for my $fh (@fhs) {
$fh->close();
}
sub bootstrap_signs {
foreach my $sign (@signs) {
$sign->sync_time();
my %files = ();
$sign->configure_files(
'A' => Sign::TextFile->new(mode("HOLD")),
);
$sign->clear_priority_text_file_text();
}
}
sub update_priority_message {
my @alerts_to_show = sort keys %alert_status;
if (@alerts_to_show) {
foreach my $sign (@signs) {
#$sign->set_priority_text_file_text(mode("HOLD"), lines(map { flash_text($_) } @alerts_to_show));
$sign->set_priority_text_file_text(mode("HOLD"), lines(map { $_} @alerts_to_show));
}
}
else {
foreach my $sign (@signs) {
$sign->clear_priority_text_file_text();
}
}
}
sub run_service {
my ($http_port) = @_;
my $httpd = AnyEvent::HTTPD->new(port => $http_port);
$httpd->reg_cb(
'' => sub {
my ($httpd, $req) = @_;
unless ($req->method eq 'POST') {
$req->respond([ 405, "Method not allowed", { 'Content-Type' => 'text/plain' }, 'Please POST to me' ]);
return;
}
my $result = handle_request($req);
if ($result) {
$req->respond([ 200, "OK", { 'Content-Type' => 'text/plain' }, 'OK' ]);
}
else {
$req->respond([ 500, "Not OK", { 'Content-Type' => 'text/plain' }, 'Not OK' ]);
}
},
);
$httpd->run;
}
sub handle_request {
my ($req) = @_;
my $url = $req->url;
unless ($url->path eq '/') {
return 0;
}
my %params = $req->vars;
my $new_active = $params{active};
my $message = $params{message};
if ($new_active) {
$alert_status{$message} = 1;
}
else {
delete $alert_status{$message};
}
update_priority_message();
return 1;
}