2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-28 02:44:53 +00:00

38_netatmo: changed connection check mechanism

git-svn-id: https://svn.fhem.de/fhem/trunk@17588 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
moises 2018-10-22 08:43:51 +00:00
parent ad7875c2cd
commit 0ad25b338a
2 changed files with 33 additions and 14 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it. # Do not insert empty lines here, update check depends on it.
- bugfix: 38_netatmo: changed connection check mechanism
- new: 98_SmarterCoffee: new modul for SmarterCoffee devices - new: 98_SmarterCoffee: new modul for SmarterCoffee devices
- bugfix: 74_XiaomiBTLESens: fix humidity bug - bugfix: 74_XiaomiBTLESens: fix humidity bug
- feature: 49_SSCam: direct help for attributes, new get versionNotes command - feature: 49_SSCam: direct help for attributes, new get versionNotes command

View File

@ -11,12 +11,13 @@
# #
# #
############################################################################## ##############################################################################
# Release 21 / 2018-10-06 # Release 22 / 2018-10-22
package main; package main;
use strict; use strict;
use warnings; use warnings;
no warnings qw(redefine);
use Encode qw(encode_utf8 decode_utf8); use Encode qw(encode_utf8 decode_utf8);
use JSON; use JSON;
@ -896,28 +897,37 @@ netatmo_refreshAppTokenTimer($)
} }
sub sub
netatmo_checkConnection($) netatmo_checkConnection($;$)
{ {
my ($hash) = @_; my ($hash,$forcecheck) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
return undef if($hash->{network} eq "ok"); return undef if(!$forcecheck && $hash->{network} eq "ok");
return undef if(!defined($hash->{access_token})); return undef if(!defined($hash->{access_token}));
Log3 $name, 3, "$name: refreshing connection information"; Log3 $name, 3, "$name: refreshing connection information";
my $json = '{"limit":2,"divider":3,"zoom":18,"lat_ne":0.1,"lon_ne":-0.1,"lat_sw":0.1,"lon_sw":-0.1,"date_end":"last","quality":1}';
HttpUtils_NonblockingGet({ HttpUtils_NonblockingGet({
url => "https://".$hash->{helper}{apiserver}."/api/getpublicmeasures", url => "https://".$hash->{helper}{apiserver}."/api/getuser",
method => "POST", method => "POST",
timeout => 30, timeout => 30,
header => "Content-Type: application/json\r\nAuthorization: Bearer ".$hash->{access_token}, header => "Content-Type: application/json\r\nAuthorization: Bearer ".$hash->{access_token},
hash => $hash, hash => $hash,
data => $json,
callback => \&netatmo_parseConnection, callback => \&netatmo_parseConnection,
}); });
return undef; return undef;
# my $json = '{"limit":2,"divider":3,"zoom":18,"lat_ne":0.1,"lon_ne":-0.1,"lat_sw":0.1,"lon_sw":-0.1,"date_end":"last","quality":1}';
#
# HttpUtils_NonblockingGet({
# url => "https://".$hash->{helper}{apiserver}."/api/getpublicmeasures",
# method => "POST",
# timeout => 30,
# header => "Content-Type: application/json\r\nAuthorization: Bearer ".$hash->{access_token},
# hash => $hash,
# data => $json,
# callback => \&netatmo_parseConnection,
# });
# return undef;
} }
sub sub
@ -947,12 +957,13 @@ netatmo_parseConnection($$$)
$hash->{status} = "timeout"; $hash->{status} = "timeout";
$hash->{network} = "disconnected"; $hash->{network} = "disconnected";
} }
readingsSingleUpdate($hash, "active", "error", 1);
return undef; return undef;
} elsif( $data ) { } elsif( $data ) {
$data =~ s/\n//g; $data =~ s/\n//g;
if( $data !~ m/^{.*}$/ ) { if( $data !~ m/^{.*}$/ ) {
Log3 $name, 2, "$name: invalid json on connection check"; Log3 $name, 2, "$name: invalid json on connection check";
Log3 $name, 4, "$name: ".$data;
return undef; return undef;
} }
my $json = eval { JSON->new->utf8(0)->decode($data) }; my $json = eval { JSON->new->utf8(0)->decode($data) };
@ -966,6 +977,7 @@ netatmo_parseConnection($$$)
} else { } else {
Log3 $name, 4, "$name: connection check: \n".$data; Log3 $name, 4, "$name: connection check: \n".$data;
$hash->{network} = "ok" if($json->{status} eq "ok"); $hash->{network} = "ok" if($json->{status} eq "ok");
readingsSingleUpdate($hash, "active", "ok", 1) if($json->{status} eq "ok");
} }
} }
return undef; return undef;
@ -1896,7 +1908,7 @@ netatmo_pollHeatingHome($@)
# url => "https://".$iohash->{helper}{apiserver}."/api/gethomedata", # url => "https://".$iohash->{helper}{apiserver}."/api/gethomedata",
# data => \%data, # data => \%data,
HttpUtils_NonblockingGet({ HttpUtils_NonblockingGet({
url => "https://my.netatmo.com/syncapi/v1/gethomestatus", url => "https://".$iohash->{helper}{apiserver}."/syncapi/v1/gethomestatus",
timeout => 60, timeout => 60,
noshutdown => 1, noshutdown => 1,
header => "Content-Type: application/json;charset=utf-8\r\nAuthorization: Bearer ".$iohash->{access_token}, header => "Content-Type: application/json;charset=utf-8\r\nAuthorization: Bearer ".$iohash->{access_token},
@ -2701,7 +2713,7 @@ netatmo_dispatch($$$)
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
InternalTimer(gettimeofday()+300, "netatmo_poll", $hash); InternalTimer(gettimeofday()+300, "netatmo_poll", $hash);
Log3 $name, 2, "$name: invalid json detected"; Log3 $name, 2, "$name: invalid json detected";
Log3 $name, 5, "$name: $data"; Log3 $name, 4, "$name: $data";
$hash->{status} = "error"; $hash->{status} = "error";
$hash->{network} = "ok" if($hash->{SUBTYPE} eq "ACCOUNT"); $hash->{network} = "ok" if($hash->{SUBTYPE} eq "ACCOUNT");
$hash->{IODev}->{network} = "ok" if($hash->{SUBTYPE} ne "ACCOUNT"); $hash->{IODev}->{network} = "ok" if($hash->{SUBTYPE} ne "ACCOUNT");
@ -3511,7 +3523,7 @@ netatmo_parseReadings($$;$)
$hash->{helper}{NEXT_POLL} = $nextdata; $hash->{helper}{NEXT_POLL} = $nextdata;
Log3 $name, 3, "$name: next extended dynamic update ($requested) at ".FmtDateTime($nextdata); Log3 $name, 3, "$name: next extended dynamic update ($requested) at ".FmtDateTime($nextdata);
} else { } else {
Log3 $name, 2, "$name: invalid time for dynamic update ($requested): ".FmtDateTime($nextdata); Log3 $name, 3, "$name: invalid time for dynamic update ($requested): ".FmtDateTime($nextdata);
} }
} }
} elsif(defined($last_time) && int($last_time) > 0) { } elsif(defined($last_time) && int($last_time) > 0) {
@ -5774,7 +5786,7 @@ netatmo_Get($$@)
return undef; return undef;
} }
} elsif( $hash->{SUBTYPE} eq "ACCOUNT" ) { } elsif( $hash->{SUBTYPE} eq "ACCOUNT" ) {
$list = "update:noArg devices:noArg homes:noArg thermostats:noArg homecoachs:noArg public showAccount:noArg"; $list = "update:noArg devices:noArg homes:noArg thermostats:noArg homecoachs:noArg public showAccount:noArg"; #checkConnection:noArg";
if( $cmd eq "update" ) { if( $cmd eq "update" ) {
netatmo_poll($hash); netatmo_poll($hash);
@ -5795,6 +5807,12 @@ netatmo_Get($$@)
return "username: $username\npassword: $password"; return "username: $username\npassword: $password";
} }
if( $cmd eq 'checkConnection' )
{
netatmo_checkConnection($hash,1);
return undef;
}
if( $cmd eq "devices" ) { if( $cmd eq "devices" ) {
my $devices = netatmo_getDevices($hash,1); my $devices = netatmo_getDevices($hash,1);
my $ret; my $ret;