Advertisement
Guest User

(Broken) Keekoon control script for Zoneminder

a guest
Jul 18th, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.61 KB | None | 0 0
  1. # ==========================================================================
  2. #
  3. # ZoneMinder Keekoon Control Protocol Module
  4. #
  5. # This code was mostly derived from other modules
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. #
  21. # ==========================================================================
  22. #
  23. # Keekoon Control Protocol:
  24. #
  25. # ControlAddress in the camera definition, use the format :
  26. #   http(s)://username:password@address:port
  27. #
  28. #   eg : http://admin:adminpass@10.10.10.1:80
  29. #   or : https://admin:password@mycamera.example.co.uk:80
  30. #
  31. # These cameras have a default admin user but can have six more defined
  32. #     with membership of three groups
  33. # https is not directly supported but could be via say HA Proxy, so that
  34. #     is included rather than hardstrapping http://
  35. # =========================================================================
  36. #  
  37. # Example command from docs, at http://www.keekoonvision.com/for-developers-a:
  38. # Up: http://camera_ip:web_port/decoder_control.cgi?command=0&user=username&pwd=password
  39. #
  40. # BUT ... the docs are cobblers and basic auth is used instead of the last two parameters
  41. #  - verified with curl
  42. #
  43. ##### TODO: There are more commands than documented,
  44. #####       I must get Wireshark out and watch commands in response to clicks
  45. #####       Jon Gerdes
  46. #
  47. # ==========================================================================
  48. ### Lint:
  49. ### perl -Mstrict -Mdiagnostics -cw Keekoon.pm
  50.  
  51.  
  52. package ZoneMinder::Control::Keekoon;
  53.  
  54. use 5.006;
  55. use strict;
  56. use warnings;
  57.  
  58. require ZoneMinder::Control;
  59.  
  60. our @ISA = qw(ZoneMinder::Control);
  61.  
  62. use ZoneMinder::Logger qw(:all);
  63. use ZoneMinder::Config qw(:all);
  64. use Time::HiRes qw( usleep );
  65.  
  66. sub new
  67. {
  68.  
  69.     my $class = shift;
  70.     my $id = shift;
  71.     my $self = ZoneMinder::Control->new( $id );
  72.     my $logindetails = "";
  73.     bless( $self, $class );
  74.     srand( time() );
  75.     return $self;
  76. }
  77.  
  78. our $AUTOLOAD;
  79.  
  80. sub AUTOLOAD
  81. {
  82.     my $self = shift;
  83.     my $class = ref( ) || croak( "$self not object" );
  84.     my $name = $AUTOLOAD;
  85.     $name =~ s/.*://;
  86.     if ( exists($self->{$name}) )
  87.     {
  88.         return( $self->{$name} );
  89.     }
  90.     Fatal( "Can't access $name member of object of class $class" );
  91. }
  92.  
  93. sub open
  94. {
  95.     my $self = shift;
  96.  
  97.     $self->loadMonitor();
  98.  
  99.     use LWP::UserAgent;
  100.  
  101.     $self->{ua} = LWP::UserAgent->new;
  102.     $self->{ua}->agent( "ZoneMinder Control Agent/".ZoneMinder::Base::ZM_VERSION );
  103.  
  104.     $self->{state} = 'open';
  105. }
  106.  
  107. sub close
  108. {
  109.     my $self = shift;
  110.     $self->{state} = 'closed';
  111. }
  112.  
  113. sub printMsg
  114. {
  115.     my $self = shift;
  116.     my $msg = shift;
  117.     my $msg_len = length($msg);
  118.  
  119.     Debug( $msg."[".$msg_len."]" );
  120. }
  121.  
  122. # curl -XGET -u user:pass "http://cam.example.co.uk:80/decoder_control.cgi?command=1
  123. sub sendCmd
  124. {
  125.     my $self = shift;
  126.     my $cmd = shift;
  127.     my $result = undef;
  128.  
  129.     my ( $protocol, $user, $pass, $addr, $port )
  130.         = $self->{Monitor}->{ControlAddress} =~ /^(https?):\/\/(.*):(.*)@(.*):(\d+)$/;
  131.  
  132.     my $url = $protocol."://".$addr.":".$port."/decoder_control.cgi?command=".$cmd;
  133.  
  134.     my $req = HTTP::Request->new( GET=>$url );
  135.    
  136.     # Do Basic Auth
  137.     $req->authorization_basic($user, $pass);
  138.    
  139.     my $res = $self->{ua}->request($req);
  140.  
  141.     if ( $res->is_success )
  142.     {
  143.         $result = !undef;
  144.     }
  145.     else
  146.     {
  147.         Error( "Error check failed:'".$res->status_line()."'" );
  148.     }
  149.  
  150.     return( $result );
  151. }
  152.  
  153. # Set autoStop timeout on the Control tab for the camera
  154. sub autoStop
  155. {
  156.     my $self = shift;
  157.     my $stop_command = shift;
  158.     my $autostop = shift;
  159.    
  160.     if( $stop_command && $autostop)
  161.     {
  162.         Debug( "Auto Stop" );
  163.         usleep( $autostop );
  164.         my $cmd = $stop_command;
  165.         $self->sendCmd( $cmd );
  166.     }
  167. }
  168.  
  169. sub moveConUp
  170. {
  171.     my $self = shift;
  172.     my $cmd = "0";
  173.     my $stop_command = "1";
  174.     Debug( "Move Up" );
  175.     $self->sendCmd( $cmd );
  176.     $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
  177. }
  178.  
  179. sub moveConDown
  180. {
  181.     my $self = shift;
  182.     my $cmd = "2";
  183.     my $stop_command = "3";
  184.     Debug( "Move Down" );
  185.     $self->sendCmd( $cmd );
  186.     $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
  187. }
  188.  
  189. sub moveConLeft
  190. {
  191.     my $self = shift;
  192.     my $cmd = "4";
  193.     my $stop_command = "5";
  194.     Debug( "Move Left" );
  195.     $self->sendCmd( $cmd );
  196.     $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
  197. }
  198.  
  199. sub moveConRight
  200. {
  201.     my $self = shift;
  202.     my $cmd = "6";
  203.     my $stop_command = "7";
  204.     Debug( "Move Right" );
  205.     $self->sendCmd( $cmd );
  206.     $self->autoStop( $stop_command, $self->{Monitor}->{AutoStopTimeout} );
  207. }
  208.  
  209. sub moveConUpRight
  210. {
  211.     my $self = shift;
  212.     Debug( "Move Diagonally Up Right" );
  213.     $self->moveConUp( );
  214.     $self->moveConRight( );
  215. }
  216.  
  217. sub moveConDownRight
  218. {
  219.     my $self = shift;
  220.     Debug( "Move Diagonally Down Right" );
  221.     $self->moveConDown( );
  222.     $self->moveConRight( );
  223. }
  224.  
  225. sub moveConUpLeft
  226. {
  227.     my $self = shift;
  228.     Debug( "Move Diagonally Up Left" );
  229.     $self->moveConUp( );
  230.     $self->moveConLeft( );
  231. }
  232.  
  233. sub moveConDownLeft
  234. {
  235.     my $self = shift;
  236.     Debug( "Move Diagonally Down Left" );
  237.     $self->moveConDown( );
  238.     $self->moveConLeft( );
  239. }
  240.  
  241. # SET: 30,32,34,36,38,40 for presets 1-6
  242. sub presetSet
  243. {
  244.     my $self = shift;
  245.     my $params = shift;
  246.     my $preset = $self->getParam( $params, 'preset' );
  247.     Debug( "Set Preset $preset" );
  248.  
  249.     if (( $preset >= 1 ) && ( $preset <= 6 )) {
  250.         my $cmd = "decoder_control.cgi?command=".(($preset*2) + 28);
  251.         $self->sendCmd( $cmd );
  252.     }
  253. }
  254.  
  255. # GOTO: 31,33,35,37,39,41 for presets 1-6
  256. sub presetGoto
  257. {
  258.     my $self = shift;
  259.     my $params = shift;
  260.     my $preset = $self->getParam( $params, 'preset' );
  261.     Debug( "Goto Preset $preset" );
  262.  
  263.     if (( $preset >= 1 ) && ( $preset <= 6 )) {
  264.         my $cmd = "decoder_control.cgi?command=".(($preset*2) + 29);
  265.         $self->sendCmd( $cmd );
  266.     }
  267. }
  268.  
  269. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement