code cleanup
removed unused sub´s
This commit is contained in:
parent
4b7a09ff0b
commit
470c4e135a
158
98_TadoAPI.pm
158
98_TadoAPI.pm
@ -15,9 +15,9 @@
|
|||||||
# NOTES: ---
|
# NOTES: ---
|
||||||
# AUTHOR: Philipp Wolfmajer
|
# AUTHOR: Philipp Wolfmajer
|
||||||
# ORGANIZATION:
|
# ORGANIZATION:
|
||||||
# VERSION: 1.0
|
# VERSION: 0.6
|
||||||
# CREATED: 04/12/2019 07:55:44 PM
|
# CREATED: 04/12/2019
|
||||||
# REVISION: 11/21/2019 05:17:22 PM
|
# REVISION: 12/10/2019
|
||||||
#===============================================================================
|
#===============================================================================
|
||||||
package main;
|
package main;
|
||||||
|
|
||||||
@ -62,7 +62,6 @@ my %gets = (
|
|||||||
"getMobileDevices" => "noArg"
|
"getMobileDevices" => "noArg"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
sub
|
sub
|
||||||
TadoAPI_Initialize($)
|
TadoAPI_Initialize($)
|
||||||
{
|
{
|
||||||
@ -77,7 +76,6 @@ TadoAPI_Initialize($)
|
|||||||
"mobileID " .
|
"mobileID " .
|
||||||
"updateIntervall " .
|
"updateIntervall " .
|
||||||
$main::readingFnAttributes;
|
$main::readingFnAttributes;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub TadoAPI_Init($$)
|
sub TadoAPI_Init($$)
|
||||||
@ -114,7 +112,6 @@ sub TadoAPI_Define($$)
|
|||||||
|
|
||||||
if ($main::init_done) {
|
if ($main::init_done) {
|
||||||
# do something?
|
# do something?
|
||||||
|
|
||||||
return TadoAPI_Catch($@) if $@;
|
return TadoAPI_Catch($@) if $@;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,9 +155,6 @@ sub TadoAPI_Set(@) {
|
|||||||
return "Unknown argument $a[1], choose one of " . join(" ", @cmds);
|
return "Unknown argument $a[1], choose one of " . join(" ", @cmds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (($cmd ne "password"))
|
if (($cmd ne "password"))
|
||||||
{
|
{
|
||||||
my $pwd = TadoAPI_readPassword($name);
|
my $pwd = TadoAPI_readPassword($name);
|
||||||
@ -243,7 +237,6 @@ sub TadoAPI_Set(@) {
|
|||||||
InternalTimer( gettimeofday() + 10, "TadoAPI_Update", $hash, 0 );
|
InternalTimer( gettimeofday() + 10, "TadoAPI_Update", $hash, 0 );
|
||||||
Log3 $name, 3, "TadoAPI $name" . ": " . "$cmd finished\n";
|
Log3 $name, 3, "TadoAPI $name" . ": " . "$cmd finished\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return $message if $message;
|
return $message if $message;
|
||||||
return TadoAPI_Catch($@) if $@;
|
return TadoAPI_Catch($@) if $@;
|
||||||
}
|
}
|
||||||
@ -342,7 +335,6 @@ sub TadoAPI_Get(@) {
|
|||||||
last;
|
last;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return $message if $message;
|
return $message if $message;
|
||||||
return TadoAPI_Catch($@) if $@;
|
return TadoAPI_Catch($@) if $@;
|
||||||
return undef;
|
return undef;
|
||||||
@ -376,6 +368,15 @@ sub TadoAPI_LoadToken(@){
|
|||||||
$tokenLifeTime = 0 if(!defined $tokenLifeTime || $tokenLifeTime eq '');
|
$tokenLifeTime = 0 if(!defined $tokenLifeTime || $tokenLifeTime eq '');
|
||||||
my $Token = undef;
|
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){
|
if($apiStatus){
|
||||||
eval {
|
eval {
|
||||||
open(TOKENFILE, '<', $tokenFileName) or die("ERROR: $!");
|
open(TOKENFILE, '<', $tokenFileName) or die("ERROR: $!");
|
||||||
@ -383,7 +384,8 @@ sub TadoAPI_LoadToken(@){
|
|||||||
if($@ || $tokenLifeTime < gettimeofday()){
|
if($@ || $tokenLifeTime < gettimeofday()){
|
||||||
Log3 $name, 5, "TadoAPI $name" . ": " . "Error while loading: $@ ,requesting new one" if $@;
|
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();
|
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{
|
}else{
|
||||||
Log3 $name, 5, "TadoAPI $name" . ": " . "Token expires at " . localtime($tokenLifeTime);
|
Log3 $name, 5, "TadoAPI $name" . ": " . "Token expires at " . localtime($tokenLifeTime);
|
||||||
# if token is about to expire, refresh him
|
# if token is about to expire, refresh him
|
||||||
@ -395,15 +397,6 @@ sub TadoAPI_LoadToken(@){
|
|||||||
close(TOKENFILE);
|
close(TOKENFILE);
|
||||||
return $Token if $Token;
|
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);
|
HttpUtils_NonblockingGet($param);
|
||||||
|
|
||||||
return undef;
|
return undef;
|
||||||
@ -418,7 +411,6 @@ sub TadoAPI_NewTokenRequest(@) {
|
|||||||
|
|
||||||
Log3 $name, 5, "TadoAPI $name" . ": " . "calling NewTokenRequest()";
|
Log3 $name, 5, "TadoAPI $name" . ": " . "calling NewTokenRequest()";
|
||||||
|
|
||||||
|
|
||||||
$data = {
|
$data = {
|
||||||
client_id => $client_id,
|
client_id => $client_id,
|
||||||
client_secret => $client_secret,
|
client_secret => $client_secret,
|
||||||
@ -729,6 +721,9 @@ sub TadoAPI_callback($){
|
|||||||
my $name = $hash->{NAME};
|
my $name = $hash->{NAME};
|
||||||
$param->{code} = 0 unless defined $param->{code};
|
$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){
|
if($param->{code} == 401 || $param->{code} == 400){
|
||||||
$apiStatus = 1;
|
$apiStatus = 1;
|
||||||
$hash->{STATE}="reachable";
|
$hash->{STATE}="reachable";
|
||||||
@ -753,7 +748,6 @@ sub TadoAPI_UpdateAllZoneReadingsCallback($){
|
|||||||
Log3 $name, 3, "Error in TadoAPI_UpdateZoneCallback while requesting ".$param->{url}." - $err";
|
Log3 $name, 3, "Error in TadoAPI_UpdateZoneCallback while requesting ".$param->{url}." - $err";
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
elsif($data ne "")
|
elsif($data ne "")
|
||||||
{
|
{
|
||||||
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
||||||
@ -822,7 +816,6 @@ sub Tado_UpdateZoneOverlayCallback($)
|
|||||||
}
|
}
|
||||||
# finaly update readings
|
# finaly update readings
|
||||||
TadoAPI_GetZoneReadingsById($hash, $zoneID);
|
TadoAPI_GetZoneReadingsById($hash, $zoneID);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub TadoAPI_LogInfoCallback($){
|
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($){
|
sub TadoAPI_SetGeoByIdCallback($){
|
||||||
my ($param, $err, $data) = @_;
|
my ($param, $err, $data) = @_;
|
||||||
my $hash = $param->{hash};
|
my $hash = $param->{hash};
|
||||||
@ -887,7 +844,6 @@ sub TadoAPI_SetGeoByIdCallback($){
|
|||||||
{
|
{
|
||||||
Log3 $name, 3, "Error in TadoAPI_SetGeoByIdCallback while requesting ".$param->{url}." - $err";
|
Log3 $name, 3, "Error in TadoAPI_SetGeoByIdCallback while requesting ".$param->{url}." - $err";
|
||||||
}
|
}
|
||||||
|
|
||||||
elsif($data ne "")
|
elsif($data ne "")
|
||||||
{
|
{
|
||||||
Log3 $name, 3, "url ".$param->{url}." returned: $data";
|
Log3 $name, 3, "url ".$param->{url}." returned: $data";
|
||||||
@ -969,10 +925,8 @@ sub TadoAPI_GetZoneInfo(@) {
|
|||||||
hash => $hash,
|
hash => $hash,
|
||||||
callback => \&TadoAPI_LogInfoCallback
|
callback => \&TadoAPI_LogInfoCallback
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpUtils_NonblockingGet($request);
|
HttpUtils_NonblockingGet($request);
|
||||||
|
|
||||||
|
|
||||||
# Mobileinfo
|
# Mobileinfo
|
||||||
$URL = $QueryURL . qq{/$homeID/mobileDevices};
|
$URL = $QueryURL . qq{/$homeID/mobileDevices};
|
||||||
$request = {
|
$request = {
|
||||||
@ -984,7 +938,6 @@ sub TadoAPI_GetZoneInfo(@) {
|
|||||||
hash => $hash,
|
hash => $hash,
|
||||||
callback => \&TadoAPI_LogInfoCallback
|
callback => \&TadoAPI_LogInfoCallback
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpUtils_NonblockingGet($request);
|
HttpUtils_NonblockingGet($request);
|
||||||
|
|
||||||
my @mobDev = TadoAPI_GetMobileDevices($hash);
|
my @mobDev = TadoAPI_GetMobileDevices($hash);
|
||||||
@ -1000,7 +953,6 @@ sub TadoAPI_GetZoneInfo(@) {
|
|||||||
hash => $hash,
|
hash => $hash,
|
||||||
callback => \&TadoAPI_LogInfoCallback
|
callback => \&TadoAPI_LogInfoCallback
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpUtils_NonblockingGet($request);
|
HttpUtils_NonblockingGet($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1019,7 +971,6 @@ sub TadoAPI_GetZoneInfo(@) {
|
|||||||
hash => $hash,
|
hash => $hash,
|
||||||
callback => \&TadoAPI_LogInfoCallback
|
callback => \&TadoAPI_LogInfoCallback
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpUtils_NonblockingGet($request);
|
HttpUtils_NonblockingGet($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1052,7 +1003,6 @@ sub TadoAPI_SetGeoById(@){
|
|||||||
hash => $hash,
|
hash => $hash,
|
||||||
callback => \&TadoAPI_SetGeoByIdCallback
|
callback => \&TadoAPI_SetGeoByIdCallback
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpUtils_NonblockingGet($request);
|
HttpUtils_NonblockingGet($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1083,27 +1033,27 @@ sub TadoAPI_GetHomeId(@){
|
|||||||
method => 'GET',
|
method => 'GET',
|
||||||
timeout => 2,
|
timeout => 2,
|
||||||
hash => $hash,
|
hash => $hash,
|
||||||
};
|
};
|
||||||
|
|
||||||
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
|
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
|
||||||
my ($err, $data) = HttpUtils_BlockingGet($param);
|
my ($err, $data) = HttpUtils_BlockingGet($param);
|
||||||
|
|
||||||
if($err ne "")
|
if($err ne "")
|
||||||
{
|
{
|
||||||
Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Error while requesting ".$param->{url}." - $err";
|
Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Error while requesting ".$param->{url}." - $err";
|
||||||
}
|
}
|
||||||
elsif($data ne "")
|
elsif($data ne "")
|
||||||
{
|
{
|
||||||
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
||||||
my $decoded_data = eval { decode_json($data) };
|
my $decoded_data = eval { decode_json($data) };
|
||||||
if ($@){
|
if ($@){
|
||||||
Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Decode_json failed, invalid json. error:$@" if $@;
|
Log3 $name, 3, "TadoAPI $name" . ": " . "GetHomeId: Decode_json failed, invalid json. error:$@" if $@;
|
||||||
$hash->{LastRequest}="error";
|
$hash->{LastRequest}="error";
|
||||||
}else{
|
}else{
|
||||||
$hash->{LastRequest}="OK";
|
$hash->{LastRequest}="OK";
|
||||||
return $decoded_data->{'homes'}->[0]->{'id'} if (exists($decoded_data->{'homes'}));
|
return $decoded_data->{'homes'}->[0]->{'id'} if (exists($decoded_data->{'homes'}));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
@ -1124,27 +1074,27 @@ sub TadoAPI_GetGeoById(@){
|
|||||||
hash => $hash,
|
hash => $hash,
|
||||||
};
|
};
|
||||||
|
|
||||||
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
|
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
|
||||||
my ($err, $data) = HttpUtils_BlockingGet($param);
|
my ($err, $data) = HttpUtils_BlockingGet($param);
|
||||||
|
|
||||||
if($err ne "")
|
if($err ne "")
|
||||||
{
|
{
|
||||||
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: Error while requesting ".$param->{url}." - $err";
|
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: Error while requesting ".$param->{url}." - $err";
|
||||||
}
|
}
|
||||||
elsif($data ne "")
|
elsif($data ne "")
|
||||||
{
|
{
|
||||||
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
||||||
my $decoded_data = eval { decode_json($data) };
|
my $decoded_data = eval { decode_json($data) };
|
||||||
if ($@){
|
if ($@){
|
||||||
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: decode_json failed, invalid json. error:$@\n";
|
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: decode_json failed, invalid json. error:$@\n";
|
||||||
}else{
|
}else{
|
||||||
my $setting = $decoded_data->{'geoTrackingEnabled'};
|
my $setting = $decoded_data->{'geoTrackingEnabled'};
|
||||||
readingsBeginUpdate($hash);
|
readingsBeginUpdate($hash);
|
||||||
readingsBulkUpdate($hash, "GeoLocation_" . $mobileID, $setting);
|
readingsBulkUpdate($hash, "GeoLocation_" . $mobileID, $setting);
|
||||||
readingsEndUpdate( $hash, 1 );
|
readingsEndUpdate( $hash, 1 );
|
||||||
return $setting;
|
return $setting;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user