File Coverage

blib/lib/Lab/Moose/Instrument/Bluefors_Temp.pm
Criterion Covered Total %
statement 35 125 28.0
branch 0 12 0.0
condition 0 12 0.0
subroutine 12 23 52.1
pod 10 10 100.0
total 57 182 31.3


line stmt bran cond sub pod time code
1             package Lab::Moose::Instrument::Bluefors_Temp;
2             $Lab::Moose::Instrument::Bluefors_Temp::VERSION = '3.900';
3             #ABSTRACT: Bluefors temperature control
4              
5 1     1   2249 use v5.20;
  1         4  
6              
7 1     1   11 use strict;
  1         3  
  1         30  
8 1     1   8 use Moose;
  1         3  
  1         10  
9 1     1   7973 use Moose::Util::TypeConstraints qw/enum/;
  1         3  
  1         13  
10 1     1   503 use MooseX::Params::Validate;
  1         6  
  1         12  
11 1         75 use Lab::Moose::Instrument qw/
12             validated_getter
13             validated_setter
14             validated_no_param_setter
15             setter_params
16 1     1   474 /;
  1         3  
17 1     1   6 use Carp;
  1         3  
  1         51  
18 1     1   7 use namespace::autoclean;
  1         2  
  1         11  
19 1     1   99 use Time::HiRes qw/time usleep/;
  1         3  
  1         12  
20              
21             # HTTP request JSON stuff
22 1     1   2190 use DateTime;
  1         628083  
  1         57  
23 1     1   1441 use JSON;
  1         12688  
  1         9  
24 1     1   177 use Data::Dumper;
  1         2  
  1         1536  
