2019-09-24 00:54:27 +08:00
|
|
|
#!@PERL_EXECUTABLE@ -wT
|
2009-06-08 17:19:55 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# 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
|
2016-12-26 23:23:16 +08:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-06-08 17:19:55 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
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
|
2009-06-08 17:19:55 +08:00
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# User config
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
|
|
|
use constant SLEEP_TIME => 10000; # In microseconds
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Don't change anything from here on down
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2013-10-01 22:38:51 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2009-06-08 17:19:55 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use DBI;
|
|
|
|
use POSIX;
|
2015-04-16 13:42:56 +08:00
|
|
|
use autouse 'Data::Dumper'=>qw(Dumper);
|
2009-06-08 17:19:55 +08:00
|
|
|
use Getopt::Long;
|
2015-04-16 12:51:05 +08:00
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
2009-06-08 17:19:55 +08:00
|
|
|
use Time::HiRes qw( usleep );
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
2016-03-12 05:28:16 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
|
2009-06-08 17:19:55 +08:00
|
|
|
$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);
|
2009-06-08 17:19:55 +08:00
|
|
|
|
|
|
|
my ( $detaint_mid ) = $mid =~ /^(\d+)$/;
|
|
|
|
$mid = $detaint_mid;
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
logInit($mid?(id=>'zmtrack_m'.$mid):());
|
|
|
|
logSetSignal();
|
|
|
|
|
2015-04-16 12:51:05 +08:00
|
|
|
print( "Tracker daemon $mid (experimental) starting at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
."\n"
|
|
|
|
);
|
2009-06-08 17:19:55 +08:00
|
|
|
|
|
|
|
my $dbh = zmDbConnect();
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
my $sql = 'SELECT C.*,M.* FROM Monitors as M
|
2015-04-16 12:51:05 +08:00
|
|
|
LEFT JOIN Controls as C on M.ControlId = C.Id
|
2019-09-24 00:38:58 +08:00
|
|
|
WHERE M.Id = ?'
|
2015-04-16 12:51:05 +08:00
|
|
|
;
|
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
2019-09-24 00:38:58 +08:00
|
|
|
or Fatal("Can't prepare '$sql': ".$dbh->errstr());
|
2009-06-08 17:19:55 +08:00
|
|
|
|
2015-04-16 12:51:05 +08:00
|
|
|
my $res = $sth->execute( $mid )
|
2019-09-24 00:38:58 +08:00
|
|
|
or Fatal("Can't execute '$sql': ".$sth->errstr());
|
2009-06-08 17:19:55 +08:00
|
|
|
my $monitor = $sth->fetchrow_hashref();
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
if ( !$monitor ) {
|
|
|
|
Fatal("Can't find monitor '$mid'");
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
2019-09-24 00:38:58 +08:00
|
|
|
if ( !$monitor->{Controllable} ) {
|
|
|
|
Fatal("Monitor '$mid' is not controllable");
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
2019-09-24 00:38:58 +08:00
|
|
|
if ( !$monitor->{TrackMotion} ) {
|
|
|
|
Fatal("Monitor '$mid' is not configured to track motion");
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
if ( !$monitor->{CanMoveMap} ) {
|
|
|
|
if ( $monitor->{CanMoveRel} ) {
|
|
|
|
Warning("Monitor '$mid' cannot move in map mode, falling back to pseudo map mode");
|
|
|
|
} else {
|
|
|
|
Fatal("Monitor '$mid' cannot move in map mode");
|
|
|
|
}
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
exit(-1) if !zmMemVerify($monitor);
|
2009-06-08 17:19:55 +08:00
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
sub Suspend {
|
|
|
|
my $monitor = shift;
|
|
|
|
zmMonitorSuspend($monitor);
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
sub Resume {
|
|
|
|
my $monitor = shift;
|
|
|
|
sleep($monitor->{TrackDelay});
|
|
|
|
zmMonitorResume($monitor);
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
sub Track {
|
|
|
|
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);
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
sub Return {
|
|
|
|
my $monitor = shift;
|
2015-04-16 12:51:05 +08:00
|
|
|
|
2019-09-24 00:38:58 +08:00
|
|
|
my $ctrlCommand = $Config{ZM_PATH_BIN}
|
|
|
|
.'/zmcontrol.pl -i '
|
|
|
|
.$monitor->{Id}
|
|
|
|
;
|
|
|
|
if ( $monitor->{ReturnLocation} > 0 ) {
|
|
|
|
$ctrlCommand .= ' --command=presetGoto --preset='
|
|
|
|
.$monitor->{ReturnLocation}
|
2015-04-16 12:51:05 +08:00
|
|
|
;
|
2019-09-24 00:38:58 +08:00
|
|
|
} else {
|
|
|
|
$ctrlCommand .= ' --command=presetHome';
|
|
|
|
}
|
|
|
|
executeShellCommand($ctrlCommand);
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my $last_alarm = 0;
|
2019-09-24 00:38:58 +08:00
|
|
|
if ( ($monitor->{ReturnLocation} >= 0) ) {
|
|
|
|
Suspend($monitor);
|
|
|
|
Return($monitor);
|
|
|
|
Resume($monitor);
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my $alarmed = undef;
|
2019-09-24 00:38:58 +08:00
|
|
|
while( 1 ) {
|
|
|
|
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");
|
|
|
|
Suspend($monitor);
|
|
|
|
Track($monitor, $alarm_x, $alarm_y);
|
|
|
|
Resume($monitor);
|
|
|
|
$last_alarm = time();
|
|
|
|
$alarmed = !undef;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( logDebugging() && $alarmed ) {
|
|
|
|
Info('Left alarm state');
|
|
|
|
$alarmed = undef;
|
2015-04-16 12:51:05 +08:00
|
|
|
}
|
2019-09-24 00:38:58 +08:00
|
|
|
if ( ($monitor->{ReturnLocation} >= 0)
|
|
|
|
&& ($last_alarm > 0)
|
|
|
|
&& ((time()-$last_alarm) > $monitor->{ReturnDelay})
|
|
|
|
) {
|
|
|
|
Debug("Returning to location ".$monitor->{ReturnLocation});
|
|
|
|
Suspend($monitor);
|
|
|
|
Return($monitor);
|
|
|
|
Resume($monitor);
|
|
|
|
$last_alarm = 0;
|
2015-04-16 12:51:05 +08:00
|
|
|
}
|
2019-09-24 00:38:58 +08:00
|
|
|
}
|
|
|
|
usleep(SLEEP_TIME);
|
2009-06-08 17:19:55 +08:00
|
|
|
}
|
2019-09-24 00:38:58 +08:00
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|