Add Object_Types table and perl code to use it.

pull/3933/head
Isaac Connor 2024-03-29 18:42:46 -04:00
parent 443e3bd0fd
commit d5aa5bfa46
6 changed files with 116 additions and 4 deletions

6
db/Object_Types.sql Normal file
View File

@ -0,0 +1,6 @@
CREATE TABLE Object_Types (
Id int(10) NOT NULL AUTO_INCREMENT,
Name varchar(32) UNIQUE,
Human TEXT,
PRIMARY KEY (Id)
);

View File

@ -1256,6 +1256,7 @@ CREATE TABLE `Events_Tags` (
CONSTRAINT `Events_Tags_ibfk_2` FOREIGN KEY (`EventId`) REFERENCES `Events` (`Id`) ON DELETE CASCADE
) ENGINE=@ZM_MYSQL_ENGINE@;
source @PKGDATADIR@/db/Object_Types.sql
-- We generally don't alter triggers, we drop and re-create them, so let's keep them in a separate file that we can just source in update scripts.
source @PKGDATADIR@/db/triggers.sql

View File

@ -18,7 +18,7 @@
%global zmtargetdistro %{?rhel:el%{rhel}}%{!?rhel:fc%{fedora}}
Name: zoneminder
Version: 1.37.56
Version: 1.37.57
Release: 2%{?dist}
Summary: A camera monitoring and analysis tool
Group: System Environment/Daemons

View File

@ -22,7 +22,6 @@
# This module contains the common definitions and functions used by the rest
# of the ZoneMinder scripts
#
package ZoneMinder::Object;
use 5.006;
@ -32,6 +31,7 @@ use Time::HiRes qw{ gettimeofday tv_interval };
use Carp qw( cluck );
require ZoneMinder::Base;
require ZoneMinder::Object_Type;
our @ISA = qw(ZoneMinder::Base);
@ -361,7 +361,7 @@ sub set {
if ( $params ) {
foreach my $field ( keys %{$params} ) {
$log->debug("field: $field, ".def_or_undef($$self{$field}).' =? param: '.def_or_undef($$params{$field})) if $debug;
$log->debug("field: $field, ".def_or_undef($$self{$field}).' =? param: '.def_or_undef($$params{$field})) if $debug or DEBUG_ALL;
if ( ( ! defined $$self{$field} ) or ($$self{$field} ne $params->{$field}) ) {
# Only make changes to fields that have changed
if ( defined $fields{$field} ) {
@ -886,6 +886,62 @@ sub clone {
return $new;
} # end sub clone
sub Object_Type {
if ( $_[0]{object_type_id} ) {
$_[0]{Object_Type} = new ZoneMinder::Object_Type( $_[0]{object_type_id} );
} else {
$_[0]{Object_Type} = ZoneMinder::Object_Type->find_one( name=>ref $_[0] );
$_[0]{Object_Type} = new ZoneMinder::Object_Type() if ! $_[0]{Object_Type};
} # end if
return $_[0]{Object_Type};
} # end sub Object_Type
sub object_type {
if ( @_ > 1 ) {
my $Type = ZoneMinder::Object_Type->find_one( name => $_[1] );
if ( ! $Type ) {
$Type = new ZoneMinder::Object_Type();
$Type->save({ Name=>$_[1], Human=>$_[1] });
} # end if
$_[0]{object_type} = $Type->Name();
$_[0]{object_type_id} = $Type->Id();
} # end if
if ( ! $_[0]{object_type} ) {
$_[0]{object_type} = new ZoneMinder::Object_Type( $_[0]{object_type_id} )->Name();
} # end if
return $_[0]{object_type};
} # end sub object_type
sub Object {
my $self = shift;
if ( @_ ) {
$self->object_type( ref $_[0] );
$$self{object_id} = $_[0]{id};
$$self{Object} = $_[0];
} # end if
my $type = $self->object_type();
if ( !$type ) {
Error('No type in Object::Object'. $self->to_string()) if ref $self ne 'ZoneMinder::Log';
return undef;
} # end if
my ( $module ) = $type =~ /ZoneMinder::(.*)/;
if ( $module ) {
eval {
require "ZoneMinder/$module.pm";
};
if ( ! $$self{Object} ) {
$_ = $type->new($$self{object_id});
Debug( 'Returning object of type ' . ref $_ ) if $debug;
$$self{Object} = $_;
}
return $$self{Object};
} else {
Error("Unvalid object $type");
return new ZoneMinder::Object();
}
} # end sub Object
sub AUTOLOAD {
my $type = ref($_[0]);
Carp::cluck("No type in autoload") if ! $type;

View File

@ -0,0 +1,49 @@
use strict;
package ZoneMinder::Object_Type;
our @ISA = qw( ZoneMinder::Object );
use vars qw( $debug $table %fields %transforms %defaults $primary_key );
$debug = 0;
$table = 'Object_Types';
$primary_key = 'id';
%fields = (
Id => 'Id',
Name => 'Name',
Human => 'Human',
);
%defaults = (
);
%transforms = (
Name => [ 's/^\s+//', 's/\s+$//', 's/\s\s+/ /g' ],
Human => [ 's/^\s+//', 's/\s+$//', 's/\s\s+/ /g', 's/^ZoneMinder:://' ],
);
sub Object {
if ( $_[0]{Name} ) {
my $name = $_[0]{Name};
$name =~ s/::/\//g;
eval {
require $name.'.pm';
};
$ZoneMinder::log->error("failed requiring $name $@") if $@;
return $_[0]{Name}->new($_[1]);
}
my ($caller, undef, $line) = caller;
$ZoneMinder::log->error("Unknown object from $caller:$line");
return new ZoneMinder::Object();
} # end sub Object
sub Human {
if ( @_ > 1 ) {
$_[0]{Human} = $_[1];
}
if ( ! $_[0]{Human} ) {
$_[0]{Human} = $_[0]{Name};
$_[0]{Human} =~ s/^ZoneMinder:://;
}
return $_[0]{Human};
}
1;
__END__

View File

@ -1 +1 @@
1.37.56
1.37.57