Skip to content

Commit 9e5cdd1

Browse files
committed
Add daemons for serial dumper and file process script
1 parent 49589d4 commit 9e5cdd1

6 files changed

+399
-141
lines changed

process-sigs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use ADBOS::DB;
7+
use ADBOS::Parse;
8+
use ADBOS::Config;
9+
use File::Slurp;
10+
use Any::Daemon;
11+
use Log::Report qw(adbos);
12+
13+
sub process($;$);
14+
15+
my $config = simple_config;
16+
17+
my $parser = ADBOS::Parse->new();
18+
my $db = ADBOS::DB->new($config);
19+
20+
# See if text is being piped in. If so, read it
21+
if (not -t STDIN and my $message = do { local $/; <STDIN> })
22+
{
23+
process($message);
24+
exit;
25+
}
26+
27+
# Otherwise read files from the cache
28+
my $cache = $config->{queuedir};
29+
30+
my $daemon = Any::Daemon->new
31+
( user => $config->{user}
32+
, group => $config->{group}
33+
, pid_file => $config->{pidprocess}
34+
, workdir => $cache
35+
);
36+
37+
dispatcher SYSLOG => 'syslog'
38+
, identity => 'process-sigs'
39+
, facility => "local0"
40+
, flags => "pid ndelay nowait"
41+
, mode => 1;
42+
43+
info __x"Waiting to process signals in $cache...";
44+
45+
$daemon->run
46+
( max_childs => 1
47+
, child_task => \&process_sigs
48+
);
49+
50+
sub process_sigs()
51+
{
52+
while (1) {
53+
# Look for files in cache
54+
my $dh;
55+
opendir($dh, $cache);
56+
while (defined(my $f = readdir($dh)))
57+
{
58+
my $file = "$cache/$f";
59+
next unless -f $file;
60+
if (my $message = read_file($file))
61+
{
62+
process($message, $f);
63+
unlink $file;
64+
}
65+
}
66+
close $dh;
67+
sleep 1;
68+
}
69+
}
70+
71+
sub process($;$)
72+
{
73+
my ($message,$f) = @_;
74+
75+
if ($f)
76+
{
77+
info __x"Attempting to parse message for file $f";
78+
} else {
79+
info __x"Attempting to parse message ";
80+
}
81+
82+
# Clean up extra line breaks
83+
$message =~ s/\r*//g;
84+
85+
if ($message =~ /^CHANNEL CHECK$/m)
86+
{ # Ignore for the moment
87+
# XXXX Log in DB in future
88+
}
89+
elsif (my $values = $parser->parse($message))
90+
{
91+
my $status;
92+
$db->signalProcess($values, \$status);
93+
info __x"$status";
94+
} elsif ($db->signalOther($message)) {
95+
info __x"Parsed signal as an associated signal";
96+
} else {
97+
$db->signalStore($message);
98+
info __x"Parse failed";
99+
}
100+
}

process-sigs.rc

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#! /bin/bash
2+
3+
### BEGIN INIT INFO
4+
# Provides: process-sigs
5+
# Required-Start: $remote_fs $syslog mysql
6+
# Required-Stop: $remote_fs $syslog mysql
7+
# Default-Start: 2 3 4 5
8+
# Default-Stop: 0 1 6
9+
# Short-Description: Manages process-sigs script
10+
# Description: Manages the script that dumps signals from RS232 to the file cache
11+
### END INIT INFO
12+
13+
PATH=/sbin:/bin:/usr/sbin:/usr/bin
14+
NAME=process-sigs
15+
DAEMON=/usr/local/opdef/$NAME
16+
DAEMON=/var/www/opdef.andybev.com/$NAME
17+
SNAME=process-sigs # Name of script
18+
DESC="ADBOS signal dump script"
19+
PIDFILE="/var/run/$NAME.pid"
20+
XNAME=/usr/bin/perl # Needed when start-stop-daemon is searching for process
21+
# export SIMPLE_CONF=/usr/local/simplelists/etc/resend.conf
22+
23+
. /lib/lsb/init-functions
24+
25+
test -f $DAEMON || exit 0
26+
27+
case "$1" in
28+
start)
29+
log_daemon_msg "Starting $DESC" "$NAME "
30+
31+
# Start the daemon, phase 1: see if it is already running.
32+
start-stop-daemon --start --pidfile $PIDFILE --exec $XNAME \
33+
--startas $DAEMON --test > /dev/null
34+
if [ "$?" != "0" ]; then
35+
log_warning_msg "(already running)"
36+
exit 0
37+
fi
38+
39+
# Start the daemon, phase 2: it was not running, so actually start it now.
40+
start-stop-daemon --start --quiet --pidfile $PIDFILE --name $NAME \
41+
--exec $DAEMON
42+
if [ "$?" -ne "0" ]; then
43+
log_end_msg 1
44+
exit 1
45+
fi
46+
47+
# Started successfully.
48+
log_end_msg 0
49+
exit 0
50+
;;
51+
52+
53+
stop)
54+
log_action_begin_msg "Stopping $DESC"
55+
56+
# See if it is actually running
57+
start-stop-daemon --stop --pidfile $PIDFILE --name $NAME \
58+
--exec $XNAME --test > /dev/null
59+
if [ "$?" -eq "1" ]; then
60+
# Already stopped.
61+
log_warning_msg "(not running)"
62+
exit 0
63+
fi
64+
65+
# Stop it
66+
start-stop-daemon --stop --pidfile $PIDFILE --name $NAME \
67+
--exec $XNAME --retry TERM/forever/TERM/5
68+
69+
if [ "$?" -eq "2" ]; then
70+
# Failed to stop.
71+
log_action_end_msg 1
72+
exit 2
73+
fi
74+
75+
76+
if [ "$?" -eq "0" ]; then
77+
# Success
78+
log_action_end_msg 0
79+
fi
80+
;;
81+
82+
83+
reload|force-reload|restart)
84+
log_daemon_msg "Restarting $DESC"
85+
$0 stop
86+
$0 start
87+
;;
88+
89+
90+
*)
91+
N=/etc/init.d/$SNAME
92+
echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
93+
exit 1
94+
;;
95+
96+
esac
97+
98+
exit 0

process.pl

-72
This file was deleted.

serial-dump

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Device::SerialPort;
7+
use File::Temp qw/tempfile/;
8+
use ADBOS::DB;
9+
use ADBOS::Config;
10+
use Any::Daemon;
11+
use Log::Report qw(adbos);
12+
13+
my $config = simple_config;
14+
my $db = ADBOS::DB->new($config);
15+
16+
my $port;
17+
my $dev = "/dev/ttyS0";
18+
19+
if ($port = Device::SerialPort->new($dev))
20+
{
21+
$port->databits(7);
22+
$port->baudrate(9600);
23+
$port->parity("odd");
24+
$port->stopbits(1);
25+
$port->handshake("off");
26+
}
27+
else
28+
{
29+
die "Failed to open serial port";
30+
}
31+
32+
my $STALL_DEFAULT=1000; # how many seconds to wait for new input
33+
34+
$port->read_char_time(0); # don't wait for each character
35+
$port->read_const_time(1000); # 1 second per unfulfilled "read" call
36+
37+
my $chars=0;
38+
my $buffer="";
39+
40+
my $cache = $config->{queuedir};
41+
42+
my $daemon = Any::Daemon->new
43+
( user => $config->{user}
44+
, group => $config->{group}
45+
, pid_file => $config->{piddump}
46+
, workdir => $cache
47+
);
48+
49+
dispatcher SYSLOG => 'syslog'
50+
, identity => 'serial-dump'
51+
, facility => "local0"
52+
, flags => "pid ndelay nowait"
53+
, mode => 1;
54+
55+
info __x"Listening for signals on $dev...";
56+
57+
$daemon->run
58+
( max_childs => 1
59+
, child_task => \&dump_messages
60+
);
61+
62+
sub dump_messages()
63+
{
64+
65+
$SIG{TERM} = \&close;
66+
$SIG{INT} = \&close;
67+
68+
while (1) {
69+
my ($count,$saw)=$port->read(255); # will read _up to_ 255 chars
70+
71+
if ($count > 0) {
72+
$chars+=$count;
73+
$buffer.=$saw;
74+
75+
info __x"DATA RX";
76+
if ($buffer =~ /\RNNNN/)
77+
{
78+
(my $message, $buffer) = split /\RNNNN/, $buffer;
79+
info __x"MESSAGE FOUND";
80+
$message =~ s/\r\n/\n/g;
81+
$message =~ s/(\n|.)*VZCZC//g;
82+
83+
# Dump to disk
84+
my ($fh) = tempfile(DIR => $cache);
85+
print $fh $message;
86+
close $fh;
87+
88+
# Update status
89+
$db->statusSet({ last_signal => \'NOW()' });
90+
$db->sequenceSet($1)
91+
if ($message =~ m/JCT(\d\d\d)/m);
92+
}
93+
}
94+
}
95+
}
96+
97+
sub close()
98+
{
99+
info __x"Stopping listening on $dev...";
100+
$port->close if $port;
101+
undef $port;
102+
}
103+

0 commit comments

Comments
 (0)