2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

49_Arlo.pm: login again after invalid access token error

git-svn-id: https://svn.fhem.de/fhem/trunk@20219 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
maluk 2019-09-21 16:34:16 +00:00
parent 0d41a53e95
commit df8e3406bf

View File

@ -445,7 +445,8 @@ sub Arlo_BlockingRequest($$;$$$$) {
sub Arlo_DefaultCallback($$$) { sub Arlo_DefaultCallback($$$) {
my ($hash, $err, $jsonData) = @_; my ($hash, $err, $jsonData) = @_;
my $name = $modules{$MODULE}{defptr}{"account"}{NAME}; my $account = $modules{$MODULE}{defptr}{"account"};
my $name = $account->{NAME};
if ($err) { if ($err) {
Log3 $name, 2, "Error occured when calling Arlo daemon: $err"; Log3 $name, 2, "Error occured when calling Arlo daemon: $err";
return undef; return undef;
@ -463,6 +464,12 @@ sub Arlo_DefaultCallback($$$) {
if ($origin && $data->{error} eq '2059' && $data->{reason} eq 'Device is offline.') { if ($origin && $data->{error} eq '2059' && $data->{reason} eq 'Device is offline.') {
readingsSingleUpdate($origin, 'state', 'offline', 1) if (ReadingsVal($origin->{NAME}, 'state', '') ne 'offline'); readingsSingleUpdate($origin, 'state', 'offline', 1) if (ReadingsVal($origin->{NAME}, 'state', '') ne 'offline');
$logLevel = 5; $logLevel = 5;
} elsif ($data->{error} eq '1022' && $data->{reason} eq 'Access token is invalid') {
Log3 $name, 3, "Arlo access token was invalid. Reconnect to Arlo.";
if ($hash->{STATE} eq 'active') {
Arlo_Login($account);
}
$logLevel = 5;
} }
} }
Log3 $name, $logLevel, "Arlo call was not successful: $jsonData"; Log3 $name, $logLevel, "Arlo call was not successful: $jsonData";
@ -932,17 +939,17 @@ sub Arlo_DownloadLastVideo($) {
my $length = @recordings; my $length = @recordings;
if ($length > 0) { if ($length > 0) {
my $rec = $recordings[0]; my $rec = $recordings[0];
my $lastVideoTime = $hash->{lastVideoTime}; my $lastVideoTime = $hash->{lastVideoTime};
my $newVideoTime = int($rec->{time}); my $newVideoTime = int($rec->{time});
my $account = $modules{$MODULE}{defptr}{"account"}; my $account = $modules{$MODULE}{defptr}{"account"};
if (!defined($lastVideoTime) || $newVideoTime > $lastVideoTime) { if (!defined($lastVideoTime) || $newVideoTime > $lastVideoTime) {
$hash->{lastVideoTime} = $newVideoTime; $hash->{lastVideoTime} = $newVideoTime;
Log3 $account->{NAME}, 4, "Download new recording $newVideoTime."; Log3 $account->{NAME}, 4, "Download new recording $newVideoTime.";
Arlo_SetReadingAndDownload($account, 'lastVideoThumbnailUrl', $rec->{thumbnail}, $cameraId, '_thumb.jpg', \0); Arlo_SetReadingAndDownload($account, 'lastVideoThumbnailUrl', $rec->{thumbnail}, $cameraId, '_thumb.jpg', \0);
Arlo_SetReadingAndDownload($account, 'lastVideoUrl', $rec->{video}, $cameraId, '.mp4', \1); Arlo_SetReadingAndDownload($account, 'lastVideoUrl', $rec->{video}, $cameraId, '.mp4', \1);
} else { } else {
Log3 $account->{NAME}, 4, "Don't download recording because there is now new recording. Last Video: $lastVideoTime New Video: $newVideoTime"; Log3 $account->{NAME}, 4, "Don't download recording because there is now new recording. Last Video: $lastVideoTime New Video: $newVideoTime";
} }
} }
} }
@ -958,6 +965,8 @@ sub Arlo_Login($) {
if (AttrVal($name, 'disable', 0) == 1) { if (AttrVal($name, 'disable', 0) == 1) {
return; return;
} }
$hash->{STATE} = 'inactive';
my $password = encode_base64($hash->{helper}{password}, ''); my $password = encode_base64($hash->{helper}{password}, '');
my $input = {email => $hash->{helper}{username}, password => $password, EnvSource => 'prod', language => 'de'}; my $input = {email => $hash->{helper}{username}, password => $password, EnvSource => 'prod', language => 'de'};
@ -1103,8 +1112,8 @@ sub Arlo_EventPolling($) {
} }
return; return;
} }
$content = $content . $buf; $content = $content . $buf;
$nfound = select($rout=$rin, undef, undef, 0.1); $nfound = select($rout=$rin, undef, undef, 0.1);
} }
Arlo_ProcessResponse($hash, $content) if ($content ne ''); Arlo_ProcessResponse($hash, $content) if ($content ne '');
if ($hash->{SSE_STATUS} == 299) { if ($hash->{SSE_STATUS} == 299) {
@ -1167,7 +1176,7 @@ sub Arlo_ProcessResponse($$) {
} }
} elsif ($check ne 'event' && $check ne 'Cache' && $check ne 'Conte' && $check ne 'Date:' && $check ne 'Pragm' && $check ne 'Server' } elsif ($check ne 'event' && $check ne 'Cache' && $check ne 'Conte' && $check ne 'Date:' && $check ne 'Pragm' && $check ne 'Server'
&& substr($check, 0, 2) ne 'X-' && $check ne 'trans' && $check ne 'Serve' && $check ne 'Expir' && $check ne 'Stric' && $check ne 'Trans' && substr($check, 0, 2) ne 'X-' && $check ne 'trans' && $check ne 'Serve' && $check ne 'Expir' && $check ne 'Stric' && $check ne 'Trans'
&& $check ne 'Expec' && $check ne 'CF-RA') { && $check ne 'Expec' && $check ne 'CF-RA') {
Log3 $hash->{NAME}, 2, "Invalid Arlo event response: $line"; Log3 $hash->{NAME}, 2, "Invalid Arlo event response: $line";
} }
} }
@ -1220,7 +1229,7 @@ sub Arlo_ProcessEvent($$) {
my $activityState = $props->{activityState}; my $activityState = $props->{activityState};
if ($activityState) { if ($activityState) {
my $camera = $modules{$MODULE}{defptr}{"C$cameraId"}; my $camera = $modules{$MODULE}{defptr}{"C$cameraId"};
my $oldActivityState = ReadingsVal($camera->{NAME}, 'activityState', ''); my $oldActivityState = ReadingsVal($camera->{NAME}, 'activityState', '');
Arlo_SetReading($hash, $cameraId, 'activityState', $activityState); Arlo_SetReading($hash, $cameraId, 'activityState', $activityState);
if ($activityState eq 'startUserStream') { if ($activityState eq 'startUserStream') {
my $streamURL = $props->{streamURL}; my $streamURL = $props->{streamURL};
@ -1230,7 +1239,7 @@ sub Arlo_ProcessEvent($$) {
} elsif ($activityState eq 'userStreamActive') { } elsif ($activityState eq 'userStreamActive') {
InternalTimer(gettimeofday() + 0.5, "Arlo_StartRecordingStep2", $camera); InternalTimer(gettimeofday() + 0.5, "Arlo_StartRecordingStep2", $camera);
} elsif ($activityState eq 'idle' && ($oldActivityState eq 'alertStreamActive' || $oldActivityState eq 'userStreamActive') && AttrVal($name, 'videoDownloadFix', 0) == 1) { } elsif ($activityState eq 'idle' && ($oldActivityState eq 'alertStreamActive' || $oldActivityState eq 'userStreamActive') && AttrVal($name, 'videoDownloadFix', 0) == 1) {
Log3 $name, 4, "Download latest video for $camera->{NAME} in 2 seconds"; Log3 $name, 4, "Download latest video for $camera->{NAME} in 2 seconds";
InternalTimer(gettimeofday() + 2, "Arlo_DownloadLastVideo", $camera); InternalTimer(gettimeofday() + 2, "Arlo_DownloadLastVideo", $camera);
} }
} }