Files
zoneminder/scripts/zmtrack.pl.in
T

302 lines
8.6 KiB
Plaintext

#!@PERL_EXECUTABLE@ -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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ==========================================================================
=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;
use autouse 'Data::Dumper'=>qw(Dumper);
use Getopt::Long;
use autouse 'Pod::Usage'=>qw(pod2usage);
use Time::HiRes qw( usleep );
$| = 1;
$ENV{PATH} = '@ZM_SCRIPT_PATH@';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my $mid = 0;
GetOptions( 'monitor=s'=>\$mid )
or pod2usage(-exitstatus => -1);
my ( $detaint_mid ) = $mid =~ /^(\d+)$/;
$mid = $detaint_mid;
logInit($mid?(id=>'zmtrack_m'.$mid):());
logSetSignal();
print( "Tracker daemon $mid (experimental) starting at "
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
."\n"
);
my $dbh = zmDbConnect();
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());
my $res = $sth->execute( $mid )
or Fatal("Can't execute '$sql': ".$sth->errstr());
my $monitor = $sth->fetchrow_hashref();
if ( !$monitor ) {
Fatal("Can't find monitor '$mid'");
}
if ( !$monitor->{Controllable} ) {
Fatal("Monitor '$mid' is not controllable");
}
if ( !$monitor->{TrackMotion} ) {
Fatal("Monitor '$mid' is not configured to track motion");
}
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");
}
}
exit(-1) if !zmMemVerify($monitor);
sub Suspend {
my $monitor = shift;
zmMonitorSuspend($monitor);
}
sub Resume {
my $monitor = shift;
sleep($monitor->{TrackDelay});
zmMonitorResume($monitor);
}
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}
;
if ( $monitor->{CanMoveMap} ) {
( $x, $y ) = mapPoint($monitor, $x, $y);
$ctrlCommand .= " --command=moveMap --xcoord=$x --ycoord=$y";
} else {
my $command = pseudoMapCommand($monitor, $x, $y);
return if !$command;
$ctrlCommand .= ' --command='.$command;
}
executeShellCommand($ctrlCommand);
}
# The alarm location is in the rotated or flipped image; moveMap takes a point
# in the camera's own Width x Height frame.
sub mapPoint {
my ( $monitor, $x, $y ) = @_;
my $orientation = $monitor->{Orientation};
my ( $width, $height ) = ( $monitor->{Width}, $monitor->{Height} );
if ( $orientation eq 'ROTATE_90' ) {
( $x, $y ) = ( $y, $height - $x );
} elsif ( $orientation eq 'ROTATE_180' ) {
( $x, $y ) = ( $width - $x, $height - $y );
} elsif ( $orientation eq 'ROTATE_270' ) {
( $x, $y ) = ( $width - $y, $x );
} elsif ( $orientation eq 'FLIP_HORI' ) {
$x = $width - $x;
} elsif ( $orientation eq 'FLIP_VERT' ) {
$y = $height - $y;
}
return ( $x, $y );
}
# No control module implements movePseudoMap, so turn the point into a
# relative move the same way the web does in buildControlCommand().
sub pseudoMapCommand {
my ( $monitor, $x, $y ) = @_;
my $orientation = $monitor->{Orientation};
my ( $width, $height ) = ( $monitor->{Width}, $monitor->{Height} );
( $width, $height ) = ( $height, $width )
if $orientation eq 'ROTATE_90' or $orientation eq 'ROTATE_270';
my $xFactor = ($x - $width/2)/($width/2);
my $yFactor = ($y - $height/2)/($height/2);
if ( $orientation eq 'ROTATE_90' ) {
( $xFactor, $yFactor ) = ( $yFactor, -$xFactor );
} elsif ( $orientation eq 'ROTATE_180' ) {
( $xFactor, $yFactor ) = ( -$xFactor, -$yFactor );
} elsif ( $orientation eq 'ROTATE_270' ) {
( $xFactor, $yFactor ) = ( -$yFactor, $xFactor );
} elsif ( $orientation eq 'FLIP_HORI' ) {
$xFactor = -$xFactor;
} elsif ( $orientation eq 'FLIP_VERT' ) {
$yFactor = -$yFactor;
}
my $blind = 0.1; # Threshold for blind spot
my $dirn = ( $yFactor > $blind ? 'Down' : $yFactor < -$blind ? 'Up' : '' )
.( $xFactor > $blind ? 'Right' : $xFactor < -$blind ? 'Left' : '' );
return if !$dirn; # In the blind spot in the middle
my $command = 'moveRel'.$dirn;
$command .= axisOptions($monitor, 'Pan', abs($xFactor)) if $dirn =~ /(Left|Right)$/;
$command .= axisOptions($monitor, 'Tilt', abs($yFactor)) if $dirn =~ /^(Up|Down)/;
return $command;
}
sub axisOptions {
my ( $monitor, $axis, $factor ) = @_;
my $turbo = 0.9; # Threshold for turbo speed
my $scale = sub {
my ( $min, $max, $f ) = @_;
$min //= 0;
$max //= 0;
# Rounds half away from zero, as PHP round() does; some ranges are negative.
my $v = $min+(($max-$min)*$f);
return $v < 0 ? -int(0.5-$v) : int($v+0.5);
};
my $options = ' --'.lc($axis).'step='
.$scale->($monitor->{"Min${axis}Step"}, $monitor->{"Max${axis}Step"}, $factor);
if ( $monitor->{"Has${axis}Speed"} ) {
my $speed;
if ( !$monitor->{"HasTurbo${axis}"} ) {
$speed = $scale->($monitor->{"Min${axis}Speed"}, $monitor->{"Max${axis}Speed"}, $factor);
} elsif ( $factor >= $turbo ) {
$speed = $monitor->{"Turbo${axis}Speed"};
} else {
$speed = $scale->($monitor->{"Min${axis}Speed"}, $monitor->{"Max${axis}Speed"}, $factor/$turbo);
}
$options .= ' --'.lc($axis).'speed='.$speed if defined($speed);
}
return $options;
}
sub Return {
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) ) {
Suspend($monitor);
Return($monitor);
Resume($monitor);
}
my $alarmed = undef;
while( 1 ) {
# zmc may have restarted since the last pass; verify (and re-attach) the
# shared memory before every read.
if ( !zmMemVerify($monitor) ) {
usleep(SLEEP_TIME);
next;
}
if ( zmIsAlarmed($monitor) ) {
my ( $alarm_x, $alarm_y ) = zmGetAlarmLocation($monitor);
if ( defined($alarm_x) && defined($alarm_y) && $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 ( $alarmed ) {
Info('Left alarm state') if logDebugging();
$alarmed = undef;
}
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;
}
}
usleep(SLEEP_TIME);
}
1;
__END__