| 1 | package Lab::Instrument::TemperatureDiode; | ||
| 2 | #ABSTRACT: ????? | ||
| 3 | $Lab::Instrument::TemperatureDiode::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Math::Complex; | ||
| 8 | use Lab::Exception; | ||
| 9 | use Scalar::Util qw(weaken); | ||
| 10 | use Carp qw(croak cluck); | ||
| 11 | use Data::Dumper; | ||
| 12 | our $AUTOLOAD; | ||
| 13 | |||
| 14 | our %fields = ( | ||
| 15 | instrument => undef, | ||
| 16 | |||
| 17 | device_cache => { | ||
| 18 | id => undef, | ||
| 19 | }, | ||
| 20 | |||
| 21 | device_cache_order => ['id'], | ||
| 22 | ); | ||
| 23 | |||
| 24 | our @ISA = (); | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $proto = shift; | ||
| 28 | my $class = ref($proto) || $proto; | ||
| 29 | my $config = undef; | ||
| 30 | if ( ref $_[0] eq 'HASH' ) { | ||
| 31 | $config = shift; | ||
| 32 | } # try to be flexible about options as hash/hashref | ||
| 33 | else { $config = {@_} } | ||
| 34 | my $self = {}; | ||
| 35 | bless( $self, $class ); | ||
| 36 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 37 | |||
| 38 | while ( my ( $k, $v ) = each %{$config} ) { | ||
| 39 | $self->{$k} = $v; | ||
| 40 | } | ||
| 41 | |||
| 42 | if ( not defined $self->instrument() ) { | ||
| 43 | Lab::Exception::Error->throw( error => $self->get_id() | ||
| 44 | . ": No intrument for temperature measurment defined!" ); | ||
| 45 | } | ||
| 46 | elsif ( not ref( $self->instrument() ) =~ /^(Lab::Instrument)/ ) { | ||
| 47 | Lab::Exception::Error->throw( error => $self->get_id() | ||
| 48 | . ": Object for temperature measurement is not an instrument!" | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | |||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _construct { # _construct(__PACKAGE__); | ||
| 56 | ( my $self, my $package ) = ( shift, shift ); | ||
| 57 | my $class = ref($self); | ||
| 58 | my $fields = undef; | ||
| 59 | { | ||
| 60 | no strict 'refs'; | ||
| 61 | $fields = *${ \( $package . '::fields' ) }{HASH}; | ||
| 62 | } | ||
| 63 | my $twin = undef; | ||
| 64 | |||
| 65 | foreach my $element ( keys %{$fields} ) { | ||
| 66 | $self->{_permitted}->{$element} = $fields->{$element}; | ||
| 67 | } | ||
| 68 | @{$self}{ keys %{$fields} } = values %{$fields}; | ||
| 69 | } | ||
| 70 | |||
| 71 | # converts given measurementvalue to a temperature in Kelvin | ||
| 72 | |||
| 73 | sub config { # $value = self->config($key); | ||
| 74 | ( my $self, my $key ) = ( shift, shift ); | ||
| 75 | |||
| 76 | if ( !defined $key ) { | ||
| 77 | return $self->{'config'}; | ||
| 78 | } | ||
| 79 | elsif ( ref($key) =~ /HASH/ ) { | ||
| 80 | return $self->{'config'} = $key; | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | return $self->{'config'}->{$key}; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub set_id { | ||
| 88 | my $self = shift; | ||
| 89 | my ($id) = $self->_check_args( \@_, ['id'] ); | ||
| 90 | $self->{'device_cache'}->{'id'} = $id; | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | sub get_id { | ||
| 95 | my $self = shift; | ||
| 96 | return $self->{'device_cache'}->{'id'}; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub get_value { | ||
| 100 | my $self = shift; | ||
| 101 | |||
| 102 | return $self->get_T(@_); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub get_T { | ||
| 106 | my $self = shift; | ||
| 107 | my $options = undef; | ||
| 108 | if ( ref $_[0] eq 'HASH' ) { $options = shift } | ||
| 109 | else { $options = {@_} } | ||
| 110 | |||
| 111 | if ( $options->{read_mode} eq 'cache' | ||
| 112 | and defined $self->{'device_cache'}->{'T'} ) { | ||
| 113 | return $self->{'device_cache'}->{'T'}; | ||
| 114 | } | ||
| 115 | |||
| 116 | my $value = $self->instrument()->get_value($options); | ||
| 117 | if ( defined $value ) { | ||
| 118 | return $self->device_cache()->{T} = $self->convert2Kelvin($value); | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | return undef; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | sub convert2Kelvin { | ||
| 126 | my $self = shift; | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _check_args { | ||
| 131 | my $self = shift; | ||
| 132 | my $args = shift; | ||
| 133 | my $params = shift; | ||
| 134 | |||
| 135 | my $arguments; | ||
| 136 | |||
| 137 | my $i = 0; | ||
| 138 | my $tempo_hash = {}; | ||
| 139 | |||
| 140 | foreach my $arg ( @{$args} ) { | ||
| 141 | if ( ref($arg) ne "HASH" ) { | ||
| 142 | if ( defined @{$params}[$i] ) { | ||
| 143 | $tempo_hash->{ @{$params}[$i] } = $arg; | ||
| 144 | |||
| 145 | } | ||
| 146 | $i++; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | %{$arguments} = ( %{$tempo_hash}, %{ @{$args}[$i] } ); | ||
| 150 | last; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | if ( not defined $arguments ) { | ||
| 155 | $arguments = $tempo_hash; | ||
| 156 | } | ||
| 157 | |||
| 158 | my @return_args = (); | ||
| 159 | |||
| 160 | foreach my $param ( @{$params} ) { | ||
| 161 | |||
| 162 | if ( exists $arguments->{$param} ) { | ||
| 163 | push( @return_args, $arguments->{$param} ); | ||
| 164 | delete $arguments->{$param}; | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | push( @return_args, undef ); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | foreach my $param ( 'from_device', 'from_cache' | ||
| 172 | ) # Delete Standard option parameters from $arguments hash if not defined in device driver function | ||
| 173 | { | ||
| 174 | if ( exists $arguments->{$param} ) { | ||
| 175 | delete $arguments->{$param}; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | if ( scalar( keys %{$arguments} ) > 0 ) { | ||
| 180 | my $errmess = "Unknown parameter given in $self :"; | ||
| 181 | while ( my ( $k, $v ) = each %{$arguments} ) { | ||
| 182 | $errmess .= $k . " => " . $v . "\t"; | ||
| 183 | } | ||
| 184 | print Lab::Exception::Warning->new( error => $errmess ); | ||
| 185 | } | ||
| 186 | |||
| 187 | return @return_args; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub AUTOLOAD { | ||
| 191 | |||
| 192 | my $self = shift; | ||
| 193 | my $type = ref($self) or croak "$self is not an object"; | ||
| 194 | |||
| 195 | my $name = $AUTOLOAD; | ||
| 196 | $name =~ s/.*://; # strip fully qualified portion | ||
| 197 | |||
| 198 | unless ( exists $self->{_permitted}->{$name} ) { | ||
| 199 | cluck( "AUTOLOAD in " | ||
| 200 | . __PACKAGE__ | ||
| 201 | . " couldn't access field '${name}'.\n" ); | ||
| 202 | Lab::Exception::Error->throw( error => "AUTOLOAD in " | ||
| 203 | . __PACKAGE__ | ||
| 204 | . " couldn't access field '${name}'.\n" ); | ||
| 205 | } | ||
| 206 | |||
| 207 | if (@_) { | ||
| 208 | return $self->{$name} = shift; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | return $self->{$name}; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | # needed so AUTOLOAD doesn't try to call DESTROY on cleanup and prevent the inherited DESTROY | ||
| 216 | sub DESTROY { | ||
| 217 | my $self = shift; | ||
| 218 | $self->SUPER::DESTROY if $self->can("SUPER::DESTROY"); | ||
| 219 | } | ||
| 220 | |||
| 221 | # defined sensors | ||
| 222 | |||
| 223 | 1; | ||
| 224 | |||
| 225 | __END__ |