File Coverage

blib/lib/Device/NeurioTools.pm
Criterion Covered Total %
statement 39 272 14.3
branch 1 82 1.2
condition 0 30 0.0
subroutine 14 36 38.8
pod 20 21 95.2
total 74 441 16.7


line stmt bran cond sub pod time code
1             package Device::NeurioTools;
2              
3 1     1   863 use warnings;
  1         2  
  1         35  
4 1     1   5 use strict;
  1         2  
  1         19  
5 1     1   23 use 5.006_001;
  1         13  
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( new
20             get_flat_cost get_flat_rate get_ISO8601_date get_ISO8601_time get_ISO8601_timezone
21             get_kwh_consumed get kwh_generated get_power_consumed get_TwoTier_cost
22             get_TwoTier_rate set_flat_rate set_ISO8601_timezone set_TwoTier_rate
23             ) ] );
24              
25             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26             our @EXPORT = qw();
27              
28             BEGIN
29             {
30 1 50   1   3447 if ($^O eq "MSWin32"){
  0            
31 1     1   1046 use Device::Neurio;
  1         73846  
  1         147  
32 1     1   10 use Time::Local;
  1         3  
  1         53  
33 1     1   1020 use DateTime::Format::ISO8601;
  1         233171  
  1         81  
34 1     1   12 use POSIX;
  1         3  
  1         11  
35 1     1   2959 use Data::Dumper;
  1         2  
  1         63  
36 0           } else {
37 1     1   6 use Device::Neurio;
  1         2  
  1         84  
38 1     1   6 use Time::Local;
  1         3  
  1         49  
39 1     1   5 use DateTime::Format::ISO8601;
  1         2  
  1         24  
40 1     1   5 use POSIX;
  1         1  
  1         5  
41 1     1   4633 use Data::Dumper;
  1         2  
  1         49  
42             }
43             }
44              
45              
46             =head1 NAME
47              
48             Device::NeurioTools - More complex methods and tools for accessing data
49             collected by a Neurio sensor module.
50              
51             =head1 VERSION
52              
53             Version 0.07
54              
55             =cut
56              
57             our $VERSION = '0.08';
58              
59             ###################################################################################################
60              
61             =head1 SYNOPSIS
62              
63             This module allows access to more complex and detailed data derived from data
64             collected by a Neurio sensor. This is done via an extended set of methods:
65             - new
66             - connect
67             - set_flat_rate
68             - get_flat_rate
69             - get_flat_cost
70             - get_kwh
71            
72             Please note that in order to use this module you will require three parameters
73             (key, secret, sensor_id) as well as an Energy Aware Neurio sensor installed in
74             your house.
75              
76             The module is written entirely in Perl and has been developped on Raspbian Linux.
77              
78              
79             =head1 SAMPLE CODE
80              
81             use Device::Neurio;
82             use Device::NeurioTools;
83              
84             $my_Neurio = Device::Neurio->new($key,$secret,$sensor_id);
85              
86             $my_Neurio->connect();
87              
88             $my_NeurioTools = Device::NeurioTools->new($my_Neurio,$debug);
89              
90             $my_NeurioTools->set_timezone();
91             $my_NeurioTools->set_flat_rate(0.08);
92            
93             $start = "2014-06-24T00:00:00".$my_NeurioTools->get_timezone();
94             $end = "2014-06-24T23:59:59".$my_NeurioTools->get_timezone();
95             $kwh = $my_NeurioTools->get_kwh($start,"minutes",$end,"5");
96              
97             undef $my_NeurioTools;
98             undef $my_Neurio;
99              
100             =head2 EXPORT
101              
102             All by default.
103              
104              
105             ###################################################################################################
106              
107             =head2 new - the constructor for a NeurioTools object
108              
109             Creates a new instance of NeurioTools which will be able to fetch data from
110             a unique Neurio sensor.
111              
112             my $Neurio = Device::NeurioTools->new($neurio, $debug);
113              
114             This method accepts the following parameters:
115             - $neurio : a valid CONNECTED Neurio object
116             - $debug : enable or disable debug messages (disabled by default - optional)
117              
118             Returns 1 on success
119             Returns 0 on failure
120            
121             =cut
122              
123             sub new {
124 0     0 1   my $class = shift;
125 0           my $self;
126              
127 0           $self->{'neurio'} = shift;
128 0           $self->{'debug'} = shift;
129 0           $self->{'flat_rate'} = 0;
130 0           $self->{'TwoTier_rate1'} = 0;
131 0           $self->{'TwoTier_cutoff'} = 0;
132 0           $self->{'TwoTier_rate2'} = 0;
133 0           $self->{'timezone'} = "+00:00";
134            
135 0 0         if (!defined $self->{'debug'}) {
136 0           $self->{'debug'} = 0;
137             }
138            
139 0 0         if (!defined $self->{'neurio'}) {
140 0           $self->{'neurio'}->printLOG("NeurioTools->new(): a valid Neurio object is a REQUIRED parameter.\n");
141 0           return 0;
142             }
143 0           bless $self, $class;
144 0           return $self;
145             }
146              
147              
148             ###################################################################################################
149              
150             =head2 DESTROY - the destructor for a NeurioTools object
151              
152             Destroys a previously created NeuriTools object.
153            
154             =cut
155             sub DESTROY {
156 0     0     my $self = shift;
157              
158 0           print "\nDestroying ".ref($self)."...\n\n";
159              
160 0           undef $self->{'neurio'};
161 0           undef $self->{'debug'};
162 0           undef $self->{'flat_rate'};
163 0           undef $self->{'TwoTier_rate1'};
164 0           undef $self->{'TwoTier_cutoff'};
165 0           undef $self->{'TwoTier_rate2'};
166 0           undef $self->{'timezone'};
167             }
168              
169              
170             ###################################################################################################
171              
172             =head2 set_flat_rate - set the rate charged by your electicity provider
173              
174             Defines the rate charged by your electricity provider.
175              
176             $NeurioTools->set_flat_rate($rate);
177            
178             This method accepts the following parameters:
179             - $rate : rate charged per kwh - Required
180            
181             Returns 1 on success
182             Returns 0 on failure
183            
184             =cut
185              
186             sub set_flat_rate {
187 0     0 1   my ($self,$rate) = @_;
188            
189 0 0         if (defined $rate) {
190 0           $self->{'flat_rate'} = $rate;
191 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->set_flat_rate(): $self->{'flat_rate'}\n");}
  0            
192 0           return 1;
193             } else {
194 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->set_flat_rate(): No rate specified\n");}
  0            
195 0           return 0;
196             }
197             }
198              
199              
200             ###################################################################################################
201              
202             =head2 set_TwoTier_rate - set the two tier rates charged by your electicity provider
203              
204             Defines the two tier rates charged by your electricity provider.
205             The two tiers are defined according to the power consumed.
206            
207             For example:
208             - $0.05 for the first 20 kWh per day
209             - $0.08 for the remaining kWh per day
210              
211             $NeurioTools->set_TwoTier_rate($rate1,$cutoff,$rate2);
212            
213             This method accepts the following parameters:
214             - $rate1 : rate charged per kwh for usage up to the cutoff - Required
215             - $cutoff : power consumtion point in kWh at which the rate changes - Required
216             - $rate2 : rate charged per kwh for usage abpve the cutoff - Required
217            
218             Returns 1 on success
219             Returns 0 on failure
220            
221             =cut
222              
223             sub set_TwoTier_rate {
224 0     0 1   my ($self,$rate1,$cutoff,$rate2) = @_;
225            
226 0 0 0       if (defined $rate1 and defined $cutoff and defined $rate2) {
      0        
227 0           $self->{'TwoTier_rate1'} = $rate1;
228 0           $self->{'TwoTier_cutoff'} = $cutoff;
229 0           $self->{'TwoTier_rate2'} = $rate2;
230 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->set_TwoTier_rate(): $self->{'TwoTier_rate1'}\n");}
  0            
231 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->set_TwoTier_rate(): $self->{'TwoTier_cutoff'}\n");}
  0            
232 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->set_TwoTier_rate(): $self->{'TwoTier_rate2'}\n");}
  0            
233 0           return 1;
234             } else {
235 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->set_TwoTier_rate(): No rate specified\n");}
  0            
236 0           return 0;
237             }
238             }
239              
240              
241             ###################################################################################################
242              
243             =head2 get_flat_rate - return the flat rate charged by your electicity provider
244              
245             Returns the value for the flat rate set using 'set_flat_rate()'
246              
247             $NeurioTools->get_flat_rate();
248            
249             This method accepts no parameters
250            
251             Returns rate
252            
253             =cut
254              
255             sub get_flat_rate {
256 0     0 1   my $self = shift;
257 0           return $self->{'flat_rate'};
258             }
259              
260              
261             ###################################################################################################
262              
263             =head2 get_TwoTier_rate - return the cutoff and two tier rates charged by your electicity provider
264              
265             Returns the value for the cutoff and two tier rates set using 'set_TwoTier_rate()'
266              
267             $NeurioTools->get_TwoTier_rate();
268            
269             This method accepts no parameters
270            
271             Returns list containing cutoff and rates
272            
273             =cut
274              
275             sub get_TwoTier_rate {
276 0     0 1   my $self = shift;
277 0           return ($self->{'TwoTier_rate1'},$self->{'TwoTier_cutoff'},$self->{'TwoTier_rate2'});
278             }
279              
280              
281             ###################################################################################################
282              
283             =head2 set_ISO8601_timezone - set the timezone offset for ISO8601
284              
285             Sets the timezone offset in ISO8601 format. If no parameter is specified it
286             sets the system defined timezone offset.
287              
288             $NeurioTools->set_ISO8601_timezone($offset);
289            
290             This method accepts the following parameters:
291             - $offset : specified timezone offset in minutes - Optional
292            
293             Returns 1 on success
294             Returns 0 on failure
295            
296             =cut
297              
298             sub set_ISO8601_timezone {
299 0     0 1   my ($self,$offset) = @_;
300 0           my ($total,$hours,$mins,$tz);
301            
302 0 0         if (defined $offset) {
303 0           $total = $offset;
304 0           $hours = sprintf("%+03d",$total / 60);
305 0           $mins = sprintf("%02d",$total % 60);
306 0           $tz = "$hours:$mins";
307             } else {
308 0           $tz = strftime("%z", localtime(time()));
309 0           substr($tz,3,0,":");
310             }
311 0           $self->{'timezone'} = $tz;
312 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->set_ISO8601_timezone(): $self->{'timezone'}\n");}
  0            
313            
314 0           return 1;
315             }
316              
317              
318             ###################################################################################################
319              
320             =head2 get_flat_cost - calculate the cost of consumed power for the specified period
321              
322             Calculates the cost of consumed power over the period specified.
323              
324             $NeurioTools->get_flat_cost($start,$granularity,$end,$frequency);
325            
326             This method requires that a 'flat rate' be set using the set_flat_rate() method
327            
328             This method accepts the following parameters:
329             - $start : yyyy-mm-ddThh:mm:ssZ - Required
330             - $granularity : seconds|minutes|hours|days - Required
331             - $end : yyyy-mm-ddThh:mm:ssZ - Optional
332             - $frequency : an integer - Optional
333            
334             Returns the cost on success
335             Returns 0 on failure
336            
337             =cut
338              
339             sub get_flat_cost {
340 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
341 0           my $i=0;
342            
343 0 0         if ($self->{'flat_rate'} == 0 ) {
344 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->get_flat_cost(): Cannot calculate cost since rate is set to zero\n");}
  0            
345 0           return 0;
346             }
347            
348 0           my $kwh = $self->get_kwh_consumed($start,$granularity,$end,$frequency);
349 0           my $cost = $kwh*$self->{'flat_rate'};
350            
351 0           return $cost;
352             }
353              
354              
355             ###################################################################################################
356              
357             =head2 get_TwoTier_cost - calculate the cost of consumed power for the specified period
358              
359             Calculates the cost of consumed power over the period specified.
360              
361             $NeurioTools->get_TwoTier_cost($start,$granularity,$end,$frequency);
362            
363             This method requires that a 'TwoTier rate' be set using the set_TwoTier_rate() method
364            
365             This method accepts the following parameters:
366             - $start : yyyy-mm-ddThh:mm:ssZ - Required
367             - $granularity : seconds|minutes|hours|days - Required
368             - $end : yyyy-mm-ddThh:mm:ssZ - Optional
369             - $frequency : an integer - Optional
370            
371             Returns the cost on success
372             Returns 0 on failure
373            
374             =cut
375              
376             sub get_TwoTier_cost {
377 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
378 0           my $i = 0;
379 0           my $cost = 0;
380            
381 0 0 0       if ($self->{'TwoTier_rate1'} == 0 or $self->{'TwoTier_cutoff'} == 0 or $self->{'TwoTier_rate2'} == 0) {
      0        
382 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("NeurioTools->get_TwoTier_cost(): Cannot calculate cost since a parameter is set to zero\n");}
  0            
383 0           return 0;
384             }
385            
386 0           my $kwh = $self->get_kwh_consumed($start,$granularity,$end,$frequency);
387            
388 0 0         if ($kwh != 0) {
389 0 0         if ($kwh <= $self->{'TwoTier_cutoff'}) {
390 0           $cost = $kwh * $self->{'TwoTier_rate1'};
391             } else {
392 0           $cost = $self->{'TwoTier_cutoff'} * $self->{'TwoTier_rate1'};
393 0           $cost = $cost + ($kwh - $self->{'TwoTier_cutoff'})*$self->{'TwoTier_rate2'};
394             }
395             }
396 0           return $cost;
397             }
398              
399              
400             ###################################################################################################
401              
402             =head2 get_kwh_consumed - kwh of consumed power for the specified period
403              
404             Calculates the total kwh of consumed power over the period specified.
405              
406             $NeurioTools->get_kwh_consumed($start,$granularity,$end,$frequency);
407            
408             This method accepts the following parameters:
409             - $start : yyyy-mm-ddThh:mm:ssZ - Required
410             specified using ISO8601 format
411             - $granularity : seconds|minutes|hours|days - Required
412             - $end : yyyy-mm-ddThh:mm:ssZ - Optional
413             specified using ISO8601 format
414             - $frequency : an integer - Optional
415            
416             Returns the kwh on success
417             Returns 0 on failure
418            
419             =cut
420              
421             sub get_kwh_consumed {
422 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
423 0           my $energy = 0;
424 0           my $samples = 0;
425 0           my $kwh = 0;
426            
427 0           my $data = $self->{'neurio'}->fetch_Stats_Energy($start,$granularity,$end,$frequency,"1","5000");
428 0 0         if ($data != 0) {
429 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
430 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
431 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
432 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
433            
434 0           while (defined $data->[$samples]->{'consumptionEnergy'}) {
435 0           $energy = $energy + $data->[$samples]->{'consumptionEnergy'};
436 0           $samples++;
437             }
438 0           $kwh = $energy/(1000*3600);
439             }
440            
441 0           return $kwh;
442             }
443              
444              
445             ###################################################################################################
446              
447             =head2 get_kwh_generated - kwh of generated power for the specified period
448              
449             Calculates the total kwh of generated power over the period specified.
450              
451             $NeurioTools->get_kwh_generated($start,$granularity,$end,$frequency);
452            
453             This method accepts the following parameters:
454             - $start : yyyy-mm-ddThh:mm:ssZ - Required
455             specified using ISO8601 format
456             - $granularity : seconds|minutes|hours|days - Required
457             - $end : yyyy-mm-ddThh:mm:ssZ - Optional
458             specified using ISO8601 format
459             - $frequency : an integer - Optional
460            
461             Returns the kwh on success
462             Returns 0 on failure
463            
464             =cut
465              
466             sub get_kwh_generated {
467 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
468 0           my $samples = 0;
469 0           my $power = 0;
470 0           my $kwh = 0;
471            
472 0           my $data = $self->{'neurio'}->fetch_Samples($start,$granularity,$end,$frequency);
473 0 0         if ($data != 0) {
474 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
475 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
476 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
477 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
478            
479 0           while (defined $data->[$samples]->{'generationPower'}) {
480 0           $power = $power + $data->[$samples]->{'generationPower'};
481 0           $samples++;
482             }
483 0           $kwh = $power/1000*$duration/60/60/$samples;
484             }
485            
486 0           return $kwh;
487             }
488              
489              
490             ###################################################################################################
491              
492             =head2 get_energy_consumed - energy consumed for the specified period
493              
494             Calculates the total energy consumed over the period specified.
495              
496             $NeurioTools->get_energy_consumed($start,$granularity,$end,$frequency);
497            
498             This method accepts the following parameters:
499             - $start : yyyy-mm-ddThh:mm:ssZ - Required
500             specified using ISO8601 format
501             - $granularity : seconds|minutes|hours|days - Required
502             - $end : yyyy-mm-ddThh:mm:ssZ - Optional
503             specified using ISO8601 format
504             - $frequency : an integer - Optional
505            
506             Returns the energy on success
507             Returns 0 on failure
508            
509             =cut
510              
511             sub get_energy_consumed {
512 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
513 0           my $samples = 0;
514 0           my $energy = 0;
515 0           my $kwh = 0;
516            
517 0           my $data = $self->{'neurio'}->fetch_Samples($start,$granularity,$end,$frequency);
518 0 0         if ($data != 0) {
519 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
520 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
521 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
522 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
523            
524 0           while (defined $data->[$samples]->{'consumptionEnergy'}) {
525 0           $energy = $energy + $data->[$samples]->{'consumptionEnergy'};
526 0           $samples++;
527             }
528 0           return $energy;
529             }
530             }
531              
532              
533             ###################################################################################################
534              
535             =head2 get_number_of_appliances - return the number of appliances
536              
537             Returns the number of appliances defined in the system.
538              
539             $NeurioTools->get_appliance_ID();
540            
541             This method accepts no parameters:
542            
543             Returns the number of appliances on success
544             Returns 0 on failure
545            
546             =cut
547              
548             sub get_number_of_appliances {
549 0     0 1   my ($self) = @_;
550 0           my $count = 0;
551 0           my $data = $self->{'neurio'}->fetch_Appliances();
552            
553 0 0 0       if ((defined $data) && ($data !=0)) {
554 0           foreach my $appliance (@{$data}) {
  0            
555 0           $count++;
556             }
557             } else {
558 0           $self->{'neurio'}->printLOG(" get_number_of_appliances: Failed to Fetch Appliance ID\n");
559 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG(Dumper($data));}
  0            
560             }
561 0           return $count;
562             }
563              
564             ###################################################################################################
565              
566             =head2 get_appliance_ID - return the id for the appliance specified
567              
568             Returns the appliance ID for the name and label specified.
569              
570             $NeurioTools->get_appliance_ID($name,$label);
571            
572             This method accepts the following parameters:
573             - $name : textual name of appliance - Required
574             - $label : textual label of appliance - Optional
575            
576             Returns the appliance ID on success
577             Returns 0 on failure
578            
579             =cut
580              
581             sub get_appliance_ID {
582 0     0 1   my ($self,$name) = @_;
583 0           my $data = $self->{'neurio'}->fetch_Appliances();
584 0           my $appliance;
585            
586 0 0 0       if ((defined $data) && ($data !=0)) {
587 0           foreach $appliance (@{$data}) {
  0            
588 0 0         if ($appliance->{'name'} eq $name) {
589 0           $self->{'neurio'}->printLOG(" appliance_ID: ".$appliance->{'id'}."\n");
590 0           return $appliance->{'id'};
591             }
592             }
593             } else {
594 0           $self->{'neurio'}->printLOG(" get_appliance_ID: Failed to Fetch Appliance ID\n");
595 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG(Dumper($data));}
  0            
596             }
597 0           $self->{'neurio'}->printLOG(" get_appliance_ID: No Appliance ID found for $name\n");
598 0           return 0;
599             }
600              
601             ###################################################################################################
602              
603             =head2 get_cycle_ID - return the id for the most recent cycle
604              
605             Returns the ID for the most recent cycle.
606              
607             $NeurioTools->get_cycle_ID();
608            
609             This method accepts no parameters
610            
611             Returns the cycle ID on success
612             Returns 0 on failure
613            
614             =cut
615              
616             sub get_cycle_ID {
617 0     0 1   my $self = shift;
618 0           my $cycle;
619 0           my $data = $self->{'neurio'}->fetch_Cycles_by_Sensor();
620              
621 0 0 0       if ((defined $data) && ($data !=0)) {
622 0           $self->{'neurio'}->printLOG(" cycle_ID: ".$data->[0]->{'id'}."\n");
623 0           return $data->[0]->{'id'};
624             } else {
625 0           $self->{'neurio'}->printLOG(" get_cycle_ID: Failed to Fetch Cycle ID\n");
626 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG(Dumper($data));}
  0            
627 0           return 0;
628             }
629             }
630              
631             ###################################################################################################
632              
633             =head2 get_cycle_group_ID - return the id for the most recent cycle group
634              
635             Returns the ID for the most recent cycle group.
636              
637             $NeurioTools->get_cycle_group_ID();
638            
639             This method accepts no parameters
640            
641             Returns the cycle group ID on success
642             Returns 0 on failure
643            
644             =cut
645              
646             sub get_cycle_group_ID {
647 0     0 1   my $self = shift;
648 0           my $data = $self->{'neurio'}->fetch_Cycle_Groups_by_Sensor();
649              
650 0 0 0       if ((defined $data) && ($data !=0)){
651 0           $self->{'neurio'}->printLOG(" cycle_Group_ID: ".$data->[0]->{'id'}."\n");
652 0           return $data->[0]->{'id'};
653             } else {
654 0           $self->{'neurio'}->printLOG(" get_cycle_group_ID: Failed to Fetch Cycle Group ID\n");
655 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG(Dumper($data));}
  0            
656 0           return 0;
657             }
658             }
659              
660             ###################################################################################################
661              
662             =head2 get_edges_ID - return an edges id
663              
664             Returns and edges ID.
665              
666             $NeurioTools->get_edges_ID();
667            
668             This method accepts no parameters
669            
670             Returns the edges ID on success
671             Returns 0 on failure
672            
673             =cut
674              
675             sub get_edge_ID {
676 0     0 0   my $self = shift;
677 0           my $data = $self->{'neurio'}->fetch_Cycles_by_Sensor();
678              
679 0 0 0       if ((defined $data->[0]->{'upEdge'}) && ($data != 0)) {
    0 0        
680 0           $self->{'neurio'}->printLOG(" edges_ID: ".$data->[0]->{'upEdge'}->{'id'}."\n");
681 0           return $data->[0]->{'upEdge'}->{'id'};
682             } elsif ((defined $data->[0]->{'downEdge'}) && ($data != 0)) {
683 0           $self->{'neurio'}->printLOG(" edges_ID: ".$data->[0]->{'downEdge'}."\n");
684 0           return $data->[0]->{'downEdge'};
685             } else {
686 0           $self->{'neurio'}->printLOG(" get_edge_ID: Failed to Fetch Edge\n");
687 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG(Dumper($data));}
  0            
688 0           return 0;
689             }
690             }
691              
692             ###################################################################################################
693              
694             =head2 get_power_consumed - power consumed for the specified period
695              
696             Calculates the total power consumed over the period specified.
697              
698             $NeurioTools->get_energy_consumed($start,$granularity,$end,$frequency);
699            
700             This method accepts the following parameters:
701             - $start : yyyy-mm-ddThh:mm:ssZ - Required
702             specified using ISO8601 format
703             - $granularity : seconds|minutes|hours|days - Required
704             - $end : yyyy-mm-ddThh:mm:ssZ - Optional
705             specified using ISO8601 format
706             - $frequency : an integer - Optional
707            
708             Returns the energy on success
709             Returns 0 on failure
710            
711             =cut
712              
713             sub get_power_consumed {
714 0     0 1   my ($self,$start,$granularity,$end,$frequency) = @_;
715 0           my $samples = 0;
716 0           my $power = 0;
717            
718 0           my $data = $self->{'neurio'}->fetch_Samples($start,$granularity,$end,$frequency);
719 0 0         if ($data != 0) {
720 0           my $start_obj = DateTime::Format::ISO8601->parse_datetime($start);
721 0           my $end_obj = DateTime::Format::ISO8601->parse_datetime($end);
722 0           my $dur_obj = $end_obj->subtract_datetime($start_obj);
723 0           my $duration = eval($dur_obj->{'minutes'}*60+$dur_obj->{'seconds'});
724            
725 0           while (defined $data->[$samples]->{'consumptionPower'}) {
726 0           $power = $power + $data->[$samples]->{'consumptionPower'};
727 0           $samples++;
728             }
729 0           return $power;
730             }
731             }
732              
733              
734             ###################################################################################################
735              
736             =head2 get_ISO8601_time - convert linux time to the time part of ISO8601
737              
738             Returns the time part in ISO8601 format of the specified linux time.
739              
740             $NeurioTools->get_ISO8601_time($time);
741            
742             This method accepts the following parameters:
743             - $time : linux time - Required
744            
745             Returns time part of ISO8601 format on success
746             Returns 0 on failure
747            
748             =cut
749              
750             sub get_ISO8601_time {
751 0     0 1   my ($self,$time) = @_;
752 0           my $ISO8601_time;
753            
754 0 0         if (!defined $time) {
755 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("\$$time is a required parameter\n");}
  0            
756 0           return 0;
757             } else {
758 0           my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($time);
759 0           $ISO8601_time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
760             }
761 0           return $ISO8601_time;
762             }
763              
764              
765             ###################################################################################################
766              
767             =head2 get_ISO8601_date - convert linux time to the date part of ISO8601
768              
769             Returns the date part in ISO8601 fomrat of the specified linux time.
770              
771             $NeurioTools->get_ISO8601_date($time);
772            
773             This method accepts the following parameters:
774             - $time : linux time - Required
775            
776             Returns date part of ISO8601 format on success
777             Returns 0 on failure
778            
779             =cut
780              
781             sub get_ISO8601_date {
782 0     0 1   my ($self,$time) = @_;
783 0           my $ISO8601_date;
784            
785 0 0         if (!defined $time) {
786 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("\$$time is a required parameter\n");}
  0            
787 0           return 0;
788             } else {
789 0           my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($time);
790 0           $ISO8601_date = sprintf("%04d\-%02d\-%02d",$year+1900,$mon+1,$mday);
791             }
792 0           return $ISO8601_date;
793             }
794              
795              
796             ###################################################################################################
797              
798             =head2 get_ISO8601_timezone - return the ISOS8601 timezone offset
799              
800             Returns the timezone part in ISO8601 format for the current location
801             from the value specified with set_ISO8601_timezone
802              
803             $NeurioTools->get_ISO8601_timezone();
804            
805             This method accepts no parameters
806            
807             Returns timezone offset
808            
809             =cut
810              
811             sub get_ISO8601_timezone {
812 0     0 1   my $self = shift;
813 0           return $self->{'timezone'};
814             }
815              
816              
817             ###################################################################################################
818              
819             =head2 get_ISO8601 - return the entire ISOS8601 formatted date/time/timezone
820              
821             Returns the entire ISO8601 formatted date/time/timezone based on the time
822             parameter passed
823              
824             $NeurioTools->get_ISO8601($time);
825            
826             This method accepts the following parameters:
827             - $time : linux time - Required
828            
829             Returns entire ISO8601 string on success
830             Returns 0 on failure
831            
832             =cut
833              
834             sub get_ISO8601 {
835 0     0 1   my ($self,$time) = @_;
836 0           my $ISO8601;
837            
838 0 0         if (!defined $time) {
839 0 0         if ($self->{'debug'}) {$self->{'neurio'}->printLOG("\$$time is a required parameter\n");}
  0            
840 0           return 0;
841             } else {
842 0           my $tz = $self->get_ISO8601_timezone();
843 0           my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($time);
844 0           $ISO8601 = sprintf("%04d\-%02d\-%02dT%02d:%02d:%02d".$tz,$year+1900,$mon+1,$mday,$hour,$min,$sec);
845             }
846 0           return $ISO8601;
847             }
848              
849              
850             ###################################################################################################
851              
852             =head1 AUTHOR
853              
854             Kedar Warriner, C
855              
856             =head1 BUGS
857              
858             Please report any bugs or feature requests to C
859             or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Device-NeurioTools
860             I will be notified, and then you'll automatically be notified of progress on
861             your bug as I make changes.
862              
863              
864             =head1 SUPPORT
865              
866             You can find documentation for this module with the perldoc command.
867              
868             perldoc Device::NeurioTools
869              
870              
871             You can also look for information at:
872              
873             =over 5
874              
875             =item * RT: CPAN's request tracker
876              
877             L
878              
879             =item * AnnoCPAN: Annotated CPAN documentation
880              
881             L
882              
883             =item * CPAN Ratings
884              
885             L
886              
887             =item * Search CPAN
888              
889             L
890              
891             =back
892              
893              
894             =head1 ACKNOWLEDGEMENTS
895              
896             Many thanks to:
897             The guys at Energy Aware Technologies for creating the Neurio sensor and
898             developping the API.
899             Everyone involved with CPAN.
900              
901             =head1 LICENSE AND COPYRIGHT
902              
903             Copyright 2014 Kedar Warriner .
904              
905             This program is free software; you can redistribute it and/or modify it
906             under the terms of either: the GNU General Public License as published
907             by the Free Software Foundation; or the Artistic License.
908              
909             See http://dev.perl.org/licenses/ for more information.
910              
911              
912             =cut
913              
914             #************************************************************************
915             1; # End of Device::NeurioTools - Return success to require/use statement
916             #************************************************************************
917