2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 18:59:33 +00:00

AutomowerConnect: improved error handling, fix missig password bug

git-svn-id: https://svn.fhem.de/fhem/trunk@27131 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
Ellert 2023-01-27 17:14:19 +00:00
parent 7974eefd1b
commit 1d22821d04
2 changed files with 208 additions and 183 deletions

View File

@ -1,5 +1,6 @@
# 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.
- bugfix: 74_AutomowerConnect: improved error handling, fix missig password bug
- feature: 74_AutomowerConnect: add disabledForIntervals, Internal MODEL
- feature: 75_AutomowerConnectDevice: add disabledForIntervals, Internal MODEL
- bugfix: 98_WeekdayTimer: !$we handling

View File

@ -97,6 +97,7 @@ sub Initialize() {
$hash->{AttrFn} = \&Attr;
$hash->{AttrList} = "interval " .
"disable:1,0 " .
"debug:1,0 " .
"disabledForIntervals " .
"mapImagePath " .
"mapImageWidthHeight " .
@ -217,13 +218,21 @@ EOF
$hash->{VERSION}=$1;
}
AddExtension( $name, \&GetMap, "$type/$name/map" );
if( $hash->{helper}->{passObj}->getReadPassword($name) ) {
RemoveInternalTimer($hash);
InternalTimer( gettimeofday() + 2, \&APIAuth, $hash, 1);
InternalTimer( gettimeofday() + 30, \&readMap, $hash, 0);
AddExtension( $name, \&GetMap, "$type/$name/map" );
readingsSingleUpdate( $hash, 'state', 'defined', 1 );
} else {
readingsSingleUpdate( $hash, 'state', 'defined - client_secret missing', 1 );
}
return undef;
}
@ -278,6 +287,7 @@ sub APIAuth {
});
}
} else {
RemoveInternalTimer( $hash, \&APIAuth);
InternalTimer(gettimeofday() + 20, \&APIAuth, $hash, 0);
@ -291,28 +301,21 @@ sub APIAuthResponse {
my $hash = $param->{hash};
my $name = $hash->{NAME};
my $type = $hash->{TYPE};
my $statuscode = $param->{code};
my $interval = $hash->{helper}{interval};
my $iam = "$type $name APIAuthResponse:";
if($err) {
Log3 $name, 1, "\ndebug $iam \n\$statuscode [$statuscode]\n\$err [$err],\n \$data [$data] \n\$param->url $param->{url}" if ( AttrVal($name, 'debug', '') );
Log3 $name, 2, "$iam error [$err], data [$data] for url $param->{url}";
readingsSingleUpdate( $hash, 'state', 'error', 1 );
} elsif( $data ) {
if( !$err && $statuscode == 200 && $data) {
my $result = eval { decode_json($data) };
if ($@) {
Log3 $name, 2, "$iam JSON error [ $@ ]";
readingsSingleUpdate( $hash, 'state', 'error', 1 );
return undef;
}
if ($result->{errors} || $result->{message}) {
Log3 $name, 2, "$iam error [ $data ]";
readingsSingleUpdate( $hash, 'state', 'error', 1 );
Log3 $name, 2, "$iam JSON error [ $@ ]";
readingsSingleUpdate( $hash, 'state', 'error JSON', 1 );
} else {
Log3 $name, 5, "$iam $data";
$hash->{helper}->{auth} = $result;
@ -338,9 +341,20 @@ sub APIAuthResponse {
readingsEndUpdate($hash, 1);
getMower( $hash );
return undef;
}
} else {
readingsSingleUpdate( $hash, 'state', "error statuscode $statuscode", 1 );
Log3 $name, 1, "\n$iam\n\$statuscode [$statuscode]\n\$err [$err],\n\$data [$data]\n\$param->url $param->{url}";
}
return undef;
RemoveInternalTimer( $hash, \&APIAuth );
InternalTimer( gettimeofday() + $interval, \&APIAuth, $hash, 0 );
return undef;
}
@ -382,36 +396,34 @@ sub getMowerResponse {
my $hash = $param->{hash};
my $name = $hash->{NAME};
my $type = $hash->{TYPE};
my $statuscode = $param->{code};
my $interval = $hash->{helper}{interval};
my $iam = "$type $name getMowerResponse:";
my $mowerNumber = $hash->{helper}{mowerNumber};
if($err ne "") {
Log3 $name, 1, "\ndebug $iam \n\$statuscode [$statuscode]\n\$err [$err],\n \$data [$data] \n\$param->url $param->{url}" if ( AttrVal($name, 'debug', '') );
Log3 $name, 2, "$iam request error ".$param->{url}." - $err";
if( !$err && $statuscode == 200 && $data) {
} elsif ( $data ) {
if ( $data eq "[]" ) {
Log3 $name, 2, "$iam Please register an automower first";
Log3 $name, 2, "$iam no mower data present";
} else {
#Log3 $name, 5, $data;
my $result = eval { decode_json($data) };
if ($@) {
Log3( $name, 2, "$iam - JSON error while request: $@");
return undef;
}
if ($result->{message}) {
Log3( $name, 2, "$iam - API error while request: $data");
return undef;
}
} else {
$hash->{helper}{mowers} = $result->{data};
my $maxMower = @{$hash->{helper}{mowers}};
my $maxMower = 0;
$maxMower = @{$hash->{helper}{mowers}} if ( ref ( $hash->{helper}{mowers} ) eq 'ARRAY' );
if ($maxMower <= $mowerNumber || $mowerNumber < 0 ) {
Log3 $name, 2, "$iam mower number $mowerNumber not available. Change definition of $name.";
Log3 $name, 2, "$iam wrong mower number $mowerNumber ($maxMower mower available). Change definition of $name.";
return undef;
}
@ -443,6 +455,7 @@ sub getMowerResponse {
$hash->{helper}{mower} = dclone( $hash->{helper}{mowers}[$mowerNumber] );
my $storediff = $hash->{helper}{mower}{attributes}{metadata}{statusTimestamp} - $hash->{helper}{mowerold}{attributes}{metadata}{statusTimestamp};
if ($storediff) {
AlignArray( $hash );
FW_detailFn_Update ($hash) if (AttrVal($name,'showMap',1));
@ -526,11 +539,19 @@ sub getMowerResponse {
RemoveInternalTimer( $hash, \&APIAuth );
InternalTimer( gettimeofday() + $interval, \&APIAuth, $hash, 0 );
return undef;
}
}
} else {
readingsSingleUpdate( $hash, 'state', "error statuscode $statuscode", 1 );
Log3 $name, 1, "\ndebug $iam \n\$statuscode [$statuscode]\n\$err [$err],\n \$data [$data] \n\$param->url $param->{url}";
}
RemoveInternalTimer( $hash, \&APIAuth );
InternalTimer( gettimeofday() + $interval, \&APIAuth, $hash, 0 );
return undef;
}
@ -607,44 +628,47 @@ sub CMDResponse {
my $hash = $param->{hash};
my $name = $hash->{NAME};
my $type = $hash->{TYPE};
my $statuscode = $param->{code};
my $interval = $hash->{helper}{interval};
my $iam = "$type $name CMDResponse:";
if($err ne "") {
readingsSingleUpdate( $hash, 'state', 'error', 1 );
Log3 $name, 2, "$iam error while requesting ".$param->{url}." - $err";
Log3 $name, 1, "\ndebug $iam \n\$statuscode [$statuscode]\n\$err [$err],\n \$data [$data] \n\$param->url $param->{url}" if ( AttrVal($name, 'debug', '') );
} elsif($data ne "") {
if( !$err && $statuscode == 202 && $data ) {
my $result = eval { decode_json($data) };
if ($@) {
Log3( $name, 2, "$iam - JSON error while request: $@");
return;
}
} else {
$hash->{helper}{CMDResponse} = $result;
if ($result->{message}) {
readingsSingleUpdate( $hash, 'state', 'error', 1 );
Log3 $name, 2, "$iam" . $data;
$hash->{helper}->{mower_commandStatus} = $result->{message};
if ($result->{data}) {
} elsif ($result->{errors}) {
Log3 $name, 2, "$iam" . $data;
readingsSingleUpdate( $hash, 'state', 'error', 1 );
$hash->{helper}->{mower_commandStatus} = 'ERROR - '. $result->{errors}[0]{title};
} elsif ($result->{data}) {
Log3 $name, 5, $data;
if ( ref ($result->{data}) eq 'ARRAY') {
$hash->{helper}->{mower_commandStatus} = 'OK - '. $result->{data}[0]{type};
} else {
$hash->{helper}->{mower_commandStatus} = 'OK - '. $result->{data}{type};
}
}
readingsSingleUpdate($hash, 'mower_commandStatus', $hash->{helper}->{mower_commandStatus} ,1);
return undef;
}
}
}
readingsSingleUpdate($hash, 'mower_commandStatus', "ERROR statuscode $statuscode" ,1);
Log3 $name, 2, "\n$iam \n\$statuscode [$statuscode]\n\$err [$err],\n\$data [$data]\n\$param->url $param->{url}";
return undef;
}
#########################
@ -652,7 +676,7 @@ sub Get {
my ($hash,@val) = @_;
my $type = $hash->{TYPE};
return "$type $hash->{NAME} Get: needs at least an argument" if ( @val < 2 );
return "$type $hash->{NAME} Get: needs at least one argument" if ( @val < 2 );
my ($name,$setName,$setVal,$setVal2,$setVal3) = @val;
my $iam = "$type $name Get:";
@ -671,7 +695,7 @@ sub Set {
my ($hash,@val) = @_;
my $type = $hash->{TYPE};
return "$type $hash->{NAME} Set: needs at least an argument" if ( @val < 2 );
return "$type $hash->{NAME} Set: needs at least one argument" if ( @val < 2 );
my ($name,$setName,$setVal,$setVal2,$setVal3) = @val;
my $iam = "$type $name Set:";
@ -705,6 +729,8 @@ sub Set {
my ($passResp, $passErr) = $hash->{helper}->{passObj}->setStorePassword($name, $setVal);
Log3 $name, 1, "$iam error: $passErr" if ($passErr);
return "$iam $passErr" if( $passErr );
RemoveInternalTimer($hash, \&APIAuth);
APIAuth($hash);
return undef;
}
@ -721,7 +747,6 @@ sub Set {
}
} elsif ( !IsDisabled($name) && $setName eq 'getNewAccessToken' ) {
if ( $setVal ) {
readingsBeginUpdate($hash);
readingsBulkUpdateIfChanged( $hash, '.access_token', '', 0 );
@ -732,7 +757,6 @@ sub Set {
RemoveInternalTimer($hash, \&APIAuth);
APIAuth($hash);
return undef;
}
} elsif (ReadingsVal( $name, 'state', 'defined' ) !~ /defined|initialized|authentification|authenticated|update/ && $setName =~ /ParkUntilFurtherNotice|ParkUntilNextSchedule|Pause|ResumeSchedule|sendScheduleFromAttributeToMower/) {
sendCMD($hash,$setName);