DAHUA PTZ control

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
bobylapointe69300
Posts: 10
Joined: Wed Feb 14, 2018 6:51 pm

DAHUA PTZ control

Post by bobylapointe69300 » Wed Feb 14, 2018 9:24 pm

Hi,
Here's a PTZ control script based on the onvif.pm already available. My IPC is a DAHUA model P2 (equivalent to an IPC-AW12W), but this should be working with any dome like ptz IPC from Dahua, as long as you can control it with API HTTP commands. I strongly advise you to test the http control of your camera in a browser, using API commands listed in below documentation.

My ZM is v1.30.4, on Ubuntu 16.04 LTS server.

With this script you should control following functions on your ipc :
- Pan move
- Tilt move
- Diagonal move
- Zoom in and out
- Brightness +/-
- Contrast +/-
- Presets (goto and set)
- Reboot


HTTP commands are based on last DAHUA API for IPC doc (dahua_http_api_for_ipc_v1.56.pdf, downloadable from here:
https://www.telecamera.ru/bitrix/compon ... n=download).

I will not guide you through all the installation process of a control script as this has already been covered, and you can check ZM doc. Here is the code that should be placed in a proper .pm file, in control folder:

Code: Select all

# ==========================================================================
#
# ZoneMinder ONVIF Control Protocol Module
# 
# 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.
#
# ==========================================================================
#
# This module contains the implementation of the ONVIF device control protocol
# for a DAHUA P2 (PTZ dome like IPC)
package ZoneMinder::Control::onvif;

use 5.006;
use strict;
use warnings;

require ZoneMinder::Control;

our @ISA = qw(ZoneMinder::Control);

our %CamParams = ();

# ==========================================================================
#
# ONVIF Control Protocol
#
# Control Address field on Control tab should be like :
#   admin:password@192.168.1.XX:80
#   adjust with your own parameters (IP, port, user...)
# ==========================================================================

use ZoneMinder::Logger qw(:all);
use ZoneMinder::Config qw(:all);

use Time::HiRes qw( usleep );

sub new
{

    my $class = shift;
    my $id = shift;
    my $self = ZoneMinder::Control->new( $id );
    my $logindetails = "";
    bless( $self, $class );
    srand( time() );
    return $self;
}

our $AUTOLOAD;

sub AUTOLOAD
{
    my $self = shift;
    my $class = ref( ) || 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/".ZoneMinder::Base::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" );
# this line tells how API http orders will be passed to the camera    
    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 );
}

#autoStop
#To Auto Stop you need to fill Timeout field with a value in Control Tab. This is used to stop a continuous movement
sub autoStop
{
    my $self = shift;
    my $stop_command = shift;
    my $autostop = shift;
    if( $stop_command && $autostop)
    {
        Debug( "Auto Stop" );
        usleep( $autostop );
# this line is used to stop a specific movement/command that is already launched 
        my $cmd = "cgi-bin/ptz.cgi?action=stop&channel=0".$stop_command;
        $self->sendCmd( $cmd );
    }
}

# Reboot the Camera
sub reset
{
    my $self = shift;
    Debug( "Camera Reset" );
    my $cmd = "cgi-bin/magicBox.cgi?action=reboot";
    $self->sendCmd( $cmd );
}

