-
Notifications
You must be signed in to change notification settings - Fork 1
/
signserver.pl
133 lines (99 loc) · 2.64 KB
/
signserver.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
#!/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 Device::SerialPort;
our %alert_status = ();
my @serial_devices = @ARGV;
my $http_port = 8081;
my @signs = ();
my @fhs = ();
die "You didn't give me any serial ports!\n" unless @serial_devices;
foreach my $fn (@serial_devices) {
my $port = Device::SerialPort->new($fn);
if ($port) {
$port->baudrate(9600) || die "Failed to set the baud rate on $fn\n";
$port->close();
}
else {
die "Failed to open the serial port $fn to set the baud rate\n";
}
sysopen(my $fh, $fn, O_RDWR) || die "Can't open serial port $fn: $!\n";
push @signs, Sign->new($fh);
push @fhs, $fh;
}
bootstrap_signs();
run_service($http_port);
for my $fh (@fhs) {
close($fh);
}
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));
}
}
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;
}