File Coverage

blib/lib/Proliphix.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Proliphix;
2 1     1   23941 use strict;
  1         2  
  1         39  
3 1     1   444 use Moose;
  0            
  0            
4             use LWP::UserAgent;
5              
6             our $VERSION = '0.01';
7              
8             my $mk_base = sub { my $self = shift; $self->{base_url}='http://'.$self->ip.':'.$self->{port}; };
9             has 'ip' => (is => 'rw', isa=>'Str', trigger=>$mk_base );
10             has 'port' => (is => 'rw', isa=>'Int', default=>80, trigger=>$mk_base );
11             has 'base_url' => (is => 'ro', isa=>'Str' );
12             has 'password' => (is=>'rw', isa=>'Str');
13             has 'ua' => (is => 'rw', isa=>'LWP::UserAgent');
14             has 'values' => (is =>'rw', isa=>'HashRef' );
15              
16             our $oid2name = {};
17             do 'oid_defs.pl';
18             foreach my $oid (keys %$oid2name) {
19             my $name = $oid2name->{$oid};
20             has $name => (is=>'rw', isa=>'Value');
21             }
22              
23             sub BUILD {
24             my $self = shift;
25             my $opt = shift || {};
26             $self->values({});
27             if ($opt->{ip} and $opt->{password}) {
28             $self->connect();
29             }
30             }
31              
32             sub connect {
33             my $self = shift;
34             my $ua = new LWP::UserAgent;
35             $ua->credentials($self->ip.':'.$self->port, 'tstat', admin => $self->password);
36             $self->ua($ua);
37             }
38              
39             sub get_oids {
40             my $self = shift;
41             my $oids = shift || [];
42             my $req = {};
43             map { $req->{'OID'.$_} = '' } @$oids;
44             my $response = $self->ua->post($self->base_url.'/get/', $req);
45             $self->set_tokens($response->content);
46             }
47              
48             sub value {
49             my $self = shift;
50             my $name = shift;
51             return $self->values->{$name} || $self->get_oids([$name]) || undef;
52             }
53              
54             sub set_oid {
55             my $self = shift;
56             my ($oid, $value) = @_;
57             $self->set_oids($oid=>$value);
58             return $self->values->{$oid};
59             }
60              
61             sub set_oids {
62             my $self = shift;
63             my (%oids) = @_;
64             foreach my $oid (keys %oids) { $oids{"OID$oid"} = $oids{$oid}; delete $oids{$oid}; }
65             my $response = $self->ua->post($self->base_url.'/pdp/', [%oids, submit=>'Submit']);
66             $self->set_tokens($response->content);
67             }
68              
69             sub set_tokens {
70             my $self = shift;
71             my $input = shift;
72             foreach my $pair (split(/\&/,$input)) {
73             my ($key,$value) = split(/=/,$pair);
74             $key=~s/^OID//;
75             $self->values->{$key} = $value;
76             warn "Error when setting $key\n" if $value=~/^ERROR/;
77             }
78             }
79              
80             1;
81              
82              
83             =head1 NAME
84              
85             Proliphix - Talks to Proliphix network thermostats
86              
87             =head1 SYNOPSYS
88              
89             #!/usr/bin/perl
90             use Proliphix;
91            
92             my $thermostat = new Proliphix(ip=>'mythermaddress', password=>'mythermpassword');
93            
94             #requests these OIDs (documented in PDF API available from Proliphix) from the thermostat
95             $thermostat->get_oids([qw/4.3.2.1 4.1.1 4.1.3/]);
96            
97             #sets (writable) OIDs in thermostat. Many values are not writable, see Proliphix documentation.
98             $thermostat->set_oids('10.1.6'=>$ARGV[0]);
99            
100             #lists known values in thermostat. Pounding the device too hard will cause trouble so the module
101             #holds on to values until another call to get_oids
102             $thermostat->values();
103              
104             =head1 DESCRIPTION
105              
106             Module to communicate with Proliphix IP network thermostats. Module is a skeleton at best and
107             should probably be written differently for a multitude of reasons. I wanted to play around
108             with Moose and was disappointed that no one else had written this module, so here's a quick
109             iteration that should handle most cases without too much crying.
110              
111             =head1 BUGS
112              
113             Almost certainly. Let me know, I'll probably fix them, or send me a patch.
114              
115             =head1 SEE ALSO
116              
117             Documentation and additional information about Proliphix thermostats is available on
118             the Proliphix website http://www.proliphix.com/
119              
120             =head1 AUTHOR
121              
122             John Lifsey, <nebulous@crashed.net>
123              
124             =head1 LICENSE
125              
126             This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself
127              
128             =cut
129              
130