cleanup of print statements

This commit is contained in:
Rickard Andersson 2010-05-03 15:30:06 +00:00
parent fbcb2c8ff5
commit 65748500c5

View file

@ -163,7 +163,7 @@ sub usage() {
} }
sub println($) { sub printLine($) {
my ($text) = @_; my ($text) = @_;
print "$text\n"; print "$text\n";
@ -256,18 +256,14 @@ sub get_info_from_program() {
my $prog = $cfg_set{"program"}; my $prog = $cfg_set{"program"};
my $command = "$prog --list"; my $command = "$prog --list";
my $text = "Executing command: '$command'"; my $text = "Executing command: '$command'";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
my @result = qx($command); my @result = qx($command);
$text = "Command result: '@result'";
#println $text if ($option{"verbose"});
foreach my $line (@result) { foreach my $line (@result) {
chomp($line); chomp($line);
$line =~ s/\s+/ /g; $line =~ s/\s+/ /g;
if ($line =~ /^\d+/i) { if ($line =~ /^\d+/i) {
#println $line if ($option{"verbose"});
my ($id, $name, $state) = split(/\s+/, $line); my ($id, $name, $state) = split(/\s+/, $line);
if (defined($name)) { if (defined($name)) {
$cfg_alias{lc($name)} = 'off'; $cfg_alias{lc($name)} = 'off';
@ -288,7 +284,7 @@ sub read_config($) {
my ($infile) = @_; my ($infile) = @_;
my $text = "> Reading configurationfile started"; my $text = "> Reading configurationfile started";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
open(my $indata, $infile) or die "Couldn't read configfile '$infile'!"; open(my $indata, $infile) or die "Couldn't read configfile '$infile'!";
@ -298,19 +294,18 @@ sub read_config($) {
$line =~ s/\s+/ /g; $line =~ s/\s+/ /g;
if ($line =~ /^Set\s+/i) { if ($line =~ /^Set\s+/i) {
#println $line if ($option{"verbose"});
my (undef, $key, $val, $suffix) = split(/\s+/, $line); my (undef, $key, $val, $suffix) = split(/\s+/, $line);
if (defined($suffix)) { if (defined($suffix)) {
println "Wrong argument '$suffix' in line '$line'!"; printLine "Wrong argument '$suffix' in line '$line'!";
} else { } else {
if (defined($key)) { if (defined($key)) {
if (defined($val)) { if (defined($val)) {
$cfg_set{lc($key)} = $val; $cfg_set{lc($key)} = $val;
} else { } else {
println "Wrong value in line '$line'!"; printLine "Wrong value in line '$line'!";
} }
} else { } else {
println "Wrong key in line '$line'!"; printLine "Wrong key in line '$line'!";
} }
} }
next; next;
@ -328,7 +323,6 @@ sub read_config($) {
$line =~ s/\s+/ /g; $line =~ s/\s+/ /g;
if ($line =~ /^Group\s+/i) { if ($line =~ /^Group\s+/i) {
#println $line if ($option{"verbose"});
my (undef, $name, $id, $delay, $aliases) = split(/\s+/, $line, 5); my (undef, $name, $id, $delay, $aliases) = split(/\s+/, $line, 5);
$name = lc($name); $name = lc($name);
$delay = lc($delay); $delay = lc($delay);
@ -342,7 +336,7 @@ sub read_config($) {
my @aliaslist = split(/\s+/, $aliases); my @aliaslist = split(/\s+/, $aliases);
foreach my $alias (@aliaslist) { foreach my $alias (@aliaslist) {
if (! exists($cfg_alias{$alias})) { if (! exists($cfg_alias{$alias})) {
println "Wrong alias '$alias' in line '$line'!"; printLine "Wrong alias '$alias' in line '$line'!";
$error += 1; $error += 1;
} }
} }
@ -350,29 +344,28 @@ sub read_config($) {
$cfg_group{$name} = "$delay $aliases"; $cfg_group{$name} = "$delay $aliases";
} }
} else { } else {
println "Wrong delay time '$delay' in line '$line'!"; printLine "Wrong delay time '$delay' in line '$line'!";
} }
next; next;
} }
if ($line =~ /^Rule\s+/i) { if ($line =~ /^Rule\s+/i) {
#println $line if ($option{"verbose"});
my (undef, $alias, $on, $off, $suffix) = split(/\s+/, $line); my (undef, $alias, $on, $off, $suffix) = split(/\s+/, $line);
if (defined($suffix)) { if (defined($suffix)) {
println "Wrong argument '$suffix' in line '$line'!"; printLine "Wrong argument '$suffix' in line '$line'!";
} else { } else {
$alias = lc($alias); $alias = lc($alias);
$on = lc($on); $on = lc($on);
$off = lc($off); $off = lc($off);
if ($on eq "") { if ($on eq "") {
println "Wrong on time '$on' in line '$line'!"; printLine "Wrong on time '$on' in line '$line'!";
} elsif ($off eq "") { } elsif ($off eq "") {
println "Wrong off time '$off' in line '$line'!"; printLine "Wrong off time '$off' in line '$line'!";
} else { } else {
if ($on !~ /[\w\/\+\-\:\$\(\)]+/) { if ($on !~ /[\w\/\+\-\:\$\(\)]+/) {
println "Wrong on time '$on' in line '$line'!"; printLine "Wrong on time '$on' in line '$line'!";
} elsif ($off !~ /[\w\/\+\-\:\$\(\)]+/) { } elsif ($off !~ /[\w\/\+\-\:\$\(\)]+/) {
println "Wrong off time '$off' in line '$line'!"; printLine "Wrong off time '$off' in line '$line'!";
} }
} }
if (exists($cfg_alias{$alias})) { if (exists($cfg_alias{$alias})) {
@ -380,7 +373,7 @@ sub read_config($) {
} elsif (exists($cfg_group{$alias})) { } elsif (exists($cfg_group{$alias})) {
push @cfg_rule, [$alias, $on, $off]; push @cfg_rule, [$alias, $on, $off];
} else { } else {
println "Wrong alias '$alias' in line '$line'!"; printLine "Wrong alias '$alias' in line '$line'!";
} }
} }
next; next;
@ -390,23 +383,23 @@ sub read_config($) {
close $indata; close $indata;
$text = "=== Set ==="; $text = "=== Set ===";
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
foreach my $key (sort keys %cfg_set) { foreach my $key (sort keys %cfg_set) {
$text = "$key = $cfg_set{$key}"; $text = "$key = $cfg_set{$key}";
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
} }
$text = "=== Alias ==="; $text = "=== Alias ===";
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
foreach my $key (sort keys %cfg_alias) { foreach my $key (sort keys %cfg_alias) {
$text = "$key($cfg_aliasId{$key}) = $cfg_alias{$key}"; $text = "$key($cfg_aliasId{$key}) = $cfg_alias{$key}";
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
} }
$text = "=== Group ==="; $text = "=== Group ===";
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
foreach my $key (sort keys %cfg_group) { foreach my $key (sort keys %cfg_group) {
my $val = $cfg_group{$key}; my $val = $cfg_group{$key};
@ -414,11 +407,11 @@ sub read_config($) {
$text = "$key($cfg_groupId{$key}) ="; $text = "$key($cfg_groupId{$key}) =";
$text .= " delay time $delay seconds"; $text .= " delay time $delay seconds";
$text .= " between aliases ($aliases)"; $text .= " between aliases ($aliases)";
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
} }
$text = "=== Rule ==="; $text = "=== Rule ===";
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
foreach my $rule (@cfg_rule) { foreach my $rule (@cfg_rule) {
my ($alias, $on, $off) = @$rule; my ($alias, $on, $off) = @$rule;
@ -426,12 +419,12 @@ sub read_config($) {
$text .= " on at $on" if ($on !~ /no/); $text .= " on at $on" if ($on !~ /no/);
$text .= " and" if (($on !~ /no/) && ($off !~ /no/)); $text .= " and" if (($on !~ /no/) && ($off !~ /no/));
$text .= " off at $off" if ($off !~ /no/); $text .= " off at $off" if ($off !~ /no/);
println $text if ($option{"check"}); printLine $text if ($option{"check"});
printlogger $text; printlogger $text;
} }
$text = "> Reading configurationfile finished"; $text = "> Reading configurationfile finished";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
} }
@ -441,7 +434,7 @@ sub call_program($$) {
if ($option{"test"}) { if ($option{"test"}) {
my $text = "Test mode, no real device will be used"; my $text = "Test mode, no real device will be used";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
} else { } else {
my $mode = "--off"; my $mode = "--off";
@ -455,7 +448,7 @@ sub call_program($$) {
my $prog = $cfg_set{"program"}; my $prog = $cfg_set{"program"};
my $command = "$prog $mode $device"; my $command = "$prog $mode $device";
my $text = "Executing command: '$command'"; my $text = "Executing command: '$command'";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
system($command); system($command);
} }
} }
@ -463,25 +456,25 @@ sub call_program($$) {
sub load_device_rules () { sub load_device_rules () {
my $text = "> Loading device rules started"; my $text = "> Loading device rules started";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
my $now = get_datetime_now(); my $now = get_datetime_now();
$text = "Time = $now"; $text = "Time = $now";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
my $today = get_datetime_now(); my $today = get_datetime_now();
$today->set( hour => 0, minute => 0, second => 0 ); $today->set( hour => 0, minute => 0, second => 0 );
$text = "Today = $today"; $text = "Today = $today";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
my $sunrise = get_datetime_sunrise($today); my $sunrise = get_datetime_sunrise($today);
$text = "Sunrise = $sunrise"; $text = "Sunrise = $sunrise";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
my $sunset = get_datetime_sunset($today); my $sunset = get_datetime_sunset($today);
$text = "Sunset = $sunset"; $text = "Sunset = $sunset";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
foreach my $rule (@cfg_rule) { foreach my $rule (@cfg_rule) {
@ -536,17 +529,17 @@ sub load_device_rules () {
@device = sort { $a->[0] cmp $b->[0] } @device; @device = sort { $a->[0] cmp $b->[0] } @device;
$text = "=== Device ==="; $text = "=== Device ===";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
foreach my $rule (@device) { foreach my $rule (@device) {
my ($time, $alias, $value) = @$rule; my ($time, $alias, $value) = @$rule;
$text = "$alias = $value at $time"; $text = "$alias = $value at $time";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
} }
$text = "> Loading device rules finished"; $text = "> Loading device rules finished";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
} }
@ -554,8 +547,6 @@ sub load_device_rules () {
sub get_rule_datetime($$$$) { sub get_rule_datetime($$$$) {
my ($rule, $now, $sunrise, $sunset) = @_; my ($rule, $now, $sunrise, $sunset) = @_;
#println "Rule='$rule'";
my ($date, $time) = split(/\//, $rule); my ($date, $time) = split(/\//, $rule);
if (not defined($time)) { if (not defined($time)) {
$time = $date; $time = $date;
@ -613,7 +604,7 @@ sub get_rule_datetime($$$$) {
$time = undef; $time = undef;
} }
} else { } else {
println "Wrong date '$date' for rule '$rule'!"; printLine "Wrong date '$date' for rule '$rule'!";
$time = undef; $time = undef;
} }
} }
@ -627,7 +618,6 @@ sub get_rule_datetime($$$$) {
my $op = ""; my $op = "";
my $rest = ""; my $rest = "";
my $mins = 0; my $mins = 0;
#println "rule='$time'";
while ($time ne "") { while ($time ne "") {
$op = $lastOp; $op = $lastOp;
@ -660,18 +650,16 @@ sub get_rule_datetime($$$$) {
} elsif ($expr =~ /^(\d\d):(\d\d)$/) { } elsif ($expr =~ /^(\d\d):(\d\d)$/) {
$expr = $1*60+$2; $expr = $1*60+$2;
} else { } else {
println "Wrong time '$expr' for rule '$rule'!"; printLine "Wrong time '$expr' for rule '$rule'!";
return; return;
$expr = ""; $expr = "";
} }
#println "expr='$expr', op='$op', rest='$rest'";
if ($op eq "+") { if ($op eq "+") {
$mins += $expr; $mins += $expr;
} elsif ($op eq "-") { } elsif ($op eq "-") {
$mins -= $expr; $mins -= $expr;
} }
#println "mins='$mins'";
} }
if ($mins <= 0) { if ($mins <= 0) {
$mins = $mins % (24*60); $mins = $mins % (24*60);
@ -683,12 +671,10 @@ sub get_rule_datetime($$$$) {
$hours = int $mins/60; $hours = int $mins/60;
$minutes = int $mins%60; $minutes = int $mins%60;
# UTC ? # Hopefully we will deal with UTC and daylightsavingtime in a decent fashion?
$time = get_datetime_now(); $time = get_datetime_now();
$time->set(hour => 0, minute => 0, second => 0); $time->set(hour => 0, minute => 0, second => 0);
$time->add(hours => $hours+24*$days, minutes => $minutes, seconds => 0); $time->add(hours => $hours+24*$days, minutes => $minutes, seconds => 0);
#println "days='$days', hours='$hours', minutes='$minutes'";
#println "time='$time'";
} else { } else {
$time = undef; $time = undef;
} }
@ -710,28 +696,28 @@ sub load_dimmer_rules($$$$$$$$){
$offset = ''; $offset = '';
} }
if ($offset !~ /^\d\d:\d\d$/) { if ($offset !~ /^\d\d:\d\d$/) {
println "Wrong dimmer time offset '$offset' for rule '$expr'!"; printLine "Wrong dimmer time offset '$offset' for rule '$expr'!";
next; next;
} }
if (!defined($start)) { if (!defined($start)) {
$start = ''; $start = '';
} }
if (($start !~ /^(\d+)$/) || (($start < 0) || ($start > 255))) { if (($start !~ /^(\d+)$/) || (($start < 0) || ($start > 255))) {
println "Wrong dimmer start level '$start' for rule '$expr'!"; printLine "Wrong dimmer start level '$start' for rule '$expr'!";
next; next;
} }
if (!defined($stop)) { if (!defined($stop)) {
$stop = ''; $stop = '';
} }
if (($stop !~ /^(\d+)$/) || (($stop < 0) || ($stop > 255))) { if (($stop !~ /^(\d+)$/) || (($stop < 0) || ($stop > 255))) {
println "Wrong dimmer stop level '$stop' for rule '$expr'!"; printLine "Wrong dimmer stop level '$stop' for rule '$expr'!";
next; next;
} }
if (!defined($step)) { if (!defined($step)) {
$step = ''; $step = '';
} }
if (($step !~ /^(\d+)$/) || (($step < 1) || ($step > 255))) { if (($step !~ /^(\d+)$/) || (($step < 1) || ($step > 255))) {
println "Wrong dimmer step level '$step' for rule '$expr'!"; printLine "Wrong dimmer step level '$step' for rule '$expr'!";
next; next;
} }
@ -771,7 +757,7 @@ sub load_dimmer_rules($$$$$$$$){
sub check_device_rules($) { sub check_device_rules($) {
my ($now) = @_; my ($now) = @_;
println "Checking device rules '$now'" if ($option{verbose}); printLine "Checking device rules '$now'" if ($option{verbose});
my $rule = $device[0]; my $rule = $device[0];
while (defined(@$rule)) { while (defined(@$rule)) {
@ -792,7 +778,7 @@ sub change_device_state($$) {
call_program($device, $state); call_program($device, $state);
my $text = "Device $device = $state"; my $text = "Device $device = $state";
println $text if ($option{"verbose"}); printLine $text if ($option{"verbose"});
printlogger $text; printlogger $text;
} }
@ -810,8 +796,8 @@ sub get_device_state() {
} }
if ($cfg_alias{$device}) { if ($cfg_alias{$device}) {
my $state = $cfg_alias{$device}; my $state = $cfg_alias{$device};
println "$state" if (not $option{"verbose"}); printLine "$state" if (not $option{"verbose"});
println "Device $device = $state" if ($option{"verbose"}); printLine "Device $device = $state" if ($option{"verbose"});
return; return;
} }
if (defined($cfg_idGroup{$device})) { if (defined($cfg_idGroup{$device})) {
@ -825,10 +811,10 @@ sub get_device_state() {
foreach my $alias (@aliaslist) { foreach my $alias (@aliaslist) {
$states .= " $cfg_alias{$alias}"; $states .= " $cfg_alias{$alias}";
} }
println "Group $device =$states"; printLine "Group $device =$states";
return; return;
} }
println "No alias or group found with name/id '$device'!"; printLine "No alias or group found with name/id '$device'!";
} }
} }
@ -840,11 +826,11 @@ sub set_device_state() {
if (defined($option{"state"})) { if (defined($option{"state"})) {
my $state = $option{"state"}; my $state = $option{"state"};
if ($state !~ /^(on|off|\d+)$/i) { if ($state !~ /^(on|off|\d+)$/i) {
println "No state found with name '$state'!"; printLine "No state found with name '$state'!";
return; return;
} }
if (($state =~ /^(\d+)$/i) && (($state < 0) || ($state > 255))) { if (($state =~ /^(\d+)$/i) && (($state < 0) || ($state > 255))) {
println "State needs to be an integer between 0 and 255 for dimmers!"; printLine "State needs to be an integer between 0 and 255 for dimmers!";
return; return;
} }
my $device = $option{"device"}; my $device = $option{"device"};
@ -868,7 +854,7 @@ sub set_device_state() {
} }
return; return;
} }
println "No alias or group found with name '$device'!"; printLine "No alias or group found with name '$device'!";
} }
} }
} }
@ -913,7 +899,7 @@ sub swap_first_device_state() {
} }
return; return;
} }
println "No alias or group found with name '$device'!"; printLine "No alias or group found with name '$device'!";
} }
} }
} }
@ -957,7 +943,7 @@ sub swap_device_state() {
} }
return; return;
} }
println "No alias or group found with name '$device'!"; printLine "No alias or group found with name '$device'!";
} }
} }
} }
@ -965,7 +951,7 @@ sub swap_device_state() {
sub list_all_devices() { sub list_all_devices() {
foreach my $key (sort keys %cfg_alias) { foreach my $key (sort keys %cfg_alias) {
println "Device $key($cfg_aliasId{$key}) = $cfg_alias{$key}"; printLine "Device $key($cfg_aliasId{$key}) = $cfg_alias{$key}";
} }
foreach my $key (sort keys %cfg_group) { foreach my $key (sort keys %cfg_group) {
my $val = $cfg_group{$key}; my $val = $cfg_group{$key};
@ -975,19 +961,19 @@ sub list_all_devices() {
foreach my $device (@aliaslist) { foreach my $device (@aliaslist) {
$states .= " $cfg_alias{$device}"; $states .= " $cfg_alias{$device}";
} }
println "Group $key($cfg_groupId{$key}) =$states"; printLine "Group $key($cfg_groupId{$key}) =$states";
} }
} }
sub list_all_aliases { sub list_all_aliases {
foreach my $key (sort keys %cfg_alias) { foreach my $key (sort keys %cfg_alias) {
println "Alias $key($cfg_aliasId{$key}) = receiver"; printLine "Alias $key($cfg_aliasId{$key}) = receiver";
} }
foreach my $key (sort keys %cfg_group) { foreach my $key (sort keys %cfg_group) {
my $val = $cfg_group{$key}; my $val = $cfg_group{$key};
my ($delay, $aliases) = split(/\s+/, $val, 2); my ($delay, $aliases) = split(/\s+/, $val, 2);
println "Group $key($cfg_groupId{$key}) = delay $delay seconds, aliases ($aliases)"; printLine "Group $key($cfg_groupId{$key}) = delay $delay seconds, aliases ($aliases)";
} }
} }
@ -1000,7 +986,7 @@ sub daemon_loop() {
my $next_day = get_datetime_now(); my $next_day = get_datetime_now();
$next_day->add( hours => 24 ); $next_day->add( hours => 24 );
$next_day->set( hour => 0, minute => 0, second => 0 ); $next_day->set( hour => 0, minute => 0, second => 0 );
println "Next reload of device rules = $next_day" if ($option{"verbose"}); printLine "Next reload of device rules = $next_day" if ($option{"verbose"});
my $wait_time = 5; my $wait_time = 5;
my $loop = 1; my $loop = 1;