| 1 | package IPDevice::Allnet::ALL4000; | ||
| 2 | 1 | ▶ | use 5.010000; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | our $VERSION = '0.13'; | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use XML::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | sub new | ||
| 11 | { | ||
| 12 | my ($class, %args) = @_; | ||
| 13 | my $self = {}; | ||
| 14 | |||
| 15 | # Some Defaults | ||
| 16 | $self->{USERNAME} = undef; | ||
| 17 | $self->{PASSWORD} = undef; | ||
| 18 | $self->{PORT} = 80; | ||
| 19 | |||
| 20 | foreach my $arg (keys %args) | ||
| 21 | { | ||
| 22 | $self->{$arg} = $args{$arg}; | ||
| 23 | } | ||
| 24 | |||
| 25 | unless( $self->{USERNAME} && $self->{USERNAME} =~ m/\w*/ ) | ||
| 26 | { | ||
| 27 | die( "Username not given or in correct format\n" ); | ||
| 28 | } | ||
| 29 | unless( $self->{PASSWORD} && $self->{PASSWORD} =~ m/\w*/ ) | ||
| 30 | { | ||
| 31 | die( "Password not given or in correct format\n" ); | ||
| 32 | } | ||
| 33 | |||
| 34 | unless( $self->{HOST} ) | ||
| 35 | { | ||
| 36 | die( "Host must be defined\n" ); | ||
| 37 | } | ||
| 38 | |||
| 39 | unless( $self->{PORT} && $self->{PORT} =~ m/^\d*$/ ) | ||
| 40 | { | ||
| 41 | die( "Port must be defined, and must be an integer\n" ); | ||
| 42 | } | ||
| 43 | |||
| 44 | $self->{URL} = "http://$self->{HOST}:$self->{PORT}/xml"; | ||
| 45 | |||
| 46 | my $ua = LWP::UserAgent->new; | ||
| 47 | $ua->credentials("$self->{HOST}:$self->{PORT}", "ALL4000", $self->{USERNAME}, $self->{PASSWORD} ); | ||
| 48 | $self->{UA} = $ua; | ||
| 49 | |||
| 50 | my $parser = new XML::Parser(Style => 'Tree'); | ||
| 51 | $parser->setHandlers( Start => \&_start_handler, | ||
| 52 | Final => \&_final_handler, | ||
| 53 | ); | ||
| 54 | $self->{PARSER} = $parser; | ||
| 55 | |||
| 56 | bless($self); | ||
| 57 | return($self); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub getData | ||
| 61 | { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | my $response = $self->{UA}->get( $self->{URL} ); | ||
| 65 | unless ($response->is_success) | ||
| 66 | { | ||
| 67 | die( "Error connecting to server: " . $response->status_line . "\n" ); | ||
| 68 | } | ||
| 69 | my $page = $response->content; | ||
| 70 | unless( $page =~ m/(<xml>.*<\/xml>)/s ) | ||
| 71 | { | ||
| 72 | die( "Could not find the XML element in the page\n" ); | ||
| 73 | } | ||
| 74 | $self->{DATA} = $self->{PARSER}->parse( $1 ); | ||
| 75 | return $self->{DATA}; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub lastData | ||
| 80 | { | ||
| 81 | my $self = shift; | ||
| 82 | return $self->{DATA}; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Handler for start of XML element | ||
| 86 | sub _start_handler | ||
| 87 | { | ||
| 88 | my( $expat, $element ) = @_; | ||
| 89 | if( $element eq 'data' ) | ||
| 90 | { | ||
| 91 | $expat->setHandlers( Char => \&_char_handler ); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | # Handler for end of XML element | ||
| 96 | sub _final_handler | ||
| 97 | { | ||
| 98 | my( $expat, $element ) = @_; | ||
| 99 | delete( $expat->{ALL4000_DATA}->{data} ); | ||
| 100 | return $expat->{ALL4000_DATA}; | ||
| 101 | } | ||
| 102 | |||
| 103 | # gets the actual data from the XML | ||
| 104 | sub _char_handler | ||
| 105 | { | ||
| 106 | my ($p, $data ) = @_; | ||
| 107 | if( $data ) | ||
| 108 | { | ||
| 109 | $p->{ALL4000_DATA}->{$p->current_element} = $data; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | __END__ | ||