Skip to content

Instantly share code, notes, and snippets.

@imandarabi
Created June 24, 2014 05:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imandarabi/b6ebbc7a89c4ef6e6c34 to your computer and use it in GitHub Desktop.
Save imandarabi/b6ebbc7a89c4ef6e6c34 to your computer and use it in GitHub Desktop.
ZoneMinder Sony Network Camera SNC-EP521 Control Protocol Module
# ==========================================================================
#
# ZoneMinder Sony Network Camera SNC-EP521 Control Protocol Module, date: Sun Jun 22 10:26:25 IRDT 2014
# Copyright (C) 2013-2014 Iman Darabi <iman.darabi@gmail.com>
#
# 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 implementation of the Sony Network Camera SNC-EP521
# API camera control protocol
#
package ZoneMinder::Control::SNCEP521;
use 5.006;
use strict;
use warnings;
require ZoneMinder::Base;
require ZoneMinder::Control;
our @ISA = qw(ZoneMinder::Control);
our $VERSION = $ZoneMinder::Base::VERSION;
# ==========================================================================
#
# Sony Network Camera SNC-EP521 Control Protocol
#
# ==========================================================================
use ZoneMinder::Logger qw(:all);
use ZoneMinder::Config qw(:all);
sub new
{
my $class = shift;
my $id = shift;
my $self = ZoneMinder::Control->new( $id );
bless( $self, $class );
srand( time() );
return $self;
}
our $AUTOLOAD;
sub AUTOLOAD
{
my $self = shift;
my $class = ref($self) || croak( "$self not object" );
my $name = $AUTOLOAD;
$name =~ s/.*://;
if ( exists($self->{$name}) )
{
return( $self->{$name} );
}
Fatal( "Can't access $name member of object of class $class" );
}
sub open
{
my $self = shift;
$self->loadMonitor();
use LWP::UserAgent;
$self->{ua} = LWP::UserAgent->new;
$self->{ua}->agent( "ZoneMinder Control Agent/".ZM_VERSION );
$self->{state} = 'open';
}
sub close
{
my $self = shift;
$self->{state} = 'closed';
}
sub printMsg
{
my $self = shift;
my $msg = shift;
my $msg_len = length($msg);
Debug( $msg."[".$msg_len."]" );
}
sub sendCmd
{
my $self = shift;
my $cmd = shift;
my $result = undef;
printMsg( $cmd, "Tx" );
#print( "http://$address/$cmd\n" );
my $req = HTTP::Request->new( GET=>"http://".$self->{Monitor}->{ControlAddress}."/$cmd" );
my $res = $self->{ua}->request($req);
if ( $res->is_success )
{
$result = !undef;
}
else
{
Error( "Error check failed: '".$res->status_line()."'" );
}
return( $result );
}
sub moveConUp
{
my $self = shift;
Debug( "Move Up" );
my $cmd = "/command/ptzf.cgi?Move=up,8,1";
$self->sendCmd( $cmd );
}
sub moveConDown
{
my $self = shift;
Debug( "Move Down" );
my $cmd = "/command/ptzf.cgi?Move=down,8,1";
$self->sendCmd( $cmd );
}
sub moveConLeft
{
my $self = shift;
Debug( "Move Left" );
my $cmd = "/command/ptzf.cgi?Move=left,8,1";
$self->sendCmd( $cmd );
}
sub moveConRight
{
my $self = shift;
Debug( "Move Right" );
my $cmd = "/command/ptzf.cgi?Move=right,8,1";
$self->sendCmd( $cmd );
}
sub moveConUpRight
{
my $self = shift;
Debug( "Move Up/Right" );
my $cmd = "/command/ptzf.cgi?Move=up-right,8,1";
$self->sendCmd( $cmd );
}
sub moveConUpLeft
{
my $self = shift;
Debug( "Move Up/Left" );
my $cmd = "/command/ptzf.cgi?Move=up-left,8,1";
$self->sendCmd( $cmd );
}
sub moveConDownRight
{
my $self = shift;
Debug( "Move Down/Right" );
my $cmd = "/command/ptzf.cgi?Move=down-right,8,1";
$self->sendCmd( $cmd );
}
sub moveConDownLeft
{
my $self = shift;
Debug( "Move Down/Left" );
my $cmd = "/command/ptzf.cgi?Move=down-left,8,1";
$self->sendCmd( $cmd );
}
sub moveMap
{
my $self = shift;
my $params = shift;
my $xcoord = $self->getParam( $params, 'xcoord' );
my $ycoord = $self->getParam( $params, 'ycoord' );
Debug( "Move Map to $xcoord,$ycoord" );
my $cmd = "/command/ptzf.cgi?AreaZoom=$xcoord,$ycoord,$self->{Monitor}->{Width},$self->{Monitor}->{Height}";
$self->sendCmd( $cmd );
}
sub moveRelUp
{
my $self = shift;
my $params = shift;
my $step = $self->getParam( $params, 'tiltstep' );
Debug( "Step Up $step" );
my $cmd = "/command/ptzf.cgi?relative=08$step";
$self->sendCmd( $cmd );
}
sub moveRelDown
{
my $self = shift;
my $params = shift;
my $step = $self->getParam( $params, 'tiltstep' );
Debug( "Step Down $step" );
my $cmd = "/command/ptzf.cgi?relative=02$step";
$self->sendCmd( $cmd );
}
sub moveRelLeft
{
my $self = shift;
my $params = shift;
my $step = $self->getParam( $params, 'panstep' );
Debug( "Step Left $step" );
my $cmd = "/command/ptzf.cgi?relative=04$step";
$self->sendCmd( $cmd );
}
sub moveRelRight
{
my $self = shift;
my $params = shift;
my $step = $self->getParam( $params, 'panstep' );
Debug( "Step Right $step" );
my $cmd = "/command/ptzf.cgi?relative=06$step";
$self->sendCmd( $cmd );
}
sub moveRelUpRight
{
my $self = shift;
my $params = shift;
my $panstep = $self->getParam( $params, 'panstep' );
my $tiltstep = $self->getParam( $params, 'tiltstep' );
Debug( "Step Up/Right $tiltstep/$panstep" );
my $cmd = "/command/ptzf.cgi?relative=0905";
$self->sendCmd( $cmd );
}
sub moveRelUpLeft
{
my $self = shift;
my $params = shift;
my $panstep = $self->getParam( $params, 'panstep' );
my $tiltstep = $self->getParam( $params, 'tiltstep' );
Debug( "Step Up/Left $tiltstep/$panstep" );
my $cmd = "/command/ptzf.cgi?relative=0705";
$self->sendCmd( $cmd );
}
sub moveRelDownRight
{
my $self = shift;
my $params = shift;
my $panstep = $self->getParam( $params, 'panstep' );
my $tiltstep = $self->getParam( $params, 'tiltstep' );
Debug( "Step Down/Right $tiltstep/$panstep" );
my $cmd = "/command/ptzf.cgi?relative=0305";
$self->sendCmd( $cmd );
}
sub moveRelDownLeft
{
my $self = shift;
my $params = shift;
my $panstep = $self->getParam( $params, 'panstep' );
my $tiltstep = $self->getParam( $params, 'tiltstep' );
Debug( "Step Down/Left $tiltstep/$panstep" );
my $cmd = "/command/ptzf.cgi?relative=0105";
$self->sendCmd( $cmd );
}
sub moveStop
{
my $self = shift;
Debug( "Move Stop" );
my $cmd = "/command/ptzf.cgi?Move=stop,pantilt,1";
$self->sendCmd( $cmd );
my $cmd = "/command/ptzf.cgi?Move=stop,zoom,1";
$self->sendCmd( $cmd );
}
sub zoomRelTele
{
my $self = shift;
my $params = shift;
my $step = $self->getParam( $params, 'step' );
Debug( "Zoom Tele" );
my $cmd = "/command/ptzf.cgi?Move=tele,8";
$self->sendCmd( $cmd );
$self->sendCmd( $cmd ); # do it twice, so faster, why <speed> don't affect?
my $cmd = "/command/ptzf.cgi?Move=stop,zoom,1";
$self->sendCmd( $cmd );
}
sub zoomRelWide
{
my $self = shift;
my $params = shift;
my $step = $self->getParam( $params, 'step' );
Debug( "Zoom Wide" );
my $cmd = "/command/ptzf.cgi?Move=wide,8";
$self->sendCmd( $cmd );
$self->sendCmd( $cmd ); # do it twice, so faster, why <speed> don't affect?
my $cmd = "/command/ptzf.cgi?Move=stop,zoom,1";
$self->sendCmd( $cmd );
}
sub zoomConWide
{
my $self = shift;
Debug( "zoom ConWide" );
my $cmd = "/command/ptzf.cgi?Move=wide,4";
$self->sendCmd( $cmd );
}
sub zoomConTele
{
my $self = shift;
Debug( "zoom ConTele" );
my $cmd = "/command/ptzf.cgi?Move=tele,4";
$self->sendCmd( $cmd );
}
sub presetClear
{
my $self = shift;
my $params = shift;
my $preset = $self->getParam( $params, 'preset' );
Debug( "Clear Preset $preset" );
my $cmd = "/command/presetposition.cgi?PresetClear=$preset,1";
$self->sendCmd( $cmd );
}
sub presetSet
{
my $self = shift;
my $params = shift;
my $preset = $self->getParam( $params, 'preset' );
Debug( "Set Preset $preset" );
my $cmd = "/command/presetposition.cgi?PresetSet=$preset,No.$preset,on,1";
$self->sendCmd( $cmd );
}
sub presetGoto
{
my $self = shift;
my $params = shift;
my $preset = $self->getParam( $params, 'preset' );
Debug( "Goto Preset $preset" );
my $cmd = "/command/presetposition.cgi?PresetCall=$preset,12,1";
$self->sendCmd( $cmd );
}
sub presetHome
{
my $self = shift;
Debug( "Home Preset" );
my $cmd = "/command/presetposition.cgi?HomePos=ptz-recall";
$self->sendCmd( $cmd );
}
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
iman darabi, E<lt>iman.darabi@gmail.com<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2013-2014 Iman Darabi
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
@IncompleteString
Copy link

