| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2020-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use v5.26; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Object::Pad 0.800; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | class App::Device::Chip::sensor 0.08; | ||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use Feature::Compat::Defer; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | use Sublike::Extended; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | 7 | ▶ | use Device::Chip::Adapter; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | use Device::Chip::Sensor 0.19; # ->type | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 21 | 7 | ▶ | use Future::IO 0.08; # ->alarm | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 22 | 7 | ▶ | use Getopt::Long qw( GetOptionsFromArray ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | use List::Util 1.29 qw( max pairgrep ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 24 | 7 | ▶ | use Scalar::Util qw( refaddr ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | C<App::Device::Chip::sensor> - Base class to build C<Device::Chip::Sensor>-based applications on | ||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | =for highlighter language=perl | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||
| 35 | use v5.26; | ||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | use Object::Pad; | ||||||||||||||||||||||||||||||||||||
| 38 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | class App extends App::Device::Chip::sensor | ||||||||||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||||||||||
| 42 | method output_readings ( $now, $sensors, $values ) | ||||||||||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||||||||||
| 44 | print "At time $now, we have some sensor values...\n"; | ||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | await App->new->parse_argv->run; | ||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | This module provides a base class to assist in writing applications that | ||||||||||||||||||||||||||||||||||||
| 53 | process data periodically from one or more L<Device::Chip>-based sensors, via | ||||||||||||||||||||||||||||||||||||
| 54 | the L<Device::Chip::Sensor> interface. A typical program using this module | ||||||||||||||||||||||||||||||||||||
| 55 | would derive a subclass from it, provide the remaining methods as necessary, | ||||||||||||||||||||||||||||||||||||
| 56 | and eventually call the L</run> method to start the application. | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | =head1 COMMANDLINE OPTIONS | ||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | The following commandline options are recognised by the base class and may be | ||||||||||||||||||||||||||||||||||||
| 63 | used in addition to any defined by the actual application logic. | ||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||
| 65 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | =item * --blib, -b | ||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||
| 69 | Uses the L<blib> module to add additional paths into C<@INC> to search for | ||||||||||||||||||||||||||||||||||||
| 70 | more Perl modules. May be useful when testing chip drivers under development | ||||||||||||||||||||||||||||||||||||
| 71 | without needing to install them. | ||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | =item * --interval, -i TIME | ||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | Specifies the time, in seconds, between every round of collecting sensor | ||||||||||||||||||||||||||||||||||||
| 76 | readings and invoking the L</output_readings> method. | ||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | Defaults to 10 seconds. | ||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | =item * --adapter, -A STR | ||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||
| 82 | Adapter configuration string to pass to L<Device::Chip::Adapter/new_from_description> | ||||||||||||||||||||||||||||||||||||
| 83 | to construct the chip adapter used for communication with the actual chip | ||||||||||||||||||||||||||||||||||||
| 84 | hardware. | ||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||
| 86 | =item * --filter, -F STR | ||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | Specifies the kind of filtering to apply to gauge values. See L</FILTERING> | ||||||||||||||||||||||||||||||||||||
| 89 | for more detail. | ||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | =item * --mid3, -m | ||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||
| 93 | Enable "middle-of-3" filtering of gauge values, to reduce sensor noise from | ||||||||||||||||||||||||||||||||||||
| 94 | unreliable sensors. This is equivalent to setting C<-F mid3>. | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | =item * --best-effort, -B | ||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | Enables best-effort mode, which causes failures of sensor readings to be | ||||||||||||||||||||||||||||||||||||
| 99 | ignored, reporting C<undef> instead. In this mode, the C<on_sensor_fail> | ||||||||||||||||||||||||||||||||||||
| 100 | method may be invoked for failures; it can further refine what the behaviour | ||||||||||||||||||||||||||||||||||||
| 101 | should be. | ||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||
| 103 | =back | ||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 7 | field @_CHIPCONFIGS; | |||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | method _chipconfigs { @_CHIPCONFIGS } # for unit testing | ||||||||||||||||||||||||||||||||||
|
Subroutine
_chipconfigs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 3 | field $_interval :mutator = 10; | |||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | field $_best_effort :mutator; | ||||||||||||||||||||||||||||||||||
|
Subroutine
best_effort: not called CC=1 SCAR=0.6
Subroutine
interval: called CC=1 SCAR=0.6
Pod
undocumented
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | field $_filter :mutator; | ||||||||||||||||||||||||||||||||||
|
Subroutine
filter: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | method OPTSPEC | ||||||||||||||||||||||||||||||||||||
| 117 | 11 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
OPTSPEC: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 118 | return ( | ||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | 'b|blib' => sub { require blib; blib->import; }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||
| 121 | 'i|interval=i' => \$_interval, | ||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | 'F|filter=s' => \$_filter, | ||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | 'm|mid3' => sub { $_filter = "mid3" }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||
| 127 | 11 | 'B|best-effort' => \$_best_effort, | |||||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||||||
| 129 | 7 | } | |||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | =head1 PROVIDED METHODS | ||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | The following methods are provided on the base class, intended for subclasses | ||||||||||||||||||||||||||||||||||||
| 134 | or applications to invoke. | ||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | =head2 parse_argv | ||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | $app->parse_argv(); | ||||||||||||||||||||||||||||||||||||
| 141 | $app->parse_argv( \@argv ); | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | Provides a list of commandline arguments for parsing, either from a given | ||||||||||||||||||||||||||||||||||||
| 144 | array reference or defaulting to the process C<@ARGV> if not supplied. | ||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | This uses L</OPTSPEC> to collect the defined arguments, whose references | ||||||||||||||||||||||||||||||||||||
| 147 | should handle the results. | ||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||
| 151 | 11 | ▶ | method parse_argv ( $argv = \@ARGV ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
parse_argv: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||||||||||
| 153 | 11 | my %optspec = $self->OPTSPEC; | |||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||
| 155 | 11 | @_CHIPCONFIGS = (); | |||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | 11 | my $ADAPTERDESC; my $adapter; | |||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | GetOptionsFromArray( $argv, %optspec, | ||||||||||||||||||||||||||||||||||||
| 160 | 8 | ▶ | 'adapter|A=s' => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 161 | 8 | $ADAPTERDESC = $_[1]; | |||||||||||||||||||||||||||||||||||
| 162 | 8 | undef $adapter; | |||||||||||||||||||||||||||||||||||
| 163 | }, | ||||||||||||||||||||||||||||||||||||
| 164 | 12 | ▶ | '<>' => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||
| 165 | 12 | my ( $chiptype, $opts ) = split m/:/, $_[0], 2; | |||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||
| 167 | 12 | ▶ | $adapter //= Device::Chip::Adapter->new_from_description( $ADAPTERDESC ); | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$adapter //= "Device::Chip::Adapter"->new_from_description($ADAPTERDESC)
MC/DC0%0/2
$adapter //= "Device::Chip::Adapter"->new_from_description($ADAPTERDESC)
$adapter
"Device::Chip::Adapter"->new_from_description($ADAPTERDESC)
Truth table66%2/3
A //= B
A= $adapterB= "Device::Chip::Adapter"->new_from_description($ADAPTERDESC)
| |||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | 12 | my %config = ( | |||||||||||||||||||||||||||||||||||
| 170 | type => $chiptype, | ||||||||||||||||||||||||||||||||||||
| 171 | adapter => $adapter, | ||||||||||||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||
| 174 | 12 | ▶ | while( length $opts ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 175 | 1 | ▶ | if( $opts =~ s/^-C:(.*?)=(.*)(?:$|,)// ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 176 | 1 | $config{config}{$1} = $2; | |||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||
| 178 | elsif( $opts =~ s/^-M:(.*?)=(.*)(?:$|,)// ) { | ||||||||||||||||||||||||||||||||||||
| 179 | 0 | $config{mountopts}{$1} = $2; | |||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||||||||||
| 182 | 0 | croak "Unable to parse chip configuration options '$opts' for $chiptype'"; | |||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | 12 | $self->add_chip( %config ); | |||||||||||||||||||||||||||||||||||
| 187 | }, | ||||||||||||||||||||||||||||||||||||
| 188 | 11 | ▶ | ) or exit 1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||
| 190 | 11 | return $self; | |||||||||||||||||||||||||||||||||||
| 191 | 7 | } | |||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | =head2 add_chip | ||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||
| 195 | $app->add_chip( %config ); | ||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||
| 197 | I<Since version 0.05.> | ||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||
| 199 | Adds a new chip to the stored configuration, as if it had been given as a | ||||||||||||||||||||||||||||||||||||
| 200 | commandline argument. Takes the following named arguments: | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||
| 204 | =item type => STR | ||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||
| 206 | Required string that gives the name of the chip class. | ||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||
| 208 | =item adapter => Device::Chip::Adapter | ||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||
| 210 | Required L<Device::Chip::Adapter> instance. | ||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||
| 212 | =item mountopts => HASH | ||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||
| 214 | Optional HASH reference containing extra mount parameters. | ||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||
| 216 | =item config => HASH | ||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||
| 218 | Optional HASH reference containing extra chip configuration to set up using | ||||||||||||||||||||||||||||||||||||
| 219 | the C<configure> method once mounted. | ||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||
| 225 | 12 | ▶ | extended method add_chip ( :$type, :$adapter, %config ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
add_chip: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 226 | { | ||||||||||||||||||||||||||||||||||||
| 227 | push @_CHIPCONFIGS, { | ||||||||||||||||||||||||||||||||||||
| 228 | type => $type, | ||||||||||||||||||||||||||||||||||||
| 229 | adapter => $adapter, | ||||||||||||||||||||||||||||||||||||
| 230 | 12 | ▶ | pairgrep { defined $b } %config{qw( mountopts config )} | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 231 | }; | ||||||||||||||||||||||||||||||||||||
| 232 | 7 | } | |||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||
| 234 | =head2 chips | ||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||
| 236 | @chips = await $app->chips; | ||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||
| 238 | An asynchronous memoized lazy accessor for the list of L<Device::Chip> | ||||||||||||||||||||||||||||||||||||
| 239 | instances, whose class names are taken from the remaining commandline | ||||||||||||||||||||||||||||||||||||
| 240 | arguments after the options are parsed. | ||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||
| 242 | =cut | ||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||
| 244 | 7 | field $_chips; # arrayref | |||||||||||||||||||||||||||||||||||
| 245 | async method chips | ||||||||||||||||||||||||||||||||||||
| 246 | 16 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
chips: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 247 | 16 | ▶ | return @$_chips if $_chips; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | 7 | foreach my $chipconfig ( @_CHIPCONFIGS ) { | |||||||||||||||||||||||||||||||||||
| 250 | 7 | my $chiptype = $chipconfig->{type}; | |||||||||||||||||||||||||||||||||||
| 251 | 7 | my $adapter = $chipconfig->{adapter}; | |||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||
| 253 | 7 | my $class = "Device::Chip::$chiptype"; | |||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | 7 | require ( "$class.pm" ) =~ s(::)(/)gr; | |||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||
| 257 | 7 | my $chip = $class->new; | |||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||
| 259 | 7 | my %mountopts; | |||||||||||||||||||||||||||||||||||
| 260 | 7 | ▶ | %mountopts = $chipconfig->{mountopts}->%* if $chipconfig->{mountopts}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | 7 | await $chip->mount( $adapter, %mountopts ); | |||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||
| 264 | 7 | await $chip->protocol->power(1); | |||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||
| 266 | 7 | ▶ | if( $chipconfig->{config} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 267 | 0 | await $chip->change_config( $chipconfig->{config}->%* ); | |||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | 7 | ▶ | if( $chip->can( "initialize_sensors" ) ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 271 | 1 | await $chip->initialize_sensors; | |||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||
| 274 | 7 | push @$_chips, $chip; | |||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||
| 277 | 7 | return @$_chips; | |||||||||||||||||||||||||||||||||||
| 278 | 7 | } | |||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||
| 280 | =head2 sensors | ||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||
| 282 | @sensors = await $app->sensors; | ||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||
| 284 | An asynchronous memoized lazy accessor for the list of L<Device::Chip::Sensor> | ||||||||||||||||||||||||||||||||||||
| 285 | instances of each of the configured chips (from the L</chips> method). | ||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||
| 287 | =cut | ||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||
| 289 | 7 | field $_sensors; # arrayref | |||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||
| 291 | 7 | field $_chipname_width; | |||||||||||||||||||||||||||||||||||
| 292 | 7 | field $_sensorname_width; | |||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||
| 294 | 9 | ▶ | sub _chipname ( $chip ) { return ( ref $chip ) =~ s/^Device::Chip:://r } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_chipname: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||
| 296 | async method sensors | ||||||||||||||||||||||||||||||||||||
| 297 | 9 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
sensors: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 298 | 9 | ▶ | return @$_sensors if $_sensors; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||
| 300 | 7 | @$_sensors = map { $_->list_sensors } await $self->chips; | |||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||
| 302 | 7 | $_chipname_width = max map { length _chipname $_ } @$_chips; | |||||||||||||||||||||||||||||||||||
| 303 | 7 | $_sensorname_width = max map { length $_->name } @$_sensors; | |||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||
| 305 | 7 | await $self->after_sensors( @$_sensors ); | |||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||
| 307 | 7 | return @$_sensors; | |||||||||||||||||||||||||||||||||||
| 308 | 7 | } | |||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||
| 310 | 6 | ▶ | async method after_sensors ( @sensors ) { } | ||||||||||||||||||||||||||||||||||
|
Subroutine
after_sensors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||
| 312 | =head2 run | ||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||
| 314 | await $app->run; | ||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||
| 316 | An asynchronous method which performs the actual run loop of the sensor | ||||||||||||||||||||||||||||||||||||
| 317 | application. This implements the main application logic, of regular collection | ||||||||||||||||||||||||||||||||||||
| 318 | of values from all of the sensor instances and reporting them to the | ||||||||||||||||||||||||||||||||||||
| 319 | L</output_readings> method. | ||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||
| 321 | In normal circumstances the L<Future> instance returned by this method would | ||||||||||||||||||||||||||||||||||||
| 322 | remain pending for the lifetime of the program, and not complete. For an | ||||||||||||||||||||||||||||||||||||
| 323 | application that has nothing else to perform concurrently it can simply | ||||||||||||||||||||||||||||||||||||
| 324 | C<await> this future to run the logic. If it has other logic to perform as | ||||||||||||||||||||||||||||||||||||
| 325 | well it could combine this with other futures using a C<< Future->needs_all >> | ||||||||||||||||||||||||||||||||||||
| 326 | or similar techniques. | ||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||
| 328 | =cut | ||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||
| 330 | 7 | field %filters_by_sensor; | |||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||
| 332 | 8 | ▶ | async method run () | ||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 333 | 8 | { | |||||||||||||||||||||||||||||||||||
| 334 | 8 | my @chips = await $self->chips; | |||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | $SIG{INT} = $SIG{TERM} = sub { exit 1; }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||
| 338 | 8 | defer { | |||||||||||||||||||||||||||||||||||
| 339 | 8 | try { | |||||||||||||||||||||||||||||||||||
| 340 | 8 | ▶ | $chips[0] and $chips[0]->protocol->power(0)->get; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||
| 342 | catch ($e) { | ||||||||||||||||||||||||||||||||||||
| 343 | 0 | warn "Failed to turn off power while shutting down: $e"; | |||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||
| 347 | 8 | my @sensors = await $self->sensors; | |||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||
| 349 | 8 | my $waittime = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||
| 350 | 8 | while(1) { | |||||||||||||||||||||||||||||||||||
| 351 | # Read concurrently | ||||||||||||||||||||||||||||||||||||
| 352 | 25 | my $now = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||
| 354 | my @values = await Future->needs_all( | ||||||||||||||||||||||||||||||||||||
| 355 | map { | ||||||||||||||||||||||||||||||||||||
| 356 | 25 | my $sensor = $_; | |||||||||||||||||||||||||||||||||||
| 357 | 26 | my $f = $sensor->read; | |||||||||||||||||||||||||||||||||||
| 358 | 1 | $f = $f->then( | |||||||||||||||||||||||||||||||||||
| 359 | 1 | ▶ | async sub ($reading) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 360 | 1 | $self->on_sensor_ok( $sensor ); | |||||||||||||||||||||||||||||||||||
| 361 | 1 | return $reading; | |||||||||||||||||||||||||||||||||||
| 362 | 3 | }, | |||||||||||||||||||||||||||||||||||
| 363 | 3 | ▶ | async sub ($failure, @) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 364 | 3 | $self->on_sensor_fail( $sensor, $failure ); | |||||||||||||||||||||||||||||||||||
| 365 | 3 | return undef; | |||||||||||||||||||||||||||||||||||
| 366 | 26 | ▶ | }, | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 367 | ) if $_best_effort; | ||||||||||||||||||||||||||||||||||||
| 368 | 26 | $f; | |||||||||||||||||||||||||||||||||||
| 369 | } @sensors | ||||||||||||||||||||||||||||||||||||
| 370 | ); | ||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||
| 372 | 25 | foreach my $idx ( 0 .. $#sensors ) { | |||||||||||||||||||||||||||||||||||
| 373 | 26 | my $sensor = $sensors[$idx]; | |||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||
| 375 | 26 | ▶ | my $filter = $filters_by_sensor{ refaddr $sensor } //= $self->make_filter_for_sensor( $sensor ); | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$filters_by_sensor{builtin::refaddr($sensor)} //= $self->make_filter_for_sensor($sensor)
MC/DC0%0/2
$filters_by_sensor{builtin::refaddr($sensor)} //= $self->make_filter_for_sensor($sensor)
$filters_by_sensor{builtin::refaddr($sensor)}
$self->make_filter_for_sensor($sensor)
Truth table66%2/3
A //= B
A= $filters_by_sensor{builtin::refaddr($sensor)}B= $self->make_filter_for_sensor($sensor)
| |||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||
| 377 | 26 | $values[$idx] = $filter->filter( $values[$idx] ); | |||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||
| 380 | 25 | $self->output_readings( $now, \@sensors, \@values ); | |||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||
| 382 | 25 | $waittime += $_interval; | |||||||||||||||||||||||||||||||||||
| 383 | 25 | await Future::IO->alarm( $waittime ); | |||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||
| 385 | 7 | } | |||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||
| 387 | 7 | ▶ | method make_filter_for_sensor ( $sensor ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
make_filter_for_sensor: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 388 | { | ||||||||||||||||||||||||||||||||||||
| 389 | # We only filter gauges currently | ||||||||||||||||||||||||||||||||||||
| 390 | 7 | ▶ | return App::Device::Chip::sensor::Filter::Null->new if $sensor->type ne "gauge"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||
| 392 | 6 | ▶ | if( !length $_filter or $_filter eq "null" ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
not length $_filter or $_filter eq "null"
MC/DC50%1/2
not length $_filter or $_filter eq "null"
not length $_filter
$_filter eq "null"
Truth table66%2/3
not(A) or B
A= not length $_filterB= $_filter eq "null"
| |||||||||||||||||||||||||||||||||||||
| 393 | 3 | return App::Device::Chip::sensor::Filter::Null->new; | |||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||
| 395 | elsif( $_filter =~ m/^mid(\d+)$/ ) { | ||||||||||||||||||||||||||||||||||||
| 396 | 2 | return App::Device::Chip::sensor::Filter::MidN->new( n => $1 ); | |||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||
| 398 | elsif( $_filter =~ m/^ravg(\d+)$/ ) { | ||||||||||||||||||||||||||||||||||||
| 399 | 1 | return App::Device::Chip::sensor::Filter::Ravg->new( alpha => 2 ** -$1 ); | |||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||
| 401 | else { | ||||||||||||||||||||||||||||||||||||
| 402 | 0 | die "Unrecognised filter name $_filter"; | |||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||
| 404 | 7 | } | |||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||
| 406 | =head2 print_readings | ||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||
| 408 | $app->print_readings( $sensors, $values ); | ||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||
| 410 | Prints the sensor names and current readings in a human-readable format to the | ||||||||||||||||||||||||||||||||||||
| 411 | currently-selected output handle (usually C<STDOUT>). | ||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||
| 413 | =cut | ||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||
| 415 | 2 | ▶ | method _format_reading ( $sensor, $value ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
_format_reading: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||
| 416 | { | ||||||||||||||||||||||||||||||||||||
| 417 | 2 | ▶ | return undef if !defined $value; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||
| 419 | # Take account of extra precision required due to filtering | ||||||||||||||||||||||||||||||||||||
| 420 | 2 | my $filter = $filters_by_sensor{ refaddr $sensor }; | |||||||||||||||||||||||||||||||||||
| 421 | 2 | ▶ | my $extra_digits = $filter ? $filter->extra_digits : 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 422 | 2 | return sprintf "%.*f", $sensor->precision + $extra_digits, $value; | |||||||||||||||||||||||||||||||||||
| 423 | 7 | } | |||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||
| 425 | 1 | ▶ | method print_readings ( $sensors, $values ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
print_readings: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 426 | { | ||||||||||||||||||||||||||||||||||||
| 427 | 1 | foreach my $i ( 0 .. $#$sensors ) { | |||||||||||||||||||||||||||||||||||
| 428 | 2 | my $sensor = $sensors->[$i]; | |||||||||||||||||||||||||||||||||||
| 429 | 2 | my $value = $values->[$i]; | |||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||
| 431 | 2 | my $chip = $sensor->chip; | |||||||||||||||||||||||||||||||||||
| 432 | 2 | my $chipname = _chipname $chip; | |||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||
| 434 | 2 | my $units = $sensor->units; | |||||||||||||||||||||||||||||||||||
| 435 | 2 | ▶ | $units = " $units" if defined $units; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||
| 437 | 2 | my $valuestr; | |||||||||||||||||||||||||||||||||||
| 438 | 2 | ▶ | if( !defined $value ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 439 | 0 | $valuestr = "<undef>"; | |||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||
| 441 | elsif( $sensor->type eq "gauge" ) { | ||||||||||||||||||||||||||||||||||||
| 442 | 1 | ▶ | $valuestr = sprintf "%s%s", $self->_format_reading( $sensor, $value ), $units // ""; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$units // ""
MC/DC0%0/1
$units // ""
$units
Truth table50%1/2
A
A= $units
| |||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||
| 444 | else { | ||||||||||||||||||||||||||||||||||||
| 445 | 1 | ▶ | $valuestr = sprintf "%s%s/sec", $self->_format_reading( $sensor, $value / $self->interval ), $units // ""; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$units // ""
MC/DC0%0/1
$units // ""
$units
Truth table50%1/2
A
A= $units
| |||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||
| 448 | 2 | printf "% *s/% *s: %s\n", | |||||||||||||||||||||||||||||||||||
| 449 | $_chipname_width, $chipname, $_sensorname_width, $sensor->name, $valuestr; | ||||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||||
| 451 | 7 | } | |||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||
| 453 | =head1 REQUIRED METHODS | ||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||
| 455 | This base class itself is incomplete, requiring the following methods to be | ||||||||||||||||||||||||||||||||||||
| 456 | provided by an implementing subclass to contain the actual application logic. | ||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||
| 458 | =cut | ||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||
| 460 | =head2 output_readings | ||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||
| 462 | $app->output_readings( $now, $sensors, $values ); | ||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||
| 464 | This method is invoked regularly by the L</run> method, to provide the | ||||||||||||||||||||||||||||||||||||
| 465 | application with the latest round of sensor readings. It is passed the current | ||||||||||||||||||||||||||||||||||||
| 466 | UNIX epoch timestamp as C<$now>, an array reference containing the individual | ||||||||||||||||||||||||||||||||||||
| 467 | L<Device::Chip::Sensor> instances as C<$sensors>, and a congruent array | ||||||||||||||||||||||||||||||||||||
| 468 | reference containing the most recent readings taken from them, as plain | ||||||||||||||||||||||||||||||||||||
| 469 | numbers. | ||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||
| 471 | The application should put the bulk of its processing logic in here, for | ||||||||||||||||||||||||||||||||||||
| 472 | example writing the values to some sort of file or database, displaying them | ||||||||||||||||||||||||||||||||||||
| 473 | in some form, or whatever else the application is supposed to do. | ||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||
| 475 | =cut | ||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||
| 477 | =head1 OVERRIDABLE METHODS | ||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||
| 479 | The base class provides the following methods, but it is expected that | ||||||||||||||||||||||||||||||||||||
| 480 | applications may wish to override them to customise the logic contained in | ||||||||||||||||||||||||||||||||||||
| 481 | them. | ||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||
| 483 | If using L<Object::Pad> to do so, don't forget to provide the C<:override> | ||||||||||||||||||||||||||||||||||||
| 484 | method attribute. | ||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||
| 486 | =cut | ||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||
| 488 | =head2 OPTSPEC | ||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||
| 490 | %optspec = $app->OPTSPEC; | ||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||
| 492 | This method is invoked by the L</parse_argv> method to construct a definition | ||||||||||||||||||||||||||||||||||||
| 493 | of the commandline options understood by the program. These are returned in a | ||||||||||||||||||||||||||||||||||||
| 494 | key/value list to be processed by L<Getopt::Long>. If the application wishes | ||||||||||||||||||||||||||||||||||||
| 495 | to parse additional arguments it should override this method, call the | ||||||||||||||||||||||||||||||||||||
| 496 | superclass version, and append any extra argument specifications it requires. | ||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||
| 498 | As this is invoked as a regular instance method, a convenient way to store the | ||||||||||||||||||||||||||||||||||||
| 499 | parsed values is to pass references to instance slot variables created by the | ||||||||||||||||||||||||||||||||||||
| 500 | L<Object::Pad> C<field> keyword: | ||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||
| 502 | field $_title; | ||||||||||||||||||||||||||||||||||||
| 503 | field $_bgcol = "#cccccc"; | ||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||
| 505 | method OPTSPEC :override | ||||||||||||||||||||||||||||||||||||
| 506 | { | ||||||||||||||||||||||||||||||||||||
| 507 | return ( $self->SUPER::OPTSPEC, | ||||||||||||||||||||||||||||||||||||
| 508 | 'title=s' => \$_title, | ||||||||||||||||||||||||||||||||||||
| 509 | 'background-color=s' => \$_bgcol, | ||||||||||||||||||||||||||||||||||||
| 510 | ); | ||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||
| 513 | =cut | ||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||
| 515 | =head2 after_sensors | ||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||
| 517 | await $app->after_sensors( @sensors ); | ||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||
| 519 | This method is invoked once on startup by the L</run> method, after it has | ||||||||||||||||||||||||||||||||||||
| 520 | configured the chip adapter and chips and obtained their individual sensor | ||||||||||||||||||||||||||||||||||||
| 521 | instances. The application may wish to perform one-time startup tasks in here, | ||||||||||||||||||||||||||||||||||||
| 522 | such as creating database files with knowledge of the specific sensor data | ||||||||||||||||||||||||||||||||||||
| 523 | types, or other such behaviours. | ||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||
| 525 | =cut | ||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||
| 527 | =head2 on_sensor_ok | ||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||
| 529 | $app->on_sensor_ok( $sensor ); | ||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||
| 531 | This method is invoked in C<--best-effort> mode after a successful reading | ||||||||||||||||||||||||||||||||||||
| 532 | from sensor; typically this is used to clear a failure state. | ||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||
| 534 | The default implementation does nothing. | ||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||
| 536 | =cut | ||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||
| 538 | 0 | ▶ | method on_sensor_ok ( $sensor ) { } | ||||||||||||||||||||||||||||||||||
|
Subroutine
on_sensor_ok: not called CC=1 SCAR=3.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||
| 540 | =head2 on_sensor_fail | ||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||
| 542 | $app->on_sensor_fail( $sensor, $failure ); | ||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||
| 544 | This method is invoked in C<--best-effort> mode after a failure of the given | ||||||||||||||||||||||||||||||||||||
| 545 | sensor. The caught exception is passed as C<$failure>. | ||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||
| 547 | The default implementation prints this as a warning using the core C<warn()> | ||||||||||||||||||||||||||||||||||||
| 548 | function. | ||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||
| 550 | =cut | ||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||
| 552 | 0 | ▶ | method on_sensor_fail ( $sensor, $failure ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
on_sensor_fail: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 553 | { | ||||||||||||||||||||||||||||||||||||
| 554 | 0 | my $sensorname = $sensor->name; | |||||||||||||||||||||||||||||||||||
| 555 | 0 | my $chipname = ref ( $sensor->chip ); | |||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||
| 557 | 0 | warn "Unable to read ${sensorname} of ${chipname}: $failure"; | |||||||||||||||||||||||||||||||||||
| 558 | 7 | } | |||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||
| 560 | =head1 FILTERING | ||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||
| 562 | The C<--filter> setting accepts the following filter names | ||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||
| 564 | =cut | ||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||
| 566 | =head2 null | ||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||
| 568 | No filtering is applied. Each sensor reading is reported as it stands. | ||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||
| 570 | =cut | ||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||
| 572 | class App::Device::Chip::sensor::Filter::Null | ||||||||||||||||||||||||||||||||||||
| 573 | { | ||||||||||||||||||||||||||||||||||||
| 574 | 7 | ▶ | use constant extra_digits => 0; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||
| 576 | 10 | ▶ | method filter ( $value ) { return $value } | ||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||
| 579 | 7 | =head2 midI<n> | |||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||
| 581 | The most recent I<n> values are sorted, and the middle of these is reported. | ||||||||||||||||||||||||||||||||||||
| 582 | To be well-behaved, I<n> should be an odd number. (C<mid3>, C<mid5>, C<mid7>, | ||||||||||||||||||||||||||||||||||||
| 583 | etc...) | ||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||
| 585 | =cut | ||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||
| 587 | class App::Device::Chip::sensor::Filter::MidN | ||||||||||||||||||||||||||||||||||||
| 588 | { | ||||||||||||||||||||||||||||||||||||
| 589 | 7 | ▶ | use List::Util 1.29 qw( all ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||
| 591 | 7 | field $n :param; | |||||||||||||||||||||||||||||||||||
| 592 | 7 | field @readings; | |||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||
| 594 | 7 | ▶ | use constant extra_digits => 0; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||
| 596 | 12 | ▶ | method filter ( $value ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 597 | { | ||||||||||||||||||||||||||||||||||||
| 598 | # Accumulate the past 3 readings | ||||||||||||||||||||||||||||||||||||
| 599 | 12 | push @readings, $value; | |||||||||||||||||||||||||||||||||||
| 600 | 12 | ▶ | shift @readings while @readings > $n; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||
| 602 | # Take the middle of the 3 | ||||||||||||||||||||||||||||||||||||
| 603 | 12 | ▶ | return $value unless @readings == $n and all { defined } @readings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
@readings == $n and &all(sub {
defined $_;
}
, @readings)
MC/DC50%1/2
@readings == $n and &all(sub {
defined $_;
}
, @readings)
@readings == $n
&all(sub {
defined $_;
}
, @readings)
Truth table66%2/3
A and B
A= @readings == $nB= &all(sub {
defined $_;
}
, @readings)
| |||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||
| 605 | 6 | my @sorted = sort { $a <=> $b } @readings; | |||||||||||||||||||||||||||||||||||
| 606 | 6 | return $sorted[($n-1) / 2]; | |||||||||||||||||||||||||||||||||||
| 607 | 7 | } | |||||||||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||
| 610 | 7 | =head2 ravgI<n> | |||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||
| 612 | Recursive average with weighting of C<2 ** -n>. | ||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||
| 614 | =cut | ||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||
| 616 | class App::Device::Chip::sensor::Filter::Ravg | ||||||||||||||||||||||||||||||||||||
| 617 | { | ||||||||||||||||||||||||||||||||||||
| 618 | 7 | field $alpha :param; | |||||||||||||||||||||||||||||||||||
| 619 | 7 | field $prev; | |||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||
| 621 | 7 | ▶ | use constant extra_digits => 2; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||
| 623 | 4 | ▶ | method filter ( $value ) | ||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 624 | { | ||||||||||||||||||||||||||||||||||||
| 625 | 4 | ▶ | return $prev = $value if !defined $prev; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 626 | 3 | return $prev = $prev + $alpha * ( $value - $prev ); | |||||||||||||||||||||||||||||||||||
| 627 | 7 | } | |||||||||||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||
| 630 | 7 | =head1 AUTHOR | |||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||
| 632 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||
| 634 | =cut | ||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||
| 636 | 7 | 0x55AA; | |||||||||||||||||||||||||||||||||||