Moose/Connection/Zhinst.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 157 SCAR 54.8
CC
15
Cov
0%
get_value
72.0
Query
30.0
sync_poll
12.0
CRAP 240.0
1 package Lab::Moose::Connection::Zhinst;
2 $Lab::Moose::Connection::Zhinst::VERSION = '3.881';
3 #ABSTRACT: Connection back end to Zurich Instrument's LabOne measurement control API
4  
5 use v5.20;
6  
7  
8  
9 use Moose;
10 use MooseX::Params::Validate;
11 use Moose::Util::TypeConstraints qw(enum);
12 use Carp;
13  
14 use Lab::Zhinst;
15 use YAML::XS 'Load';
16 use Data::Dumper;
17 use namespace::autoclean;
18  
19 has host => (
20 is => 'ro',
21 isa => 'Str',
22 required => 1,
23 );
24  
25 has port => (
26 is => 'ro',
27 isa => 'Int',
28 required => 1,
29 );
30  
31 has connection => (
32 is => 'ro',
33 isa => 'Lab::Zhinst',
34 init_arg => undef,
35 writer => '_connection',
36 );
37  
38 sub _handle_error {
39 my $rv = shift;
40 if ($rv) {
41 my ( undef, $msg ) = ziAPIGetError($rv);
42 croak "Error in Zhinst backend. Value: $rv. Message: $msg.";
43 }
44  
45 my $value = shift;
46 return $value;
47 }
48  
49 sub BUILD {
50 my $self = shift;
51  
52 # Will croak on error.
53 my $connection = _handle_error( Lab::Zhinst->Init() );
54 $self->_connection($connection);
55 _handle_error( $connection->Connect( $self->host(), $self->port() ) );
56 }
57  
58 sub Query {
59 my $self = shift;
60 my ($command) = validated_list(
61 \@_,
62 command => { isa => 'Str' },
63 );
64 my %args = %{ Load $command};
65 my $method = delete $args{method};
66 if ( $method eq 'ListNodes' ) {
67  
68 # result length is ~ 12000 for MFIA. Be generous.
69 my $read_length = 100000;
70 my $connection = $self->connection();
71 return _handle_error(
72 $connection->ListNodes( $args{path}, $read_length, $args{mask} )
73 );
74 }
75 elsif ( $method eq 'Get' ) {
76 return $self->get_value(%args);
77 }
78 elsif ( $method eq 'SyncSet' ) {
79 return $self->sync_set_value(%args);
80 }
81 elsif ( $method eq 'SyncPoll' ) {
82 return $self->sync_poll(%args);
83 }
84 else {
85 croak "unknown method $method";
86 }
87 }
88  
89 sub sync_set_value {
90 my $self = shift;
91 my ( $path, $type, $value ) = validated_list(
92 \@_,
93 path => { isa => 'Str' },
94 type => { isa => enum( [qw/I D B/] ) },
95 value => { isa => 'Str' },
96 );
97 my $method = "SyncSetValue$type";
98 my $connection = $self->connection();
99 return _handle_error( $connection->$method( $path, $value ) );
100 }
101  
102 sub get_value {
103 my $self = shift;
104 my ( $path, $type, $read_length ) = validated_list(
105 \@_,
106 path => { isa => 'Str' },
107 type => { isa => enum( [qw/I D B Demod AuxIn DIO/] ) },
108 read_length => { isa => 'Int', optional => 1 },
109  
110 );
111  
112 my $method = 'Get';
113 $method .=
114 $type eq 'I' ? 'ValueI'
115 : $type eq 'D' ? 'ValueD'
116 : $type eq 'B' ? 'ValueB'
117 : $type eq 'Demod' ? 'DemodSample'
118 : $type eq 'AuxIn' ? 'AuxInSample'
119 : 'DIOSample';
120  
121 my $connection = $self->connection();
122 if ( $type eq 'B' ) {
123 if ( not defined $read_length ) {
124 croak "Need read_length arg to set byte string.";
125 }
126 return _handle_error( $connection->$method( $path, $read_length ) );
127 }
128 return _handle_error( $connection->$method($path) );
129 }
130  
131 sub _timeout_arg {
132 my $self = shift;
133 my %arg = @_;
134 my $timeout = $arg{timeout} // $self->timeout();
135 return sprintf( "%.0f", $timeout * 1000 );
136 }
137  
138 sub sync_poll {
139 my ( $self, %args ) = validated_hash(
140 \@_,
141 path => { isa => 'Str' },
142 timeout => { isa => 'Num', optional => 1 },
143 );
144 my $timeout = $self->_timeout_arg(%args);
145 my $path = $args{path};
146 my $connection = $self->connection();
147  
148 my $event = ziAPIAllocateEventEx();
149 _handle_error( $connection->Subscribe($path) );
150  
151 # Ensure that we get a recent value. See LabOne manual
152 # '1.4.4. Obtaining Data from the Instrument'
153 _handle_error( $connection->Sync() );
154  
155 my $data = _handle_error( $connection->PollDataEx( $event, $timeout ) );
156 _handle_error( $connection->UnSubscribe($path) );
157  
158 if ( $data->{valueType} == ZI_VALUE_TYPE_NONE ) {
159 croak
160 "Possible timeout in PollDataEx. Got event type ZI_VALUE_TYPE_NONE .";
161 }
162 if ( $data->{count} == 0 ) {
163  
164 # Never reached?
165 croak "Event with zero count.";
166 }
167  
168 # Return only last (most recent) event.
169 return $data->{values}[-1];
170 }
171  
172 sub Write {
173 croak "not implemented";
174 }
175  
176 sub Read {
177 croak "not implemented";
178 }
179  
180 sub Clear {
181 croak "not implemented";
182 }
183  
184 # Get timeout attribute.
185 with qw/Lab::Moose::Connection/;
186  
187 __PACKAGE__->meta->make_immutable();
188  
189 1;
190  
191 __END__
192  
193 =pod
194  
195 =encoding UTF-8
196  
197 =head1 NAME
198  
199 Lab::Moose::Connection::Zhinst - Connection back end to Zurich Instrument's LabOne measurement control API
200  
201 =head1 VERSION
202  
203 version 3.881
204  
205 =head1 SYNOPSIS
206  
207 use Lab::Moose;
208 my $instrument = instrument(
209 type => 'Random',
210 connection_type => 'Zhinst',
211 connection_options => {host => ..., port => ...}
212 );
213  
214 =head1 DESCRIPTION
215  
216 This module translates between YAML text commands and L<Lab::Zhinst>
217 method calls. The YAML commands are produced in Lab::Moose::Instrument::Zhinst.
218  
219 =head1 COPYRIGHT AND LICENSE
220  
221 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
222  
223 Copyright 2017 Andreas K. Huettel, Simon Reinhardt
224 2020 Andreas K. Huettel
225  
226  
227 This is free software; you can redistribute it and/or modify it under
228 the same terms as the Perl 5 programming language system itself.
229  
230 =cut