25              
26              
27             extends 'Lab::Moose::Instrument';
28              
29             has default_channel => (
30             is => 'ro',
31             isa => 'Num',
32             default => 1,
33             );
34              
35             has default_heater => (
36             is => 'ro',
37             isa => 'Num',
38             default => 1,
39             );
40              
41             has default_time => (
42             is => 'ro',
43             isa => 'Num',
44             default => 90,
45             );
46              
47             has json => (
48             is => 'ro',
49             isa => 'Any',
50             builder => '_build_json',
51             );
52             sub _build_json {
53 0     0     return JSON->new;
54             }
55              
56             around default_connection_options => sub {
57             my $orig = shift;
58             my $self = shift;
59             my $options = $self->$orig();
60              
61             $options->{HTTP}{port} = 5001;
62             return $options;
63             };
64              
65              
66             sub get_measurement {
67 0     0 1   my ( $self, %args ) = validated_hash(
68             \@_,
69             channel_nr => { isa => enum([ (1..12) ]) },
70             time => { isa => 'Lab::Moose::PosNum' },
71             fields => { isa => 'ArrayRef' },
72             );
73 0           my $channel_nr = $args{'channel_nr'};
74 0           my $time = $args{'time'};
75 0           my $fields = $args{'fields'};
76              
77 0           my $time_stop = DateTime->now."Z";
78 0           my $time_start = DateTime->from_epoch(epoch => time()-$time)."Z";
79              
80 0           my %hash_json = ( 'channel_nr' => $channel_nr,
81             'start_time' => $time_start,
82             'stop_time' => $time_stop,
83             'fields' => $fields,
84             );
85 0           my $json = $self->json->encode(\%hash_json);
86              
87 0           my $endpoint = '/channel/historical-data';
88              
89 0           my $response = $self->write( endpoint => $endpoint, body => $json);
90 0           my $hashref = $self->json->decode($response->content);
91              
92 0           return $hashref->{'measurements'};
93             }
94              
95              
96             sub get_heater_measurement {
97 0     0 1   my ( $self, %args ) = validated_hash(
98             \@_,
99             heater_nr => { isa => enum([ (1..4) ])},
100             time => { isa => 'Lab::Moose::PosNum' },
101             fields => { isa => 'ArrayRef' },
102             );
103 0           my $heater_nr = delete $args{'heater_nr'};
104 0           my $time = delete $args{'time'};
105 0           my $fields = delete $args{'fields'};
106              
107 0           my $time_stop = DateTime->now."Z";
108 0           my $time_start = DateTime->from_epoch(epoch => time()-$time)."Z";
109              
110 0           my %hash_json = ( 'heater_nr' => $heater_nr,
111             'start_time' => $time_start,
112             'stop_time' => $time_stop,
113             'fields' => $fields,
114             );
115 0           my $json = $self->json->encode(\%hash_json);
116              
117 0           my $endpoint = '/heater/historical-data';
118              
119 0           my $response = $self->write( endpoint => $endpoint, body => $json);
120 0           my $hashref = $self->json->decode($response->content);
121              
122 0           return $hashref->{'measurements'};
123             }
124              
125              
126             sub set_heater {
127 0     0 1   my ($self, %args) = validated_hash(
128             \@_,
129             heater_nr => { isa => enum([ (1..4) ]) },
130             active => { isa => enum( [qw/0 1/]), optional => 1 },
131             pid_mode => { isa => enum( [qw/0 1/]), optional => 1 },
132             resistance => { isa => 'Num', optional => 1 },
133             power => { isa => 'Num', optional => 1 },
134             target_temperature => { isa => 'Num', optional => 1 },
135             control_algorithm_settings => { isa => 'HashRef', optional => 1 },
136             setpoint => { isa => 'Num', optional => 1 },
137             );
138 0 0 0       if (exists $args{'active'} and defined $args{'active'}) {
139 0 0         if ( $args{"active"} eq 1 ) {
140 0           $args{"active"} = JSON::true;
141             } else {
142 0           $args{"active"} = JSON::false;
143             }
144             }
145 0           my $json = $self->json->encode(\%args);
146              
147 0           my $endpoint = '/heater/update';
148 0           my $response = $self->write( endpoint => $endpoint, body => $json);
149 0           my $hashref = $self->json->decode($response->content);
150              
151 0           return $hashref;
152             }
153              
154              
155             sub set_channel {
156 0     0 1   my ($self, %args) = validated_hash(
157             \@_,
158             channel_nr => { isa => enum([ (1..12) ]) },
159             active => { isa => enum( [qw/0 1/] ), optional => 1 },
160             excitation_mode => { isa => enum( [qw/0 1 2/] ), optional => 1 },
161             excitation_current_range => { isa => enum([ (1..22) ]), optional => 1 },
162             excitation_cmn_range => { isa => enum( [qw/1 2/] ), optional => 1 },
163             excitation_vmax_range => { isa => enum( [qw/1 2/] ), optional => 1 },
164             use_non_default_timeconstants => { isa => enum( [qw/0 1/] ), optional => 1 },
165             wait_time => { isa => 'Num', optional => 1 },
166             meas_time => { isa => 'Num', optional => 1 },
167             );
168 0 0 0       if (exists $args{'active'} and defined $args{'active'}) {
169 0 0         if ( $args{"active"} eq 1 ) {
170 0           $args{"active"} = JSON::true;
171             } else {
172 0           $args{"active"} = JSON::false;
173             }
174             }
175 0           my $json = $self->json->encode(\%args);
176              
177 0           my $endpoint = '/channel/update';
178 0           my $response = $self->write( endpoint => $endpoint, body => $json);
179 0           my $hashref = $self->json->decode($response->content);
180              
181 0           return $hashref;
182             }
183              
184              
185             sub set_statemachine {
186 0     0 1   my ($self, %args) = validated_hash(
187             \@_,
188             wait_time => { isa => enum([ (1..100) ]) },
189             meas_time => { isa => enum([ (1..100) ]) },
190             );
191              
192 0           my $json = $self->json->encode(\%args);
193              
194 0           my $endpoint = '/statemachine/update';
195 0           my $response = $self->write( endpoint => $endpoint, body => $json);
196 0           my $hashref = $self->json->decode($response->content);
197              
198 0           return $hashref;
199             }
200              
201              
202             sub idn {
203 0     0 1   my $self = shift;
204              
205 0           my $endpoint = '/system';
206 0           my $response = $self->read( endpoint => $endpoint );
207 0           my $hashref = $self->json->decode($response->content);
208              
209 0           my $ret = '';
210 0           $ret = $ret . $hashref->{api_version} . ":";
211 0           $ret = $ret . $hashref->{type} . ":";
212 0           $ret = $ret . $hashref->{serial} . ":";
213 0           $ret = $ret . $hashref->{label} . ":";
214 0           $ret = $ret . $hashref->{addinfo} . ":";
215 0           $ret = $ret . $hashref->{software_version};
216              
217 0           return $ret;
218             }
219              
220              
221             sub set_network_config {
222 0     0 1   my ($self, %args) = validated_hash(
223             \@_,
224             ip_configuration => { isa => 'Str' },
225             ip_address => { isa => 'Str', optional => 1 },
226             netmask => { isa => 'Str', optional => 1 },
227             );
228 0           my $json = $self->json->encode(\%args);
229 0           say $json;
230              
231 0           my $endpoint = '/system/network/update';
232 0           my $response = $self->write( endpoint => $endpoint, body => $json);
233 0           my $hashref = $self->json->decode($response->content);
234              
235 0           return $hashref;
236             }
237              
238              
239             sub set_channel_heater_relation {
240 0     0 1   my ($self, %args) = validated_hash(
241             \@_,
242             channel_nr => { isa => enum([ (0..12) ]) },
243             heater_nr => { isa => enum([ (0..4) ]) },
244             );
245 0           my $json = $self->json->encode(\%args);
246              
247 0           my $endpoint = '/channel/heater/update';
248 0           my $response = $self->write( endpoint => $endpoint, body => $json);
249 0           my $hashref = $self->json->decode($response->content);
250              
251 0           return $hashref;
252             }
253              
254              
255             # High level functions for sweeps
256              
257              
258             sub get_T {
259 0     0 1   my ($self, %args) = validated_hash(
260             \@_,
261             channel_nr => { isa => enum([ (1..12) ]), optional => 1 },
262             );
263              
264 0           my $channel;
265 0 0 0       if( exists $args{'channel_nr'} and defined $args{'channel_nr'} ) {
266 0           $channel = delete $args{'channel_nr'};
267             } else {
268 0           $channel = $self->default_channel;
269             }
270              
271 0           my $response = $self->get_measurement( channel_nr => $channel,
272             time => $self->default_time,
273             fields => [ "temperature" ] );
274 0           return $response->{'temperature'}->[-1];
275              
276             }
277              
278              
279             sub set_T {
280 0     0 1   my ($self, %args) = validated_hash(
281             \@_,
282             value => { isa => 'Num' },
283             heater_nr => { isa => enum([ (1..4) ]), optional => 1 },
284             );
285              
286 0           my $heater;
287 0 0 0       if( exists $args{'heater_nr'} and defined $args{'heater_nr'} ) {
288 0           $heater = delete $args{'heater_nr'};
289             } else {
290 0           $heater = $self->default_heater;
291             }
292 0           my $value = delete $args{'value'};
293              
294 0           return $self->set_heater( heater_nr => $heater, active => 1, setpoint => $value );
295             }
296              
297             __PACKAGE__->meta->make_immutable;
298              
299             1;
300              
301             __END__
302              
303             =pod
304              
305             =encoding UTF-8
306              
307             =head1 NAME
308              
309             Lab::Moose::Instrument::Bluefors_Temp - Bluefors temperature control
310              
311             =head1 VERSION
312              
313             version 3.900
314              
315             =head1 SYNOPSIS
316              
317             use Lab::Moose;
318              
319             # Constructor
320             my $tc = instrument(
321             type => 'Bluefors_Temp',
322             connection_type => 'HTTP',
323             connection_options => {
324             ip => '192.32.14.24',
325             },
326             );
327              
328             =head1 METHODS
329              
330             =head2 get_measurement
331              
332             my $measurement = $tc->get_measurement( channel_nr => (1..12), time => 60, fields => [ 'temperature', 'resistance' ] );
333              
334             The get_measurement() function returns the measurements of a specific channel in the specified time.
335             The option "time" controls how far back the measurements are fetched from the device.
336             In the example "time => 60" gets the measurements done in the last 60 seconds.
337             The last option "fields" controls which measurements are fetched. A full list can be found in the API reference.
338              
339             This function returns a hash reference with array references of all measurements.
340             The latest temperature measurement can be retrieved using $measurement->{'temperature'}[-1].
341              
342             =head2 get_heater_measurement
343              
344             my $heater_meas = $tc->get_heater_measurement( heater_nr => (1..4), time => 60, fields => [ 'power', 'current' ] );
345              
346             The get_heater_measurement() function returns the measurements of a specific heater.
347             It works the same as the get_measurement() function.
348              
349             =head2 set_heater
350              
351             $tc->set_heater( heater_nr => (1..4), active => 1, power => 0.03, ... );
352              
353             The set_heater() function controls the settings of a specific heater.
354             Supported options are:
355             - heater_nr
356             - active
357             - pid_mode
358             - resistance
359             - power
360             - target_temperature
361             - control_algorithm_settings
362             - setpoint
363              
364             =head2 set_channel
365              
366             $tc->set_channel( channel_nr => (1..12), active => 1, ... );
367              
368             The set_channel() function controls the settings of a specific channel.
369             Supported options are:
370             - channel_nr
371             - active
372             - excitation_mode
373             - excitation_current_range
374             - excitation_cmn_range
375             - excitation_vmax_range
376             - use_non_default_timeconstants
377             - wait_time
378             - meas_time
379              
380             =head2 set_statemachine
381              
382             my $state = $tc->set_statemachine( wait_time => 2, meas_time => 2 );
383              
384             The set_statemachine() function controls the wait time after changing channel
385             and the measurement time. Both values are given in seconds.
386             The response is a hash reference with all information about the statemachine
387             described in the API reference.
388              
389             =head2 idn
390              
391             say $tc->idn();
392              
393             Returns all available system information concatenated as a string.
394              
395             =head2 set_network_config
396              
397             my $configuration = $tc->set_network_config( ip_configuration => 'static', ip_address => '192.168.0.12' );
398              
399             Supported options:
400             - ip_configuration
401             - ip_address
402             - netmask
403              
404             =head2 set_channel_heater_relation
405              
406             my $relation = $tc->set_channel_heater_relation( channel_nr => 8, heater_nr => 2 );
407              
408             Updates the relation between a channel and a heater.
409              
410             =head2 get_T
411              
412             my $temp = $tc->get_T();
413              
414             Returns the latest temperature measurement of the default channel using the default time parameter.
415             High level function for the built-in sweep environment.
416             Use the get_measurement function for all other use cases.
417              
418             =head2 set_T
419              
420             $tc->set_T( value => 1.2 );
421              
422             Sets the target temperature for the default heater and sets the active state to ON.
423             High level function for the in-built sweep environment.
424             Use the set_heater function for all other use cases.
425              
426             =head1 COPYRIGHT AND LICENSE
427              
428             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
429              
430             Copyright 2023 Andreas K. Huettel, Mia Schambeck
431              
432              
433             This is free software; you can redistribute it and/or modify it under
434             the same terms as the Perl 5 programming language system itself.
435              
436             =cut