2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-07 23:09:26 +00:00

98_RandomTimer: some beautifying of code, trying to compute the real state before switchung (http://forum.fhem.de/index.php/topic,14010.msg372263.html#msg372263)

git-svn-id: https://svn.fhem.de/fhem/trunk@10171 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
dietmar63 2015-12-14 20:16:58 +00:00
parent cd1157dff0
commit 8379afbb7c

View File

@ -33,7 +33,7 @@ use Time::HiRes qw(gettimeofday);
use Time::Local 'timelocal_nocheck';
sub RandomTimer_stopTimeReached($);
sub schaltZeitenErmitteln ($$);
sub RandomTimer_schaltZeitenErmitteln ($$);
sub RandomTimer_setState($);
sub RandomTimer_Initialize($)
@ -104,8 +104,10 @@ sub RandomTimer_Define($$)
$hash->{REL} = $rel;
$hash->{S_REP} = $srep;
$hash->{S_REL} = $srel;
$hash->{COMMAND} = "off";
$hash->{COMMAND} = Value($hash->{DEVICE});
Log3 $hash, 4, "[".$hash->{NAME}."]"." IstZustand Start------------>$hash->{COMMAND}";
myRemoveInternalTimer("SetTimer", $hash);
myInternalTimer ("SetTimer", time()+1, "RandomTimer_SetTimer", $hash, 0);
@ -123,7 +125,7 @@ sub RandomTimer_SetTimer($)
$hash->{active} = 0;
schaltZeitenErmitteln($hash, $now);
RandomTimer_schaltZeitenErmitteln($hash, $now);
RandomTimer_setState($hash);
Log3 $hash, 4, "[".$hash->{NAME}."]" . " timings RandomTimer on $hash->{DEVICE}: "
@ -208,7 +210,7 @@ sub RandomTimer_Exec($) {
RandomTimer_setState($hash);
RandomTimer_device_toggle($hash) if (!$disabled);
my $nextSwitch = time() + getSecsToNextAbschaltTest($hash);
my $nextSwitch = time() + RandomTimer_getSecsToNextAbschaltTest($hash);
myRemoveInternalTimer("Exec", $hash);
myInternalTimer ("Exec", $nextSwitch, "RandomTimer_Exec", $hash, 0);
@ -228,6 +230,7 @@ sub RandomTimer_down($) {
my ($hash) = @_;
$hash->{COMMAND} = AttrVal($hash->{NAME}, "keepDeviceAlive", 0) ? "on" : "off";
Log3 $hash, 4, "[".$hash->{NAME}."]"." SollZustand-down----------->$hash->{COMMAND}";
RandomTimer_device_switch($hash);
}
########################################################################
@ -277,7 +280,7 @@ sub RandomTimer_set_switchmode ($$) {
}
}
########################################################################
sub getSecsToNextAbschaltTest($)
sub RandomTimer_getSecsToNextAbschaltTest($)
{
my ($hash) = @_;
my $intervall = $hash->{TIMETOSWITCH};
@ -289,15 +292,15 @@ sub getSecsToNextAbschaltTest($)
return $nextSecs;
}
########################################################################
sub schaltZeitenErmitteln ($$) {
sub RandomTimer_schaltZeitenErmitteln ($$) {
my ($hash,$now) = @_;
startZeitErmitteln($hash, $now);
stopZeitErmitteln ($hash, $now);
RandomTimer_startZeitErmitteln($hash, $now);
RandomTimer_stopZeitErmitteln ($hash, $now);
}
########################################################################
sub zeitBerechnen ($$$$) {
sub RandomTimer_zeitBerechnen ($$$$) {
my ($now, $hour, $min, $sec) = @_;
my @jetzt_arr = localtime($now);
@ -307,7 +310,7 @@ sub zeitBerechnen ($$$$) {
return $next;
}
########################################################################
sub addDays ($$) {
sub RandomTimer_addDays ($$) {
my ($now, $days) = @_;
my @jetzt_arr = localtime($now);
@ -317,7 +320,7 @@ sub addDays ($$) {
}
########################################################################
sub startZeitErmitteln ($$) {
sub RandomTimer_startZeitErmitteln ($$) {
my ($hash,$now) = @_;
my $timespec_start = $hash->{TIMESPEC_START};
@ -333,13 +336,13 @@ sub startZeitErmitteln ($$) {
if($rel) {
$startTime = $now + 3600* $hour + 60* $min + $sec;
} else {
$startTime = zeitBerechnen($now, $hour, $min, $sec);
$startTime = RandomTimer_zeitBerechnen($now, $hour, $min, $sec);
}
$hash->{startTime} = $startTime;
$hash->{STARTTIME} = strftime("%d.%m.%Y %H:%M:%S",localtime($startTime));
}
########################################################################
sub stopZeitErmitteln ($$) {
sub RandomTimer_stopZeitErmitteln ($$) {
my ($hash,$now) = @_;
my $timespec_stop = $hash->{TIMESPEC_STOP};
@ -355,34 +358,36 @@ sub stopZeitErmitteln ($$) {
if($rel) {
$stopTime = $hash->{startTime} + 3600* $hour + 60* $min + $sec;
} else {
$stopTime = zeitBerechnen($now, $hour, $min, $sec);
$stopTime = RandomTimer_zeitBerechnen($now, $hour, $min, $sec);
}
if ($hash->{startTime} > $stopTime) {
$stopTime = addDays($stopTime, 1);
$stopTime = RandomTimer_addDays($stopTime, 1);
}
$hash->{stopTime} = $stopTime;
$hash->{STOPTIME} = strftime("%d.%m.%Y %H:%M:%S",localtime($stopTime));
}
########################################################################
sub RandomTimer_device_toggle ($)
{
sub RandomTimer_device_toggle ($) {
my ($hash) = @_;
my $sigma = ($hash->{COMMAND} eq "on") ? $hash->{SIGMAON} : $hash->{SIGMAOFF};
my $status = Value($hash->{DEVICE});
my $sigma = ($status eq "on") ? $hash->{SIGMAON} : $hash->{SIGMAOFF};
Log3 $hash, 4, "[".$hash->{NAME}."]"." IstZustand------------>$status";
my $zufall = int(rand(1000));
Log3 $hash, 4, "[".$hash->{NAME}."]"." Zustand:$hash->{COMMAND} sigma:$sigma random:$zufall->" . ($zufall < $sigma);
Log3 $hash, 4, "[".$hash->{NAME}."]"." IstZustand:$status sigma-$status:$sigma random:$zufall->" . (($zufall < $sigma)?"true":"false");
if ($zufall < $sigma ) {
$hash->{COMMAND} = ($hash->{COMMAND} eq "on") ? "off" : "on";
$hash->{COMMAND} = ($status eq "on") ? "off" : "on";
Log3 $hash, 4, "[".$hash->{NAME}."]"." SollZustand------------>$hash->{COMMAND}";
RandomTimer_device_switch($hash);
}
}
########################################################################
sub RandomTimer_device_switch ($)
{
sub RandomTimer_device_switch ($) {
my ($hash) = @_;
my $command = "set @ $hash->{COMMAND}";
if ($hash->{COMMAND} eq "on") {
$command = AttrVal($hash->{NAME}, "onCmd", $command);