File Coverage

blib/lib/Device/NeurioTools.pm
Criterion Covered Total %
statement 34 131 25.9
branch 1 14 7.1
condition n/a
subroutine 12 22 54.5
pod 10 10 100.0
total 57 177 32.2


line stmt bran cond sub pod time code
1             package Device::NeurioTools;
2              
3 1     1   1244 use warnings;
  1         2  
  1         64  
4 1     1   4 use strict;
  1         3  
  1         29  
5 1     1   87 use 5.006_001;
  1         12  
  1         201  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10              
11             # Items to export into callers namespace by default. Note: do not export
12             # names by default without a very good reason. Use EXPORT_OK instead.
13             # Do not simply export all your public functions/methods/constants.
14              
15             # This allows declaration use Device::NeurioTools ':all';
16             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17             # will save memory.
18              
19             our %EXPORT_TAGS = ( 'all' => [ qw(
20             new get_flat_cost get_energy_consumed get_kwh_consumed get kwh_generated get_power_consumed get_flat_rate get_timezone set_flat_rate set_timezone
21             ) ] );
22              
23             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24             our @EXPORT = qw();
25              
26             BEGIN
27             {
28 1 50   1   1230 if ($^O eq "MSWin32"){
29 1     1   2076 use Device::Neurio;
  1         146958  
  1         122  
30 1     1   11 use Time::Local;
  1         3  
  1         62  
31 1     1   1196 use DateTime::Format::ISO8601;
  1         530482  
  1         74  
32 1     1   14 use Data::Dumper;
  1         2  
  1         69  
33             } else {
34 1     1   5 use Device::Neurio;
  1         2  
  1         110  
35 1     1   5 use Time::Local;
  1         2  
  1         45  
36 1     1   5 use DateTime::Format::ISO8601;
  1         3  
  1         15  
37 1     1   4 use Data::Dumper;
  1         1  
  1         31  
38             }
39             }
40              
41              
42             =head1 NAME
43              
44             Device::NeurioTools - More complex methods for accessing data collected by a
45             Neurio sensor module.
46              
47             =head1 VERSION
48              
49             Version 0.06
50              
51             =cut
52              
53             our $VERSION = '0.06';
54              
55             #*****************************************************************
56              
57             =head1 SYNOPSIS
58              
59             This module allows access to more complex and detailed data derived from data
60             collected by a Neurio sensor. This is done via an extended set of methods:
61             - new
62             - connect
63             - set_flat_rate
64             - get_flat_rate
65             - get_flat_cost
66             - get_kwh
67            
68             Please note that in order to use this module you will require three parameters
69             (key, secret, sensor_id) as well as an Energy Aware Neurio sensor installed in
70             your house.
71              
72             The module is written entirely in Perl and has been developped on Raspbian Linux.
73              
74              
75             =head1 SAMPLE CODE
76              
77             use Device::Neurio;
78             use Device::NeurioTools;
79              
80             $my_Neurio = Device::Neurio->new($key,$secret,$sensor_id);
81              
82             $my_Neurio->connect();
83              
84             $my_NeurioTools = Device::NeurioTools->new($my_Neurio,$debug);
85              
86             $my_NeurioTools->set_timezone();
87             $my_NeurioTools->set_flat_rate(0.08);
88            
89             $start = "2014-06-24T00:00:00".$my_NeurioTools->get_timezone();
90             $end = "2014-06-24T23:59:59".$my_NeurioTools->get_timezone();
91             $kwh = $my_NeurioTools->get_kwh($start,"minutes",$end,"5");
92              
93             undef $my_NeurioTools;
94             undef $my_Neurio;
95              
96             =head2 EXPORT
97              
98             All by default.
99              
100              
101             #*****************************************************************
102              
103             =head2 new - the constructor for a NeurioTools object
104              
105             Creates a new instance of NeurioTools which will be able to fetch data from
106             a unique Neurio sensor.
107              
108             my $Neurio = Device::NeurioTools->new($neurio, $debug);
109              
110             This method accepts the following parameters:
111             - $neurio : a valid CONNECTED Neurio object
112             - $debug : enable or disable debug messages (disabled by default - optional)
113              
114             Returns 1 on success
115             Returns 0 on failure
116             =cut
117             sub new {
118 0     0 1   my $class = shift;
119 0           my $self;
120              
121 0           $self->{'neurio'} = shift;
122 0           $self->{'debug'} = shift;
123 0           $self->{'flat_rate'} = 0;
124 0           $self->{'timezone'} = "+00:00";
125            
126 0 0         if (!defined $self->{'debug'}) {
127 0           $self->{'debug'} = 0;
128             }
129            
130 0 0         if (!defined $self->{'neurio'}) {
131 0           print "NeurioTools->new(): a valid Neurio object is a REQUIRED parameter.\n";
132 0           return 0;
133             }
134            
135 0           bless $self, $class;
136            
137 0           return $self;
138             }
139              
140              
141             #*****************************************************************
142              
143             =head2 set_flat_rate - set the rate charged by your electicity provider
144              
145             Defines the rate charged by your electricity provider.
146              
147             $NeurioTools->set_flat_rate($rate);
148            
149             This method accepts the following parameters:
150             - $rate : amount charged per kwh - Required
151            
152             Returns 1 on success
153             Returns 0 on failure
154             =cut
155             sub set_flat_rate {
156 0     0 1   my ($self,$rate) = @_;
157            
158 0 0         if (defined $rate) {
159 0           $self->{'flat_rate'} = $rate;
160 0 0         print "NeurioTools->set_flat_rate(): $self->{'flat_rate'}\n" if ($self->{'debug'});
161 0           return 1;
162             } else {
163 0           print "NeurioTools->set_flat_rate(): No rate specified\n";
164 0           return 0;
165             }
166             }
167              
168              
169             #*****************************************************************
170              
171             =head2 get_flat_rate - return the rate charged by your electicity provider
172              
173             Returns the value for rate set using 'set_flat_rate()'
174              
175             $NeurioTools->get_flat_rate();
176            
177             This method accepts no parameters
178            
179             Returns rate
180             =cut
181             sub get_flat_rate {
182 0     0 1   my $self = shift;
183 0           return $self->{'flat_rate'};
184             }
185              
186              
187             #*****************************************************************
188              
189             =head2 set_timezone - set the timezone offset
190              
191             Sets the timezone offset. If no parameter is specified it uses the system
192             defined timezone offset.
193              
194             $NeurioTools->set_timezone($offset);
195            
196             This method accepts the following parameters:
197             - $offset : specified timezone offset in minutes - Optional
198            
199             Returns 1 on success
200             Returns 0 on failure
201             =cut
202             sub set_timezone {
203 0     0 1   my ($self,$offset) = @_;
204 0           my ($total,$hours,$mins);
205            
206 0 0         if (defined $offset) {
207 0           $total = $offset;
208             } else {
209 0           my @utc = gmtime();
210 0           my @loc = localtime();
211 0           $total = ($loc[2]*60+$loc[1])-($utc[2]*60+$utc[1]);
212             }
213            
214 0           $hours = sprintf("%+03d",$total / 60);
215 0           $mins = sprintf("%02d",$total % 60);
216 0           $self->{'timezone'} = "$hours:$mins";
217 0           print "NeurioTools->set_timezone(): $self->{'timezone'}\n";
218            
219 0           return 1;
220             }
221              
222              
223             #*****************************************************************
224              
225             =head2 get_timezone - return the timezone offset
226              
227             Returns the value for the timezone offset in minutes
228              
229             $NeurioTools->get_timezone();
230            
231             This method accepts no parameters
232            
233             Returns timezone offset
234             =cut
235             sub get_timezone {
236 0     0 1   my $self = shift;
237            
238 0           return $self->{'timezone'};
239             }
240              
241              
242             #*****************************************************************
243              
244             =head2 get_flat_cost - calculate the cost of consumed power for the specified period
245              
246             Calculates the cost of consumed power over the period specified.
247              
248             $NeurioTools->get_flat_cost($start,$granularity,$end,$frequency);
249            
250             This method requires that a 'flat rate' be set using the set_flat_rate() method
251            
252             This method accepts the following parameters:
253             - start : yyyy-mm-ddThh:mm:ssZ - Required
254             - granularity : seconds|minutes|hours|days - Required
255             - end : yyyy-mm-ddThh:mm:ssZ - Optional
256             - frequency : an integer - Optional
257            
258             Returns the cost on success
259             Returns 0 on failure
260             =cut
261             sub get_flat_cost {
262 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
263 0           my $i=0;
264            
265 0 0         if ($self->{'flat_rate'} == 0 ) {
266 0           print "NeurioTools->get_flat_cost(): Cannot calculate cost since rate is set to zero\n";
267 0           return 0;
268             }
269            
270 0           my $kwh = $self->get_kwh_consumed($start,$granularity,$end,$frequency);
271 0           my $cost = $kwh*$self->{'flat_rate'};
272            
273 0           return $cost;
274             }
275              
276              
277             #*****************************************************************
278              
279             =head2 get_kwh_consumed - kwh of consumed power for the specified period
280              
281             Calculates the total kwh of consumed power over the period specified.
282              
283             $NeurioTools->get_kwh_consumed($start,$granularity,$end,$frequency);
284            
285             This method accepts the following parameters:
286             - start : yyyy-mm-ddThh:mm:ssZ - Required
287             specified using ISO8601 format
288             - granularity : seconds|minutes|hours|days - Required
289             - end : yyyy-mm-ddThh:mm:ssZ - Optional
290             specified using ISO8601 format
291             - frequency : an integer - Optional
292            
293             Returns the kwh on success
294             Returns 0 on failure
295             =cut
296             sub get_kwh_consumed {
297 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
298 0           my $energy = 0;
299 0           my $samples = 0;
300 0           my $kwh;
301            
302 0           my $data = $self->{'neurio'}->fetch_Energy_Stats($start,$granularity,$end,$frequency,"1","5000");
303 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
304 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
305 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
306 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
307            
308 0           while (defined $data->[$samples]->{'consumptionEnergy'}) {
309 0           $energy = $energy + $data->[$samples]->{'consumptionEnergy'};
310 0           $samples++;
311             }
312            
313 0           $kwh = $energy/(1000*3600);
314              
315 0           return $kwh;
316             }
317              
318              
319             #*****************************************************************
320              
321             =head2 get_kwh_generated - kwh of generated power for the specified period
322              
323             Calculates the total kwh of generated power over the period specified.
324              
325             $NeurioTools->get_kwh_generated($start,$granularity,$end,$frequency);
326            
327             This method accepts the following parameters:
328             - start : yyyy-mm-ddThh:mm:ssZ - Required
329             specified using ISO8601 format
330             - granularity : seconds|minutes|hours|days - Required
331             - end : yyyy-mm-ddThh:mm:ssZ - Optional
332             specified using ISO8601 format
333             - frequency : an integer - Optional
334            
335             Returns the kwh on success
336             Returns 0 on failure
337             =cut
338             sub get_kwh_generated {
339 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
340 0           my $samples = 0;
341 0           my $power = 0;
342 0           my $kwh;
343            
344 0           my $data = $self->{'neurio'}->fetch_Samples($start,$granularity,$end,$frequency);
345 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
346 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
347 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
348 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
349            
350 0           while (defined $data->[$samples]->{'generationPower'}) {
351 0           $power = $power + $data->[$samples]->{'generationPower'};
352 0           $samples++;
353             }
354            
355 0           $kwh = $power/1000*$duration/60/60/$samples;
356              
357 0           return $kwh;
358             }
359              
360              
361             #*****************************************************************
362              
363             =head2 get_energy_consumed - energy consumed for the specified period
364              
365             Calculates the total energy consumed over the period specified.
366              
367             $NeurioTools->get_energy_consumed($start,$granularity,$end,$frequency);
368            
369             This method accepts the following parameters:
370             - start : yyyy-mm-ddThh:mm:ssZ - Required
371             specified using ISO8601 format
372             - granularity : seconds|minutes|hours|days - Required
373             - end : yyyy-mm-ddThh:mm:ssZ - Optional
374             specified using ISO8601 format
375             - frequency : an integer - Optional
376            
377             Returns the energy on success
378             Returns 0 on failure
379             =cut
380             sub get_energy_consumed {
381 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
382 0           my $samples = 0;
383 0           my $energy = 0;
384 0           my $kwh;
385            
386 0           my $data = $self->{'neurio'}->fetch_Samples($start,$granularity,$end,$frequency);
387 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
388 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
389 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
390 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
391            
392 0           while (defined $data->[$samples]->{'consumptionEnergy'}) {
393 0           $energy = $energy + $data->[$samples]->{'consumptionEnergy'};
394 0           $samples++;
395             }
396            
397 0           return $energy;
398             }
399              
400              
401             #*****************************************************************
402              
403             =head2 get_power_consumed - power consumed for the specified period
404              
405             Calculates the total power consumed over the period specified.
406              
407             $NeurioTools->get_energy_consumed($start,$granularity,$end,$frequency);
408            
409             This method accepts the following parameters:
410             - start : yyyy-mm-ddThh:mm:ssZ - Required
411             specified using ISO8601 format
412             - granularity : seconds|minutes|hours|days - Required
413             - end : yyyy-mm-ddThh:mm:ssZ - Optional
414             specified using ISO8601 format
415             - frequency : an integer - Optional
416            
417             Returns the energy on success
418             Returns 0 on failure
419             =cut
420             sub get_power_consumed {
421 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
422 0           my $samples = 0;
423 0           my $power = 0;
424            
425 0           my $data = $self->{'neurio'}->fetch_Samples($start,$granularity,$end,$frequency);
426 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
427 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
428 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
429 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
430            
431 0           while (defined $data->[$samples]->{'consumptionPower'}) {
432 0           $power = $power + $data->[$samples]->{'consumptionPower'};
433 0           $samples++;
434             }
435            
436 0           return $power;
437             }
438              
439              
440             #*****************************************************************
441              
442             =head1 AUTHOR
443              
444             Kedar Warriner, C
445              
446             =head1 BUGS
447              
448             Please report any bugs or feature requests to C
449             or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Device-NeurioTools
450             I will be notified, and then you'll automatically be notified of progress on
451             your bug as I make changes.
452              
453              
454             =head1 SUPPORT
455              
456             You can find documentation for this module with the perldoc command.
457              
458             perldoc Device::NeurioTools
459              
460              
461             You can also look for information at:
462              
463             =over 5
464              
465             =item * RT: CPAN's request tracker
466              
467             L
468              
469             =item * AnnoCPAN: Annotated CPAN documentation
470              
471             L
472              
473             =item * CPAN Ratings
474              
475             L
476              
477             =item * Search CPAN
478              
479             L
480              
481             =back
482              
483              
484             =head1 ACKNOWLEDGEMENTS
485              
486             Many thanks to:
487             The guys at Energy Aware Technologies for creating the Neurio sensor and
488             developping the API.
489             Everyone involved with CPAN.
490              
491             =head1 LICENSE AND COPYRIGHT
492              
493             Copyright 2014 Kedar Warriner .
494              
495             This program is free software; you can redistribute it and/or modify it
496             under the terms of either: the GNU General Public License as published
497             by the Free Software Foundation; or the Artistic License.
498              
499             See http://dev.perl.org/licenses/ for more information.
500              
501              
502             =cut
503              
504             #************************************************************************
505             1; # End of Device::NeurioTools - Return success to require/use statement
506             #************************************************************************
507