mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-05-04 01:53:19 +00:00
74_AutomowerConnect: some library code rework.
git-svn-id: https://svn.fhem.de/fhem/trunk@29396 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
49a169e8bf
commit
2e11269128
@ -875,6 +875,7 @@ sub getMowerResponse {
|
||||
} else {
|
||||
|
||||
my $result = eval { JSON::XS->new->utf8( not $::unicodeEncoding )->decode( $data ) };
|
||||
|
||||
if ($@) {
|
||||
|
||||
Log3( $name, 2, "$iam - JSON error while request: $@");
|
||||
@ -882,8 +883,10 @@ sub getMowerResponse {
|
||||
} else {
|
||||
|
||||
$hash->{helper}{mowers} = $result->{data};
|
||||
|
||||
my $maxMower = 0;
|
||||
$maxMower = @{$hash->{helper}{mowers}} if ( ref ( $hash->{helper}{mowers} ) eq 'ARRAY' );
|
||||
|
||||
if ($maxMower <= $mowerNumber || $mowerNumber < 0 ) {
|
||||
|
||||
Log3 $name, 2, "$iam wrong mower number $mowerNumber ($maxMower mower available). Change definition of $name.";
|
||||
@ -897,8 +900,53 @@ sub getMowerResponse {
|
||||
$foundMower .= "\n" . $i .' => '. $hash->{helper}{mowers}[$i]{attributes}{system}{name} . ' ' . $hash->{helper}{mowers}[$i]{id};
|
||||
|
||||
}
|
||||
|
||||
$hash->{helper}{foundMower} = $foundMower;
|
||||
Log3 $name, 5, "$iam found $foundMower ";
|
||||
|
||||
processingMowerResponse( $hash );
|
||||
|
||||
# schedule new access token
|
||||
RemoveInternalTimer( $hash, \&getNewAccessToken );
|
||||
InternalTimer( ReadingsVal($name, '.expires', 600)-37, \&getNewAccessToken, $hash, 0 );
|
||||
|
||||
# Websocket initialisieren, schedule ping, reopen
|
||||
RemoveInternalTimer( $hash, \&wsReopen );
|
||||
InternalTimer( gettimeofday() + 1.5, \&wsReopen, $hash, 0 );
|
||||
$hash->{helper}{midnightCycle} = 0;
|
||||
|
||||
return undef;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
readingsSingleUpdate( $hash, 'device_state', "error statuscode $statuscode", 1 );
|
||||
Log3 $name, 1, "$iam \$statuscode >$statuscode<, \$err >$err<, \$param->url $param->{url} \n\$data >$data<";
|
||||
DoTrigger($name, "MOWERAPI ERROR");
|
||||
|
||||
}
|
||||
|
||||
RemoveInternalTimer( $hash, \&APIAuth );
|
||||
InternalTimer( gettimeofday() + $hash->{helper}{retry_interval_getmower}, \&APIAuth, $hash, 0 );
|
||||
Log3 $name, 1, "$iam failed retry in $hash->{helper}{retry_interval_getmower} seconds.";
|
||||
|
||||
return undef;
|
||||
|
||||
}
|
||||
|
||||
#########################
|
||||
sub processingMowerResponse {
|
||||
|
||||
my ( $hash ) = @_;
|
||||
my $name = $hash->{NAME};
|
||||
my $type = $hash->{TYPE};
|
||||
my $iam = "$type $name processingMowerResponse:";
|
||||
my $mowerNumber = $hash->{helper}{mowerNumber};
|
||||
my $foundMower = defined( $hash->{helper}{foundMower} ) ? $hash->{helper}{foundMower} : undef;
|
||||
|
||||
if ( defined ( $hash->{helper}{mower}{id} ) && $hash->{helper}{midnightCycle} ) { # update dataset
|
||||
|
||||
$hash->{helper}{mowerold}{attributes}{metadata}{statusTimestamp} = $hash->{helper}{mower}{attributes}{metadata}{statusTimestamp};
|
||||
@ -937,45 +985,16 @@ sub getMowerResponse {
|
||||
$hash->{helper}{storediff} = $hash->{helper}{mower}{attributes}{metadata}{statusTimestamp} - $hash->{helper}{mowerold}{attributes}{metadata}{statusTimestamp};
|
||||
|
||||
calculateStatistics( $hash ) if ( $hash->{helper}{midnightCycle} );
|
||||
|
||||
# Update readings
|
||||
readingsBeginUpdate($hash);
|
||||
|
||||
readingsBulkUpdateIfChanged($hash, 'api_MowerFound', $foundMower );
|
||||
fillReadings( $hash );
|
||||
readingsBulkUpdate($hash, 'device_state', 'connected' );
|
||||
|
||||
readingsEndUpdate($hash, 1);
|
||||
|
||||
|
||||
# schedule new access token
|
||||
RemoveInternalTimer( $hash, \&getNewAccessToken );
|
||||
InternalTimer( ReadingsVal($name, '.expires', 600)-37, \&getNewAccessToken, $hash, 0 );
|
||||
|
||||
# Websocket initialisieren, schedule ping, reopen
|
||||
RemoveInternalTimer( $hash, \&wsReopen );
|
||||
InternalTimer( gettimeofday() + 1.5, \&wsReopen, $hash, 0 );
|
||||
$hash->{helper}{midnightCycle} = 0;
|
||||
|
||||
return undef;
|
||||
# Update readings
|
||||
readingsBeginUpdate( $hash );
|
||||
|
||||
}
|
||||
readingsBulkUpdateIfChanged( $hash, 'api_MowerFound', $foundMower ) if ( $foundMower );
|
||||
fillReadings( $hash );
|
||||
readingsBulkUpdate( $hash, 'device_state', 'connected' );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
readingsSingleUpdate( $hash, 'device_state', "error statuscode $statuscode", 1 );
|
||||
Log3 $name, 1, "$iam \$statuscode >$statuscode<, \$err >$err<, \$param->url $param->{url} \n\$data >$data<";
|
||||
DoTrigger($name, "MOWERAPI ERROR");
|
||||
|
||||
}
|
||||
|
||||
RemoveInternalTimer( $hash, \&APIAuth );
|
||||
InternalTimer( gettimeofday() + $hash->{helper}{retry_interval_getmower}, \&APIAuth, $hash, 0 );
|
||||
Log3 $name, 1, "$iam failed retry in $hash->{helper}{retry_interval_getmower} seconds.";
|
||||
|
||||
return undef;
|
||||
readingsEndUpdate( $hash, 1 );
|
||||
|
||||
}
|
||||
|
||||
@ -3293,7 +3312,7 @@ sub wsRead {
|
||||
|
||||
}
|
||||
|
||||
autoDstSync( $hash ) if ( AttrVal( $name, "mowerAutoSyncTime", 0 ) );
|
||||
autoDstSync( $hash ) if ( AttrVal( $name, "mowerAutoSyncTime", 0 ) && $hash->{helper}{mower}{attributes}{metadata}{connected} );
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user