diff --git a/98_TadoAPI.pm b/98_TadoAPI.pm index 5f6da0b..51874b3 100644 --- a/98_TadoAPI.pm +++ b/98_TadoAPI.pm @@ -15,9 +15,9 @@ # NOTES: --- # AUTHOR: Philipp Wolfmajer # ORGANIZATION: -# VERSION: 1.0 -# CREATED: 04/12/2019 07:55:44 PM -# REVISION: 11/21/2019 05:17:22 PM +# VERSION: 0.6 +# CREATED: 04/12/2019 +# REVISION: 12/10/2019 #=============================================================================== package main; @@ -62,7 +62,6 @@ my %gets = ( "getMobileDevices" => "noArg" ); - sub TadoAPI_Initialize($) { @@ -77,7 +76,6 @@ TadoAPI_Initialize($) "mobileID " . "updateIntervall " . $main::readingFnAttributes; - } sub TadoAPI_Init($$) @@ -114,7 +112,6 @@ sub TadoAPI_Define($$) if ($main::init_done) { # do something? - return TadoAPI_Catch($@) if $@; } @@ -158,9 +155,6 @@ sub TadoAPI_Set(@) { return "Unknown argument $a[1], choose one of " . join(" ", @cmds); } - - - if (($cmd ne "password")) { my $pwd = TadoAPI_readPassword($name); @@ -243,7 +237,6 @@ sub TadoAPI_Set(@) { InternalTimer( gettimeofday() + 10, "TadoAPI_Update", $hash, 0 ); Log3 $name, 3, "TadoAPI $name" . ": " . "$cmd finished\n"; } - return $message if $message; return TadoAPI_Catch($@) if $@; } @@ -342,7 +335,6 @@ sub TadoAPI_Get(@) { last; }; } - return $message if $message; return TadoAPI_Catch($@) if $@; return undef; @@ -376,6 +368,15 @@ 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: $!"); @@ -383,7 +384,8 @@ sub TadoAPI_LoadToken(@){ if($@ || $tokenLifeTime < gettimeofday()){ 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); + $Token = TadoAPI_NewTokenRequest($hash); + HttpUtils_NonblockingGet($param); }else{ Log3 $name, 5, "TadoAPI $name" . ": " . "Token expires at " . localtime($tokenLifeTime); # if token is about to expire, refresh him @@ -395,15 +397,6 @@ sub TadoAPI_LoadToken(@){ close(TOKENFILE); return $Token if $Token; } - - # test api status if no token could be loaded - my $param = { - url => $AuthURL, - timeout => 3, - hash => $hash, - method => "GET", - callback => \&TadoAPI_callback - }; HttpUtils_NonblockingGet($param); return undef; @@ -418,7 +411,6 @@ sub TadoAPI_NewTokenRequest(@) { Log3 $name, 5, "TadoAPI $name" . ": " . "calling NewTokenRequest()"; - $data = { client_id => $client_id, client_secret => $client_secret, @@ -729,6 +721,9 @@ sub TadoAPI_callback($){ my $name = $hash->{NAME}; $param->{code} = 0 unless defined $param->{code}; +# todo remove this + Log3 $name, 3, "TadoAPI $name" . ": " . "API Callback running..... Callback Status: " . $param->{code}; + if($param->{code} == 401 || $param->{code} == 400){ $apiStatus = 1; $hash->{STATE}="reachable"; @@ -753,7 +748,6 @@ sub TadoAPI_UpdateAllZoneReadingsCallback($){ Log3 $name, 3, "Error in TadoAPI_UpdateZoneCallback while requesting ".$param->{url}." - $err"; return undef; } - elsif($data ne "") { Log3 $name, 5, "url ".$param->{url}." returned: $data"; @@ -822,7 +816,6 @@ sub Tado_UpdateZoneOverlayCallback($) } # finaly update readings TadoAPI_GetZoneReadingsById($hash, $zoneID); - } sub TadoAPI_LogInfoCallback($){ @@ -842,42 +835,6 @@ sub TadoAPI_LogInfoCallback($){ } } -sub TadoAPI_UpdateTadoDeviceInformationCallback($){ - my ($param, $err, $data) = @_; - my $hash = $param->{hash}; - my $name = $hash->{NAME}; - - if($err ne "") - { - Log3 $name, 3, "Error in UpdateTadoDeviceInformationCallback while requesting ".$param->{url}." - $err"; - } - - elsif($data ne "") - { - Log3 $name, 5, "url ".$param->{url}." returned: $data"; - my $decoded_data = eval { decode_json($data) }; - - # if api returns error - eval { my $error = @$decoded_data; }; - - if ($@){ - Log3 $name, 3, "TadoAPI $name" . ": " . "UpdateTadoDeviceInformationCallback: decode_json failed, invalid json. error:$@\n" if $@; - Log3 $name, 3, "TadoAPI $name" . ": " . "UpdateTadoDeviceInformationCallback: Error in decoded data, Code: " . $decoded_data->{'errors'}->[0]->{'code'}; - $hash->{LastRequest}="error"; - }else{ - foreach my $zone ( @$decoded_data ){ - my $devices = $zone->{'devices'}; - foreach my $device ( @$devices ){ - readingsBeginUpdate($hash); - readingsBulkUpdate($hash, "Battery_" . $device->{'serialNo'}, $device->{'batteryState'}); - readingsEndUpdate( $hash, 1 ); - $hash->{LastRequest}="OK"; - } - } - } - } -} - sub TadoAPI_SetGeoByIdCallback($){ my ($param, $err, $data) = @_; my $hash = $param->{hash}; @@ -887,7 +844,6 @@ sub TadoAPI_SetGeoByIdCallback($){ { Log3 $name, 3, "Error in TadoAPI_SetGeoByIdCallback while requesting ".$param->{url}." - $err"; } - elsif($data ne "") { Log3 $name, 3, "url ".$param->{url}." returned: $data"; @@ -969,10 +925,8 @@ sub TadoAPI_GetZoneInfo(@) { hash => $hash, callback => \&TadoAPI_LogInfoCallback }; - HttpUtils_NonblockingGet($request); - # Mobileinfo $URL = $QueryURL . qq{/$homeID/mobileDevices}; $request = { @@ -984,7 +938,6 @@ sub TadoAPI_GetZoneInfo(@) { hash => $hash, callback => \&TadoAPI_LogInfoCallback }; - HttpUtils_NonblockingGet($request); my @mobDev = TadoAPI_GetMobileDevices($hash); @@ -1000,7 +953,6 @@ sub TadoAPI_GetZoneInfo(@) { hash => $hash, callback => \&TadoAPI_LogInfoCallback }; - HttpUtils_NonblockingGet($request); } @@ -1019,7 +971,6 @@ sub TadoAPI_GetZoneInfo(@) { hash => $hash, callback => \&TadoAPI_LogInfoCallback }; - HttpUtils_NonblockingGet($request); } } @@ -1052,7 +1003,6 @@ sub TadoAPI_SetGeoById(@){ hash => $hash, callback => \&TadoAPI_SetGeoByIdCallback }; - HttpUtils_NonblockingGet($request); } } @@ -1083,27 +1033,27 @@ sub TadoAPI_GetHomeId(@){ method => 'GET', timeout => 2, hash => $hash, - }; + }; - #Log3 $name, 5, 'Blocking GET: ' . Dumper($param); - my ($err, $data) = HttpUtils_BlockingGet($param); + #Log3 $name, 5, 'Blocking GET: ' . Dumper($param); + my ($err, $data) = HttpUtils_BlockingGet($param); - if($err ne "") - { - Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Error while requesting ".$param->{url}." - $err"; - } - elsif($data ne "") - { - Log3 $name, 5, "url ".$param->{url}." returned: $data"; - my $decoded_data = eval { decode_json($data) }; - if ($@){ - Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Decode_json failed, invalid json. error:$@" if $@; - $hash->{LastRequest}="error"; - }else{ - $hash->{LastRequest}="OK"; - return $decoded_data->{'homes'}->[0]->{'id'} if (exists($decoded_data->{'homes'})); - } + if($err ne "") + { + Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Error while requesting ".$param->{url}." - $err"; + } + elsif($data ne "") + { + Log3 $name, 5, "url ".$param->{url}." returned: $data"; + my $decoded_data = eval { decode_json($data) }; + if ($@){ + Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Decode_json failed, invalid json. error:$@" if $@; + $hash->{LastRequest}="error"; + }else{ + $hash->{LastRequest}="OK"; + return $decoded_data->{'homes'}->[0]->{'id'} if (exists($decoded_data->{'homes'})); } + } } return undef; } @@ -1124,27 +1074,27 @@ sub TadoAPI_GetGeoById(@){ hash => $hash, }; - #Log3 $name, 5, 'Blocking GET: ' . Dumper($param); - my ($err, $data) = HttpUtils_BlockingGet($param); + #Log3 $name, 5, 'Blocking GET: ' . Dumper($param); + my ($err, $data) = HttpUtils_BlockingGet($param); - if($err ne "") - { - Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: Error while requesting ".$param->{url}." - $err"; - } - elsif($data ne "") - { - Log3 $name, 5, "url ".$param->{url}." returned: $data"; - my $decoded_data = eval { decode_json($data) }; - if ($@){ - Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: decode_json failed, invalid json. error:$@\n"; - }else{ - my $setting = $decoded_data->{'geoTrackingEnabled'}; - readingsBeginUpdate($hash); - readingsBulkUpdate($hash, "GeoLocation_" . $mobileID, $setting); - readingsEndUpdate( $hash, 1 ); - return $setting; - } + if($err ne "") + { + Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: Error while requesting ".$param->{url}." - $err"; + } + elsif($data ne "") + { + Log3 $name, 5, "url ".$param->{url}." returned: $data"; + my $decoded_data = eval { decode_json($data) }; + if ($@){ + Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: decode_json failed, invalid json. error:$@\n"; + }else{ + my $setting = $decoded_data->{'geoTrackingEnabled'}; + readingsBeginUpdate($hash); + readingsBulkUpdate($hash, "GeoLocation_" . $mobileID, $setting); + readingsEndUpdate( $hash, 1 ); + return $setting; } + } } return undef; }