Modified perl module structure.

git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@1699 e3e1d417-86f3-4887-817a-d78f3d33393f
This commit is contained in:
stan 2005-12-20 13:47:26 +00:00
parent 4542cfb305
commit cadaef69e7
8 changed files with 803 additions and 265 deletions

View File

@ -190,6 +190,7 @@ AC_DEFINE_DIR([SYSCONFDIR],[sysconfdir],[Expanded configuration directory])
AC_SUBST(ZM_CONFIG,"$SYSCONFDIR/zm.conf")
AC_OUTPUT(Makefile src/Makefile web/Makefile web/graphics/Makefile scripts/Makefile db/Makefile)
AC_OUTPUT(zm.conf zmconfig.pl src/zm_config.h web/zm_config.php scripts/ZoneMinder/lib/ZoneMinder.pm scripts/zm db/zmschema.sql db/zmdrop.sql)
AC_OUTPUT(zm.conf zmconfig.pl src/zm_config.h web/zm_config.php scripts/zm db/zmschema.sql db/zmdrop.sql)
AC_OUTPUT(scripts/ZoneMinder/lib/ZoneMinder/Base.pm scripts/ZoneMinder/lib/ZoneMinder/Config.pm)
AC_CHECK_FILE(scripts/ZoneMinder/Makefile,,[cd scripts/ZoneMinder;perl Makefile.PL])

View File

@ -4,10 +4,17 @@ use ExtUtils::MakeMaker;
# the contents of the Makefile that is written.
WriteMakefile(
NAME => 'ZoneMinder',
VERSION_FROM => 'lib/ZoneMinder.pm', # finds $VERSION
VERSION_FROM => 'lib/ZoneMinder/Base.pm', # finds $VERSION
PREREQ_PM => {}, # e.g., Module::Name => 1.1
PM => {'lib/ZoneMinder.pm' => '$(INST_LIBDIR)/ZoneMinder.pm'},
PM => {
'lib/ZoneMinder.pm' => '$(INST_LIBDIR)/ZoneMinder.pm',
'lib/ZoneMinder/Base.pm' => '$(INST_LIBDIR)/ZoneMinder/Base.pm',
'lib/ZoneMinder/Config.pm' => '$(INST_LIBDIR)/ZoneMinder/Config.pm',
'lib/ZoneMinder/Debug.pm' => '$(INST_LIBDIR)/ZoneMinder/Debug.pm',
'lib/ZoneMinder/Database.pm' => '$(INST_LIBDIR)/ZoneMinder/Database.pm',
'lib/ZoneMinder/SharedMem.pm' => '$(INST_LIBDIR)/ZoneMinder/SharedMem.pm',
},
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => 'lib/ZoneMinder.pm', # retrieve abstract from module
AUTHOR => 'Philip Coombes <stan@localdomain>') : ()),
AUTHOR => 'Philip Coombes <philip.coombes@zoneminder.com>') : ()),
);

View File

@ -0,0 +1,114 @@
# ==========================================================================
#
# ZoneMinder Common Module, $Date$, $Revision$
# Copyright (C) 2003, 2004, 2005 Philip Coombes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This module contains the common definitions and functions used by the rest
# of the ZoneMinder scripts
#
package ZoneMinder;
use 5.006;
use strict;
use warnings;
require Exporter;
use ZoneMinder::Base;
use ZoneMinder::Config;
use ZoneMinder::Debug;
use ZoneMinder::Database;
use ZoneMinder::SharedMem;
our @ISA = qw(Exporter ZoneMinder::Base ZoneMinder::Config ZoneMinder::Debug ZoneMinder::Database ZoneMinder::SharedMem);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use ZoneMinder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = (
@ZoneMinder::Base::EXPORT,
@ZoneMinder::Debug::EXPORT,
@ZoneMinder::Config::EXPORT,
@ZoneMinder::Database::EXPORT,
@ZoneMinder::SharedMem::EXPORT
);
our $VERSION = $ZoneMinder::Base::VERSION;
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
ZoneMinder - Perl extension for blah blah blah
=head1 SYNOPSIS
use ZoneMinder;
blah blah blah
=head1 DESCRIPTION
Stub documentation for ZoneMinder, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head2 EXPORT
None by default.
=head1 SEE ALSO
Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.
If you have a mailing list set up for your module, mention it here.
If you have a web site set up for your module, mention it here.
=head1 AUTHOR
Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Philip Coombes
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.
=cut

View File

