zoneminder/scripts/zmtrack.pl.in

236 lines
5.8 KiB
Perl
Raw Permalink Normal View History

#!/usr/bin/perl -wT
#
# ==========================================================================
#
# ZoneMinder Experimental PTZ Tracking Script, $Date: 2009-06-08 10:11:56 +0100 (Mon, 08 Jun 2009) $, $Revision: 2908 $
# Copyright (C) 2001-2008 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.
#
# ==========================================================================
2015-04-16 12:51:05 +08:00
=head1 NAME
zmtrack.pl - ZoneMinder Experimental PTZ Tracking Script
=head1 SYNOPSIS
zmtrack.pl -m <monitor>
zmtrack.pl --monitor=<monitor>
=head1 OPTIONS
-m<monitor>, --monitor=<monitor> - Id of the monitor to track
=head1 DESCRIPTION
This script is used to trigger and cancel alarms from external sources
using an arbitrary text based format.
=cut
use strict;
use bytes;
# ==========================================================================
#
# User config
#
# ==========================================================================
use constant SLEEP_TIME => 10000; # In microseconds
# ==========================================================================
#
# Don't change anything from here on down
#
# ==========================================================================
@EXTRA_PERL_LIB@
use ZoneMinder;
use DBI;
use POSIX;
2015-04-16 13:42:56 +08:00
use autouse 'Data::Dumper'=>qw(Dumper);
use Getopt::Long;
2015-04-16 12:51:05 +08:00
use autouse 'Pod::Usage'=>qw(pod2usage);
use Time::HiRes qw( usleep );
$| = 1;
$ENV{PATH} = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my $mid = 0;
2015-04-16 12:51:05 +08:00
GetOptions( 'monitor=s'=>\$mid )
or pod2usage(-exitstatus => -1);
logInit();
logSetSignal();
my ( $detaint_mid ) = $mid =~ /^(\d+)$/;
$mid = $detaint_mid;
2015-04-16 12:51:05 +08:00
print( "Tracker daemon $mid (experimental) starting at "
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
."\n"
);
my $dbh = zmDbConnect();
2015-04-16 12:51:05 +08:00
my $sql = "SELECT C.*,M.* FROM Monitors as M
LEFT JOIN Controls as C on M.ControlId = C.Id
WHERE M.Id = ?"
;
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
2015-04-16 12:51:05 +08:00
my $res = $sth->execute( $mid )
or Fatal( "Can't execute '$sql': ".$sth->errstr() );
my $monitor = $sth->fetchrow_hashref();
if ( !$monitor )
{
2015-04-16 12:51:05 +08:00
print( "Can't find monitor '$mid'\n" );
exit( -1 );
}
if ( !$monitor->{Controllable} )
{
2015-04-16 12:51:05 +08:00
print( "Monitor '$mid' is not controllable\n" );
exit( -1 );
}
if ( !$monitor->{TrackMotion} )
{
2015-04-16 12:51:05 +08:00
print( "Monitor '$mid' is not configured to track motion\n" );
exit( -1 );
}
if ( !$monitor->{CanMoveMap} )
{
2015-04-16 12:51:05 +08:00
print( "Monitor '$mid' cannot move in map mode" );
if ( $monitor->{CanMoveRel} )
{
print( ", falling back to pseudo map mode\n" );
}
else
{
print( "\n" );
exit( -1 );
}
}
Debug( "Found monitor for id '$monitor'\n" );
exit( -1 ) if ( !zmMemVerify( $monitor ) );
sub Suspend
{
2015-04-16 12:51:05 +08:00
my $monitor = shift;
zmMonitorSuspend( $monitor );
}
sub Resume
{
2015-04-16 12:51:05 +08:00
my $monitor = shift;
sleep( $monitor->{TrackDelay} );
zmMonitorResume( $monitor );
}
sub Track
{
2015-04-16 12:51:05 +08:00
my $monitor = shift;
my ( $x, $y ) = @_;
my ( $detaint_x ) = $x =~ /^(\d+)$/; $x = $detaint_x;
my ( $detaint_y ) = $y =~ /^(\d+)$/; $y = $detaint_y;
my $ctrlCommand = $Config{ZM_PATH_BIN}
."/zmcontrol.pl -i "
.$monitor->{Id}
;
$ctrlCommand .= " --command="
.( $monitor->{CanMoveMap} ? "moveMap"
: "movePseudoMap"
)
." --xcoord=$x --ycoord=$y"
;
executeShellCommand( $ctrlCommand );
}
sub Return
{
2015-04-16 12:51:05 +08:00
my $monitor = shift;
my $ctrlCommand = $Config{ZM_PATH_BIN}
."/zmcontrol.pl -i "
.$monitor->{Id}
;
if ( $monitor->{ReturnLocation} > 0 )
{
$ctrlCommand .= " --command=presetGoto --preset="
.$monitor->{ReturnLocation}
;
}
else
{
$ctrlCommand .= " --command=presetHome";
}
executeShellCommand( $ctrlCommand );
}
my $last_alarm = 0;
if ( ($monitor->{ReturnLocation} >= 0) )
{
2015-04-16 12:51:05 +08:00
Suspend( $monitor );
Return( $monitor );
Resume( $monitor );
}
my $alarmed = undef;
while( 1 )
{
2015-04-16 12:51:05 +08:00
if ( zmIsAlarmed( $monitor ) )
{
my ( $alarm_x, $alarm_y ) = zmGetAlarmLocation( $monitor );
if ( $alarm_x >= 0 && $alarm_y >= 0 )
{
Debug( "Got alarm at $alarm_x, $alarm_y\n" );
Suspend( $monitor );
Track( $monitor, $alarm_x, $alarm_y );
Resume( $monitor );
$last_alarm = time();
$alarmed = !undef;
}
}
else
{
if ( logDebugging() && $alarmed )
{
print( "Left alarm state\n" );
$alarmed = undef;
}
if ( ($monitor->{ReturnLocation} >= 0)
&& ($last_alarm > 0)
&& ((time()-$last_alarm) > $monitor->{ReturnDelay})
)
{
Debug( "Returning to location ".$monitor->{ReturnLocation}."\n" );
Suspend( $monitor );
Return( $monitor );
Resume( $monitor );
$last_alarm = 0;
}
}
usleep( SLEEP_TIME );
}