# ==========================================================================
#
# ZoneMinder Vstarcam C7816WIP IP Control Protocol Module, $Date: 2017-01-15 01:18:10 +0100 (Wed, 04 Nov 2009) $, $Revision: 1 $
# Copyright (C) 2017 Mentor (http://www.internauta37.altervista.org)
#
# 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 is based on WanscamHW0025.pm
#
# ==========================================================================
#
# This module contains the implementation of the Vstarcam C7816WIP IP camera control
# protocol
#
package ZoneMinder::Control::C7816WIP;
 
use 5.006;
use strict;
use warnings;

require ZoneMinder::Base;
require ZoneMinder::Control;

our @ISA = qw(ZoneMinder::Control);
 
# ==========================================================================
#
# Vstarcam C7816WIP IP Control Protocol
#
# ==========================================================================
 
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 );
	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/".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}."/$cmd".$self->{Monitor}->{ControlDevice} );
	Info( "http://".$self->{Monitor}->{ControlAddress}."/$cmd".$self->{Monitor}->{ControlDevice} );
    my $res = $self->{ua}->request($req);

    if ( $res->is_success )
    {
        $result = !undef;
    }
    else
    {
        Error( "Error check failed: '".$res->status_line()."'" );
    }

    return( $result );
}

 
sub reset
{
	my $self = shift;
	Debug( "Camera Reset" );
	my $cmd = "reboot.cgi?";
	$self->sendCmd( $cmd );
}
 
#Turn IR on
sub wake
{
	my $self = shift;
	Debug( "Wake - IR on" );
	my $cmd = "camera_control.cgi?param=14&value=1&";
	$self->sendCmd( $cmd );
}
 
#Turn IR off
sub sleep
{
	my $self = shift;
	Debug( "Sleep - IR off" );
	my $cmd = "camera_control.cgi?param=14&value=0&";
	$self->sendCmd( $cmd );
}
1;
__END__
=head1 NAME
ZoneMinder::Database - Perl extension for Vstarcam C7816WIP
=head1 SYNOPSIS
  use ZoneMinder::Control::C7816WIP
  Control module for for Vstarcam C7816WIP
=head1 DESCRIPTION
This module contains the implementation of the Vstarcam C7816WIP IP
camera control protocol.
=head2 EXPORT
None by default.
=head1 SEE ALSO
You can find the documentation at:
http://www.internauta37.altervista.org/en/zoneminder-control-module-vstarcam-c7816wip
=head1 AUTHOR
Philip Coombes, <philip.coombes@zoneminder.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2001-2008  Philip Coombes
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

