98_WeekdayTimer: use global language settings + code cleanup and rearranging

git-svn-id: https://svn.fhem.de/fhem/trunk@20698 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
Beta-User 2019-12-09 19:07:23 +00:00
parent 23587f87d7
commit e76201c0d9

View File

@ -49,56 +49,95 @@ sub WeekdayTimer_Initialize($){
$readingFnAttributes; $readingFnAttributes;
} }
################################################################################ ################################################################################
sub WeekdayTimer_GetHashIndirekt ($$) { sub WeekdayTimer_Define($$) {
my ($myHash, $function) = @_; my ($hash, $def) = @_;
WeekdayTimer_InitHelper($hash);
my @a = split("[ \t\\\n]+", $def);
return "Usage: define <name> $hash->{TYPE} <device> <language> <switching times> <condition|command>"
if(@a < 4);
#fuer den modify Altlasten bereinigen
delete($hash->{helper});
my $name = shift @a;
my $type = shift @a;
my $device = shift @a;
WeekdayTimer_DeleteTimer($hash);
my $delVariables = "(CONDITION|COMMAND|profile|Profil)";
map { delete $hash->{$_} if($_=~ m/^$delVariables.*/g) } keys %{$hash};
$hash->{NAME} = $name;
$hash->{DEVICE} = $device;
InternalTimer(time(), "WeekdayTimer_Start",$hash,0);
if (!defined($myHash->{HASH})) {
Log 3, "[$function] myHash not valid";
return undef; return undef;
};
return $myHash->{HASH};
} }
################################################################################ ################################################################################
sub WeekdayTimer_InternalTimer($$$$$) { sub WeekdayTimer_Undef($$) {
my ($modifier, $tim, $callback, $hash, $waitIfInitNotDone) = @_; my ($hash, $arg) = @_;
my $timerName = "$hash->{NAME}_$modifier"; foreach my $idx (keys %{$hash->{profil}}) {
my $mHash = { HASH=>$hash, NAME=>"$hash->{NAME}_$modifier", MODIFIER=>$modifier}; WeekdayTimer_RemoveInternalTimer($idx, $hash);
if (defined($hash->{TIMER}{$timerName})) {
Log3 $hash, 1, "[$hash->{NAME}] possible overwriting of timer $timerName - please delete first";
stacktrace();
} else {
$hash->{TIMER}{$timerName} = $mHash;
} }
WeekdayTimer_RemoveInternalTimer("SetTimerOfDay", $hash);
Log3 $hash, 5, "[$hash->{NAME}] setting Timer: $timerName " . FmtDateTime($tim); delete $modules{$hash->{TYPE}}{defptr}{$hash->{NAME}};
InternalTimer($tim, $callback, $mHash, $waitIfInitNotDone); return undef;
return $mHash;
} }
################################################################################ ################################################################################
sub WeekdayTimer_RemoveInternalTimer($$) { sub WeekdayTimer_Start($) {
my ($modifier, $hash) = @_;
my $timerName = "$hash->{NAME}_$modifier";
my $myHash = $hash->{TIMER}{$timerName};
if (defined($myHash)) {
delete $hash->{TIMER}{$timerName};
Log3 $hash, 5, "[$hash->{NAME}] removing Timer: $timerName";
RemoveInternalTimer($myHash);
}
}
################################################################################
sub WeekdayTimer_InitHelper($) {
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{NAME};
my @a = split("[ \t\\\n]+", $hash->{DEF});
my $device = shift @a;
$hash->{longDays} = { "de" => ["Sonntag", "Montag","Dienstag","Mittwoch", "Donnerstag","Freitag", "Samstag", "Wochenende", "Werktags" ], my $language = WeekdayTimer_Language ($hash, \@a);
"en" => ["Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "weekend", "weekdays" ],
"fr" => ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi","Samedi", "weekend", "jours de la semaine"], my $idx = 0;
"nl" => ["Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "weekend", "werkdagen"]}; $hash->{dayNumber} = {map {$_ => $idx++} @{$hash->{shortDays}{$language}}};
$hash->{shortDays} = { "de" => ["so","mo","di","mi","do","fr","sa",'$we','!$we'], $hash->{helper}{daysRegExp} = '(' . join ("|", @{$hash->{shortDays}{$language}}) . ")";
"en" => ["su","mo","tu","we","th","fr","sa",'$we','!$we'], $hash->{helper}{daysRegExpMessage} = $hash->{helper}{daysRegExp};
"fr" => ["di","lu","ma","me","je","ve","sa",'$we','!$we'],
"nl" => ["zo","ma","di","wo","do","vr","za",'$we','!$we']}; $hash->{helper}{daysRegExp} =~ s/\$/\\\$/g;
$hash->{helper}{daysRegExp} =~ s/\!/\\\!/g;
WeekdayTimer_GlobalDaylistSpec ($hash, \@a);
my @switchingtimes = WeekdayTimer_gatherSwitchingTimes ($hash, \@a);
my $conditionOrCommand = join (" ", @a);
# test if device is defined
Log3 ($hash, 3, "[$name] device <$device> in fhem not defined, but accepted") if(!$defs{$device});
# wenn keine switchintime angegeben ist, dann Fehler
Log3 ($hash, 3, "[$name] no valid Switchingtime found in <$conditionOrCommand>, check first parameter") if (@switchingtimes == 0);
$hash->{STILLDONETIME} = 0;
$hash->{SWITCHINGTIMES} = \@switchingtimes;
$attr{$name}{verbose} = 5 if (!defined $attr{$name}{verbose} && $name =~ m/^tst.*/ );
$defs{$device}{STILLDONETIME} = 0 if($defs{$device});
$modules{$hash->{TYPE}}{defptr}{$hash->{NAME}} = $hash;
$hash->{CONDITION} = ""; $hash->{COMMAND} = "";
if($conditionOrCommand =~ m/^\(.*\)$/g) { #condition (*)
$hash->{CONDITION} = $conditionOrCommand;
} elsif(length($conditionOrCommand) > 0 ) {
$hash->{COMMAND} = $conditionOrCommand;
}
WeekdayTimer_Profile ($hash);
delete $hash->{VERZOEGRUNG};
delete $hash->{VERZOEGRUNG_IDX};
$attr{$name}{commandTemplate} =
'set $NAME '. WeekdayTimer_isHeizung($hash) .' $EVENT' if (!defined $attr{$name}{commandTemplate});
WeekdayTimer_SetTimerOfDay({ HASH => $hash});
return undef;
} }
################################################################################ ################################################################################
sub WeekdayTimer_Set($@) { sub WeekdayTimer_Set($@) {
@ -160,97 +199,56 @@ sub WeekdayTimer_Get($@) {
return "$a[0] $reading => $value"; return "$a[0] $reading => $value";
} }
################################################################################ ################################################################################
sub WeekdayTimer_Undef($$) { sub WeekdayTimer_GetHashIndirekt ($$) {
my ($hash, $arg) = @_; my ($myHash, $function) = @_;
foreach my $idx (keys %{$hash->{profil}}) { if (!defined($myHash->{HASH})) {
WeekdayTimer_RemoveInternalTimer($idx, $hash); Log 3, "[$function] myHash not valid";
}
WeekdayTimer_RemoveInternalTimer("SetTimerOfDay", $hash);
delete $modules{$hash->{TYPE}}{defptr}{$hash->{NAME}};
return undef; return undef;
};
return $myHash->{HASH};
} }
################################################################################ ################################################################################
sub WeekdayTimer_Define($$) { sub WeekdayTimer_InternalTimer($$$$$) {
my ($hash, $def) = @_; my ($modifier, $tim, $callback, $hash, $waitIfInitNotDone) = @_;
WeekdayTimer_InitHelper($hash);
my @a = split("[ \t\\\n]+", $def);
return "Usage: define <name> $hash->{TYPE} <device> <language> <switching times> <condition|command>" my $timerName = "$hash->{NAME}_$modifier";
if(@a < 4); my $mHash = { HASH=>$hash, NAME=>"$hash->{NAME}_$modifier", MODIFIER=>$modifier};
if (defined($hash->{TIMER}{$timerName})) {
#fuer den modify Altlasten bereinigen Log3 $hash, 1, "[$hash->{NAME}] possible overwriting of timer $timerName - please delete first";
delete($hash->{helper}); stacktrace();
} else {
my $name = shift @a; $hash->{TIMER}{$timerName} = $mHash;
my $type = shift @a;
my $device = shift @a;
WeekdayTimer_DeleteTimer($hash);
my $delVariables = "(CONDITION|COMMAND|profile|Profil)";
map { delete $hash->{$_} if($_=~ m/^$delVariables.*/g) } keys %{$hash};
my $language = WeekdayTimer_Language ($hash, \@a);
$hash->{NAME} = $name;
$hash->{DEVICE} = $device;
InternalTimer(time(), "WeekdayTimer_Start",$hash,0);
return undef;
}
sub WeekdayTimer_Start($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my @a = split("[ \t\\\n]+", $hash->{DEF});
my $device = shift @a;
my $language = WeekdayTimer_Language ($hash, \@a);
my $idx = 0;
$hash->{dayNumber} = {map {$_ => $idx++} @{$hash->{shortDays}{$language}}};
$hash->{helper}{daysRegExp} = '(' . join ("|", @{$hash->{shortDays}{$language}}) . ")";
$hash->{helper}{daysRegExpMessage} = $hash->{helper}{daysRegExp};
$hash->{helper}{daysRegExp} =~ s/\$/\\\$/g;
$hash->{helper}{daysRegExp} =~ s/\!/\\\!/g;
WeekdayTimer_GlobalDaylistSpec ($hash, \@a);
my @switchingtimes = WeekdayTimer_gatherSwitchingTimes ($hash, \@a);
my $conditionOrCommand = join (" ", @a);
# test if device is defined
Log3 ($hash, 3, "[$name] device <$device> in fhem not defined, but accepted") if(!$defs{$device});
# wenn keine switchintime angegeben ist, dann Fehler
Log3 ($hash, 3, "[$name] no valid Switchingtime found in <$conditionOrCommand>, check first parameter") if (@switchingtimes == 0);
$hash->{STILLDONETIME} = 0;
$hash->{SWITCHINGTIMES} = \@switchingtimes;
$attr{$name}{verbose} = 5 if (!defined $attr{$name}{verbose} && $name =~ m/^tst.*/ );
$defs{$device}{STILLDONETIME} = 0 if($defs{$device});
$modules{$hash->{TYPE}}{defptr}{$hash->{NAME}} = $hash;
$hash->{CONDITION} = ""; $hash->{COMMAND} = "";
if($conditionOrCommand =~ m/^\(.*\)$/g) { #condition (*)
$hash->{CONDITION} = $conditionOrCommand;
} elsif(length($conditionOrCommand) > 0 ) {
$hash->{COMMAND} = $conditionOrCommand;
} }
WeekdayTimer_Profile ($hash); Log3 $hash, 5, "[$hash->{NAME}] setting Timer: $timerName " . FmtDateTime($tim);
delete $hash->{VERZOEGRUNG}; InternalTimer($tim, $callback, $mHash, $waitIfInitNotDone);
delete $hash->{VERZOEGRUNG_IDX}; return $mHash;
}
################################################################################
sub WeekdayTimer_RemoveInternalTimer($$) {
my ($modifier, $hash) = @_;
$attr{$name}{commandTemplate} = my $timerName = "$hash->{NAME}_$modifier";
'set $NAME '. WeekdayTimer_isHeizung($hash) .' $EVENT' if (!defined $attr{$name}{commandTemplate}); my $myHash = $hash->{TIMER}{$timerName};
if (defined($myHash)) {
delete $hash->{TIMER}{$timerName};
Log3 $hash, 5, "[$hash->{NAME}] removing Timer: $timerName";
RemoveInternalTimer($myHash);
}
}
################################################################################
sub WeekdayTimer_InitHelper($) {
my ($hash) = @_;
WeekdayTimer_SetTimerOfDay({ HASH => $hash}); $hash->{longDays} = { "de" => ["Sonntag", "Montag","Dienstag","Mittwoch", "Donnerstag","Freitag", "Samstag", "Wochenende", "Werktags" ],
"en" => ["Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "weekend", "weekdays" ],
return undef; "fr" => ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi","Samedi", "weekend", "jours de la semaine"],
"nl" => ["Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "weekend", "werkdagen"]};
$hash->{shortDays} = { "de" => ["so","mo","di","mi","do","fr","sa",'$we','!$we'],
"en" => ["su","mo","tu","we","th","fr","sa",'$we','!$we'],
"fr" => ["di","lu","ma","me","je","ve","sa",'$we','!$we'],
"nl" => ["zo","ma","di","wo","do","vr","za",'$we','!$we']};
} }
################################################################################ ################################################################################
sub WeekdayTimer_Profile($) { sub WeekdayTimer_Profile($) {
@ -282,8 +280,6 @@ sub WeekdayTimer_Profile($) {
$relativeDay = $relativeDay + 7 if $relativeDay < 0 ; $relativeDay = $relativeDay + 7 if $relativeDay < 0 ;
$dayOfEchteZeit = undef if ($hash->{helper}{WEDAYS}{$relativeDay} && $overrulewday); $dayOfEchteZeit = undef if ($hash->{helper}{WEDAYS}{$relativeDay} && $overrulewday);
} }
#####
$dayOfEchteZeit = ($wday>=1&&$wday<=5) ? 6 : $wday if ($day==7); # ggf. Samstag $wday ~~ [1..5] $dayOfEchteZeit = ($wday>=1&&$wday<=5) ? 6 : $wday if ($day==7); # ggf. Samstag $wday ~~ [1..5]
$dayOfEchteZeit = ($wday==0||$wday==6) ? 1 : $wday if ($day==8); # ggf. Montag $wday ~~ [0, 6] $dayOfEchteZeit = ($wday==0||$wday==6) ? 1 : $wday if ($day==8); # ggf. Montag $wday ~~ [0, 6]
if (defined $dayOfEchteZeit) { if (defined $dayOfEchteZeit) {
@ -385,7 +381,6 @@ sub WeekdayTimer_SwitchingTime($$) {
#Log3 $hash, 3, "Tage: " . Dumper \@tage; #Log3 $hash, 3, "Tage: " . Dumper \@tage;
return (\@tage,$time,$para,$overrulewday); return (\@tage,$time,$para,$overrulewday);
} }
################################################################################ ################################################################################
sub WeekdayTimer_daylistAsArray($$){ sub WeekdayTimer_daylistAsArray($$){
my ($hash, $daylist) = @_; my ($hash, $daylist) = @_;
@ -428,7 +423,7 @@ sub WeekdayTimer_daylistAsArray($$){
@hdays{@subDays}=undef; @hdays{@subDays}=undef;
} }
} }
} else{ } else {
%hdays = (); %hdays = ();
} }
@ -553,7 +548,6 @@ E: while (@$a > 0) {
my $wp_temps = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{temp}; my $wp_temps = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{temp};
my $wp_shortDay = $wp_shortDays{$wp_days}; my $wp_shortDay = $wp_shortDays{$wp_days};
for ( my $i = 0; $i < @{$wp_temps}; $i++ ) { for ( my $i = 0; $i < @{$wp_temps}; $i++ ) {
#foreach my $st (@{$wp_temps}) {
my $itime = "00:10"; my $itime = "00:10";
$itime = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{time}[$i-1] if $i; $itime = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{time}[$i-1] if $i;
my $itemp = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{temp}[$i]; my $itemp = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{temp}[$i];
@ -584,15 +578,14 @@ sub WeekdayTimer_Language {
my $langRegExp = "(" . join ("|", keys(%{$hash->{shortDays}})) . ")"; my $langRegExp = "(" . join ("|", keys(%{$hash->{shortDays}})) . ")";
my $language = shift @$a; my $language = shift @$a;
if ($language =~ m/^$langRegExp$/g) { unless ($language =~ m/^$langRegExp$/g) {
} else {
Log3 ($hash, 3, "[$name] language: $language not recognized, use one of $langRegExp") if (length($language) == 2); Log3 ($hash, 3, "[$name] language: $language not recognized, use one of $langRegExp") if (length($language) == 2);
unshift @$a, $language; unshift @$a, $language;
$language = "de"; $language = lc(AttrVal("global","language","en"));
$language = $language =~ m/^$langRegExp$/g ? $language : "en";
} }
$hash->{LANGUAGE} = $language; $hash->{LANGUAGE} = $language;
$language = $hash->{LANGUAGE};
return ($langRegExp, $language); return ($langRegExp, $language);
} }
################################################################################ ################################################################################
@ -772,8 +765,6 @@ sub WeekdayTimer_SetTimer($) {
} }
} }
########################################################################
################################################################################ ################################################################################
sub WeekdayTimer_delayedTimerInPast($) { sub WeekdayTimer_delayedTimerInPast($) {
my ($myHash) = @_; my ($myHash) = @_;
@ -909,7 +900,7 @@ sub WeekdayTimer_Update($) {
my $disabled = AttrVal($hash->{NAME}, "disable", 0); my $disabled = AttrVal($hash->{NAME}, "disable", 0);
# ggf. Device schalten # ggf. Device schalten
WeekdayTimer_Device_Schalten($hash, $newParam, $tage) if($activeTimer); WeekdayTimer_Switch_Device($hash, $newParam, $tage) if($activeTimer);
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
readingsBulkUpdate ($hash, "nextUpdate", FmtDateTime($nextTime)); readingsBulkUpdate ($hash, "nextUpdate", FmtDateTime($nextTime));
@ -939,7 +930,6 @@ sub WeekdayTimer_isAnActiveTimer ($$$$) {
return $ret; return $ret;
} }
################################################################################ ################################################################################
# {WeekdayTimer_isHeizung($defs{HeizungKueche_an_wt})}
sub WeekdayTimer_isHeizung($) { sub WeekdayTimer_isHeizung($) {
my ($hash) = @_; my ($hash) = @_;
@ -965,7 +955,6 @@ sub WeekdayTimer_isHeizung($) {
} }
################################################################################ ################################################################################
#
sub WeekdayTimer_FensterOffen ($$$) { sub WeekdayTimer_FensterOffen ($$$) {
my ($hash, $event, $time) = @_; my ($hash, $event, $time) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -1094,9 +1083,8 @@ sub WeekdayTimer_FensterOffen ($$$) {
delete $hash->{VERZOEGRUNG_IDX} if defined($hash->{VERZOEGRUNG_IDX}); delete $hash->{VERZOEGRUNG_IDX} if defined($hash->{VERZOEGRUNG_IDX});
return 0; return 0;
} }
################################################################################ ################################################################################
sub WeekdayTimer_Device_Schalten($$$) { sub WeekdayTimer_Switch_Device($$$) {
my ($hash, $newParam, $tage) = @_; my ($hash, $newParam, $tage) = @_;
my ($command, $condition, $tageAsHash) = ""; my ($command, $condition, $tageAsHash) = "";
@ -1162,7 +1150,6 @@ sub WeekdayTimer_Condition($$$) {
$condition .= ")"; $condition .= ")";
return $condition; return $condition;
} }
################################################################################ ################################################################################
sub WeekdayTimer_TageAsCondition ($$) { sub WeekdayTimer_TageAsCondition ($$) {
@ -1179,9 +1166,7 @@ sub WeekdayTimer_TageAsCondition ($$) {
$tageExp .= ' || !$we' if defined $notWe; $tageExp .= ' || !$we' if defined $notWe;
$tageExp .= ')'; $tageExp .= ')';
return $tageExp; return $tageExp;
} }
################################################################################ ################################################################################
sub WeekdayTimer_Attr($$$$) { sub WeekdayTimer_Attr($$$$) {
@ -1204,7 +1189,7 @@ sub WeekdayTimer_Attr($$$$) {
} }
return undef; return undef;
} }
######################################################################## ################################################################################
sub WeekdayTimer_SetParm($) { sub WeekdayTimer_SetParm($) {
my ($name) = @_; my ($name) = @_;
@ -1228,7 +1213,7 @@ sub WeekdayTimer_SetAllParms(;$) { # {WeekdayTimer_SetAllParms()}
} }
Log3 undef, 3, "WeekdayTimer_SetAllParms() done on: ".join(" ",@wdtNames ); Log3 undef, 3, "WeekdayTimer_SetAllParms() done on: ".join(" ",@wdtNames );
} }
################################################################################
sub WeekdayTimer_UpdateWeekprofileReading($$$$) { sub WeekdayTimer_UpdateWeekprofileReading($$$$) {
my ($hash,$wp_name,$wp_topic,$wp_profile) = @_; my ($hash,$wp_name,$wp_topic,$wp_profile) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -1251,7 +1236,7 @@ sub WeekdayTimer_UpdateWeekprofileReading($$$$) {
readingsSingleUpdate ($hash, "weekprofiles", join(" ",@newt), 1); readingsSingleUpdate ($hash, "weekprofiles", join(" ",@newt), 1);
return 1; return 1;
} }
################################################################################
sub WeekdayTimer_GetWeekprofileReadingTriplett($$) { sub WeekdayTimer_GetWeekprofileReadingTriplett($$) {
my ($hash,$wp_name) = @_; my ($hash,$wp_name) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -1273,7 +1258,7 @@ sub WeekdayTimer_GetWeekprofileReadingTriplett($$) {
} }
return undef; return undef;
} }
################################################################################
1; 1;
=pod =pod