blib/lib/Hardware/1Wire/HA7Net.pm untested

filter: Statementstmt 0.0% 0 / 263 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 434 CC 53 SCAR 54.1
CC
53
Cov
0%
read
702.0
scan
90.0
new
56.0
CRAP 224.2
1 package Hardware::1Wire::HA7Net;
2  
3 use Exporter;
4 use LWP::UserAgent;
5 use strict;
6 use vars qw($VERSION @ISA);
7  
8 $VERSION = '1.01';
9 @ISA = qw(Exporter);
10 my ($RCSVERSION) = '$Revision: 1.7 $ ' =~ /\$Revision:\s+([^\s]+)/;
11  
12 ##############################################################################
13 package Hardware::1Wire::HA7Net::Generic;
14  
15 use Exporter;
16 use vars qw($VERSION @ISA);
17  
18 @ISA = qw(Exporter);
19 $VERSION = $Hardware::1Wire::HA7Net::VERSION;
20  
21 sub _new {
22 my ($class, $ha7net, $address) = @_;
23 my $family = substr($address, -2);
24  
25 return bless {
26 ha7net => $ha7net,
27 family => $family,
28 address => $address,
29 }, $class;
30 }
31  
32 sub isa {
33 my ($self, $type) = @_;
34  
35 die "Cannot match empty type" unless $type;
36 for my $match (@{$self->{type}}) {
37 return 1 if uc($type) eq $match;
38 }
39 return 0;
40 }
41  
42 sub ha7net { shift->{ha7net} }
43  
44 sub family { shift->{family} }
45  
46 sub address { shift->{address} }
47  
48 sub units { shift->{units} }
49  
50 sub type {
51 my $self = shift;
52 if (wantarray) {
53 return @{ $self->{type} };
54 }
55 else {
56 return $self->{type}[0];
57 }
58 }
59  
60 sub part_of { shift->{part_of} }
61  
62 ##############################################################################
63 package Hardware::1Wire::HA7Net::DS1820;
64  
65 use Exporter;
66 use vars qw($VERSION @ISA);
67  
68 @ISA = qw(Hardware::1Wire::HA7Net::Generic);
69 $VERSION = $Hardware::1Wire::HA7Net::VERSION;
70  
71 sub _new {
72 my $class = shift;
73 my $self = $class->SUPER::_new(@_);
74  
75 $self->{type} = [qw(DS1820 DS18S20 DS1920 1820 18S20 1920)];
76 $self->{units} = "Degrees C";
77 return $self;
78 }
79  
80  
81 sub temperature {
82 my $self = shift;
83 my $response = $self->{ha7net}->{ua}->get($self->{ha7net}->{baseurl} .
84 "1Wire/ReadTemperature.html?Address_Array=$self->{address}");
85 return undef unless $response->is_success;
86 $response = $response->content;
87  
88 my @response = $response =~
89 /ID="Address.*?VALUE="([\dA-F]+)"
90 .*?
91 ID="Temperature.*?VALUE="(-?[\d.]+)"/x;
92  
93 if (wantarray) {
94 return @response;
95 }
96 else {
97 return $response[1];
98 }
99 }
100  
101 ##############################################################################
102 package Hardware::1Wire::HA7Net::DS18B20;
103  
104 use Exporter;
105 use vars qw($VERSION @ISA);
106  
107 @ISA = qw(Hardware::1Wire::HA7Net::Generic);
108 $VERSION = $Hardware::1Wire::HA7Net::VERSION;
109  
110 sub _new {
111 my $class = shift;
112 my $self = $class->SUPER::_new(@_);
113  
114 $self->{resolution} = 12;
115 $self->{type} = [qw(DS18B20 18B20)];
116 $self->{units} = "Degrees C";
117 return $self;
118 }
119  
120 sub resolution {
121 my $self = shift;
122 if (@_) {
123 my $resolution = shift;
124 die "Resolution out of range of 9..12 for DS18B20 $self->{address}"
125 if $resolution < 9 || $ resolution > 12;
126 $self->{resolution} = $resolution;
127 }
128 return $self->{resolution};
129 }
130  
131 sub temperature {
132 my $self = shift;
133 my $response = $self->{ha7net}->{ua}->get($self->{ha7net}->{baseurl} .
134 "1Wire/ReadDS18B20.html?DS18B20Request={$self->{address},$self->{resolution}}");
135 return undef unless $response->is_success;
136 $response = $response->content;
137  
138 my @response = $response =~
139 /ID="Address.*?VALUE="([\dA-F]+)"
140 .*?
141 ID="Temperature.*?VALUE="(-?[\d.]+)
142 .*?
143 ID="Resolution.*?VALUE="([\d]+\+?)"/x;
144  
145 if (wantarray) {
146 return @response;
147 }
148 else {
149 return $response[1];
150 }
151 }
152  
153 ##############################################################################
154 package Hardware::1Wire::HA7Net::Analog;
155  
156 use Exporter;
157 use vars qw($VERSION @ISA);
158  
159 @ISA = qw(Hardware::1Wire::HA7Net::Generic);
160 $VERSION = $Hardware::1Wire::HA7Net::VERSION;
161  
162 sub _new {
163 my $class = shift;
164 my $self = $class->SUPER::_new(@_);
165  
166 my $response = $self->{ha7net}->{ua}->get($self->{ha7net}->{baseurl} .
167 "1Wire/ReadAnalogProbe.html?Address_Array=$self->{address}")->content;
168  
169 my ($type, $units) = $response =~
170 /ID="Probe_Type.*?VALUE="(\w+)"
171 .*?
172 ID="Probe_Units.*?VALUE="(.*?)"/x;
173  
174 $self->{type} = [ $type ];
175 $self->{units} = $units;
176 return $self;
177 }
178  
179 sub value {
180 my $self = shift;
181 my $response = $self->{ha7net}->{ua}->get($self->{ha7net}->{baseurl} .
182 "1Wire/ReadAnalogProbe.html?Address_Array=$self->{address}");
183 return undef unless $response->is_success;
184 $response = $response->content;
185  
186 my @response = $response =~
187 /ID="Probe_Address.*?VALUE="([\dA-F]+)"
188 .*?
189 ID="Probe_Value.*?VALUE="(-?[\d.]+)"
190 .*?
191 ID="Temperature_Address.*?VALUE="([\dA-F]+)"
192 .*?
193 ID="Temperature.*?VALUE="(-?[\d.]+?)"/x;
194  
195 if (wantarray) {
196 return @response;
197 }
198 else {
199 return $response[1];
200 }
201 }
202  
203 ##############################################################################
204 package Hardware::1Wire::HA7Net;
205  
206 sub new {
207 my $class = shift;
208 my $self = bless {}, $class;
209 my ($scan, $response, $sensor);
210 if (@_) {
211 ($self->{baseurl}, $scan) = @_;
212 $self->{baseurl} =~ s#/*$#/#;
213 }
214 else {
215 die "Cannot yet probe for HA7Net's";
216 }
217  
218 $self->{ua} = new LWP::UserAgent;
219 $self->{ua}->agent("$class/$VERSION");
220  
221 #
222 # If we do not want to scan the device now, just fill in empty arrays and
223 # return. Otherwise, continue with scanning the devices.
224 #.
225 if (defined $scan && $scan == 0) {
226 @{ $self->{sensors} } = ();
227 %{ $self->{by_addr} } = ();
228 return $self;
229 }
230  
231 $response = $self->{ua}->get($self->{baseurl} .
232 "1Wire/Search.html?ConditionalSearch=on");
233 die "Cannot access $self->{baseurl}" unless $response->is_success;
234  
235 ($self->{version}) = $response->content =~ /HA7Net: (\d+(\.\d+)+)/;
236  
237 for my $addr ($response->content =~ /ID="ADDRESS.*?VALUE="([\dA-F]+)"/g) {
238 if ($addr =~ /10$/) {
239 $sensor = _new Hardware::1Wire::HA7Net::DS1820 ($self, $addr);
240 }
241 elsif ($addr =~ /28$/) {
242 $sensor = _new Hardware::1Wire::HA7Net::DS18B20 ($self, $addr);
243 }
244 elsif ($addr =~ /12$/) {
245 $sensor = _new Hardware::1Wire::HA7Net::Analog ($self, $addr);
246 }
247 push @{ $self->{sensors} }, $sensor;
248 $self->{by_addr}->{$sensor->{address}} = $sensor;
249 }
250  
251 _generate_associated_addresses($self);
252 return $self;
253 }
254  
255 sub scan {
256 my $self = shift;
257 my ($response, $sensor, %by_addr, @new);
258  
259 @{ $self->{sensors} } = ();
260 %by_addr = %{ $self->{by_addr} };
261 %{ $self->{by_addr} } = ();
262  
263 $response = $self->{ua}->get($self->{baseurl} .
264 "1Wire/Search.html?ConditionalSearch=on");
265  
266 for my $addr ($response->content =~ /ID="ADDRESS.*?VALUE="([\dA-F]+)"/g) {
267 unless ($sensor = delete $by_addr{$addr}) {
268 if ($addr =~ /10$/) {
269 $sensor = _new Hardware::1Wire::HA7Net::DS1820 ($self, $addr);
270 }
271 elsif ($addr =~ /28$/) {
272 $sensor = _new Hardware::1Wire::HA7Net::DS18B20 ($self, $addr);
273 }
274 elsif ($addr =~ /12$/) {
275 $sensor = _new Hardware::1Wire::HA7Net::Analog ($self, $addr);
276 }
277 push @new, $sensor;
278 }
279 push @{ $self->{sensors} }, $sensor;
280 $self->{by_addr}->{$sensor->{address}} = $sensor;
281 }
282  
283 _generate_associated_addresses($self);
284  
285 if (defined $_[0]) {
286 if (ref $_[0] eq "ARRAY") {
287 @{ $_[0] } = @new;
288 }
289 else {
290 $_[0] = \@new;
291 }
292 }
293 if (defined $_[1]) {
294 if (ref $_[1] eq "ARRAY") {
295 @{ $_[1] } = values %by_addr;
296 }
297 else {
298 $_[1] = [ values %by_addr ];
299 }
300 }
301 return (@new + values %by_addr); # Number of (existence of) changes
302 }
303  
304 #
305 # Find the addresses of the Temperature sensor in the analog sensors. Not
306 # to be called externally.
307 #
308 sub _generate_associated_addresses {
309 my $self = shift;
310 my $response;
311  
312 ANALOG: for my $addr (keys %{ $self->{by_addr} }) {
313 my $assoc_addr;
314 next unless $addr =~ /12$/;
315 for my $chk (values %{ $self->{by_addr} }) {
316 no warnings;
317 next ANALOG if $chk->{part_of} == $self->{by_addr}->{$addr};
318 }
319 $response = $self->{ua}->get($self->{baseurl} .
320 "1Wire/ReadAnalogProbe.html?Address_Array=$addr")->content;
321 ($assoc_addr) =
322 $response =~ /ID="Temperature_Address.*?VALUE="([\dA-F]+)"/;
323 $self->{by_addr}->{$assoc_addr}->{part_of} = $self->{by_addr}->{$addr};
324 }
325 }
326  
327 sub sensors {
328 @{ shift->{sensors} }
329 }
330  
331 sub read {
332 my $self = shift;
333 my (@objs, @ds1820, @ds18b20, @analog, %analog_objs, $response,
334 $sensor, $sub, @answer, @results);
335  
336 #
337 # Read everything by default, else read the set specified by the caller
338 #
339 @_ = @{$self->{sensors}} unless @_;
340 #
341 # First, convert all request entities to objects, preparing for batched
342 # reads. If there is no object associated with a particular address,
343 # create the object (which means reading it). We exclude those from a
344 # second scan, of course. If there are any previously unknown sensors,
345 # there will be @results, so we'll have to _generate_associated_addresses
346 #
347 for my $addr (@_) {
348 if (ref($addr) =~ /^Hardware::1Wire::HA7Net::/) {
349 push @objs, $addr;
350 }
351 elsif (ref($addr)) {
352 die "Cannot use read on a non Hardware::1Wire::HA7Net:: object";
353 }
354 elsif ($addr !~ /^[\dA-Fa-f]{16}/) {
355 die "Illegal Dalls Semiconductor device address";
356 }
357 elsif (exists $self->{by_addr}->{$addr}) {
358 push @objs, $self->{by_addr}->{$addr};
359 }
360 else {
361 #
362 # Add a new device!
363 #
364 if ($addr =~ /10$/) {
365 $response = $self->{ua}->get($self->{baseurl} .
366 "1Wire/ReadTemperature.html?Address_Array=$addr")->content;
367 if (@answer = $response =~
368 /ID="Address.*?VALUE="([\dA-F]+)"
369 .*?
370 ID="Temperature.*?VALUE="(-?[\d.]+)"/gx) {
371 $sensor = _new Hardware::1Wire::HA7Net::DS1820 ($self, $addr);
372 }
373 else {
374 warn "Cannot read device at address $addr";
375 }
376 }
377 elsif ($addr =~ /28$/) {
378 $response = $self->{ua}->get($self->{baseurl} .
379 "1Wire/ReadDS18B20.html?DS18B20Request={$addr,12}")->content;
380  
381 if (@answer = $response =~
382 /ID="Address.*?VALUE="([\dA-F]+)"
383 .*?
384 ID="Temperature.*?VALUE="(-?[\d.]+)/gx) {
385 $sensor = _new Hardware::1Wire::HA7Net::DS18B20 ($self, $addr);
386 }
387 else {
388 warn "Cannot read device at address $addr";
389 }
390 }
391 elsif ($addr =~ /12$/) {
392 $response = $self->{ua}->get($self->{baseurl} .
393 "1Wire/ReadAnalogProbe.html?Address_Array=$addr")->content;
394  
395 if (@answer = $response =~
396 /ID="Probe_Address.*?VALUE="([\dA-F]+)"
397 .*?
398 ID="Probe_Value.*?VALUE="(-?[\d.]+)"
399 .*?
400 ID="Temperature_Address.*?VALUE="([\dA-F]+)"
401 .*?
402 ID="Temperature.*?VALUE="(-?[\d.]+?)"/gx) {
403 $sensor = _new Hardware::1Wire::HA7Net::Analog ($self, $addr);
404 #
405 # Analog devices have a temperature sensor associated
406 # with them - create that device, too (and enter it here)
407 #
408 if ($answer[2] =~ /10$/) {
409 $sub = _new Hardware::1Wire::HA7Net::DS1820 ($self, $answer[2]);
410 }
411 elsif ($answer[2] =~ /28$/) {
412 $sub = _new Hardware::1Wire::HA7Net::DS18B20 ($self, $answer[2]);
413 }
414 else {
415 die "Unknown co-sensor associated with $addr";
416 }
417 push @{ $self->{sensors} }, $sub;
418 $self->{by_addr}->{$sub->{address}} = $sub;
419 }
420 else {
421 warn "Cannot read device at address $addr";
422 }
423 }
424 else {
425 die "Unknown sensor type $addr";
426 }
427 if (@answer) {
428 push @{ $self->{sensors} }, $sensor;
429 $self->{by_addr}->{$sensor->{address}} = $sensor;
430 push @results, @answer;
431 }
432 }
433 }
434 _generate_associated_addresses($self) if @results;
435 #
436 #
437 # Next find all the analog sensors in the set of addresses given
438 #
439 for my $sensor (@objs) {
440 if ($sensor->{ha7net} != $self) {
441 warn "Cannot read sensor from a different HA7Net";
442 next;
443 }
444 if ($sensor->isa("hmp2001s")) {
445 push @analog, $sensor->{address};
446 $analog_objs{$sensor}++;
447 }
448 }
449 #
450 # Then find all the temperature sensors, eliminating those that would
451 # be read by the analog read routine (so we don't read them twice)
452 #
453 for my $sensor (@objs) {
454 if ($sensor->{ha7net} != $self) {
455 warn "Cannot read sensor from a different HA7Net";
456 next;
457 }
458 if ($sensor->isa("ds1820")) {
459 no warnings; # Otherwise the "unless" will elicit them
460 push @ds1820, $sensor->{address}
461 unless $analog_objs{$sensor->part_of};
462 }
463 elsif ($sensor->isa("ds18b20")) {
464 no warnings; # Otherwise the "unless" will elicit them
465 push @ds18b20, "{$sensor->{address},$sensor->{resolution}}"
466 unless $analog_objs{$sensor->part_of};
467 }
468 }
469 #
470 # Now actually read the devices
471 #
472 if (@ds1820) {
473 $response = $self->{ua}->get($self->{baseurl} .
474 "1Wire/ReadTemperature.html?Address_Array=" .
475 join ",", @ds1820)->content;
476 push @results, $response =~
477 /ID="Address.*?VALUE="([\dA-F]+)"
478 .*?
479 ID="Temperature.*?VALUE="(-?[\d.]+)"/gx;
480 }
481 if (@ds18b20) {
482 $response = $self->{ua}->get($self->{baseurl} .
483 "1Wire/ReadDS18B20.html?DS18B20Request=" .
484 join ",", @ds18b20)->content;
485  
486 push @results, $response =~
487 /ID="Address.*?VALUE="([\dA-F]+)"
488 .*?
489 ID="Temperature.*?VALUE="(-?[\d.]+)/gx;
490 }
491 if (@analog) {
492 $response = $self->{ua}->get($self->{baseurl} .
493 "1Wire/ReadAnalogProbe.html?Address_Array=" .
494 join ",", @analog)->content;
495  
496 push @results, $response =~
497 /ID="Probe_Address.*?VALUE="([\dA-F]+)"
498 .*?
499 ID="Probe_Value.*?VALUE="(-?[\d.]+)"
500 .*?
501 ID="Temperature_Address.*?VALUE="([\dA-F]+)"
502 .*?
503 ID="Temperature.*?VALUE="(-?[\d.]+?)"/gx;
504 }
505  
506 return @results;
507 }
508  
509 sub version { shift->{version} }
510  
511 1;
512  
513 __END__