IncompleteString commented Jan 8, 2020

I was getting the following errors, so I updated the script to make it compile correctly. Works fine on ZM 1.33.16 now

root@zm:/usr/share/perl5/ZoneMinder/Control# perl SNCEP521.pm
"my" variable $cmd masks earlier declaration in same scope at SNCEP521.pm line 304.
"my" variable $cmd masks earlier declaration in same scope at SNCEP521.pm line 321.
"my" variable $cmd masks earlier declaration in same scope at SNCEP521.pm line 336.
Bareword "ZM_VERSION" not allowed while "strict subs" in use at SNCEP521.pm line 80.

My revisions follow:

Line 80 reads as follows:
$self->{ua}->agent( "ZoneMinder Control Agent/".ZoneMinder::Base::ZM_VERSION );

Line 304 / 305 reads as follows:

    my $cmd1 = "/command/ptzf.cgi?Move=stop,zoom,1";
    $self->sendCmd( $cmd1 );

Line 321 / 322 read as follows:

    my $cmd1 = "/command/ptzf.cgi?Move=stop,zoom,1";
    $self->sendCmd( $cmd1 );

Line 336 / 337 read as follows:

    my $cmd1 = "/command/ptzf.cgi?Move=stop,zoom,1";
    $self->sendCmd( $cmd1 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment