| 1 | package Device::Modbus::Unit; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use Device::Modbus::Unit::Route; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 5 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 11 | croak "Missing required parameter: id" | ||||||||||||||||||||||||||||
| 12 | 5 | ▶ | unless defined $args{id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 4 | my %routes = ( | |||||||||||||||||||||||||||
| 14 | 'discrete_coils:read' => [], | ||||||||||||||||||||||||||||
| 15 | 'discrete_coils:write' => [], | ||||||||||||||||||||||||||||
| 16 | 'discrete_inputs:read' => [], | ||||||||||||||||||||||||||||
| 17 | 'input_registers:read' => [], | ||||||||||||||||||||||||||||
| 18 | 'holding_registers:read' => [], | ||||||||||||||||||||||||||||
| 19 | 'holding_registers:write' => [], | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | 4 | return bless { %args, routes => \%routes }, $class; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | sub id { | ||||||||||||||||||||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 26 | 3 | return $self->{id}; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | sub routes { | ||||||||||||||||||||||||||
|
Subroutine
routes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | 4 | return $self->{routes}; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub init_unit { | ||||||||||||||||||||||||||
|
Subroutine
init_unit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 1 | croak "Device::Modbus::Unit subclasses must implement init_unit"; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 9 | ▶ | sub put { | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 9 | my ($self, $zone, $address, $qty, $method) = @_; | |||||||||||||||||||||||||||
| 40 | 9 | ▶ | if (!ref $method) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 7 | $method = $self->can($method); # returns a ref to method | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 9 | ▶ | croak "'put' could not resolve a code reference for address $address in zone $zone" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $method and ref $method eq "CODE"
MC/DC100%2/2
ref $method and ref $method eq "CODE"
ref $method
ref $method eq "CODE"
Truth table100%3/3
A and B
A= ref $methodB= ref $method eq "CODE"
| |||||||||||||||||||||||||||||
| 44 | unless ref $method && ref $method eq 'CODE'; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 7 | my $addr = Device::Modbus::Unit::Route->new( | |||||||||||||||||||||||||||
| 47 | address => $address, | ||||||||||||||||||||||||||||
| 48 | zone => $zone, | ||||||||||||||||||||||||||||
| 49 | quantity => $qty, | ||||||||||||||||||||||||||||
| 50 | read_write => 'write', | ||||||||||||||||||||||||||||
| 51 | routine => $method | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 7 | push @{$self->{routes}->{"$zone:write"}}, $addr; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 11 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 11 | my ($self, $zone, $address, $qty, $method) = @_; | |||||||||||||||||||||||||||
| 59 | 11 | ▶ | if (!ref $method) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 9 | $method = $self->can($method); # returns a ref to method | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 11 | ▶ | croak "'get' could not resolve a code reference for address $address in zone $zone" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $method and ref $method eq "CODE"
MC/DC100%2/2
ref $method and ref $method eq "CODE"
ref $method
ref $method eq "CODE"
Truth table100%3/3
A and B
A= ref $methodB= ref $method eq "CODE"
| |||||||||||||||||||||||||||||
| 63 | unless ref $method && ref $method eq 'CODE'; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 9 | my $route = Device::Modbus::Unit::Route->new( | |||||||||||||||||||||||||||
| 66 | address => $address, | ||||||||||||||||||||||||||||
| 67 | zone => $zone, | ||||||||||||||||||||||||||||
| 68 | quantity => $qty, | ||||||||||||||||||||||||||||
| 69 | read_write => 'read', | ||||||||||||||||||||||||||||
| 70 | routine => $method | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 9 | push @{$self->{routes}->{"$zone:read"}}, $route; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Tests a requested zone, address, qty against existing addresses. | ||||||||||||||||||||||||||||
| 77 | # Returns the first successful match. Returns the Modbus error number | ||||||||||||||||||||||||||||
| 78 | # otherwise (3 for invalid qty and 2 for invalid address) | ||||||||||||||||||||||||||||
| 79 | 22 | ▶ | sub route { | ||||||||||||||||||||||||||
|
Subroutine
route: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 22 | my ($self, $zone, $mode, $addr, $qty) = @_; | |||||||||||||||||||||||||||
| 81 | 22 | my $addresses = $self->{routes}->{"$zone:$mode"}; | |||||||||||||||||||||||||||
| 82 | 22 | ▶ | return 1 unless @$addresses; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 20 | my $match; | |||||||||||||||||||||||||||
| 85 | 20 | foreach my $address (@$addresses) { | |||||||||||||||||||||||||||
| 86 | 26 | ▶ | next unless $address->test_route($addr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 13 | $match = $address; | |||||||||||||||||||||||||||
| 88 | 13 | ▶ | return $match if $match->test_quantity($qty); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # return 3 if defined $match; # Address matched, not quantity # INCORRECT | ||||||||||||||||||||||||||||
| 92 | 8 | return 2; # Did not match | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 5 | 1; | |||||||||||||||||||||||||||