fixed errors in token getter

no return value in tokenrenew and tokenrefresh
This commit is contained in:
Philipp Wo 2019-11-22 12:31:36 +01:00
parent 65fe7cf1ed
commit c7cf608943
1 changed files with 34 additions and 24 deletions

View File

@ -375,17 +375,17 @@ sub TadoAPI_LoadToken(@){
if($@ || $tokenLifeTime < gettimeofday()){
Log3 $name, 5, "TadoAPI $name" . ": " . "Error while loading: $@ ,requesting new one";
Log3 $name, 5, "TadoAPI $name" . ": " . "Token is expired, requesting new one" if $tokenLifeTime < gettimeofday();
TadoAPI_NewTokenRequest($hash);
$Token = TadoAPI_NewTokenRequest($hash);
}else{
Log3 $name, 5, "TadoAPI $name" . ": " . "Token expires at " . localtime($tokenLifeTime);
# if token is about to expire, refresh him
if(($tokenLifeTime-45) < gettimeofday()){
Log3 $name, 5, "TadoAPI $name" . ": " . "Token will expire soon, refreshing";
TadoAPI_TokenRefresh($hash);
$Token = TadoAPI_TokenRefresh($hash);
}
}
close(TOKENFILE);
return $Token;
return $Token if $Token;
}
return undef;
}
@ -416,7 +416,7 @@ sub TadoAPI_NewTokenRequest(@) {
data => $data
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -440,6 +440,7 @@ sub TadoAPI_NewTokenRequest(@) {
$hash->{TOKEN_LIFETIME_HR} = localtime($hash->{TOKEN_LIFETIME});
Log3 $name, 5, "TadoAPI $name" . ": " . "Retrived new authentication token successfully. Valid until " . localtime($hash->{TOKEN_LIFETIME});
$hash->{STATE}="reachable";
return $decoded_data;
}
}
}
@ -470,7 +471,7 @@ sub TadoAPI_TokenRefresh(@) {
data => $data
};
Log3 $name, 5, 'Blocking GET TokenRefresh: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET TokenRefresh: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -482,7 +483,6 @@ sub TadoAPI_TokenRefresh(@) {
elsif($data ne "")
{
Log3 $name, 5, "url ".$param->{url}." returned: $data";
my $decoded_data = eval{decode_json($data);};
if ($@){
@ -499,6 +499,7 @@ sub TadoAPI_TokenRefresh(@) {
$hash->{TOKEN_LIFETIME_HR} = localtime($hash->{TOKEN_LIFETIME});
Log3 $name, 5, "TadoAPI $name" . ": " . "TokenRefresh: Refreshed authentication token successfully. Valid until " . localtime($hash->{TOKEN_LIFETIME});
$hash->{STATE}="reachable";
return $decoded_data;
}
}
}
@ -587,7 +588,7 @@ sub TadoAPI_SetZoneOverlayById(@){
data => $myjson
};
Log3 $name, 5, 'NonBlocking Request: ' . Dumper($request);
#Log3 $name, 5, 'NonBlocking Request: ' . Dumper($request);
HttpUtils_NonblockingGet($request);
}
@ -628,6 +629,13 @@ sub TadoAPI_UpdateAllZonesCallback($){
{
Log3 $name, 5, "url ".$param->{url}." returned: $data";
my $zonecount = TadoAPI_GetZoneCount($hash);
# fix for empty zonecount
if($zonecount == 0){
Log3 $name, 3, "TadoAPI $name" . ": " . "Error, no zones found, retrying...";
$TokenData = TadoAPI_LoadToken($hash);
$zonecount = TadoAPI_GetZoneCount($hash);
Log3 $name, 3, "TadoAPI $name" . ": " . "Retry finished. Zonecount now: %zonecount ";
}
for (my $i=1; $i <= $zonecount; $i++) {
Log3 $name, 5, "TadoAPI $name" . ": " . "Set Reading Update for Zone $i ";
my ($temperature, $humidity, $desiredTemp, $currentHeatingPower, $overlay ) = TadoAPI_GetZoneReadingsById($hash, $i);
@ -676,7 +684,7 @@ sub TadoAPI_UpdateFn(@){
url => $URL,
header => { "Content-Type"=>"application/json;charset=UTF-8","Authorization" => "$TokenData->{'token_type'} $TokenData->{'access_token'}" },
method => 'GET',
timeout => 6,
timeout => 10,
incrementalTimout => 1,
callback => \&TadoAPI_UpdateMobileReadingsCallback,
hash => $hash
@ -714,7 +722,7 @@ sub TadoAPI_GetGeoById(@){
hash => $hash,
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -1026,7 +1034,7 @@ sub TadoAPI_RequestTadoDevices(@) {
hash => $hash
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -1037,6 +1045,7 @@ sub TadoAPI_RequestTadoDevices(@) {
{
Log3 $name, 5, "url ".$param->{url}." returned: $data";
my $decoded_data = eval { decode_json($data) };
if ($@){
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_RequestTadoDevices: decode_json failed, invalid json. error:$@\n";
}else{
@ -1072,7 +1081,7 @@ sub TadoAPI_ZoneRequest(@) {
zoneID => $i
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -1082,9 +1091,8 @@ sub TadoAPI_ZoneRequest(@) {
elsif($data ne "")
{
Log3 $name, 5, "url ".$param->{url}." returned: $data";
# An dieser Stelle die Antwort parsen / verarbeiten mit $data
my $decoded_data = eval { decode_json($data) };
if ($@){
Log3 $name, 3, "TadoAPI $name" . ": " . "RequestZones: Zone $i decode_json failed, invalid json. error:$@\n";
}else{
@ -1113,12 +1121,12 @@ sub TadoAPI_GetMobileDevices(@) {
hash => $hash
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
{
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetMobileDevices: Error while requesting ".$param->{url}." - $err";
Log3 $name, 3, "TadoAPI $name" . ": " . "GetMobileDevices: Error while requesting ".$param->{url}." - $err";
}
elsif($data ne "")
{
@ -1126,14 +1134,17 @@ sub TadoAPI_GetMobileDevices(@) {
Log3 $name, 5, "url ".$param->{url}." returned: $data";
my $decoded_data = eval { decode_json($data) };
if ($@){
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetMobileDevices: decode_json failed, invalid json. error:$@\n";
Log3 $name, 3, "TadoAPI $name" . ": " . "GetMobileDevices: decode_json failed, invalid json. error:$@\n";
}else{
foreach my $item( @$decoded_data ) {
if(ref($decoded_data) eq 'ARRAY'){
foreach my $item( @$decoded_data ) {
push @devices, $item;
}
return @devices;
}elsif(ref($decoded_data) eq 'HASH'){
Log3 $name, 3, "TadoAPI $name" . ": " . "GetMobileDevices: " . $decoded_data->{'errors'}->[0]->{'code'} if (exists($decoded_data->{'errors'}));
}
}
return @devices;
}
}
return undef;
@ -1167,7 +1178,7 @@ sub TadoAPI_GetHomeId(@){
hash => $hash,
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -1207,7 +1218,7 @@ sub TadoAPI_GetZoneCount(@) {
hash => $hash
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -1219,7 +1230,6 @@ sub TadoAPI_GetZoneCount(@) {
my @devices = ();
Log3 $name, 5, "url ".$param->{url}." returned: $data";
my $decoded_data = eval { decode_json($data) };
#print Dumper($decoded_data);
if ($@){
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetZoneCount: decode_json failed, invalid json. error:$@\n";
@ -1252,7 +1262,7 @@ sub TadoAPI_GetZoneNameById(@) {
hash => $hash
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")
@ -1302,7 +1312,7 @@ sub TadoAPI_GetZoneReadingsById(@){
hash => $hash
};
Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
my ($err, $data) = HttpUtils_BlockingGet($param);
if($err ne "")