reading distance from home added
set Attr showPosData to 1
This commit is contained in:
parent
91dc626e4c
commit
e2a3946e3e
113
98_TadoAPI.pm
113
98_TadoAPI.pm
@ -74,6 +74,7 @@ TadoAPI_Initialize($)
|
|||||||
$hash->{AttrList} =
|
$hash->{AttrList} =
|
||||||
"homeID " .
|
"homeID " .
|
||||||
"mobileID " .
|
"mobileID " .
|
||||||
|
"showPosData:0,1 " .
|
||||||
"updateIntervall " .
|
"updateIntervall " .
|
||||||
$main::readingFnAttributes;
|
$main::readingFnAttributes;
|
||||||
}
|
}
|
||||||
@ -712,7 +713,7 @@ sub TadoAPI_UpdateFn(@){
|
|||||||
url => $URL,
|
url => $URL,
|
||||||
header => { "Content-Type"=>"application/json;charset=UTF-8","Authorization" => "$TokenData->{'token_type'} $TokenData->{'access_token'}" },
|
header => { "Content-Type"=>"application/json;charset=UTF-8","Authorization" => "$TokenData->{'token_type'} $TokenData->{'access_token'}" },
|
||||||
method => 'GET',
|
method => 'GET',
|
||||||
timeout => 10,
|
timeout => 7,
|
||||||
incrementalTimout => 1,
|
incrementalTimout => 1,
|
||||||
callback => \&TadoAPI_UpdateMobileReadingsCallback,
|
callback => \&TadoAPI_UpdateMobileReadingsCallback,
|
||||||
hash => $hash
|
hash => $hash
|
||||||
@ -876,20 +877,7 @@ sub TadoAPI_UpdateMobileReadingsCallback($){
|
|||||||
$hash->{LastRequest}="error";
|
$hash->{LastRequest}="error";
|
||||||
}else{
|
}else{
|
||||||
foreach my $item ( @$decoded_data ){
|
foreach my $item ( @$decoded_data ){
|
||||||
readingsBeginUpdate($hash);
|
TadoAPI_GetGeoById($hash, $item->{'id'}, $item);
|
||||||
readingsBulkUpdate($hash, "GeoTracking_" . $item->{'id'}, $item->{'settings'}->{'geoTrackingEnabled'});
|
|
||||||
if(defined($item->{'location'}->{'atHome'}) && $item->{'location'}->{'atHome'}){
|
|
||||||
# present
|
|
||||||
readingsBulkUpdate($hash, "GeoLocation_" . $item->{'id'}, "present");
|
|
||||||
}elsif(defined($item->{'location'}->{'atHome'})){
|
|
||||||
# away
|
|
||||||
readingsBulkUpdate($hash, "GeoLocation_" . $item->{'id'}, "away");
|
|
||||||
}else{
|
|
||||||
# no state
|
|
||||||
readingsDelete($hash, "GeoLocation_" . $item->{'id'})
|
|
||||||
}
|
|
||||||
readingsEndUpdate( $hash, 1 );
|
|
||||||
$hash->{LastRequest}="OK";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1061,46 +1049,83 @@ sub TadoAPI_GetHomeId(@){
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub TadoAPI_GetGeoById(@){
|
sub TadoAPI_GetGeoById(@){
|
||||||
my ($hash, $mobileID) = @_;
|
# returns geo setting and distance from home; takes an item object or querys itself
|
||||||
|
my ($hash, $mobileID, $item) = @_;
|
||||||
my $name = $hash->{NAME};
|
my $name = $hash->{NAME};
|
||||||
my $homeID = $attr{$name}{homeID};
|
my $homeID = $attr{$name}{homeID};
|
||||||
my $URL=$QueryURL.qq{/$homeID/mobileDevices/$mobileID/settings};
|
my $URL=$QueryURL.qq{/$homeID/mobileDevices};
|
||||||
my $TokenData = TadoAPI_LoadToken($hash);
|
|
||||||
|
|
||||||
if(defined($TokenData)){
|
if(!defined($item)){
|
||||||
my $param = {
|
my $TokenData = TadoAPI_LoadToken($hash);
|
||||||
url => $URL,
|
|
||||||
header => {"Content-Type"=>"application/json;charset=UTF-8","Authorization" => "$TokenData->{'token_type'} $TokenData->{'access_token'}"},
|
|
||||||
method => 'GET',
|
|
||||||
timeout => 4,
|
|
||||||
hash => $hash,
|
|
||||||
};
|
|
||||||
|
|
||||||
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
|
if(defined($TokenData)){
|
||||||
my ($err, $data) = HttpUtils_BlockingGet($param);
|
my $param = {
|
||||||
|
url => $URL,
|
||||||
|
header => {"Content-Type"=>"application/json;charset=UTF-8","Authorization" => "$TokenData->{'token_type'} $TokenData->{'access_token'}"},
|
||||||
|
method => 'GET',
|
||||||
|
timeout => 4,
|
||||||
|
hash => $hash,
|
||||||
|
};
|
||||||
|
|
||||||
if($err ne "")
|
#Log3 $name, 5, 'Blocking GET: ' . Dumper($param);
|
||||||
{
|
my ($err, $data) = HttpUtils_BlockingGet($param);
|
||||||
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: Error while requesting ".$param->{url}." - $err";
|
|
||||||
}
|
if($err ne "")
|
||||||
elsif($data ne "")
|
{
|
||||||
{
|
Log3 $name, 3, "TadoAPI $name" . ": " . "GetGeoById: Error while requesting ".$param->{url}." - $err";
|
||||||
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
}
|
||||||
my $decoded_data = eval { decode_json($data) };
|
elsif($data ne "")
|
||||||
if ($@){
|
{
|
||||||
Log3 $name, 3, "TadoAPI $name" . ": " . "TadoAPI_GetGeoById: decode_json failed, invalid json. error:$@\n";
|
Log3 $name, 5, "url ".$param->{url}." returned: $data";
|
||||||
}else{
|
my $decoded_data = eval { decode_json($data) };
|
||||||
my $setting = $decoded_data->{'geoTrackingEnabled'};
|
if ($@){
|
||||||
readingsBeginUpdate($hash);
|
Log3 $name, 3, "TadoAPI $name" . ": " . "GetGeoById: Decode_json failed, invalid json. error:$@\n" if $@;
|
||||||
readingsBulkUpdate($hash, "GeoLocation_" . $mobileID, $setting);
|
Log3 $name, 3, "TadoAPI $name" . ": " . "GetGeoById: Error in UpdateMobileReadingsCallback, Code: " . $decoded_data->{'errors'}->[0]->{'code'};
|
||||||
readingsEndUpdate( $hash, 1 );
|
$hash->{LastRequest}="error";
|
||||||
return $setting;
|
}else{
|
||||||
|
foreach my $item ( @$decoded_data ){
|
||||||
|
if($item->{'id'} eq $mobileID){
|
||||||
|
return my ($setting, $distance) = TadoAPI_ParseMobileItem($hash, $item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}elsif(defined($item)){
|
||||||
|
Log3 $name, 3, "TadoAPI $name" . ": " . "GetGeoById: parsing item";
|
||||||
|
return my ($setting, $distance) = TadoAPI_ParseMobileItem($hash, $item);
|
||||||
}
|
}
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub TadoAPI_ParseMobileItem(@){
|
||||||
|
my ($hash, $item) = @_;
|
||||||
|
my $name = $hash->{NAME};
|
||||||
|
my $setting = 0;
|
||||||
|
$setting = 1 if $item->{'settings'}->{'geoTrackingEnabled'};
|
||||||
|
my $distance = "-";
|
||||||
|
$distance = $item->{'location'}->{'relativeDistanceFromHomeFence'} if $setting;
|
||||||
|
|
||||||
|
readingsBeginUpdate($hash);
|
||||||
|
readingsBulkUpdate($hash, "GeoTracking_" . $item->{'id'}, $item->{'settings'}->{'geoTrackingEnabled'});
|
||||||
|
if(defined($item->{'location'}->{'atHome'}) && $item->{'location'}->{'atHome'}){
|
||||||
|
# present
|
||||||
|
readingsBulkUpdate($hash, "GeoLocation_" . $item->{'id'}, "present");
|
||||||
|
}elsif(defined($item->{'location'}->{'atHome'})){
|
||||||
|
# away
|
||||||
|
readingsBulkUpdate($hash, "GeoLocation_" . $item->{'id'}, "away");
|
||||||
|
}else{
|
||||||
|
# no state
|
||||||
|
readingsDelete($hash, "GeoLocation_" . $item->{'id'});
|
||||||
|
}
|
||||||
|
readingsBulkUpdate($hash, "GeoDistance_" . $item->{'id'}, $distance) if $setting && $attr{$name}{showPosData};
|
||||||
|
readingsDelete($hash, "GeoDistance_" . $item->{'id'}) if !defined($attr{$name}{showPosData}) || $attr{$name}{showPosData} == 0 || !$setting;
|
||||||
|
readingsEndUpdate( $hash, 1 );
|
||||||
|
|
||||||
|
$hash->{LastRequest}="OK";
|
||||||
|
return ($setting, $distance);
|
||||||
|
}
|
||||||
|
|
||||||
sub TadoAPI_GetMobileDevices(@) {
|
sub TadoAPI_GetMobileDevices(@) {
|
||||||
my ($hash) = @_;
|
my ($hash) = @_;
|
||||||
my $name = $hash->{NAME};
|
my $name = $hash->{NAME};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user