improved reading updates

This commit is contained in:
Philipp Wo 2019-12-10 15:28:32 +01:00
parent df555d473b
commit 1f82ad7824
1 changed files with 29 additions and 18 deletions

View File

@ -118,6 +118,7 @@ sub TadoAPI_Define($$)
my $password = TadoAPI_readPassword($name);
if (defined($password)){
TadoAPI_CheckStatus($hash);
TadoAPI_LoadToken($hash);
# start the status update timer
@ -360,6 +361,25 @@ sub TadoAPI_Undefine($$) {
return undef;
}
sub TadoAPI_CheckStatus(@){
my ($hash) = @_;
my $name = $hash->{NAME};
# test api status
my $param = {
url => $AuthURL,
timeout => 5,
hash => $hash,
method => "GET",
header => "",
callback => \&TadoAPI_callback
};
#test if api is reachable
HttpUtils_NonblockingGet($param);
return undef;
}
sub TadoAPI_LoadToken(@){
my ($hash) = @_;
my $name = $hash->{NAME};
@ -368,15 +388,6 @@ sub TadoAPI_LoadToken(@){
$tokenLifeTime = 0 if(!defined $tokenLifeTime || $tokenLifeTime eq '');
my $Token = undef;
# test api status if no token could be loaded
my $param = {
url => $AuthURL,
timeout => 3,
hash => $hash,
method => "GET",
callback => \&TadoAPI_callback
};
if($apiStatus){
eval {
open(TOKENFILE, '<', $tokenFileName) or die("ERROR: $!");
@ -385,7 +396,7 @@ sub TadoAPI_LoadToken(@){
Log3 $name, 5, "TadoAPI $name" . ": " . "Error while loading: $@ ,requesting new one" if $@;
Log3 $name, 5, "TadoAPI $name" . ": " . "Token is expired, requesting new one" if $tokenLifeTime < gettimeofday();
$Token = TadoAPI_NewTokenRequest($hash);
HttpUtils_NonblockingGet($param);
TadoAPI_CheckStatus($hash);
}else{
Log3 $name, 5, "TadoAPI $name" . ": " . "Token expires at " . localtime($tokenLifeTime);
# if token is about to expire, refresh him
@ -397,8 +408,7 @@ sub TadoAPI_LoadToken(@){
close(TOKENFILE);
return $Token if $Token;
}
HttpUtils_NonblockingGet($param);
TadoAPI_CheckStatus($hash);
return undef;
}
@ -524,12 +534,13 @@ sub TadoAPI_Update(@){
my $nextTimer = "none";
my $intervall = 300;
$intervall = $attr{$name}{updateIntervall} if (defined($attr{$name}{updateIntervall}) && $attr{$name}{updateIntervall} =~ m/^-?\d+$/);
# if api online, try again in xx minutes
if ( $apiStatus ) {
$nextTimer = gettimeofday() + $intervall;
}
$nextTimer = gettimeofday() + $intervall;
$hash->{NEXT_UPDATE} = localtime($nextTimer);
Log3 $name, 5, "TadoAPI $name" . ": " . "Next Timer = $nextTimer";
RemoveInternalTimer($hash);
InternalTimer( $nextTimer, "TadoAPI_Update", $hash, 0 );
@ -729,7 +740,7 @@ sub TadoAPI_callback($){
}else{
$apiStatus = 0;
$hash->{STATE}="error";
Log3 $name, 5, "TadoAPI $name" . ": " . "API error: apiStatus $apiStatus ($err)";
Log3 $name, 3, "TadoAPI $name" . ": " . "API error: apiStatus $apiStatus ($err)";
}
return undef;
}
@ -770,7 +781,7 @@ sub TadoAPI_UpdateAllZoneReadingsCallback($){
readingsBulkUpdate($hash, "Heizleistung_" . $zoneName, $currentHeatingPower);
readingsBulkUpdate($hash, "OverlayType_" . $zoneName, $overlay);
readingsBulkUpdate($hash, "DesiredTemp_" . $zoneName, $desiredTemp);
readingsEndUpdate( $hash, 1 );
readingsEndUpdate( $hash, 0 );
}
# iterate through all devices in zone
my $devices = $zone->{'devices'};