Merge branch 'storageareas' into ffmpeg_output

Conflicts:
	scripts/ZoneMinder/lib/ZoneMinder/Logger.pm
	scripts/ZoneMinder/lib/ZoneMinder/Object.pm
This commit is contained in:
APHW2 MFGENG
2017-11-07 18:25:33 -08:00
63 changed files with 3372 additions and 2496 deletions

View File

@@ -115,7 +115,7 @@ BEGIN {
, $Config{ZM_DB_USER}
, $Config{ZM_DB_PASS}
) or croak( "Can't connect to db" );
my $sql = 'select * from Config';
my $sql = 'SELECT Name,Value FROM Config';
my $sth = $dbh->prepare_cached( $sql ) or croak( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or croak( "Can't execute: ".$sth->errstr() );
while( my $config = $sth->fetchrow_hashref() ) {

View File

@@ -205,6 +205,30 @@ sub zmDbGetMonitorAndControl {
return( $monitor );
}
sub start_transaction {
#my ( $caller, undef, $line ) = caller;
#$openprint::log->debug("Called start_transaction from $caller : $line");
my $d = shift;
$d = $dbh if ! $d;
my $ac = $d->{AutoCommit};
$d->{AutoCommit} = 0;
return $ac;
} # end sub start_transaction
sub end_transaction {
#my ( $caller, undef, $line ) = caller;
#$openprint::log->debug("Called end_transaction from $caller : $line");
my ( $d, $ac ) = @_;
if ( ! defined $ac ) {
Error("Undefined ac");
}
$d = $dbh if ! $d;
if ( $ac ) {
#$log->debug("Committing");
$d->commit();
} # end if
$d->{AutoCommit} = $ac;
} # end sub end_transaction
1;
__END__
# Below is stub documentation for your module. You'd better edit it!

View File

@@ -48,9 +48,38 @@ use ZoneMinder::Logger qw(:all);
use ZoneMinder::Database qw(:all);
require Date::Parse;
use vars qw/ $table $primary_key /;
use vars qw/ $table $primary_key %fields $serial @identified_by/;
$table = 'Events';
$primary_key = 'Id';
@identified_by = ('Id');
$serial = $primary_key = 'Id';
%fields = map { $_, $_ } qw(
Id
MonitorId
StorageId
Name
Cause
StartTime
EndTime
Width
Height
Length
Frames
AlarmFrames
DefaultVideo
TotScore
AvgScore
MaxScore
Archived
Videoed
Uploaded
Emailed
Messaged
Executed
Notes
StateId
Orientation
DiskSpace
);
use POSIX;
@@ -359,14 +388,16 @@ sub age {
return $_[0]{age};
}
sub DiskUsage {
sub DiskSpace {
if ( @_ > 1 ) {
$_[0]{DiskUsage} = $_[1];
Debug("Cleared DiskSpace, was $_[0]{DiskSpace}");
$_[0]{DiskSpace} = $_[1];
}
if ( ! defined $_[0]{DiskUsage} ) {
if ( ! defined $_[0]{DiskSpace} ) {
my $size = 0;
File::Find::find( { wanted=>sub { $size += -f $_ ? -s _ : 0 }, untaint=>1 }, $_[0]->Path() );
$_[0]{DiskUsage} = $size;
$_[0]{DiskSpace} = $size;
Debug("DiskSpace for event $_[0]{Id} at $_[0]{Path} Updated to $size bytes");
}
}

View File

@@ -224,7 +224,7 @@ sub Sql {
|| $term->{attr} eq 'Notes'
) {
$value = "'$temp_value'";
} elsif ( $term->{attr} eq 'DateTime' ) {
} elsif ( $term->{attr} eq 'DateTime' or $term->{attr} eq 'StartDateTime' or $term->{attr} eq 'EndDateTime' ) {
$value = DateTimeToSQL( $temp_value );
if ( !$value ) {
Error( "Error parsing date/time '$temp_value', "
@@ -232,7 +232,7 @@ sub Sql {
return;
}
$value = "'$value'";
} elsif ( $term->{attr} eq 'Date' ) {
} elsif ( $term->{attr} eq 'Date' or $term->{attr} eq 'StartDate' or $term->{attr} eq 'EndDate' ) {
$value = DateTimeToSQL( $temp_value );
if ( !$value ) {
Error( "Error parsing date/time '$temp_value', "
@@ -240,7 +240,7 @@ sub Sql {
return;
}
$value = "to_days( '$value' )";
} elsif ( $term->{attr} eq 'Time' ) {
} elsif ( $term->{attr} eq 'Time' or $term->{attr} eq 'StartTime' or $term->{attr} eq 'EndTime' ) {
$value = DateTimeToSQL( $temp_value );
if ( !$value ) {
Error( "Error parsing date/time '$temp_value', "
@@ -259,6 +259,10 @@ sub Sql {
$self->{Sql} .= " regexp $value";
} elsif ( $term->{op} eq '!~' ) {
$self->{Sql} .= " not regexp $value";
} elsif ( $term->{op} eq 'IS' ) {
$self->{Sql} .= " IS $value";
} elsif ( $term->{op} eq 'IS NOT' ) {
$self->{Sql} .= " IS NOT $value";
} elsif ( $term->{op} eq '=[]' ) {
$self->{Sql} .= " in (".join( ",", @value_list ).")";
} elsif ( $term->{op} eq '!~' ) {
@@ -277,15 +281,15 @@ sub Sql {
if ( $self->{AutoMessage} ) {
# Include all events, including events that are still ongoing
# and have no EndTime yet
$sql .= " and ( ".$self->{Sql}." )";
$sql .= ' AND ( '.$self->{Sql}.' )';
} else {
# Only include closed events (events with valid EndTime)
$sql .= " where not isnull(E.EndTime) and ( ".$self->{Sql}." )";
$sql .= ' WHERE (E.EndTime IS NOT NULL) AND ( '.$self->{Sql}.' )';
}
}
my @auto_terms;
if ( $self->{AutoArchive} ) {
push @auto_terms, "E.Archived = 0";
push @auto_terms, 'E.Archived = 0';
}
# Don't do this, it prevents re-generation and concatenation.
# If the file already exists, then the video won't be re-recreated

View File

@@ -678,7 +678,10 @@ sub Dump {
fetch()->logPrint( DEBUG, Data::Dumper->Dump( [ $var ], [ $label ] ) );
}
sub debug { fetch()->logPrint( DEBUG, @_ ); }
sub debug {
my $log = shift;
$log->logPrint( DEBUG, @_ );
}
sub Debug( @ ) {
fetch()->logPrint( DEBUG, @_ );
@@ -688,7 +691,8 @@ sub Info( @ ) {
fetch()->logPrint( INFO, @_ );
}
sub info {
fetch()->logPrint( INFO, @_ );
my $log = shift;
$log->logPrint( INFO, @_ );
}
@@ -696,14 +700,16 @@ sub Warning( @ ) {
fetch()->logPrint( WARNING, @_ );
}
sub warn {
fetch()->logPrint( WARNING, @_ );
my $log = shift;
$log->logPrint( WARNING, @_ );
}
sub Error( @ ) {
fetch()->logPrint( ERROR, @_ );
}
sub error {
fetch()->logPrint( ERROR, @_ );
my $log = shift;
$log->logPrint( ERROR, @_ );
}
sub Fatal( @ ) {

View File

@@ -153,7 +153,7 @@ sub save {
my $serial = eval '$'.$type.'::serial';
my @identified_by = eval '@'.$type.'::identified_by';
my $ac = sql::start_transaction( $local_dbh );
my $ac = ZoneMinder::Database::start_transaction( $local_dbh );
if ( ! $serial ) {
my $insert = $force_insert;
my %serial = eval '%'.$type.'::serial';
@@ -170,7 +170,7 @@ $log->debug("No serial") if $debug;
$where =~ s/\?/\%s/g;
$log->error("Error deleting: DELETE FROM $table WHERE " . sprintf($where, map { defined $_ ? $_ : 'undef' } ( @$self{@identified_by}) ).'):' . $local_dbh->errstr);
$local_dbh->rollback();
sql::end_transaction( $local_dbh, $ac );
ZoneMinder::Database::end_transaction( $local_dbh, $ac );
return $local_dbh->errstr;
} elsif ( $debug ) {
$log->debug("SQL succesful DELETE FROM $table WHERE $where");
@@ -185,8 +185,11 @@ $log->debug("No serial") if $debug;
next;
}
if ( ! $$self{$id} ) {
($$self{$id}) = ($sql{$$fields{$id}}) = $local_dbh->selectrow_array( q{SELECT nextval('} . $serial{$id} . q{')} );
$log->debug("SQL statement execution SELECT nextval('$serial{$id}') returned $$self{$id}") if $debug or DEBUG_ALL;
my $s = qq{SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '$table'};
($$self{$id}) = ($sql{$$fields{$id}}) = $local_dbh->selectrow_array( $s );
#($$self{$id}) = ($sql{$$fields{$id}}) = $local_dbh->selectrow_array( q{SELECT nextval('} . $serial{$id} . q{')} );
$log->debug("SQL statement execution SELECT $s returned $$self{$id}") if $debug or DEBUG_ALL;
$insert = 1;
} # end if
} # end foreach
@@ -200,7 +203,7 @@ $log->debug("No serial") if $debug;
$command =~ s/\?/\%s/g;
$log->error('SQL statement execution failed: ('.sprintf($command, , map { defined $_ ? $_ : 'undef' } ( @sql{@keys}) ).'):' . $local_dbh->errstr);
$local_dbh->rollback();
sql::end_transaction( $local_dbh, $ac );
ZoneMinder::Database::end_transaction( $local_dbh, $ac );
return $error;
} # end if
if ( $debug or DEBUG_ALL ) {
@@ -215,7 +218,7 @@ $log->debug("No serial") if $debug;
$command =~ s/\?/\%s/g;
$log->error('SQL failed: ('.sprintf($command, , map { defined $_ ? $_ : 'undef' } ( @sql{@keys, @$fields{@identified_by}}) ).'):' . $local_dbh->errstr);
$local_dbh->rollback();
sql::end_transaction( $local_dbh, $ac );
ZoneMinder::Database::end_transaction( $local_dbh, $ac );
return $error;
} # end if
if ( $debug or DEBUG_ALL ) {
@@ -224,22 +227,28 @@ $log->debug("No serial") if $debug;
} # end if
} # end if
} else { # not identified_by
@identified_by = ('id') if ! @identified_by;
my $need_serial = ! ( @identified_by == map { $$self{$_} ? $_ : () } @identified_by );
@identified_by = ('Id') if ! @identified_by;
# If the size of the arrays are not equal which means one or more are missing
my @identified_by_without_values = map { $$self{$_} ? () : $_ } @identified_by;
my $need_serial = @identified_by_without_values > 0;
if ( $force_insert or $need_serial ) {
if ( $need_serial ) {
if ( $serial ) {
@$self{@identified_by} = @sql{@$fields{@identified_by}} = $local_dbh->selectrow_array( q{SELECT nextval('} . $serial . q{')} );
my $s = qq{SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '$table'};
@$self{@identified_by} = @sql{@$fields{@identified_by}} = $local_dbh->selectrow_array( $s );
#@$self{@identified_by} = @sql{@$fields{@identified_by}} = $local_dbh->selectrow_array( q{SELECT nextval('} . $serial . q{')} );
if ( $local_dbh->errstr() ) {
$log->error("Error getting next id. " . $local_dbh->errstr() );
$log->error("SQL statement execution SELECT nextval('$serial') returned ".join(',',@$self{@identified_by}));
$log->error("SQL statement execution $s returned ".join(',',@$self{@identified_by}));
} elsif ( $debug or DEBUG_ALL ) {
$log->debug("SQL statement execution SELECT nextval('$serial') returned ".join(',',@$self{@identified_by}));
$log->debug("SQL statement execution $s returned ".join(',',@$self{@identified_by}));
} # end if
} # end if
} # end if
my @keys = keys %sql;
my $command = "INSERT INTO $table (" . join(',', @keys ) . ') VALUES (' . join(',', map { '?' } @sql{@keys} ) . ')';
if ( ! ( $_ = $local_dbh->prepare($command) and $_->execute( @sql{@keys} ) ) ) {
@@ -247,7 +256,7 @@ $log->debug("No serial") if $debug;
my $error = $local_dbh->errstr;
$log->error('SQL failed: ('.sprintf($command, map { defined $_ ? $_ : 'undef' } ( @sql{@keys}) ).'):' . $error);
$local_dbh->rollback();
sql::end_transaction( $local_dbh, $ac );
ZoneMinder::Database::end_transaction( $local_dbh, $ac );
return $error;
} # end if
if ( $debug or DEBUG_ALL ) {
@@ -257,14 +266,16 @@ $log->debug("No serial") if $debug;
} else {
delete $sql{created_on};
my @keys = keys %sql;
@keys = sets::exclude( [ @$fields{@identified_by} ], \@keys );
my %identified_by = map { $_, $_ } @identified_by;
@keys = map { $identified_by{$_} ? () : $$fields{$_} } @keys;
my $command = "UPDATE $table SET " . join(',', map { $_ . ' = ?' } @keys ) . ' WHERE ' . join(' AND ', map { $$fields{$_} .'= ?' } @identified_by );
if ( ! ( $_ = $local_dbh->prepare($command) and $_->execute( @sql{@keys}, @sql{@$fields{@identified_by}} ) ) ) {
my $error = $local_dbh->errstr;
$command =~ s/\?/\%s/g;
$log->error('SQL failed: ('.sprintf($command, map { defined $_ ? $_ : 'undef' } ( @sql{@keys}, @sql{@$fields{@identified_by}} ) ).'):' . $error) if $log;
$local_dbh->rollback();
sql::end_transaction( $local_dbh, $ac );
ZoneMinder::Database::end_transaction( $local_dbh, $ac );
return $error;
} # end if
if ( $debug or DEBUG_ALL ) {
@@ -273,7 +284,7 @@ $log->debug("No serial") if $debug;
} # end if
} # end if
} # end if
sql::end_transaction( $local_dbh, $ac );
ZoneMinder::Database::end_transaction( $local_dbh, $ac );
$self->load();
#if ( $$fields{id} ) {
#if ( ! $ZoneMinder::Object::cache{$type}{$$self{id}} ) {
@@ -294,30 +305,32 @@ sub set {
my $type = ref $self;
my %fields = eval ('%'.$type.'::fields');
if ( ! %fields ) {
$log->warn('ZoneMinder::Object::set called on an object with no fields');
$log->warn("ZoneMinder::Object::set called on an object ($type) with no fields".$@);
} # end if
my %defaults = eval('%'.$type.'::defaults');
if ( ref $params ne 'HASH' ) {
my ( $caller, undef, $line ) = caller;
$openprint::log->error("$type -> set called with non-hash params from $caller $line");
$log->error("$type -> set called with non-hash params from $caller $line");
}
foreach my $field ( keys %fields ) {
$log->debug("field: $field, param: ".$$params{$field}) if $debug;
if ( exists $$params{$field} ) {
$openprint::log->debug("field: $field, $$self{$field} =? param: ".$$params{$field}) if $debug;
if ( ( ! defined $$self{$field} ) or ($$self{$field} ne $params->{$field}) ) {
if ( $params ) {
$log->debug("field: $field, param: ".$$params{$field}) if $debug;
if ( exists $$params{$field} ) {
$log->debug("field: $field, $$self{$field} =? param: ".$$params{$field}) if $debug;
if ( ( ! defined $$self{$field} ) or ($$self{$field} ne $params->{$field}) ) {
# Only make changes to fields that have changed
if ( defined $fields{$field} ) {
$$self{$field} = $$params{$field} if defined $fields{$field};
push @set_fields, $fields{$field}, $$params{$field}; #mark for sql updating
} # end if
$openprint::log->debug("Running $field with $$params{$field}") if $debug;
if ( my $func = $self->can( $field ) ) {
$func->( $self, $$params{$field} );
} # end if
} # end if
} # end if
if ( defined $fields{$field} ) {
$$self{$field} = $$params{$field} if defined $fields{$field};
push @set_fields, $fields{$field}, $$params{$field}; #mark for sql updating
} # end if
$log->debug("Running $field with $$params{$field}") if $debug;
if ( my $func = $self->can( $field ) ) {
$func->( $self, $$params{$field} );
} # end if
} # end if
} # end if
} # end if $params
if ( defined $fields{$field} ) {
if ( $$self{$field} ) {
@@ -356,7 +369,7 @@ sub transform {
if ( defined $$fields{$_[1]} ) {
my @transforms = eval('@{$'.$type.'::transforms{$_[1]}}');
$openprint::log->debug("Transforms for $_[1] before $_[2]: @transforms") if $debug;
$log->debug("Transforms for $_[1] before $_[2]: @transforms") if $debug;
if ( @transforms ) {
foreach my $transform ( @transforms ) {
if ( $transform =~ /^s\// or $transform =~ /^tr\// ) {
@@ -366,15 +379,15 @@ sub transform {
$value = undef;
} # end if
} else {
$openprint::log->debug("evalling $value ".$transform . " Now value is $value" );
$log->debug("evalling $value ".$transform . " Now value is $value" );
eval '$value '.$transform;
$openprint::log->error("Eval error $@") if $@;
$log->error("Eval error $@") if $@;
}
$openprint::log->debug("After $transform: $value") if $debug;
$log->debug("After $transform: $value") if $debug;
} # end foreach
} # end if
} else {
$openprint::log->error("Object::transform ($_[1]) not in fields for $type");
$log->error("Object::transform ($_[1]) not in fields for $type");
} # end if
return $value;

View File

@@ -246,7 +246,7 @@ MAIN: while( $loop ) {
$$Event{Path} = join('/', $Storage->Path(), $day_dir,$event_path);
$Event->MonitorId( $monitor_dir );
$Event->StorageId( $Storage->Id() );
$Event->DiskUsage( undef );
$Event->DiskSpace( undef );
} # event path exists
} # end foreach event_link
chdir( $Storage->Path() );

View File

@@ -251,17 +251,23 @@ sub checkFilter {
my $filter = shift;
my @Events = $filter->Execute();
Info( join( "Checking filter '$filter->{Name}'",
($filter->{AutoDelete}?', delete':''),
($filter->{AutoArchive}?', archive':''),
($filter->{AutoVideo}?', video':''),
($filter->{AutoUpload}?', upload':''),
($filter->{AutoEmail}?', email':''),
($filter->{AutoMessage}?', message':''),
($filter->{AutoExecute}?', execute':''),
' returned ' , scalar @Events , ' events',
"\n",
) );
Info(
join(' ',
'Checking filter', $filter->{Name},
join( ', ',
($filter->{AutoDelete}?'delete':()),
($filter->{AutoArchive}?'archive':()),
($filter->{AutoVideo}?'video':()),
($filter->{AutoUpload}?'upload':()),
($filter->{AutoEmail}?'email':()),
($filter->{AutoMessage}?'message':()),
($filter->{AutoExecute}?'execute':()),
($filter->{UpdateDiskSpace}?'update disk space':()),
),
'returned' , scalar @Events , 'events',
"\n",
) );
foreach my $event ( @Events ) {
Debug( "Checking event $event->{Id}\n" );
@@ -309,6 +315,12 @@ sub checkFilter {
Error( "Unable toto delete event $event->{Id} as previous operations failed\n" );
}
} # end if AutoDelete
if ( $filter->{UpdateDiskSpace} ) {
my $Event = new ZoneMinder::Event( $$event{Id}, $event );
$Event->DiskSpace(undef);
$Event->save();
} # end if UpdateDiskSpace
} # end foreach event
}