MultiChannelInstrument.pm

filter: Statementstmt 19.8% 31 / 156 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 47.3% 9 / 19 Podpod 0.0% 0 / 6 totaltotal 15.0% 40 / 266 SCAR 69.8
CC
40
Cov
13%
AUTOLOAD
132.0
_construct
89.6
new
72.0
CRAP 1078.4
« Moose/DataFile/Gnuplot.pm ↑ Summary Instrument/TemperatureControl/TLK43.pm »
1 package Lab::MultiChannelInstrument;
2 1 $Lab::MultiChannelInstrument::VERSION = '3.881';
3 #ABSTRACT: Multi-channel instrument base class
4  
5 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lab::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::MoreUtils qw{ any };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw(cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Class::ISA qw(self_and_super_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $AUTOLOAD;
16 1 our @ISA = ('Lab::Generic');
17  
18 1 our %fields = (
19 supported_connections => [],
20  
21 connection_settings => {},
22  
23 device_settings => {
24 channels => {
25 'A' => undef,
26 'B' => undef,
27 },
28 channel_default => undef,
29  
30 },
31  
32 device_cache => {},
33  
34 device_cache_order => [],
35 );
36  
37 sub new {
38 0 ▶ my $proto = shift;
Subroutine new: not called CC=8 SCAR=42.8
Pod undocumented
39 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
40  
41 # create generic class:
42 0 my $self = ${ \(__PACKAGE__) }->SUPER::new(@_);
43 0 $self->${ \( __PACKAGE__ . '::_construct' ) }( __PACKAGE__, $class );
44  
45 # merge user config data with default config
46 0 my $config = {};
47 0 ▶ if ( ref $_[0] eq 'HASH' ) { %{$config} = ( %{$config}, %{ $_[0] } ) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
48 0 else { $config = {@_} }
49 0 $self->{config} = $config;
50 0 $self->_construct( $self->{config} );
51  
52 # load parent class lib
53 0 my @isa = Class::ISA::super_path($class);
54 0 our @ISA = $isa[2];
55 0 ▶ eval "require $ISA[0]; @ISA->import(); 1;"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $Lab::MultiChannelInstrument::ISA[0]; @Lab::MultiChannelInstrument::ISA->import(); 1;" or do "Lab::Exception::Warning"->throw("error", $@) 0 0 0
56 or do Lab::Exception::Warning->throw( error => $@ );
57  
58 # create instrument channels:
59 0 ▶ while ( my ( $channel, $value )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"device_settings"}{"channels"};}) 0 0 0
60 0 = each %{ $self->{device_settings}->{channels} } ) {
61 0 $self->{channels}->{$channel} = $class->SUPER::new( $self->{config} );
62  
63 0 $self->{channels}->{$channel}->${ \( $ISA[0] . '::_construct' ) }
64 ($class);
65  
66 0 $self->{channels}->{$channel}->{channel} = $value;
67  
68 # link shared cache values to the same cache-address in order to keep these parameters up to date for all channels (using a tied Hash):
69 0 my $device_cache;
70 0 tie %$device_cache, 'Lab::MultiChannelInstrument::DeviceCache', $self;
71  
72 0 ▶ while ( my ( $k, $v )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->{"channels"}{$channel}->device_cache;}) 0 0 0
73 0 = each %{ $self->{channels}->{$channel}->device_cache() } ) {
74 0 $device_cache->{$k} = $v;
75 }
76 0 $self->{channels}->{$channel}->{device_cache} = $device_cache;
77  
78 0 $self->{channels}->{$channel}->unregister_instrument();
79  
80 }
81  
82 0 ▶ if ( not defined $self->{device_settings}->{channel_default}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"device_settings"}{"channel_default"} and exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}) 0 0 0
Condition0%0/3
defined $self->{"device_settings"}{"channel_default"} and exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"device_settings"}{"channel_default"} and exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}
defined $self->{"device_settings"}{"channel_default"} exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}
Truth table0%0/3
A and B
A= defined $self->{"device_settings"}{"channel_default"}B= exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}
ABresult
0X0
100
111
83 or not exists $self->{channels}
84 ->{ $self->{device_settings}->{channel_default} } ) {
85 Lab::Exception::Warning->throw(
86 error => "\n\nMultiChannelDevice: default channel '"
87 . $self->{device_settings}->{channel_default}
88 0 . "' is not defined or does not exist!\n\n" );
89 }
90  
91 0 $self->register_instrument();
92  
93 0 our @ISA = ('Lab::Generic');
94 0 return $self;
95 }
96  
97 sub _construct { # _construct(__PACKAGE__);
98 0 ▶ ( my $self, my $package ) = ( shift, shift );
Subroutine _construct: not called CC=10 SCAR=45.0
99 0 my $class = shift;
100 0 my $fields = undef;
101  
102 0 ▶ if ( ref($package) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $package ne 'HASH') { } 0 0 0
103 0 my @isa = Class::ISA::self_and_super_path($class);
104 0 my $device_class = $isa[0];
105 {
106 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
107 0 $fields = *${ \( $package . '::fields' ) }{HASH};
108 $fields
109 0 = ( $fields, *${ \( $device_class . '::fields' ) }{HASH} );
110 }
111 }
112 else {
113 0 $fields = $package;
114 }
115  
116 0 foreach my $element ( keys %{$fields} ) {
117  
118 # handle special subarrays
119 0 ▶ if ( $element eq 'device_settings' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($element eq 'device_settings') { } 0 0 0
elsif ($element eq 'connection_settings') { } 0 0 0
elsif ($element eq 'channels') { } 0 0 0
120  
121 # # don't overwrite filled hash from ancestor
122 $self->{device_settings} = {}
123 0 ▶ if !exists( $self->{device_settings} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"device_settings"} 0 0 0
124 0 for my $s_key ( keys %{ $fields->{'device_settings'} } ) {
125 $self->{device_settings}->{$s_key}
126 0 = clone( $fields->{device_settings}->{$s_key} );
127 }
128 }
129 elsif ( $element eq 'connection_settings' ) {
130  
131 # don't overwrite filled hash from ancestor
132 $self->{connection_settings} = {}
133 0 ▶ if !exists( $self->{connection_settings} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"connection_settings"} 0 0 0
134 0 for my $s_key ( keys %{ $fields->{connection_settings} } ) {
135 $self->{connection_settings}->{$s_key}
136 0 = clone( $fields->{connection_settings}->{$s_key} );
137 }
138 }
139 elsif ( $element eq 'channels' ) {
140 0 $self->{device_settings}->{channels} = $fields->{$element};
141 }
142 else {
143 # handle the normal fields - can also be hash refs etc, so use clone to get a deep copy
144 0 $self->{$element} = clone( $fields->{$element} );
145  
146 #warn "here comes\n" if($element eq 'device_cache');
147 #warn Dumper($Lab::Instrument::DummySource::fields) if($element eq 'device_cache');
148 }
149 0 $self->{_permitted}->{$element} = 1;
150 }
151  
152 }
153  
154 sub channel {
155 0 ▶ my $self = shift;
Subroutine channel: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my $channel = shift;
157  
158 0 ▶ if ( exists $self->{channels}->{$channel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'channels'}{$channel}) { } 0 0 0
159 0 return $self->{channels}->{$channel};
160 }
161 else {
162 0 Lab::Exception::CorruptParameter->throw(
163 error => "\n\nMultiChannelInstrument: Channel '"
164 . $channel
165 . "' is not defined.\n\n" );
166 }
167 }
168  
169 sub sprint_config {
170 0 ▶ my $self = shift;
Subroutine sprint_config: not called CC=7 SCAR=40.3
Pod undocumented
171 0 my $config;
172  
173 my $device_cache;
174 0 $Data::Dumper::Varname = "device_cache_";
175  
176 0 ▶ while ( my ( $k, $v ) = each %{ $self->{device_cache} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"device_cache"};}) 0 0 0
177 0 ▶ if ( any { $_ eq $k } @{ $self->{multichannel_shared_cache} } ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $k; } , @{$$self{"multichannel_shared_cache"};})) 0 0 0
178 0 $device_cache->{'shared_variables'}->{$k} = $v;
179 }
180 }
181  
182 0 ▶ while ( my ( $chk, $chv ) = each %{ $self->{channels} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"channels"};}) 0 0 0
183 0 $device_cache->{'multichannel_variables'}->{$chk}->{'name'}
184 = $chv->get_name();
185 0 ▶ while ( my ( $k, $v )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"channels"}{$chk}{"device_cache"};}) 0 0 0
186 0 = each %{ $self->{channels}->{$chk}->{device_cache} } ) {
187 0 ▶ if ( any { $_ eq $k } @{ $self->{multichannel_shared_cache} } ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $k; } , @{$$self{'multichannel_shared_cache'};})) { } 0 0 0
188  
189 }
190 else {
191 0 $device_cache->{'multichannel_variables'}->{$chk}->{$k} = $v;
192 }
193 }
194 }
195  
196 0 $config .= Dumper $device_cache;
197  
198 0 $Data::Dumper::Varname = "connection_settings_";
199 0 $Data::Dumper::Maxdepth = 1;
200 0 ▶ if ( defined $self->connection() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->connection) 0 0 0
201 0 $config .= Dumper $self->connection();
202 }
203 0 return $config;
204 }
205  
206 sub register_instrument {
207 0 ▶ my $self = shift;
Subroutine register_instrument: not called CC=1 SCAR=6.9
Pod undocumented
208  
209 0 push( @{Lab::Instrument::REGISTERED_INSTRUMENTS}, $self );
210  
211 }
212  
213 sub unregister_instrument {
214 0 ▶ my $self = shift;
Subroutine unregister_instrument: not called CC=1 SCAR=6.9
Pod undocumented
215  
216 @{Lab::Instrument::REGISTERED_INSTRUMENTS}
217 0 = grep { $_ ne $self } @{Lab::Instrument::REGISTERED_INSTRUMENTS};
218  
219 }
220  
221 sub AUTOLOAD {
222 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=11 SCAR=48.8
223 0 ▶ my $type = ref($self) or croak "\$self is not an object";
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("\$self is not an object") 0 0 0
224 0 my $value = undef;
225  
226 0 my $name = $AUTOLOAD;
227 0 $name =~ s/.*://; # strip fully qualified portion
228  
229 0 ▶ if ( exists $self->{_permitted}->{$name} ) {
Branch: 5 branches
BranchTrueFalseTotal
if (exists $self->{'_permitted'}{$name}) { } 0 0 0
elsif (exists $self->{'channels'}{$name}) { } 0 0 0
elsif (exists $self->{'device_settings'}{$name}) { } 0 0 0
elsif (defined $self->{'channels'}{$self->{'device_settings'}{'channel_default'}} and $self->{'channels'}{$self->{'device_settings'}{'channel_default'}}->can($name)) { } 0 0 0
elsif (defined $self->{'channels'}{$self->{'device_settings'}{'channel_default'}} and exists $self->{'channels'}{$self->{'device_settings'}{'channel_default'}}{$name}) { } 0 0 0
230  
231 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
232 0 return $self->{$name} = shift;
233 }
234 else {
235 0 return $self->{$name};
236 }
237 }
238 elsif ( exists $self->{channels}->{$name} ) {
239 0 return $self->{channels}->{$name};
240 }
241 elsif ( exists $self->{'device_settings'}->{$name} ) {
242 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
243 0 return $self->{'device_settings'}->{$name} = shift;
244 }
245 else {
246 0 return $self->{'device_settings'}->{$name};
247 }
248 }
249 ▶ elsif (
Condition0%0/3
defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}} and $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}->can($name)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}} and $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}->can($name)
defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}} $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}->can($name)
Truth table0%0/3
A and B
A= defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}B= $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}->can($name)
ABresult
0X0
100
111
250 defined $self->{channels}
251 ->{ $self->{device_settings}->{channel_default} }
252 and $self->{channels}->{ $self->{device_settings}->{channel_default} }
253 ->can($name) ) {
254 return $self->{channels}
255 0 ->{ $self->{device_settings}->{channel_default} }->$name(@_);
256 }
257 ▶ elsif (
Condition0%0/3
defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}} and exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}} and exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}{$name}
defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}} exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}{$name}
Truth table0%0/3
A and B
A= defined $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}B= exists $self->{"channels"}{$self->{"device_settings"}{"channel_default"}}{$name}
ABresult
0X0
100
111
258 defined $self->{channels}
259 ->{ $self->{device_settings}->{channel_default} }
260 and exists $self->{channels}
261 ->{ $self->{device_settings}->{channel_default} }->{$name} ) {
262 return $self->{channels}
263 0 ->{ $self->{device_settings}->{channel_default} }->{$name};
264 }
265 else {
266 0 Lab::Exception::Warning->throw( error => "AUTOLOAD in "
267 . __PACKAGE__
268 . " couldn't access field '${name}'.\n" );
269 }
270 }
271  
272 sub device_cache {
273 0 ▶ my $self = shift;
Subroutine device_cache: not called CC=7 SCAR=40.3
Pod undocumented
274 0 my $value = undef;
275  
276 #warn "device_cache got this:\n" . Dumper(@_) . "\n";
277  
278 0 ▶ if ( scalar(@_) == 0 )
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 0 0
elsif (scalar @_ == 1) { } 0 0 0
elsif (scalar @_ > 1 and scalar(@_) % 2 == 0) { } 0 0 0
279 { # empty parameters - return whole device_settings hash
280 0 return $self->{'device_cache'};
281 }
282 elsif ( scalar(@_) == 1 )
283 { # one parm - either a scalar (key) or a hashref (try to merge)
284 0 $value = shift;
285 }
286 ▶ elsif ( scalar(@_) > 1 && scalar(@_) % 2 == 0 )
Condition0%0/3
scalar @_ > 1 and scalar(@_) % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ > 1 and scalar(@_) % 2 == 0
scalar @_ > 1 scalar(@_) % 2 == 0
Truth table0%0/3
A and B
A= scalar @_ > 1B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
287 { # even sized list - assume it's keys and values and try to merge it
288 0 $value = {@_};
289 }
290 else { # uneven sized list - don't know what to do with that one
291 0 Lab::Exception::CorruptParameter->throw(
292 error => "Corrupt parameters given to "
293 . __PACKAGE__
294 . "::device_cache().\n" );
295 }
296  
297 #warn "Keys present: \n" . Dumper($self->{device_settings}) . "\n";
298  
299 0 ▶ if ( ref($value) =~ /HASH/ ) { # it's a hash - merge into current settings
Branch: 1 branch
BranchTrueFalseTotal
if (ref($value) =~ /HASH/u) { } 0 0 0
300 0 for my $ext_key ( keys %{$value} ) {
301 0 $self->{'device_cache'}->{$ext_key} = $value->{$ext_key}
302 ; # if( exists($self->device_cache()->{$ext_key}) );
303 }
304 0 return $self->{'device_cache'};
305 }
306 else { # it's a key - return the corresponding value
307 0 return $self->{'device_cache'}->{$value};
308 }
309 }
310  
311 1 1;
312  
313 __END__
314  
315 =pod
316  
317 =encoding UTF-8
318  
319 =head1 NAME
320  
321 Lab::MultiChannelInstrument - Multi-channel instrument base class
322  
323 =head1 VERSION
324  
325 version 3.881
326  
327 =head1 COPYRIGHT AND LICENSE
328  
329 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
330  
331 Copyright 2013 Christian Butschkow, Stefan Geissler
332 2014 Andreas K. Huettel, Christian Butschkow
333 2015 Andreas K. Huettel
334 2016 Simon Reinhardt
335 2017 Andreas K. Huettel
336 2019 Simon Reinhardt
337 2020 Andreas K. Huettel
338  
339  
340 This is free software; you can redistribute it and/or modify it under
341 the same terms as the Perl 5 programming language system itself.
342  
343 =cut
« Moose/DataFile/Gnuplot.pm ↑ Summary Instrument/TemperatureControl/TLK43.pm »