@ -0,0 +1,103 @@
# ==========================================================================
#
# ZoneMinder Base Module, $Date$, $Revision$
# Copyright (C) 2003, 2004, 2005 Philip Coombes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This module contains the common definitions and functions used by the rest
# of the ZoneMinder scripts
#
package ZoneMinder::Base;
use 5.006;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use ZoneMinder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();
our $VERSION = "@VERSION@";
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
ZoneMinder - Perl extension for blah blah blah
=head1 SYNOPSIS
use ZoneMinder;
blah blah blah
=head1 DESCRIPTION
Stub documentation for ZoneMinder, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head2 EXPORT
None by default.
=head1 SEE ALSO
Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.
If you have a mailing list set up for your module, mention it here.
If you have a web site set up for your module, mention it here.
=head1 AUTHOR
Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Philip Coombes
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.
=cut

View File

@ -0,0 +1,139 @@
# ==========================================================================
#
# ZoneMinder Config Module, $Date$, $Revision$
# Copyright (C) 2003, 2004, 2005 Philip Coombes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This module contains the common definitions and functions used by the rest
# of the ZoneMinder scripts
#
package ZoneMinder::Config;
use 5.006;
use strict;
use warnings;
require Exporter;
require ZoneMinder::Base;
our @ISA = qw(Exporter ZoneMinder::Base);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use ZoneMinder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();
our @EXPORT_CONFIG; # Get populated by BEGIN
push( @EXPORT, @EXPORT_CONFIG );
our $VERSION = $ZoneMinder::Base::VERSION;
use constant ZM_CONFIG => "@ZM_CONFIG@"; # Path to the ZoneMinder config file, autogenerated do not change (from zmconfig)
# Load the config from the database into the symbol table
BEGIN
{
no strict 'refs';
open( CONFIG, "<".ZM_CONFIG ) or die( "Can't open config file: $!" );
foreach my $str ( <CONFIG> )
{
next if ( $str =~ /^\s*$/ );
next if ( $str =~ /^\s*#/ );
my ( $name, $value ) = $str =~ /^\s*([^=\\s]+)\s*=\s*(\S+)\s*$/;
$name =~ tr/a-z/A-Z/;
*{$name} = sub { $value };
push( @EXPORT_CONFIG, $name );
}
close( CONFIG );
use DBI;
my $dbh = DBI->connect( "DBI:mysql:database=".&ZM_DB_NAME.";host=".&ZM_DB_HOST, &ZM_DB_USER, &ZM_DB_PASS );
my $sql = "select * from Config";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
while( my $config = $sth->fetchrow_hashref() )
{
*{$config->{Name}} = sub { $config->{Value} };
push( @EXPORT_CONFIG, $config->{Name} );
}
$sth->finish();
$dbh->disconnect();
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
ZoneMinder - Perl extension for blah blah blah
=head1 SYNOPSIS
use ZoneMinder;
blah blah blah
=head1 DESCRIPTION
Stub documentation for ZoneMinder, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head2 EXPORT
None by default.
=head1 SEE ALSO
Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.
If you have a mailing list set up for your module, mention it here.
If you have a web site set up for your module, mention it here.
=head1 AUTHOR
Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Philip Coombes
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.
=cut

View File

@ -0,0 +1,203 @@
# ==========================================================================
#
# ZoneMinder Database Module, $Date$, $Revision$
# Copyright (C) 2003, 2004, 2005 Philip Coombes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This module contains the common definitions and functions used by the rest
# of the ZoneMinder scripts
#
package ZoneMinder::Database;
use 5.006;
use strict;
use warnings;
require Exporter;
require ZoneMinder::Base;
our @ISA = qw(Exporter ZoneMinder::Base);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use ZoneMinder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();
our $VERSION = $ZoneMinder::Base::VERSION;
# ==========================================================================
#
# Database Access
#
# ==========================================================================
use ZoneMinder::Debug;
use ZoneMinder::Config;
our $dbh = undef;
sub zmDbConnect
{
my $force = shift;
if ( $force )
{
zmDbDisconnect();
}
if ( !defined( $dbh ) )
{
$dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS );
}
return( $dbh );
}
sub zmDbDisconnect
{
if ( defined( $dbh ) )
{
$dbh->disconnect();
$dbh = undef;
}
}
use constant DB_MON_ALL => 0; # All monitors
use constant DB_MON_CAPT => 1; # All monitors that are capturing
use constant DB_MON_ACTIVE => 2; # All monitors that are active
use constant DB_MON_MOTION => 3; # All monitors that are doing motion detection
use constant DB_MON_RECORD => 4; # All monitors that are doing unconditional recording
use constant DB_MON_PASSIVE => 5; # All monitors that are in nodect state
sub zmDbGetMonitors
{
zmDbConnect();
my $function = shift || DB_MON_ALL;
my $sql = "select * from Monitors";
if ( $function )
{
if ( $function == DB_MON_CAPT )
{
$sql .= " where Function >= 'Monitor'";
}
elsif ( $function == DB_MON_ACTIVE )
{
$sql .= " where Function > 'Monitor'";
}
elsif ( $function == DB_MON_MOTION )
{
$sql .= " where Function = 'Modect' or Function = 'Mocord'";
}
elsif ( $function == DB_MON_RECORD )
{
$sql .= " where Function = 'Record' or Function = 'Mocord'";
}
elsif ( $function == DB_MON_PASSIVE )
{
$sql .= " where Function = 'Nodect'";
}
}
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
my @monitors;
while( my $monitor = $sth->fetchrow_hashref() )
{
push( @monitors, $monitor );
}
$sth->finish();
return( \@monitors );
}
sub zmDbGetMonitor( $ )
{
zmDbConnect();
my $id = shift;
return( undef ) if ( !defined($id) );
my $sql = "select * from Monitors where Id = ?";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $id ) or die( "Can't execute '$sql': ".$sth->errstr() );
my $monitor = $sth->fetchrow_hashref();
return( $monitor );
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
ZoneMinder::Database - Perl extension for blah blah blah
=head1 SYNOPSIS
use ZoneMinder::Database;
blah blah blah
=head1 DESCRIPTION
Stub documentation for ZoneMinder, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head2 EXPORT
None by default.
=head1 SEE ALSO
Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.
If you have a mailing list set up for your module, mention it here.
If you have a web site set up for your module, mention it here.
=head1 AUTHOR
Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Philip Coombes
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.
=cut

View File

@ -0,0 +1,223 @@
# ==========================================================================
#
# ZoneMinder Debug Module, $Date$, $Revision$
# Copyright (C) 2003, 2004, 2005 Philip Coombes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This module contains the debug definitions and functions used by the rest
# of the ZoneMinder scripts
#
package ZoneMinder::Debug;
use 5.006;
use strict;
use warnings;
require Exporter;
require ZoneMinder::Base;
our @ISA = qw(Exporter ZoneMinder::Base);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use ZoneMinder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
Debug
Info
Warning
Error
Fatal
);
our $VERSION = $ZoneMinder::Base::VERSION;
# ==========================================================================
#
# Debug Facilities
#
# ==========================================================================
use Carp;
use POSIX;
use Sys::Syslog;
use Time::HiRes qw/gettimeofday/;
use constant CARP_DEBUG => 0;
our $dbg_initialised = undef;
our $dbg_id = "zm?";
our $dbg_level = 0;
sub zmDbgInit
{
my $id = shift;
my $level = shift;
if ( $id )
{
$dbg_id = $id;
}
else
{
$dbg_id = main::DBG_ID;
}
if ( defined($level) )
{
$dbg_level = $level;
}
else
{
$dbg_level = main::DBG_LEVEL;
}
openlog( $dbg_id, "pid,ndelay", "local1" );
$dbg_initialised = !undef;
}
sub dbgPrint
{
my $code = shift;
my $string = shift;
my $line = shift;
$string =~ s/[\r\n]+$//g;
if ( !$dbg_initialised )
{
zmDbgInit();
}
my ($seconds, $microseconds) = gettimeofday();
if ( $line )
{
my $file = __FILE__;
$file =~ s|^.*/||g;
if ( CARP_DEBUG )
{
print( STDERR Carp::shortmess( sprintf( "%s.%06d %s[%d].%s-%s/%d [%s]", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $file, $line, $code, $string ) ) );
}
else
{
printf( STDERR "%s.%06d %s[%d].%s-%s/%d [%s]\n", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $file, $line, $code, $string );
}
}
else
{
if ( CARP_DEBUG )
{
printf( STDERR Carp::shortmess( sprintf( "%s.%06d %s[%d].%s [%s]", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $code, $string ) ) );
}
else
{
printf( STDERR "%s.%06d %s[%d].%s [%s]\n", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $code, $string );
}
}
}
sub Debug
{
dbgPrint( "DBG", $_[0] ) if ( $dbg_level >= 1 );
}
sub Info
{
dbgPrint( "INF", $_[0] ) if ( $dbg_level >= 0 );
syslog( "info", "INF [%s]", $_[0] );
}
sub Warning
{
dbgPrint( "WAR", $_[0] ) if ( $dbg_level >= -1 );
syslog( "warning", "WAR [%s]", $_[0] );
}
sub Error
{
dbgPrint( "ERR", $_[0] ) if ( $dbg_level >= -2 );
syslog( "err", "ERR [%s]", $_[0] );
}
sub Fatal
{
dbgPrint( "FAT", $_[0] ) if ( $dbg_level >= -3 );
syslog( "err", "ERR [%s]", $_[0] );
confess( $_[0] );
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
ZoneMinder::Debug - Perl extension for blah blah blah
=head1 SYNOPSIS
use ZoneMinder::Debug;
blah blah blah
=head1 DESCRIPTION
Stub documentation for ZoneMinder, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head2 EXPORT
None by default.
=head1 SEE ALSO
Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.
If you have a mailing list set up for your module, mention it here.
If you have a web site set up for your module, mention it here.
=head1 AUTHOR
Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Philip Coombes
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.
=cut

View File

@ -1,6 +1,6 @@
# ==========================================================================
#
# ZoneMinder Common Module, $Date$, $Revision$
# ZoneMinder Shared Memory Module, $Date$, $Revision$
# Copyright (C) 2003, 2004, 2005 Philip Coombes
#
# This program is free software; you can redistribute it and/or
@ -22,15 +22,16 @@
# This module contains the common definitions and functions used by the rest
# of the ZoneMinder scripts
#
package ZoneMinder;
package ZoneMinder::SharedMem;
use 5.006;
use strict;
use warnings;
require Exporter;
require ZoneMinder::Base;
our @ISA = qw(Exporter);
our @ISA = qw(Exporter ZoneMinder::Base);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
@ -45,265 +46,9 @@ our %EXPORT_TAGS = ( 'all' => [ qw(
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
Debug
Info
Warning
Error
Fatal
);
our @EXPORT_CONFIG; # Get populated by BEGIN
push( @EXPORT, @EXPORT_CONFIG );
our @EXPORT = qw();
our $VERSION = "@VERSION@";
use constant ZM_CONFIG => "@ZM_CONFIG@"; # Path to the ZoneMinder config file, autogenerated do not change (from zmconfig)
# Load the config from the database into the symbol table
BEGIN
{
no strict 'refs';
my $config_file = ZM_CONFIG;
( my $local_config_file = ZM_CONFIG ) =~ s|^.*/|./|;
if ( -s $local_config_file && -r $local_config_file )
{
$config_file = $local_config_file;
}
open( CONFIG, "<".$config_file ) or die( "Can't open config file '$config_file': $!" );
foreach my $str ( <CONFIG> )
{
next if ( $str =~ /^\s*$/ );
next if ( $str =~ /^\s*#/ );
my ( $name, $value ) = $str =~ /^\s*([^=\\s]+)\s*=\s*(\S+)\s*$/;
$name =~ tr/a-z/A-Z/;
*{$name} = sub { $value };
push( @EXPORT_CONFIG, $name );
}
close( CONFIG );
use DBI;
my $dbh = DBI->connect( "DBI:mysql:database=".&ZM_DB_NAME.";host=".&ZM_DB_HOST, &ZM_DB_USER, &ZM_DB_PASS );
my $sql = "select * from Config";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
while( my $config = $sth->fetchrow_hashref() )
{
*{$config->{Name}} = sub { $config->{Value} };
push( @EXPORT_CONFIG, $config->{Name} );
}
$sth->finish();
$dbh->disconnect();
}
# ==========================================================================
#
# Debug Facilities
#
# ==========================================================================
use Carp;
use POSIX;
use Sys::Syslog;
use Time::HiRes qw/gettimeofday/;
use constant CARP_DEBUG => 0;
our $dbg_initialised = undef;
our $dbg_id = "zm?";
our $dbg_level = 0;
sub zmDbgInit
{
my $id = shift;
my $level = shift;
if ( $id )
{
$dbg_id = $id;
}
else
{
$dbg_id = main::DBG_ID;
}
if ( defined($level) )
{
$dbg_level = $level;
}
else
{
$dbg_level = main::DBG_LEVEL;
}
openlog( $dbg_id, "pid,ndelay", "local1" );
$dbg_initialised = !undef;
}
sub dbgPrint
{
my $code = shift;
my $string = shift;
my $line = shift;
$string =~ s/[\r\n]+$//g;
if ( !$dbg_initialised )
{
zmDbgInit();
}
my ($seconds, $microseconds) = gettimeofday();
if ( $line )
{
my $file = __FILE__;
$file =~ s|^.*/||g;
if ( CARP_DEBUG )
{
print( STDERR Carp::shortmess( sprintf( "%s.%06d %s[%d].%s-%s/%d [%s]", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $file, $line, $code, $string ) ) );
}
else
{
printf( STDERR "%s.%06d %s[%d].%s-%s/%d [%s]\n", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $file, $line, $code, $string );
}
}
else
{
if ( CARP_DEBUG )
{
printf( STDERR Carp::shortmess( sprintf( "%s.%06d %s[%d].%s [%s]", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $code, $string ) ) );
}
else
{
printf( STDERR "%s.%06d %s[%d].%s [%s]\n", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $dbg_id, $$, $code, $string );
}
}
}
sub Debug
{
dbgPrint( "DBG", $_[0] ) if ( $dbg_level >= 1 );
}
sub Info
{
dbgPrint( "INF", $_[0] ) if ( $dbg_level >= 0 );
syslog( "info", "INF [%s]", $_[0] );
}
sub Warning
{
dbgPrint( "WAR", $_[0] ) if ( $dbg_level >= -1 );
syslog( "warning", "WAR [%s]", $_[0] );
}
sub Error
{
dbgPrint( "ERR", $_[0] ) if ( $dbg_level >= -2 );
syslog( "err", "ERR [%s]", $_[0] );
}
sub Fatal
{
dbgPrint( "FAT", $_[0] ) if ( $dbg_level >= -3 );
syslog( "err", "ERR [%s]", $_[0] );
confess( $_[0] );
}
# ==========================================================================
#
# Database Access
#
# ==========================================================================
our $dbh = undef;
sub zmDbConnect
{
my $force = shift;
if ( $force )
{
zmDbDisconnect();
}
if ( !defined( $dbh ) )
{
$dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS );
}
return( $dbh );
}
sub zmDbDisconnect
{
if ( defined( $dbh ) )
{
$dbh->disconnect();
$dbh = undef;
}
}
use constant DB_MON_ALL => 0; # All monitors
use constant DB_MON_CAPT => 1; # All monitors that are capturing
use constant DB_MON_ACTIVE => 2; # All monitors that are active
use constant DB_MON_MOTION => 3; # All monitors that are doing motion detection
use constant DB_MON_RECORD => 4; # All monitors that are doing unconditional recording
use constant DB_MON_PASSIVE => 5; # All monitors that are in nodect state
sub zmDbGetMonitors
{
zmDbConnect();
my $function = shift || DB_MON_ALL;
my $sql = "select * from Monitors";
if ( $function )
{
if ( $function == DB_MON_CAPT )
{
$sql .= " where Function >= 'Monitor'";
}
elsif ( $function == DB_MON_ACTIVE )
{
$sql .= " where Function > 'Monitor'";
}
elsif ( $function == DB_MON_MOTION )
{
$sql .= " where Function = 'Modect' or Function = 'Mocord'";
}
elsif ( $function == DB_MON_RECORD )
{
$sql .= " where Function = 'Record' or Function = 'Mocord'";
}
elsif ( $function == DB_MON_PASSIVE )
{
$sql .= " where Function = 'Nodect'";
}
}
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
my @monitors;
while( my $monitor = $sth->fetchrow_hashref() )
{
push( @monitors, $monitor );
}
$sth->finish();
return( \@monitors );
}
sub zmDbGetMonitor( $ )
{
zmDbConnect();
my $id = shift;
return( undef ) if ( !defined($id) );
my $sql = "select * from Monitors where Id = ?";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $id ) or die( "Can't execute '$sql': ".$sth->errstr() );
my $monitor = $sth->fetchrow_hashref();
return( $monitor );
}
our $VERSION = $ZoneMinder::Base::VERSION;
# ==========================================================================
#
@ -311,6 +56,9 @@ sub zmDbGetMonitor( $ )
#
# ==========================================================================
use ZoneMinder::Config;
use ZoneMinder::Debug;
use constant STATE_IDLE => 0;
use constant STATE_PREALARM => 1;
use constant STATE_ALARM => 2;