Possible to make different week rules for odd and even weeks.
Changed the is_rule_valid_today to check for odd and even weeks and to use the DateTime object which numbers the days 1 (monday) - 7 (sunday) like the config file.
This commit is contained in:
parent
e6e6eaaaf3
commit
145d86b8d6
2 changed files with 27 additions and 9 deletions
|
@ -334,18 +334,23 @@ sub is_time_format_correct
|
||||||
return $NO;
|
return $NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Time format sanity check routine
|
# Time format sanity check routine
|
||||||
sub is_rule_valid_today
|
sub is_rule_valid_today
|
||||||
{
|
{
|
||||||
my $device_id = $_[0];
|
my $rule = $_[0];
|
||||||
my $current_day = $_[1];
|
my $now = $_[1];
|
||||||
|
|
||||||
if (0 == $current_day) {
|
(my $week_rule) = $rule =~/^([e|o])/;
|
||||||
# Perl thinks 0 = Sunday, config file thinks 7 = Sunday
|
if ($week_rule) {
|
||||||
$current_day = 7;
|
my $is_odd_week = ($now->week_number % 2);
|
||||||
|
if (($is_odd_week && ($week_rule eq "e")) ||
|
||||||
|
(!$is_odd_week && ($week_rule eq "o"))) {
|
||||||
|
return $NO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
my $day_of_week = $now->day_of_week;
|
||||||
if ($device_cfg[$device_id][10] =~ /$current_day/) {
|
if ($rule =~ /$day_of_week/) {
|
||||||
return $YES;
|
return $YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -514,7 +519,7 @@ sub read_config
|
||||||
die("$PROGRAM_NAME: Format of off interval for device $device_cfg[$i][0] $device_cfg[$i][1]$device_cfg[$i][2] not correct: $device_cfg[$i][9]\n");
|
die("$PROGRAM_NAME: Format of off interval for device $device_cfg[$i][0] $device_cfg[$i][1]$device_cfg[$i][2] not correct: $device_cfg[$i][9]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($device_cfg[$i][10] =~ /[^1-7]/) {
|
if ($device_cfg[$i][10] =~ /^[o|e][^1-7]/) {
|
||||||
die("$PROGRAM_NAME: Device $device_cfg[$i][0] $device_cfg[$i][1]$device_cfg[$i][2]: Specified rule validity day out of range: $device_cfg[$i][10]\n");
|
die("$PROGRAM_NAME: Device $device_cfg[$i][0] $device_cfg[$i][1]$device_cfg[$i][2]: Specified rule validity day out of range: $device_cfg[$i][10]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -528,6 +533,12 @@ sub read_config
|
||||||
printf(" $weekDays[$j]");
|
printf(" $weekDays[$j]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ($device_cfg[$i][10] =~ /o/) {
|
||||||
|
printf(" (odd weeks)");
|
||||||
|
}
|
||||||
|
if ($device_cfg[$i][10] =~ /e/) {
|
||||||
|
printf(" (even weeks)");
|
||||||
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
if ($device_cfg[$i][3] == -1) {
|
if ($device_cfg[$i][3] == -1) {
|
||||||
|
@ -703,6 +714,7 @@ else {
|
||||||
###################################################################################################
|
###################################################################################################
|
||||||
# Now to the eternal loop
|
# Now to the eternal loop
|
||||||
my $first_loop = $YES;
|
my $first_loop = $YES;
|
||||||
|
my $now;
|
||||||
while (1) {
|
while (1) {
|
||||||
$n_rfcmd_calls = 0;
|
$n_rfcmd_calls = 0;
|
||||||
|
|
||||||
|
@ -710,6 +722,7 @@ while (1) {
|
||||||
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime();
|
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime();
|
||||||
$Year += 1900;
|
$Year += 1900;
|
||||||
$current_time = sprintf("%02d:%02d", $Hour, $Minute);
|
$current_time = sprintf("%02d:%02d", $Hour, $Minute);
|
||||||
|
$now = DateTime->now();
|
||||||
|
|
||||||
###################################################################################################
|
###################################################################################################
|
||||||
|
|
||||||
|
@ -760,7 +773,7 @@ while (1) {
|
||||||
|
|
||||||
|
|
||||||
# Lets check if the rule is to be applied today
|
# Lets check if the rule is to be applied today
|
||||||
if (is_rule_valid_today($i, $WeekDay) == $YES) {
|
if (is_rule_valid_today($device_cfg[$i][10], $now) == $YES) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,9 @@ pid_file = /var/run/tellstickd.pid
|
||||||
#<protocol> <housecode/channel> <valid day(s)> <on time - end random on time> <off time - end random off time> <off after sunrise in minutes> <on before sunset in minutes>
|
#<protocol> <housecode/channel> <valid day(s)> <on time - end random on time> <off time - end random off time> <off after sunrise in minutes> <on before sunset in minutes>
|
||||||
#
|
#
|
||||||
# In valid day field, days are numbered 1=Monday, 2=Tuesday, ... , 7=Sunday. I.e. 1234567 means that the rule should be active every weekday.
|
# In valid day field, days are numbered 1=Monday, 2=Tuesday, ... , 7=Sunday. I.e. 1234567 means that the rule should be active every weekday.
|
||||||
|
# The day fields can be prefixed with "o" or "e" making the rule valid on odd or even weeks. Week numbers are
|
||||||
|
# calculated according to ISO, with the first week of the year being the one containing 4 jan
|
||||||
|
|
||||||
# Time format xx:yy where xx is hour and yy is minute
|
# Time format xx:yy where xx is hour and yy is minute
|
||||||
# Multiple definitions are possible for each device
|
# Multiple definitions are possible for each device
|
||||||
#
|
#
|
||||||
|
@ -42,6 +45,8 @@ NEXA P2 1234567 07:34-07:48 23:00-23:15
|
||||||
# This device will be turned on 07:00 off between 20:00 and 20:30 and will be on daytime on sundays.
|
# This device will be turned on 07:00 off between 20:00 and 20:30 and will be on daytime on sundays.
|
||||||
NEXA P3 7 07:00 20:00-20:30
|
NEXA P3 7 07:00 20:00-20:30
|
||||||
|
|
||||||
|
# This device will be turned on 07:00 and off 20:00 on fridays and sundays on even numbered weeks
|
||||||
|
NEXA P3 e57 07:00 20:00
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue