package Net::Camera::Edimax::IC1500;

use 5.008008;
use strict;
use warnings;

require Exporter;

use Data::Dumper;
use LWP::UserAgent;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use Edimax ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
	
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
	
);

our $VERSION = '0.01';


# Preloaded methods go here.

sub new {
	my $package = shift;
	my $self = {};
	my %options = @_;

	$self->{hostname}	= $options{hostname};
	if ($options{port}) { $self->{port} = $options{port}; } else { $self->{port} = "80"; }
	$self->{username}	= $options{username};
	$self->{password}	= $options{password};

	return bless($self, $package);
}

sub createLWP {
	my $self = shift;

	my $ua = LWP::UserAgent->new;
	$ua->agent("Net::Camera::Edimax::IC1500/$VERSION");

	$ua->credentials(
		$self->{hostname} . ':' . $self->{port},
		'Internet Camera',
		$self->{username} => $self->{password}
	);

	return $ua;
}

sub imgSettingsBaseRequest {
	my $self = shift;
	my ($query) = @_;

	my $request = HTTP::Request->new(GET => 'http://' . $self->{hostname} . ':' . $self->{port} . '/form/camera?' . $query);

	return $request;
}

# resolution=1&quality=3&framerate=30&frequency=60&autoexposure=ON&b_value=50&c_value=50&s_value=50&h_value=50&w_value=10&enet_source=camera_left.asp

sub reboot {
	my $self = shift;

	my $ua = $self->createLWP();
	my $request = HTTP::Request->new(GET => 'http://' . $self->{hostname} . ':' . $self->{port} . '/form/reboot?enet_source=reboot.htm');
	my $result = $ua->request($request);
}

sub setFrequency {
	my $self = shift;
	my ($freq) = @_;

	my $ua = $self->createLWP();
	my $request = $self->imgSettingsBaseRequest('frequency=' . $freq );

	my $result = $ua->request($request);
}

sub setAutoExposure {
	my $self = shift;
	my ($autoExposure) = @_;

	my $ua = $self->createLWP();
	my $request = $self->imgSettingsBaseRequest('autoexposure=' . $autoExposure );

	my $result = $ua->request($request);
}

sub setQuality {
	my $self = shift;
	my ($quality) = @_;

	my $ua = $self->createLWP();
	my $request = $self->imgSettingsBaseRequest('quality=' . $quality );

	my $result = $ua->request($request);
}

sub setFrameRate {
	my $self = shift;
	my ($frameRate) = @_;

	my $ua = $self->createLWP();
	my $request = $self->imgSettingsBaseRequest('framerate=' . $frameRate );

	my $result = $ua->request($request);
}


sub setResolution {
	my $self = shift;
	my ($res) = @_;

	my $ua = $self->createLWP();
	my $request = $self->imgSettingsBaseRequest('resolution=' . $res );

	my $result = $ua->request($request);
}

1;
__END__

=head1 NAME

Edimax - Perl extension for managing Edimax IC1500-series network cameras

=head1 SYNOPSIS

  use Net::Camera::Edimax::IC1500;
  my $camera = Net::Camera::Edimax::IC1500->new(
    hostname => 'camera.example.com',
    port => '80',
    username => 'admin',
    password => '1234',
  );
  $camera->setResolution(1);

=head1 DESCRIPTION

The Edimax IC1500-series network cameras are managed via a web interface.
This module provides methods to control various aspects of the camera's
operation.

This module also supports the wireless version (the IC1500Wg), as the interface
is essentially identical.

=head1 METHODS

=over 4

=item new( hostname => $hostname, port => $port, username => $username, password => $password )

Creates a new Net::Camera::Edimax::IC1500 object.

  my $camera = Net::Camera::Edimax::IC1500->new(
    hostname => 'camera.example.com',
    port => '80',
    username => 'admin',
    password => '1234',
  );

=item setFrequency( 60|50|0 )

Sets the image frequency. '60' is 60Hz, '50' is 50Hz, and '0' is the 'Outdoor' setting.

=item setAutoExposure( 'ON' )

Sets the image auto exposure. At present, call anything else turns this off again, so if you want it setting, call it last.

=item setQuality( 3|6|9|12|15 )

Sets the image quality. 3, 6, 9, 12 and 15 refers to 'Highest', 'High', 'Normal', 'Low' and 'Lowest' respectively.

=item setFrameRate( 1|3|5|10|15|20|25|30 )

Sets the maximum frame rate for the MJPEG stream.

=item setResolution( 0|1|2 )

Sets the image resolution. 0 is 640x480, 1 is 320x240, and 2 is 176x144.

=item reboot()

Reboots the device.

=head1 SEE ALSO

http://meh.org.uk/perl/Net-Camera-Edimax-IC1500/

=head1 AUTHOR

Andy Smith, E<lt>ams@meh.org.ukE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008 by Andy Smith

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.8 or,
at your option, any later version of Perl 5 you may have available.


=cut
