add trigger for password setter

geändert:       lib/FHEM/backupToStorage.pm
This commit is contained in:
Marko Oldenburg 2020-06-17 16:08:05 +02:00
parent bc18022177
commit 87c2d95a20
2 changed files with 58 additions and 39 deletions

View File

@ -1,2 +1,2 @@
UPD 2020-06-17_15:03:50 3368 FHEM/98_backupToStorage.pm UPD 2020-06-17_15:03:50 3368 FHEM/98_backupToStorage.pm
UPD 2020-06-17_15:39:04 10502 lib/FHEM/backupToStorage.pm UPD 2020-06-17_16:07:42 10965 lib/FHEM/backupToStorage.pm

View File

@ -77,7 +77,7 @@ sub Define {
my $name = shift @$aArg; my $name = shift @$aArg;
$hash->{VERSION} = version->parse($VERSION)->normal; $hash->{VERSION} = version->parse($VERSION)->normal;
$hash->{NOTIFYDEV} = 'global'; $hash->{NOTIFYDEV} = 'global,' . $name;
Log3( $name, 3, "backupToStorage ($name) - defined" ); Log3( $name, 3, "backupToStorage ($name) - defined" );
@ -137,26 +137,39 @@ sub Notify {
CheckAttributsForCredentials($hash) CheckAttributsForCredentials($hash)
if ( if (
( (
grep m{^DELETEATTR.$name.(bTS_Host|bTS_User)$}xms, (
@{$events} (
or grep m{^ATTR.$name.(bTS_Host|bTS_User).\S+$}xms, grep m{^DELETEATTR.$name.(bTS_Host|bTS_User)$}xms,
@{$events} @{$events}
) or grep m{^ATTR.$name.(bTS_Host|bTS_User).\S+$}xms,
&& $devname eq 'global' @{$events}
&& $init_done )
&& $devname eq 'global'
)
|| (
(
$devname eq $name && grep m{^password:.(add|remove)$}xms,
@{$events}
)
)
)
&& $init_done
); );
readingsSingleUpdate( $hash, 'state', readingsSingleUpdate(
( $hash, 'state',
(AttrVal( $name, 'bTS_Host', 'none' ) eq 'none' (
|| AttrVal( $name, 'bTS_User', 'none' ) eq 'none' (
|| !defined( ReadPassword( $hash, $name ) ) ) AttrVal( $name, 'bTS_Host', 'none' ) eq 'none'
? 'please set storage account credentials first' || AttrVal( $name, 'bTS_User', 'none' ) eq 'none'
: 'ready' || !defined( ReadPassword( $hash, $name ) )
) )
, 1 ? 'please set storage account credentials first'
) : 'ready'
),
1
)
if ( if (
( (
grep m{^DEFINED.$name$}xms, grep m{^DEFINED.$name$}xms,
@ -218,11 +231,12 @@ sub PushToStorage {
my $hash = shift; my $hash = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
Log3 $name, 4, "backupToStorage ($name) - push to storage function"; Log3 $name, 4, "backupToStorage ($name) - push to storage function";
if ( AttrVal( $name, 'bTSType', 'Nextcloud' ) eq 'Nextcloud' ) { if ( AttrVal( $name, 'bTSType', 'Nextcloud' ) eq 'Nextcloud' ) {
Log3 $name, 4, "backupToStorage ($name) - push to storage function: Nextcloud detected"; Log3 $name, 4,
"backupToStorage ($name) - push to storage function: Nextcloud detected";
ncUpload( $hash, ReadingsVal( $name, 'fhemBackupFile', 'none' ) ); ncUpload( $hash, ReadingsVal( $name, 'fhemBackupFile', 'none' ) );
} }
@ -234,7 +248,7 @@ sub ncUpload {
my $backupFile = shift; my $backupFile = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
Log3 $name, 4, "backupToStorage ($name) - nextcloud upload function"; Log3 $name, 4, "backupToStorage ($name) - nextcloud upload function";
open FD, '<', open FD, '<',
@ -272,7 +286,7 @@ sub ncUpload {
user => $ncUser, user => $ncUser,
pwd => $ncPass, pwd => $ncPass,
callback => \&FHEM::backup::ncUploadCb, callback => \&FHEM::backup::ncUploadCb,
}; };
HttpUtils_NonblockingGet($param); HttpUtils_NonblockingGet($param);
$hash->{helper}->{HttpUtilsParam} = $param; $hash->{helper}->{HttpUtilsParam} = $param;
@ -281,12 +295,12 @@ sub ncUpload {
} }
sub ncUploadCb { sub ncUploadCb {
my $param = shift; my $param = shift;
my $err = shift; my $err = shift;
my $data = shift; my $data = shift;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
Log( 1, 'backup URL: ' . $param->{url} ); Log( 1, 'backup URL: ' . $param->{url} );
Log( 1, 'backup User: ' . $param->{user} ); Log( 1, 'backup User: ' . $param->{user} );
@ -330,6 +344,8 @@ sub StorePassword {
} }
my $err = setKeyValue( $index, $enc_pwd ); my $err = setKeyValue( $index, $enc_pwd );
DoTrigger( $name, 'password add' );
return "error while saving the password - $err" if ( defined($err) ); return "error while saving the password - $err" if ( defined($err) );
return "password successfully saved"; return "password successfully saved";
@ -382,7 +398,10 @@ sub ReadPassword {
sub DeletePassword { sub DeletePassword {
my $hash = shift; my $hash = shift;
setKeyValue( $hash->{TYPE} . "_" . $hash->{NAME} . "_passwd", undef ); my $name = $hash->{NAME};
setKeyValue( $hash->{TYPE} . "_" . $name . "_passwd", undef );
DoTrigger( $name, 'password remove' );
return; return;
} }
@ -398,13 +417,13 @@ sub CheckAttributsForCredentials {
my $ncPath = AttrVal( $name, 'bTS_Path', 'none' ); my $ncPath = AttrVal( $name, 'bTS_Path', 'none' );
my $status = 'ready'; my $status = 'ready';
$status = ($status eq 'ready' $status = (
&& $ncUser eq 'none' ? 'no user credential attribut' $status eq 'ready' && $ncUser eq 'none' ? 'no user credential attribut'
: $status eq 'ready' : $status eq 'ready'
&& $ncHost eq 'none' ? 'no host credential attribut' && $ncHost eq 'none' ? 'no host credential attribut'
: $status eq 'ready' : $status eq 'ready' && !defined($ncPass) ? 'no password set'
&& !defined($ncPass) ? 'no password set' : $status
: $status); );
return readingsSingleUpdate( $hash, 'state', $status, 1 ); return readingsSingleUpdate( $hash, 'state', $status, 1 );
} }