Improved robustness of config file reading.

This commit is contained in:
Magnus Juntti 2008-03-02 08:02:28 +00:00
parent 0b6db9eb1b
commit 37ba12675b
2 changed files with 16 additions and 9 deletions

View file

@ -324,6 +324,7 @@ sub is_time_format_correct
{
my $time = $_[0];
(my $hour, my $minute) = $time =~ /^-?(\d\d)\:(\d\d)$/;
if ($hour && $minute) {
if ($hour >= 0 && $hour <= 23 && $minute >= 0 && $minute <= 59) {
return $YES;
@ -363,7 +364,9 @@ sub read_config
open(CFG_FILE, "<$input_file") or die "$PROGRAM_NAME: Could not access config file: $conf_file\n";
my $i = 0;
my $conf_row = 0;
while ($_ = <CFG_FILE>) {
$conf_row++;
if ($_ =~ /^[\#|\s|\n]/) {
# skip lines beginning with # or space or just a new line
next;
@ -375,7 +378,7 @@ sub read_config
@inrad = split /\s+/, $_;
$device_cfg[$i][0] = $inrad[0]; # Protocol
$device_cfg[$i][1] = $inrad[1]; # Housecode A and channel for NEXA, channel for S
$device_cfg[$i][1] = $inrad[1]; # Housecode A and channel for NEXA, channel for SARTANO.
$device_cfg[$i][10] = $inrad[2]; # Rule valid days [1234567]
$device_cfg[$i][22] = 1; # Initial state set to on (1) so that they will be switched off at startup
@ -385,8 +388,12 @@ sub read_config
}
elsif ($device_cfg[$i][0] eq "NEXA") {
# Put the housecode and channel in the respective fields.
($device_cfg[$i][2]) = $device_cfg[$i][1] =~ /^[A-P](.{1})$/;
($device_cfg[$i][1]) = $device_cfg[$i][1] =~ /^(.{1})[1-3]$/;
($device_cfg[$i][2]) = $device_cfg[$i][1] =~ /^[A-P](\d)$/;
($device_cfg[$i][1]) = $device_cfg[$i][1] =~ /^([A-P])[1-3]$/;
if (!$device_cfg[$i][2] || !$device_cfg[$i][1]) {
die("$PROGRAM_NAME: Housecode/channel out of range error on row $conf_row in configuration file.\n");
}
}
else {
die("$PROGRAM_NAME: Unsupported protcol: $device_cfg[$i][0]. Only NEXA or SARTANO allowed.\n");
@ -398,10 +405,10 @@ sub read_config
# Lets find the on interval and put it into the proper fields, $device_cfg[$i][3]=On time, $device_cfg[$i][8] = On time random interval
if ($inrad[3] =~ /-/) {
($device_cfg[$i][3], $on_interval_end) = $inrad[3] =~ /^(.*)-(.*)$/;
($device_cfg[$i][3], $on_interval_end) = $inrad[3] =~ /^(\d?\d:\d\d)-(\d?\d:\d\d)$/;
}
else {
($device_cfg[$i][3]) = $inrad[3] =~ /^(.*)$/;
($device_cfg[$i][3]) = $inrad[3] =~ /^(\d?\d:\d\d)$/;
$on_interval_end = $device_cfg[$i][3];
}
@ -411,10 +418,10 @@ sub read_config
# Off time = $device_cfg[$i][4], Off time random interval = $device_cfg[$i][9]
if ($inrad[4] =~ /-/) {
($device_cfg[$i][4], $off_interval_end) = $inrad[4] =~ /^(.*)-(.*)$/;
($device_cfg[$i][4], $off_interval_end) = $inrad[4] =~ /^(\d?\d:\d\d)-(\d?\d:\d\d)$/;
}
else {
($device_cfg[$i][4]) = $inrad[4] =~ /^(.*)$/;
($device_cfg[$i][4]) = $inrad[4] =~ /^(\d?\d:\d\d)$/;
$off_interval_end = $device_cfg[$i][4];
}

View file

@ -31,9 +31,9 @@ pid_file = /var/run/tellstickd.pid
#
# Setting <on time> and <off time> to the same means that the device will always be switched on.
#
# The sample rule below will turn on the device 06:10 (no random) and off between 22:00 and 23:00 monday to friday.
# The sample rule below will turn on the device between 06:10 and 06:20 and off between 22:00 and 23:00 monday to friday.
# The device will be turned off daytime 10 minutes after sunrise and 15 before sunset
SARTANO 0000000000 12345 06:10-06:10 22:00-23:00 10 15
SARTANO 0000000000 12345 06:10-06:20 22:00-23:00 10 15
# This device will be turned on between 07:34 and 07:48, turned off between 23:00 and 23:15 (applies for every day in the week).
# It will not be turned off daytime