7 Commits

Author SHA1 Message Date
bdf9052535 fix state disable problem, see Forum #1106857 2021-01-04 14:57:08 +01:00
6c715e9b5d add support for synology station 2021-01-04 14:06:22 +01:00
48758f31ab change output string 2020-11-09 08:15:01 +01:00
631be8174f change notify log output 2020-11-09 08:11:54 +01:00
6d01e89d5b fix typo 2020-11-09 08:07:30 +01:00
2692c1bceb set output in double quots 2020-11-09 08:00:08 +01:00
a07173e5a9 add hooks 2020-11-09 06:44:28 +01:00
4 changed files with 124 additions and 73 deletions

View File

@ -57,7 +57,7 @@ sub backupToStorage_Initialize {
. 'bTS_User '
. 'bTS_Path '
. 'bTS_Proto:http '
. 'bTS_Type:Nextcloud '
. 'bTS_Type:Nextcloud,SynologyFileStation '
. 'disable:1 '
. 'disabledForIntervals';
$hash->{parseParams} = 1;

View File

@ -1,2 +1,2 @@
UPD 2020-11-06_11:03:04 6242 FHEM/98_backupToStorage.pm
UPD 2020-11-06_11:03:07 18956 lib/FHEM/backupToStorage.pm
UPD 2021-01-04_14:37:28 6262 FHEM/98_backupToStorage.pm
UPD 2021-01-04_14:53:20 19356 lib/FHEM/backupToStorage.pm

39
hooks/pre-commit Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/perl -w
use File::Basename;
use POSIX qw(strftime);
use strict;
my @filenames = ( 'FHEM/98_backupToStorage.pm',
'lib/FHEM/backupToStorage.pm',
);
my $controlsfile = 'controls_backupToStorage.txt';
open(FH, ">$controlsfile") || return("Can't open $controlsfile: $!");
for my $filename (@filenames) {
my @statOutput = stat($filename);
if (scalar @statOutput != 13) {
printf 'error: stat has unexpected return value for ' . $filename . "\n";
next;
}
my $mtime = $statOutput[9];
my $date = POSIX::strftime("%Y-%m-%d", localtime($mtime));
my $time = POSIX::strftime("%H:%M:%S", localtime($mtime));
my $filetime = $date."_".$time;
my $filesize = $statOutput[7];
printf FH 'UPD ' . $filetime . ' ' . $filesize . ' ' .$filename . "\n";
}
close(FH);
system("git add $controlsfile");
print 'Create controls File succesfully' . "\n";
exit 0;

View File

@ -150,10 +150,10 @@ sub Define {
return $@ unless ( FHEM::Meta::SetInternals($hash) );
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
return 'only one backupToStorage instance allowed'
return qq{only one backupToStorage instance allowed}
if ( devspec2array('TYPE=backupToStorage') > 1 )
; # es wird geprüft ob bereits eine Instanz unseres Modules existiert,wenn ja wird abgebrochen
return 'too few parameters: define <name> backupToStorage'
return qq{too few parameters: define <name> backupToStorage}
if ( scalar( @{$aArg} ) != 2 );
my $name = shift @$aArg;
@ -161,7 +161,7 @@ sub Define {
$hash->{NOTIFYDEV} = 'global,' . $name;
$hash->{STORAGETYPE} = AttrVal( $name, 'bTS_Type', 'Nextcloud' );
Log3( $name, 3, "backupToStorage ($name) - defined" );
Log3( $name, 3, qq{backupToStorage ($name) - defined} );
return;
}
@ -170,7 +170,7 @@ sub Undef {
my $hash = shift;
my $name = shift;
Log3( $name, 3, "backupToStorage ($name) - delete device $name" );
Log3( $name, 3, qq{backupToStorage ($name) - delete device $name} );
return;
}
@ -223,12 +223,11 @@ sub Notify {
Log3( $name, 4,
"backupToStorage ($name) - Devname: "
. $devname
. " Name: "
. $name
. " Notify: "
. Dumper $events); # mit Dumper
qq{backupToStorage ($name) -
Devname: $devname
Name: $name
Notify: } . Dumper $events
); # mit Dumper
PushToStorage($hash)
if ( ( grep m{^backup.done(.+)?$}xms, @{$events} )
@ -294,27 +293,27 @@ sub Set {
my $name = shift @$aArg;
my $cmd = shift @$aArg
// return qq{"set $name" needs at least one argument};
// return qq{set "$name" needs at least one argument};
if ( lc $cmd eq 'addpassword' ) {
return "please set Attribut bTS_User first"
return qq{please set Attribut bTS_User first}
if ( AttrVal( $name, 'bTS_User', 'none' ) eq 'none' );
return "usage: $cmd <password>" if ( scalar( @{$aArg} ) != 1 );
return qq{usage: "$cmd" <password>} if ( scalar( @{$aArg} ) != 1 );
StorePassword( $hash, $name, $aArg->[0] );
}
elsif ( lc $cmd eq 'deletepassword' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
return qq{usage: $cmd} if ( scalar( @{$aArg} ) != 0 );
DeletePassword($hash);
}
elsif ( lc $cmd eq 'active' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
return qq{usage: $cmd} if ( scalar( @{$aArg} ) != 0 );
readingsSingleUpdate( $hash, 'state', 'ready', 1 );
}
elsif ( lc $cmd eq 'inactive' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
return qq{usage: $cmd} if ( scalar( @{$aArg} ) != 0 );
readingsSingleUpdate( $hash, 'state', $cmd, 1 );
}
@ -326,7 +325,7 @@ sub Set {
: ' addpassword'
);
return 'Unknown argument ' . $cmd . ', choose one of ' . $list;
return qq{Unknown argument "$cmd", choose one of $list};
}
return;
@ -349,16 +348,22 @@ sub Attr {
return
'check disabledForIntervals Syntax HH:MM-HH:MM or HH:MM-HH:MM HH:MM-HH:MM ...'
if ( $attrVal !~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/ );
Log3( $name, 3, "backupToStorage ($name) - disabledForIntervals" );
Log3( $name, 3, qq{backupToStorage ($name) - disabledForIntervals} );
}
elsif ( $attrName eq 'disable' ) {
Log3( $name, 3, "backupToStorage ($name) - disabled" );
Log3( $name, 3, qq{backupToStorage ($name) - disabled} );
}
}
InternalTimer( gettimeofday() + 1,
'FHEM::backupToStorage::_CheckIsDisabledAfterSetAttr', $hash, 0 );
}
elsif ( $attrName eq 'bTS_Type' ) {
InternalTimer( gettimeofday() + 1,
sub { $hash->{STORAGETYPE} = AttrVal($name,'bTS_Type','Nextcloud'); }, $hash, 0 );
}
return;
}
sub _CheckIsDisabledAfterSetAttr {
@ -366,10 +371,10 @@ sub _CheckIsDisabledAfterSetAttr {
my $name = $hash->{NAME};
my $state = ( IsDisabled($name)
? 'disabled'
? 'inactive'
: 'ready' );
Log3( $name, 3, "backupToStorage ($name) - _CheckIsDisabledAfterSetAttr" );
Log3( $name, 3, qq{backupToStorage ($name) - _CheckIsDisabledAfterSetAttr} );
readingsSingleUpdate($hash, 'state', $state, 1)
if ( ReadingsVal($name, 'state', 'ready' ) ne $state );
@ -392,58 +397,65 @@ sub PushToStorage {
my $name = $hash->{NAME};
Log3( $name, 4, "backupToStorage ($name) - push to storage function" );
Log3( $name, 4, qq{backupToStorage ($name) - push to storage function} );
return
if ( ReadingsAge($name,'fhemBackupFile',1) > 180 );
Log3( $name, 4, "backupToStorage ($name) - after readings age return" );
Log3( $name, 4, qq{backupToStorage ($name) - after readings age return} );
require "SubProcess.pm";
my $subprocess = SubProcess->new( { onRun => \&FileUpload } );
if ( $hash->{STORAGETYPE} eq 'SynologyFileStation' ) {
}
else {
require "SubProcess.pm";
my $subprocess = SubProcess->new( { onRun => \&FileUpload } );
my $backupFile = ReadingsVal( $name, 'fhemBackupFile', 'none' );
my $backupFile = ReadingsVal( $name, 'fhemBackupFile', 'none' );
my @fileNameAtStorage_array = split( '/', $backupFile );
my $fileNameAtStorage = $fileNameAtStorage_array[$#fileNameAtStorage_array];
my @fileNameAtStorage_array = split( '/', $backupFile );
my $fileNameAtStorage = $fileNameAtStorage_array[$#fileNameAtStorage_array];
$subprocess->{curl} = qx(which curl);
chomp($subprocess->{curl});
$subprocess->{type} = $hash->{STORAGETYPE};
$subprocess->{host} = AttrVal( $name, 'bTS_Host', '' );
$subprocess->{user} = AttrVal( $name, 'bTS_User', '' );
$subprocess->{pass} = ReadPassword( $hash, $name );
$subprocess->{path} = AttrVal( $name, 'bTS_Path', '' );
$subprocess->{backupfile} = $backupFile;
$subprocess->{fileNameAtStorage} = $fileNameAtStorage;
$subprocess->{proto} = AttrVal( $name, 'bTS_Proto', 'https' );
$subprocess->{loglevel} = AttrVal( $name, 'verbose', 3 );
$subprocess->{curl} = qx(which curl);
chomp($subprocess->{curl});
$subprocess->{type} = $hash->{STORAGETYPE};
$subprocess->{host} = AttrVal( $name, 'bTS_Host', '' );
$subprocess->{user} = AttrVal( $name, 'bTS_User', '' );
$subprocess->{pass} = ReadPassword( $hash, $name );
$subprocess->{path} = AttrVal( $name, 'bTS_Path', '' );
$subprocess->{backupfile} = $backupFile;
$subprocess->{fileNameAtStorage} = $fileNameAtStorage;
$subprocess->{proto} = AttrVal( $name, 'bTS_Proto', 'https' );
$subprocess->{loglevel} = AttrVal( $name, 'verbose', 3 );
my $pid = $subprocess->run();
my $pid = $subprocess->run();
readingsSingleUpdate( $hash, 'state', ' file upload in progress', 1 );
readingsSingleUpdate( $hash, 'state', ' file upload in progress', 1 );
if ( !defined($pid) ) {
Log3( $name, 1,
"backupToStorage ($name) - Cannot execute command asynchronously" );
if ( !defined($pid) ) {
Log3( $name, 1,
qq{backupToStorage ($name) - Cannot execute command asynchronously} );
CleanSubprocess($hash);
readingsSingleUpdate( $hash, 'state',
'Cannot execute command asynchronously', 1 );
return undef;
CleanSubprocess($hash);
readingsSingleUpdate( $hash, 'state',
'Cannot execute command asynchronously', 1 );
return undef;
}
Log3( $name, 4,
qq{backupToStorage ($name) - execute command asynchronously (PID="$pid")}
);
$hash->{".fhem"}{subprocess} = $subprocess;
InternalTimer( gettimeofday() + 1,
"FHEM::backupToStorage::PollChild", $hash );
}
Log3( $name, 4,
"backupToStorage ($name) - execute command asynchronously (PID=$pid)"
);
$hash->{".fhem"}{subprocess} = $subprocess;
InternalTimer( gettimeofday() + 1,
"FHEM::backupToStorage::PollChild", $hash );
Log3( $hash, 4,
"backupToStorage ($name) - control passed back to main loop." );
qq{backupToStorage ($name) - control passed back to main loop.} );
return;
}
@ -459,20 +471,20 @@ sub PollChild {
if ( !defined($json) ) {
Log3( $name, 5,
"backupToStorage ($name) - still waiting ("
. $subprocess->{lasterror}
. ")." );
qq{backupToStorage ($name) - still waiting ($subprocess->{lasterror}).}
);
InternalTimer( gettimeofday() + 1,
"FHEM::backupToStorage::PollChild", $hash );
return;
}
else {
Log3( $name, 4,
"backupToStorage ($name) - got result from asynchronous parsing: "
. $json );
qq{backupToStorage ($name) - got result from asynchronous parsing: $json} );
$subprocess->wait();
Log3( $name, 4,
"backupToStorage ($name) - asynchronous finished." );
qq{backupToStorage ($name) - asynchronous finished.} );
CleanSubprocess($hash);
WriteReadings( $hash, $json );
@ -558,7 +570,7 @@ sub CleanSubprocess {
my $name = $hash->{NAME};
delete( $hash->{".fhem"}{subprocess} );
Log3( $name, 4, "backupToStorage ($name) - clean Subprocess" );
Log3( $name, 4, qq{backupToStorage ($name) - clean Subprocess} );
}
sub StorePassword {
@ -586,9 +598,9 @@ sub StorePassword {
my $err = setKeyValue( $index, $enc_pwd );
DoTrigger( $name, 'password add' );
return "error while saving the password - $err" if ( defined($err) );
return qq{error while saving the password - $err} if ( defined($err) );
return "password successfully saved";
return qq{password successfully saved};
}
sub ReadPassword {
@ -599,14 +611,14 @@ sub ReadPassword {
my $key = getUniqueId() . $index;
my ( $password, $err );
Log3( $name, 4, "backupToStorage ($name) - Read password from file" );
Log3( $name, 4, qq{backupToStorage ($name) - Read password from file} );
( $err, $password ) = getKeyValue($index);
if ( defined($err) ) {
Log3( $name, 3,
"backupToStorage ($name) - unable to read password from file: $err"
qq{backupToStorage ($name) - unable to read password from file: $err}
);
return undef;
}
@ -629,7 +641,7 @@ sub ReadPassword {
return $dec_pwd;
}
else {
Log3( $name, 3, "backupToStorage ($name) - No password in file" );
Log3( $name, 3, qq{backupToStorage ($name) - No password in file} );
return undef;
}
@ -680,7 +692,7 @@ sub WriteReadings {
my $decode_json = eval { decode_json($json) };
if ($@) {
Log3( $name, 2, "backupToStorage ($name) - JSON error: $@" );
Log3( $name, 2, qq{backupToStorage ($name) - JSON error: $@} );
return;
}