#Up Arrow
sub moveConUp
{
    my $self = shift;
# you can change the speed of movement 1-8 with arg 2, same for Down, Right...    
    my $stop_command = "&code=Up&arg1=0&arg2=1&arg3=0&arg4=0";
    Debug( "Move Up" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=Up&arg1=0&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Down Arrow
sub moveConDown
{
    my $self = shift;
    my $stop_command = "&code=Down&arg1=0&arg2=1&arg3=0&arg4=0";
    Debug( "Move Down" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=Down&arg1=0&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Left Arrow
sub moveConLeft
{
    my $self = shift;
    my $stop_command = "&code=Left&arg1=0&arg2=1&arg3=0&arg4=0";
    Debug( "Move Left" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=Left&arg1=0&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Right Arrow
sub moveConRight
{
    my $self = shift;
    my $stop_command = "&code=Right&arg1=0&arg2=1&arg3=0&arg4=0";
    Debug( "Move Right" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=Right&arg1=0&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Zoom In
sub zoomConTele
{
    my $self = shift;
    my $stop_command = "&code=ZoomTele&arg1=0&arg2=1&arg3=0&arg4=0";
    Debug( "Zoom Tele" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=ZoomTele&arg1=0&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Zoom Out
sub zoomConWide
{
    my $self = shift;
    my $stop_command = "&code=ZoomWide&arg1=0&arg2=1&arg3=0&arg4=0";
    Debug( "Zoom Wide" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=ZoomWide&arg1=0&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Diagonally Up Right Arrow
sub moveConUpRight
{
    my $self = shift;
    my $stop_command = "&code=RightUp&arg1=1&arg2=1&arg3=0&arg4=0";
    Debug( "Move Diagonally Up Right" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=RightUp&arg1=1&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );    
}

#Diagonally Down Right Arrow
sub moveConDownRight
{
    my $self = shift;
    my $stop_command = "&code=RightDown&arg1=1&arg2=1&arg3=0&arg4=0";
    Debug( "Move Diagonally Down Right" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=RightDown&arg1=1&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Diagonally Up Left Arrow
sub moveConUpLeft
{
    my $self = shift;
    my $stop_command = "&code=LeftUp&arg1=1&arg2=1&arg3=0&arg4=0";
    Debug( "Move Diagonally Up Left" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=LeftUp&arg1=1&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );}

#Diagonally Down Left Arrow
sub moveConDownLeft
{
    my $self = shift;
    my $stop_command = "&code=LeftDown&arg1=1&arg2=1&arg3=0&arg4=0";
    Debug( "Move Diagonally Up Right" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=LeftDown&arg1=1&arg2=1&arg3=0&arg4=0";
    $self->sendCmd( $cmd );
    $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
}

#Stop, for center button on control panel
sub moveStop
{
    my $self = shift;
    Debug( "Move Stop" );
# you can put any kind of code to stop the camera, I put Up by default. 
    my $cmd = "cgi-bin/ptz.cgi?action=stop&channel=0&code=Up&arg1=0&arg2=1&arg3=0";
    $self->sendCmd( $cmd );
}

#Goto preset
sub presetGoto
{
    my $self = shift;
    my $params = shift;
    my $preset = $self->getParam( $params, 'preset' );
    Debug( "Goto Preset $preset" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=GotoPreset&arg1=0&arg2=$preset&arg3=0";
    $self->sendCmd( $cmd );
}

#Set Camera Preset
sub presetSet
{
    my $self = shift;
    my $params = shift;
    my $preset = $self->getParam( $params, 'preset' );
    Debug( "Set Preset $preset" );
    my $cmd = "cgi-bin/ptz.cgi?action=start&channel=0&code=SetPreset&arg1=0&arg2=$preset&arg3=0";
    $self->sendCmd( $cmd );
   }

# Increase Brightness
sub irisAbsOpen
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    $CamParams{'brightness'} += $step;
    $CamParams{'brightness'} = 100 if ($CamParams{'brightness'} > 100);
    Debug( "Iris $CamParams{'brightness'}" );
# if this has no action on your camera, try to play with the [2] that could be either 0 or 1
# you can also check with this HTTP command : /cgi-bin/configManager.cgi?action=getConfig&name=VideoColor  
    my $cmd = "cgi-bin/configManager.cgi?action=setConfig&VideoColor[0][2].Brightness=".$CamParams{'brightness'};
    $self->sendCmd( $cmd );
}

# Decrease Brightness
sub irisAbsClose
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    $CamParams{'brightness'} -= $step;
    $CamParams{'brightness'} = 0 if ($CamParams{'brightness'} < 0);
    Debug( "Iris $CamParams{'brightness'}" );
    my $cmd = "cgi-bin/configManager.cgi?action=setConfig&VideoColor[0][2].Brightness=".$CamParams{'brightness'};
    $self->sendCmd( $cmd );
}

# Increase Contrast
sub whiteAbsIn
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    $CamParams{'contrast'} += $step;
    $CamParams{'contrast'} = 100 if ($CamParams{'contrast'} > 100);
    Debug( "Iris $CamParams{'contrast'}" );
    my $cmd = "cgi-bin/configManager.cgi?action=setConfig&VideoColor[0][2].Contrast=".$CamParams{'contrast'};
    $self->sendCmd( $cmd );
}

# Decrease Contrast
sub whiteAbsOut
{
    my $self = shift;
    my $params = shift;
    my $step = $self->getParam( $params, 'step' );
    $CamParams{'contrast'} -= $step;
    $CamParams{'contrast'} = 0 if ($CamParams{'contrast'} < 0);
    Debug( "Iris $CamParams{'contrast'}" );
    my $cmd = "cgi-bin/configManager.cgi?action=setConfig&VideoColor[0][2].Contrast=".$CamParams{'contrast'};
    $self->sendCmd( $cmd );
}

1;
and the way you should edit the corresponding control type:

Image
ImageImageImageImageImageImageImageImageImage

Remarks:
- use "white" control to adjust brightness (confusing as DAHUA IPC also have a white balance feature... better keep camera real WB in auto mode)
- use "iris" control to adjust contrast
- adjust range (0-100) for both brightness and contrast
- an improvement could be to use the tour/patrol commands of Dahua, but I do not use this feature...
- depending on your network/speed, you may want to decrease the resolution to avoid lag, or better use the PTZ presets
- feel free to adjust the number of presets
- Thanks to the guy who wrote original onvif.pm
:D :mrgreen:

brononius
Posts: 14
Joined: Thu Aug 16, 2012 5:10 am

Re: DAHUA PTZ control

Post by brononius » Sat Feb 29, 2020 6:27 pm

Hey,

What do you use as 'control adress' for the control tab?
When I just select 'Dahua as control type, it doesn't do much. :$

alabamatoy
Posts: 195
Joined: Sun Jun 05, 2016 2:53 pm

Re: DAHUA PTZ control

Post by alabamatoy » Sat Feb 29, 2020 9:43 pm

The Amcrest supposedly are rebranded Dahua (confirm, deny anyone?)

Did you try the Amcrest script? I have done no comparison of their API docs, so they may be completely different.

brononius
Posts: 14
Joined: Thu Aug 16, 2012 5:10 am

Re: DAHUA PTZ control

Post by brononius » Tue Mar 03, 2020 8:36 am

Just tried, not working either.
But not sure if I've got the remote address correct. Now I'm just using the IP.

alabamatoy
Posts: 195
Joined: Sun Jun 05, 2016 2:53 pm

Re: DAHUA PTZ control

Post by alabamatoy » Thu Mar 05, 2020 10:07 pm

the control address should be login:password@ipaddress:80

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests