mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-01-31 18:59:33 +00:00
PRESENCE/collectord: allow spaces for room names in config file (Forum: #19169), code make-up
git-svn-id: https://svn.fhem.de/fhem/trunk@8558 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
76db86933e
commit
e7ff7770f0
@ -65,18 +65,11 @@ my %queues;
|
|||||||
my $thread_counter = 0;
|
my $thread_counter = 0;
|
||||||
|
|
||||||
my %state;
|
my %state;
|
||||||
|
|
||||||
my %handle;
|
my %handle;
|
||||||
|
|
||||||
my %socket_to_handle;
|
my %socket_to_handle;
|
||||||
|
|
||||||
my $uuid;
|
my $uuid;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Getopt::Long::Configure('bundling');
|
Getopt::Long::Configure('bundling');
|
||||||
GetOptions(
|
GetOptions(
|
||||||
"d" => \$opt_d, "daemon" => \$opt_d,
|
"d" => \$opt_d, "daemon" => \$opt_d,
|
||||||
@ -85,19 +78,12 @@ GetOptions(
|
|||||||
"c=s" => \$opt_c, "configfile=s" => \$opt_c,
|
"c=s" => \$opt_c, "configfile=s" => \$opt_c,
|
||||||
"p=i" => \$opt_p, "port=i" => \$opt_p,
|
"p=i" => \$opt_p, "port=i" => \$opt_p,
|
||||||
"P=s" => \$opt_P, "pid-file=s" => \$opt_P,
|
"P=s" => \$opt_P, "pid-file=s" => \$opt_P,
|
||||||
"h" => \$opt_h, "help" => \$opt_h);
|
"h" => \$opt_h, "help" => \$opt_h
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Log 0, "=================================================" if($opt_l);
|
Log 0, "=================================================" if($opt_l);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
sub print_usage () {
|
sub print_usage () {
|
||||||
print "Usage:\n";
|
print "Usage:\n";
|
||||||
print " collectord -c <configfile> [-d] [-p <port>] [-P <pidfile>] \n";
|
print " collectord -c <configfile> [-d] [-p <port>] [-P <pidfile>] \n";
|
||||||
@ -117,11 +103,8 @@ sub print_usage () {
|
|||||||
print " log to the given logfile\n";
|
print " log to the given logfile\n";
|
||||||
print " -h, --help\n";
|
print " -h, --help\n";
|
||||||
print " Print detailed help screen\n";
|
print " Print detailed help screen\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if($opt_h)
|
if($opt_h)
|
||||||
{
|
{
|
||||||
print_usage();
|
print_usage();
|
||||||
@ -135,7 +118,6 @@ if(-e "$opt_P")
|
|||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(not $opt_c)
|
if(not $opt_c)
|
||||||
{
|
{
|
||||||
print STDERR "no config file provided\n\n";
|
print STDERR "no config file provided\n\n";
|
||||||
@ -153,7 +135,6 @@ if(not -e "$opt_c" or not -r "$opt_c")
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Log 0, "started with PID $$";
|
Log 0, "started with PID $$";
|
||||||
|
|
||||||
readConfig($opt_c);
|
readConfig($opt_c);
|
||||||
@ -163,13 +144,11 @@ if($opt_d)
|
|||||||
daemonize();
|
daemonize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Write PID file
|
||||||
|
|
||||||
open(PIDFILE, ">$opt_P");
|
open(PIDFILE, ">$opt_P");
|
||||||
print PIDFILE $$."\n";
|
print PIDFILE $$."\n";
|
||||||
close PIDFILE;
|
close PIDFILE;
|
||||||
|
|
||||||
|
|
||||||
$server = new IO::Socket::INET (
|
$server = new IO::Socket::INET (
|
||||||
LocalPort => $opt_p,
|
LocalPort => $opt_p,
|
||||||
Proto => 'tcp',
|
Proto => 'tcp',
|
||||||
@ -472,7 +451,6 @@ while(1)
|
|||||||
|
|
||||||
# now close the socket, that's it
|
# now close the socket, that's it
|
||||||
close $client;
|
close $client;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -487,9 +465,6 @@ while(1)
|
|||||||
Log 1, "server shutdown";
|
Log 1, "server shutdown";
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Log 2, "leaving main loop";
|
Log 2, "leaving main loop";
|
||||||
@ -504,7 +479,6 @@ Log 2, "leaving main loop";
|
|||||||
# to fork the process from the terminal
|
# to fork the process from the terminal
|
||||||
sub daemonize
|
sub daemonize
|
||||||
{
|
{
|
||||||
|
|
||||||
POSIX::setsid or die "setsid $!";
|
POSIX::setsid or die "setsid $!";
|
||||||
|
|
||||||
my $pid = fork();
|
my $pid = fork();
|
||||||
@ -529,16 +503,12 @@ sub daemonize
|
|||||||
open (STDIN, "</dev/null");
|
open (STDIN, "</dev/null");
|
||||||
open (STDOUT, ">/dev/null");
|
open (STDOUT, ">/dev/null");
|
||||||
open (STDERR, ">&STDOUT");
|
open (STDERR, ">&STDOUT");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# the thread subroutine which performs a request for a specific room
|
# the thread subroutine which performs a request for a specific room
|
||||||
sub doQuery($$$)
|
sub doQuery($$$)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
my ($do_config, $do_room, $do_address, $do_uuid) = @_;
|
my ($do_config, $do_room, $do_address, $do_uuid) = @_;
|
||||||
my $return;
|
my $return;
|
||||||
my $socket;
|
my $socket;
|
||||||
@ -548,8 +518,6 @@ sub doQuery($$$)
|
|||||||
my @client_handle;
|
my @client_handle;
|
||||||
my $reconnect_count = 0;
|
my $reconnect_count = 0;
|
||||||
my $client_socket = undef;
|
my $client_socket = undef;
|
||||||
# if the thread gets a termination signal, the thread must be shutdown by itself
|
|
||||||
|
|
||||||
|
|
||||||
my $last_contact = gettimeofday();
|
my $last_contact = gettimeofday();
|
||||||
|
|
||||||
@ -570,7 +538,6 @@ sub doQuery($$$)
|
|||||||
|
|
||||||
if(defined($client_socket))
|
if(defined($client_socket))
|
||||||
{
|
{
|
||||||
|
|
||||||
# send the given address to the presence daemon
|
# send the given address to the presence daemon
|
||||||
$client_socket->send($do_address."|".$values{absence_timeout}."\n");
|
$client_socket->send($do_address."|".$values{absence_timeout}."\n");
|
||||||
}
|
}
|
||||||
@ -578,10 +545,8 @@ sub doQuery($$$)
|
|||||||
{
|
{
|
||||||
$selector->remove($client_socket);
|
$selector->remove($client_socket);
|
||||||
$client_socket = undef;
|
$client_socket = undef;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# thread main loop
|
# thread main loop
|
||||||
THREADLOOP: while($run)
|
THREADLOOP: while($run)
|
||||||
{
|
{
|
||||||
@ -737,10 +702,7 @@ sub doQuery($$$)
|
|||||||
|
|
||||||
# set the previous state to the current state
|
# set the previous state to the current state
|
||||||
($previous_state, undef) = split(";", lc($return));
|
($previous_state, undef) = split(";", lc($return));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else # the socket is EOF which means the connection was closed
|
else # the socket is EOF which means the connection was closed
|
||||||
{
|
{
|
||||||
@ -750,14 +712,9 @@ sub doQuery($$$)
|
|||||||
shutdown($local_client, 2);
|
shutdown($local_client, 2);
|
||||||
close($local_client);
|
close($local_client);
|
||||||
$client_socket = undef;
|
$client_socket = undef;
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$log_queue->enqueue(threads->tid()."|exiting thread");
|
$log_queue->enqueue(threads->tid()."|exiting thread");
|
||||||
@ -776,7 +733,6 @@ my $value;
|
|||||||
|
|
||||||
my $errorcount = 0;
|
my $errorcount = 0;
|
||||||
|
|
||||||
|
|
||||||
Log 1, "reading configuration file";
|
Log 1, "reading configuration file";
|
||||||
|
|
||||||
%config = ();
|
%config = ();
|
||||||
@ -784,11 +740,11 @@ my $errorcount = 0;
|
|||||||
open (INI, "$ini") or (print STDERR timestamp()."Can't open $ini: $!\n" and exit(1));
|
open (INI, "$ini") or (print STDERR timestamp()."Can't open $ini: $!\n" and exit(1));
|
||||||
while (<INI>) {
|
while (<INI>) {
|
||||||
chomp;
|
chomp;
|
||||||
if (/^\s*?\[(\w+?)\]/) {
|
if (/^\s*?\[([^\]\n\r]+?)\]/) {
|
||||||
$section = $1;
|
$section = $1;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (/^\s*(\w+?)=(.+?)\s*(#.*)?$/) {
|
if (/^\s*(\w+?)=(.+?)\s*(#.*)?$/ and defined($section)) {
|
||||||
$keyword = $1;
|
$keyword = $1;
|
||||||
$value = $2 ;
|
$value = $2 ;
|
||||||
# put them into hash
|
# put them into hash
|
||||||
@ -817,8 +773,6 @@ my $errorcount = 0;
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(not exists($config{$room}{port}))
|
if(not exists($config{$room}{port}))
|
||||||
{
|
{
|
||||||
Log 0, "room >>$room<< has no value for >>port<< configured";
|
Log 0, "room >>$room<< has no value for >>port<< configured";
|
||||||
@ -885,7 +839,6 @@ my $errorcount = 0;
|
|||||||
|
|
||||||
|
|
||||||
sub aggregateRooms
|
sub aggregateRooms
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
my ($hash) = @_;
|
my ($hash) = @_;
|
||||||
@ -896,12 +849,13 @@ my @rooms;
|
|||||||
my $key;
|
my $key;
|
||||||
my $value;
|
my $value;
|
||||||
my $temp_name;
|
my $temp_name;
|
||||||
|
|
||||||
foreach $key (keys %$hash)
|
foreach $key (keys %$hash)
|
||||||
{
|
{
|
||||||
if($hash->{$key} ne "")
|
if($hash->{$key} ne "")
|
||||||
{
|
{
|
||||||
|
|
||||||
($value, $name) = split(";", $hash->{$key});
|
($value, $name) = split(";", $hash->{$key});
|
||||||
|
|
||||||
if($value eq "present")
|
if($value eq "present")
|
||||||
{
|
{
|
||||||
push @rooms, $key;
|
push @rooms, $key;
|
||||||
@ -914,18 +868,14 @@ foreach $key (keys %$hash)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(@rooms > 0)
|
if(@rooms > 0)
|
||||||
{
|
{
|
||||||
return "present;$temp_name;".join(",",sort @rooms);
|
return "present;$temp_name;".join(",",sort @rooms);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
return "absence";
|
return "absence";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -941,10 +891,8 @@ sub generateUUID
|
|||||||
return $uuid;
|
return $uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub timestamp
|
sub timestamp
|
||||||
{
|
{
|
||||||
|
|
||||||
return POSIX::strftime("%Y-%m-%d %H:%M:%S",localtime);
|
return POSIX::strftime("%Y-%m-%d %H:%M:%S",localtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -958,6 +906,7 @@ sub Log($$)
|
|||||||
{
|
{
|
||||||
($thread, $message) = split("\\|", $message);
|
($thread, $message) = split("\\|", $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($loglevel <= $opt_v)
|
if($loglevel <= $opt_v)
|
||||||
{
|
{
|
||||||
if($opt_l)
|
if($opt_l)
|
||||||
@ -973,6 +922,5 @@ sub Log($$)
|
|||||||
|
|
||||||
close(LOGFILE);
|
close(LOGFILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user