2019-09-24 00:54:27 +08:00
|
|
|
#!@PERL_EXECUTABLE@ -wT
|
2007-08-30 18:43:06 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder Control Script, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
# Copyright (C) 2001-2008 Philip Coombes
|
2007-08-30 18:43:06 +08:00
|
|
|
#
|
|
|
|
# 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.
|
2007-08-30 18:43:06 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
2015-04-11 02:30:21 +08:00
|
|
|
|
2007-08-30 18:43:06 +08:00
|
|
|
use strict;
|
|
|
|
|
2009-06-08 17:11:56 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2007-08-30 18:43:06 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use Getopt::Long;
|
2015-04-11 02:30:21 +08:00
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
2020-11-21 22:12:41 +08:00
|
|
|
use POSIX qw/strftime EPIPE EINTR/;
|
2007-08-30 18:43:06 +08:00
|
|
|
use Socket;
|
2019-10-09 06:06:52 +08:00
|
|
|
use Data::Dumper;
|
2019-04-06 03:18:03 +08:00
|
|
|
use Module::Load::Conditional qw{can_load};
|
2007-08-30 18:43:06 +08:00
|
|
|
|
2018-08-13 23:05:50 +08:00
|
|
|
use constant MAX_CONNECT_DELAY => 15;
|
2007-08-30 18:43:06 +08:00
|
|
|
use constant MAX_COMMAND_WAIT => 1800;
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
2016-03-12 05:28:16 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
|
2007-08-30 18:43:06 +08:00
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2019-04-06 03:18:03 +08:00
|
|
|
my $arg_string = join(' ', @ARGV);
|
2007-08-30 18:43:06 +08:00
|
|
|
|
|
|
|
my $id;
|
|
|
|
my %options;
|
|
|
|
|
2015-04-11 02:30:21 +08:00
|
|
|
GetOptions(
|
2018-08-13 23:05:50 +08:00
|
|
|
'id=i' =>\$id,
|
|
|
|
'command=s' =>\$options{command},
|
2020-06-27 04:49:55 +08:00
|
|
|
'xcoord=f' =>\$options{xcoord},
|
|
|
|
'ycoord=f' =>\$options{ycoord},
|
|
|
|
'speed=f' =>\$options{speed},
|
2018-08-13 23:05:50 +08:00
|
|
|
'step=i' =>\$options{step},
|
|
|
|
'panspeed=i' =>\$options{panspeed},
|
|
|
|
'tiltspeed=i' =>\$options{tiltspeed},
|
|
|
|
'panstep=i' =>\$options{panstep},
|
|
|
|
'tiltstep=i' =>\$options{tiltstep},
|
|
|
|
'preset=i' =>\$options{preset},
|
|
|
|
'autostop' =>\$options{autostop},
|
2015-04-11 02:30:21 +08:00
|
|
|
) or pod2usage(-exitstatus => -1);
|
2007-08-30 18:43:06 +08:00
|
|
|
|
2019-04-02 05:26:24 +08:00
|
|
|
if ( !$id ) {
|
2019-04-06 03:18:03 +08:00
|
|
|
print(STDERR "Please give a valid monitor id\n");
|
2018-08-13 23:05:50 +08:00
|
|
|
pod2usage(-exitstatus => -1);
|
2007-11-05 01:30:29 +08:00
|
|
|
}
|
|
|
|
|
2007-08-30 18:43:06 +08:00
|
|
|
( $id ) = $id =~ /^(\w+)$/;
|
2019-09-24 00:21:59 +08:00
|
|
|
logInit($id?(id=>'zmcontrol_'.$id):());
|
2007-08-30 18:43:06 +08:00
|
|
|
|
2013-12-17 05:32:02 +08:00
|
|
|
my $sock_file = $Config{ZM_PATH_SOCKS}.'/zmcontrol-'.$id.'.sock';
|
2019-04-06 03:18:03 +08:00
|
|
|
Debug("zmcontrol: arg string: $arg_string sock file $sock_file");
|
2007-08-30 18:43:06 +08:00
|
|
|
|
2019-04-06 03:18:03 +08:00
|
|
|
socket(CLIENT, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!");
|
2018-08-13 23:05:50 +08:00
|
|
|
|
|
|
|
my $saddr = sockaddr_un($sock_file);
|
2019-04-06 03:18:03 +08:00
|
|
|
|
|
|
|
if ( $options{command} ) {
|
|
|
|
# Have a command, so we are the client, connect to the server and send it.
|
|
|
|
|
|
|
|
my $tries = 10;
|
|
|
|
my $server_up;
|
|
|
|
while ( $tries and ! ( $server_up = connect(CLIENT, $saddr) ) ) {
|
2019-09-20 22:36:08 +08:00
|
|
|
Debug("Failed to connect to zmcontrol server at $sock_file");
|
2020-09-21 23:56:56 +08:00
|
|
|
runCommand("zmdc.pl start zmcontrol.pl --id $id");
|
2019-04-06 03:18:03 +08:00
|
|
|
sleep 1;
|
|
|
|
$tries -= 1;
|
|
|
|
}
|
|
|
|
if ( $server_up ) {
|
|
|
|
# The server is there, connect to it
|
|
|
|
#print( "Writing commands\n" );
|
|
|
|
CLIENT->autoflush();
|
|
|
|
|
|
|
|
if ( $options{command} ) {
|
|
|
|
my $message = jsonEncode(\%options);
|
|
|
|
print(CLIENT $message);
|
|
|
|
}
|
|
|
|
shutdown(CLIENT, 1);
|
|
|
|
} else {
|
|
|
|
Error("Unable to connect to zmcontrol server at $sock_file");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2018-08-13 23:05:50 +08:00
|
|
|
# The server isn't there
|
|
|
|
my $monitor = zmDbGetMonitorAndControl($id);
|
|
|
|
if ( !$monitor ) {
|
|
|
|
Fatal("Unable to load control data for monitor $id");
|
|
|
|
}
|
|
|
|
my $protocol = $monitor->{Protocol};
|
2020-01-11 06:20:32 +08:00
|
|
|
if ( !$protocol ) {
|
|
|
|
Fatal('No protocol is set in monitor. Please edit the monitor, edit control type, select the control capability and fill in the Protocol field');
|
|
|
|
}
|
2018-08-13 23:05:50 +08:00
|
|
|
|
|
|
|
if ( -x $protocol ) {
|
|
|
|
# Protocol is actually a script!
|
|
|
|
# Holdover from previous versions
|
|
|
|
my $command .= $protocol.' '.$arg_string;
|
|
|
|
Debug($command);
|
|
|
|
|
|
|
|
my $output = qx($command);
|
|
|
|
my $status = $? >> 8;
|
|
|
|
if ( $status || logDebugging() ) {
|
|
|
|
chomp($output);
|
|
|
|
Debug("Output: $output");
|
2007-11-05 01:30:29 +08:00
|
|
|
}
|
2018-08-13 23:05:50 +08:00
|
|
|
if ( $status ) {
|
|
|
|
Error("Command '$command' exited with status: $status");
|
|
|
|
exit($status);
|
2008-01-08 20:33:11 +08:00
|
|
|
}
|
2018-08-13 23:05:50 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Info("Starting control server $id/$protocol");
|
|
|
|
close(CLIENT);
|
|
|
|
|
|
|
|
if ( ! can_load( modules => { "ZoneMinder::Control::$protocol" => undef } ) ) {
|
|
|
|
Fatal("Can't load ZoneMinder::Control::$protocol\n$Module::Load::Conditional::ERROR");
|
|
|
|
}
|
|
|
|
|
2019-04-06 03:18:03 +08:00
|
|
|
Info("Control server $id/$protocol starting at "
|
|
|
|
.strftime('%y/%m/%d %H:%M:%S', localtime())
|
|
|
|
);
|
|
|
|
|
2020-09-21 23:56:56 +08:00
|
|
|
$0 = $0.' --id '.$id;
|
2019-04-06 03:18:03 +08:00
|
|
|
|
2020-09-21 23:56:56 +08:00
|
|
|
my $control = ('ZoneMinder::Control::'.$protocol)->new($id);
|
2019-04-06 03:18:03 +08:00
|
|
|
my $control_key = $control->getKey();
|
|
|
|
$control->loadMonitor();
|
|
|
|
|
|
|
|
$control->open();
|
|
|
|
|
|
|
|
# If we have a command when starting up, then do it.
|
|
|
|
if ( $options{command} ) {
|
|
|
|
my $command = $options{command};
|
|
|
|
$control->$command(\%options);
|
|
|
|
}
|
|
|
|
|
|
|
|
socket(SERVER, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!");
|
|
|
|
unlink($sock_file);
|
|
|
|
bind(SERVER, $saddr) or Fatal("Can't bind: $!");
|
|
|
|
listen(SERVER, SOMAXCONN) or Fatal("Can't listen: $!");
|
|
|
|
|
|
|
|
my $rin = '';
|
|
|
|
vec( $rin, fileno(SERVER), 1 ) = 1;
|
|
|
|
my $win = $rin;
|
|
|
|
my $ein = $win;
|
|
|
|
my $timeout = MAX_COMMAND_WAIT;
|
|
|
|
while( 1 ) {
|
|
|
|
my $nfound = select(my $rout = $rin, undef, undef, $timeout);
|
|
|
|
if ( $nfound > 0 ) {
|
|
|
|
if ( vec( $rout, fileno(SERVER), 1 ) ) {
|
|
|
|
my $paddr = accept(CLIENT, SERVER);
|
|
|
|
my $message = <CLIENT>;
|
|
|
|
|
|
|
|
next if !$message;
|
|
|
|
|
|
|
|
my $params = jsonDecode($message);
|
2019-10-09 06:06:52 +08:00
|
|
|
Debug( Dumper( $params ) );
|
2019-04-06 03:18:03 +08:00
|
|
|
|
|
|
|
my $command = $params->{command};
|
2019-10-09 06:06:52 +08:00
|
|
|
close(CLIENT);
|
2019-04-06 03:18:03 +08:00
|
|
|
if ( $command eq 'quit' ) {
|
|
|
|
last;
|
2019-10-09 06:06:52 +08:00
|
|
|
} elsif ( $command ) {
|
|
|
|
$control->$command($params);
|
|
|
|
} else {
|
|
|
|
Warning("No command in $message");
|
2007-08-30 18:43:06 +08:00
|
|
|
}
|
2018-08-13 23:05:50 +08:00
|
|
|
} else {
|
2019-04-06 03:18:03 +08:00
|
|
|
Fatal('Bogus descriptor');
|
2018-08-13 23:05:50 +08:00
|
|
|
}
|
2019-04-06 03:18:03 +08:00
|
|
|
} elsif ( $nfound < 0 ) {
|
2020-11-21 22:12:41 +08:00
|
|
|
if ( $! == EINTR ) {
|
|
|
|
# Likely just SIGHUP
|
|
|
|
Debug("Can't select: $!");
|
|
|
|
} elsif ( $! == EPIPE ) {
|
2019-04-06 03:18:03 +08:00
|
|
|
Error("Can't select: $!");
|
|
|
|
} else {
|
|
|
|
Fatal("Can't select: $!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#print( "Select timed out\n" );
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
} # end while forever
|
|
|
|
Info("Control server $id/$protocol exiting");
|
|
|
|
unlink($sock_file);
|
|
|
|
$control->close();
|
|
|
|
exit(0);
|
2018-08-13 23:05:50 +08:00
|
|
|
} # end if !server up
|
2007-08-30 18:43:06 +08:00
|
|
|
|
2018-08-13 23:05:50 +08:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
zmcontrol.pl - ZoneMinder control script
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
2019-04-02 05:26:24 +08:00
|
|
|
zmcontrol.pl --id {monitor_id} [--command={command}] [various options]
|
2018-08-13 23:05:50 +08:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
2007-08-30 18:43:06 +08:00
|
|
|
|
2018-08-13 23:05:50 +08:00
|
|
|
FIXME FIXME
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
|
|
|
--autostop -
|
|
|
|
--xcoord [ arg ] - X-coord
|
|
|
|
--ycoord [ arg ] - Y-coord
|
|
|
|
--speed [ arg ] - Speed
|
|
|
|
--step [ arg ] -
|
|
|
|
--panspeed [ arg ] -
|
|
|
|
--panstep [ arg ] -
|
|
|
|
--tiltspeed [ arg ] -
|
|
|
|
--tiltstep [ arg ] -
|
|
|
|
--preset [ arg ] -
|
|
|
|
|
|
|
|
=cut
|