| 1 | package Power::Outlet::Common; | ||||||||||||||||||||||||||
| 2 | 18 | ▶ | use strict; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 3 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 4 | 18 | ▶ | use Time::HiRes qw{}; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 5 | 18 | ▶ | use base qw{Package::New}; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||
| 7 | 18 | our $VERSION = '0.54'; | |||||||||||||||||||||||||
| 8 | 18 | our $STATE = 'OFF'; | |||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||
| 12 | Power::Outlet::Common - Power::Outlet base class for all power outlets | ||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||
| 16 | use base qw{Power::Outlet::Common}; | ||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||
| 20 | Power::Outlet::Common is a base class for controlling and querying a power outlets. | ||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||
| 22 | =head1 USAGE | ||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||
| 24 | use base qw{Power::Outlet::Common}; | ||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||
| 28 | =head2 new | ||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||
| 32 | =head2 action | ||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||
| 34 | Smart case insensitive text-based wrapper around methods 0|ON => on, 1|OFF => off, SWITCH|TOGGLE => switch, CYCLE => cycle, QUERY => query | ||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||
| 36 | my $state = $outlet->action("on"); | ||||||||||||||||||||||||||
| 37 | my $state = $outlet->action("1"); | ||||||||||||||||||||||||||
| 38 | my $state = $outlet->action("off"); | ||||||||||||||||||||||||||
| 39 | my $state = $outlet->action("0"); | ||||||||||||||||||||||||||
| 40 | my $state = $outlet->action("switch"); | ||||||||||||||||||||||||||
| 41 | my $state = $outlet->action("toggle"); | ||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||
| 45 | 35 | ▶ | sub action { | ||||||||||||||||||||||||
|
Subroutine
action: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||
| 46 | 35 | my $self = shift; | |||||||||||||||||||||||||
| 47 | 35 | my $action = shift; | |||||||||||||||||||||||||
| 48 | 35 | ▶ | if ($action =~ m/\A(?:1|ON)\Z/i) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 49 | 2 | $self->on; | |||||||||||||||||||||||||
| 50 | } elsif ($action =~ m/\A(?:0|OFF)\Z/i) { | ||||||||||||||||||||||||||
| 51 | 3 | $self->off; | |||||||||||||||||||||||||
| 52 | } elsif ($action =~ m/\A(?:SWITCH|TOGGLE)\Z/i) { | ||||||||||||||||||||||||||
| 53 | 4 | $self->switch; | |||||||||||||||||||||||||
| 54 | } elsif ($action =~ m/\A(?:CYCLE)\Z/i) { | ||||||||||||||||||||||||||
| 55 | 2 | $self->cycle; | |||||||||||||||||||||||||
| 56 | } elsif ($action =~ m/\A(?:QUERY)\Z/i) { | ||||||||||||||||||||||||||
| 57 | 24 | $self->query; | |||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||
| 59 | 0 | die(qq{Error: action "$action" not supported}); | |||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||
| 63 | =head2 query | ||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||
| 65 | The query method must be overridden in the sub class. | ||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||
| 67 | my $state = $outlet->query; #returns ON|OFF Note: may return other values for edge case | ||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||
| 71 | 32 | ▶ | sub query {return $STATE}; | ||||||||||||||||||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||
| 73 | =head2 on | ||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||
| 75 | The on method must be overridden in the sub class. | ||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||
| 77 | my $state = $outlet->on; #turns the outlet on reguardless of current state and returns ON. | ||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||
| 79 | Note: This should cancel any non-blocking cycle requests | ||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||
| 83 | 7 | ▶ | sub on {return $STATE = 'ON'}; | ||||||||||||||||||||||||
|
Subroutine
on: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||
| 85 | =head2 off | ||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||
| 87 | The off method must be overridden in the sub class. | ||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||
| 89 | my $state = $outlet->off; #turns the outlet off reguardless of current state and returns OFF. | ||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||
| 91 | Note: This should cancel any non-blocking cycle requests | ||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||
| 95 | 7 | ▶ | sub off {return $STATE = 'OFF'}; | ||||||||||||||||||||||||
|
Subroutine
off: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||
| 97 | =head2 switch | ||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||
| 99 | Only override the switch method if your hardware natively supports this capability. However, it should still be documented. | ||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||
| 101 | my $state = $outlet->switch; #turns the outlet off if on and on if off and returns the final state ON|OFF. | ||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||
| 103 | Note: The default implementations does not cancel non-blocking cycle requests | ||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||
| 107 | 28 | ▶ | sub switch { | ||||||||||||||||||||||||
|
Subroutine
switch: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||
| 108 | 28 | my $self = shift; | |||||||||||||||||||||||||
| 109 | 28 | my $query = $self->query; | |||||||||||||||||||||||||
| 110 | 28 | ▶ | return $query eq 'OFF' ? $self->on : | ||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||
| 111 | $query eq 'ON' ? $self->off : | ||||||||||||||||||||||||||
| 112 | $query; #e.g. CYCLE, BUSY, etc. | ||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||
| 115 | =head2 cycle | ||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||
| 117 | Only override the cycle method if your hardware natively supports this capability. However, it should still be documented. | ||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||
| 119 | my $state = $outlet->cycle; #turns the outlet off-on-off or on-off-on with a delay and returns the final state ON|OFF. | ||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||
| 121 | Note: Implementations may be blocking or non-blocking. | ||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||
| 125 | 7 | ▶ | sub cycle { | ||||||||||||||||||||||||
|
Subroutine
cycle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||
| 126 | 7 | my $self = shift; | |||||||||||||||||||||||||
| 127 | 7 | $self->switch; | |||||||||||||||||||||||||
| 128 | 7 | Time::HiRes::sleep $self->cycle_duration; #blocking. Maybe we should be non-blocking somehow. | |||||||||||||||||||||||||
| 129 | 7 | return $self->switch; | |||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||
| 132 | =head2 cycle_duration | ||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||
| 134 | Override the cycle_duration method if you want. | ||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||
| 136 | Default; 10 seconds (floating point number) | ||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||
| 140 | 7 | ▶ | sub cycle_duration { | ||||||||||||||||||||||||
|
Subroutine
cycle_duration: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||
| 141 | 7 | my $self = shift; | |||||||||||||||||||||||||
| 142 | 7 | ▶ | $self->{'cycle_duration'} = shift if @_; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 143 | 7 | ▶ | $self->{'cycle_duration'} = 10 unless defined $self->{'cycle_duration'}; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 144 | 7 | return $self->{'cycle_duration'}; | |||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||
| 147 | =head2 name | ||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||
| 149 | User friendly name for an outlet. | ||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||
| 153 | 3 | ▶ | sub name { | ||||||||||||||||||||||||
|
Subroutine
name: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||
| 154 | 3 | my $self = shift; | |||||||||||||||||||||||||
| 155 | 3 | ▶ | $self->{'name'} = shift if @_; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 156 | 3 | ▶ | $self->{'name'} = $self->_name_default unless defined $self->{'name'}; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 157 | 3 | return $self->{'name'}; | |||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub _name_default {''}; | ||||||||||||||||||||||||
|
Subroutine
_name_default: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||
| 162 | =head1 BUGS | ||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||
| 164 | Please log on RT and send an email to the author. | ||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||
| 166 | =head1 SUPPORT | ||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||
| 168 | DavisNetworks.com supports all Perl applications including this package. | ||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||
| 170 | =head1 AUTHOR | ||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||
| 172 | Michael R. Davis | ||||||||||||||||||||||||||
| 173 | CPAN ID: MRDVT | ||||||||||||||||||||||||||
| 174 | DavisNetworks.com | ||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||
| 176 | =head1 COPYRIGHT | ||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||
| 178 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||
| 180 | The full text of the license can be found in the LICENSE file included with this module. | ||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||
| 182 | =head1 SEE ALSO | ||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||
| 186 | 18 | 1; | |||||||||||||||||||||||||