Skip to content

Instantly share code, notes, and snippets.

@joshstrange
Created April 28, 2018 14:06
Show Gist options
  • Save joshstrange/73a2f24dfaf5cd5b470024096ce2680f to your computer and use it in GitHub Desktop.
Save joshstrange/73a2f24dfaf5cd5b470024096ce2680f to your computer and use it in GitHub Desktop.
Control script for Xiaomi Dafang
package ZoneMinder::Control::Xiaomi;
use 5.006;
use strict;
use warnings;
require ZoneMinder::Base;
require ZoneMinder::Control;
our @ISA = qw(ZoneMinder::Control);
our %CamParams = ();
# ==========================================================================
#
# Xiaomi Dafang Control Protocol
#
# On ControlAddress use the format :
# USERNAME:PASSWORD@ADDRESS
# eg : root:[email protected]
#
# ==========================================================================
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" );
my $req = HTTP::Request->new( GET=>"http://".$self->{Monitor}->{ControlAddress}."/cgi-bin/action.cgi?cmd=$cmd" );
my $res = $self->{ua}->request($req);
if ( $res->is_success )
{
$result = !undef;
}
else
{
Error( "Error check failed:'".$res->status_line()."'" );
}
return( $result );
}
# Reset the Camera
sub reset
{
my $self = shift;
Debug( "Camera Reset" );
$self->sendCmd( "reboot" );
}
#Up Arrow
sub moveRelUp
{
my $self = shift;
Debug( "Move Up" );
my $cmd = "decoder_control.cgi?command=0";
$self->sendCmd( "motor_up" );
}
#Down Arrow
sub moveRelDown
{
my $self = shift;
Debug( "Move Down" );
my $cmd = "decoder_control.cgi?command=2";
$self->sendCmd( "motor_down" );
}
#Left Arrow
sub moveRelLeft
{
my $self = shift;
Debug( "Move Left" );
my $cmd = "decoder_control.cgi?command=4";
$self->sendCmd( "motor_left" );
}
#Right Arrow
sub moveRelRight
{
my $self = shift;
Debug( "Move Right" );
my $cmd = "decoder_control.cgi?command=6";
$self->sendCmd( "motor_right" );
}
1;
@kaeltis
Copy link

kaeltis commented May 13, 2018

Thanks for the script!
I however noticed on my ZM installation, when clicking the round button in the middle of the Arrows (presetHome), the memory usage of zmcontrol.pl would spike up and use all available memory. This is most probably a bug of ZM, but as a workaround I added this to the script, which makes the button just do nothing instead of locking up the system:

#Home Button
sub presetHome
{
    #Unimplemented - workaround for ZM memory issue
    Debug( "Move Home" );
}

@jonny190
Copy link

Sorry to be late to the party but i'm getting:
root [15/Apr/2019:22:43:05 +0100] "GET /cgi-bin/action.cgi?cmd=motor_right HTTP/1.1" 301 0 "-" "ZoneMinder Control Agent/1.32.3"
when trying to adjust the camera could this be down to changes in zoneminder?

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