| 1 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | # Copyright (C) 2015 Christian Garbs <mitch@cgarbs.de> | ||||||||||
| 4 | # Licensed under GNU GPL v2 or later. | ||||||||||
| 5 | |||||||||||
| 6 | package Net::Fritz::Device; | ||||||||||
| 7 | # ABSTRACT: represents a TR064 device | ||||||||||
| 8 | 5 | $Net::Fritz::Device::VERSION = 'v0.0.9'; | |||||||||
| 9 | |||||||||||
| 10 | 5 | ▶ | use Net::Fritz::Data; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Net::Fritz::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 5 | ▶ | use Net::Fritz::Service; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 5 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 5 | with 'Net::Fritz::IsNoError'; | |||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | 5 | has fritz => ( is => 'ro' ); | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 5 | has xmltree => ( is => 'ro' ); | |||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 5 | has service_list => ( is => 'lazy', init_arg => undef ); | |||||||||
| 26 | |||||||||||
| 27 | 31 | ▶ | sub _build_service_list { | ||||||||
|
Subroutine
_build_service_list: called CC=3 SCAR=11.0
| |||||||||||
| 28 | 31 | my $self = shift; | |||||||||
| 29 | 31 | my $xml = $self->xmltree; | |||||||||
| 30 | 31 | my @services; | |||||||||
| 31 | |||||||||||
| 32 | 31 | ▶ | if (exists $xml->{serviceList}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 29 | foreach my $service (@{$xml->{serviceList}->[0]->{service}}) { | |||||||||
| 34 | 38 | push @services, Net::Fritz::Service->new( | |||||||||
| 35 | xmltree => $service, | ||||||||||
| 36 | fritz => $self->fritz | ||||||||||
| 37 | ); | ||||||||||
| 38 | } | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 31 | return \@services; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | 5 | has device_list => ( is => 'lazy', init_arg => undef ); | |||||||||
| 46 | |||||||||||
| 47 | 32 | ▶ | sub _build_device_list { | ||||||||
|
Subroutine
_build_device_list: called CC=3 SCAR=11.0
| |||||||||||
| 48 | 32 | my $self = shift; | |||||||||
| 49 | 32 | my $xml = $self->xmltree; | |||||||||
| 50 | 32 | my @devices; | |||||||||
| 51 | |||||||||||
| 52 | 32 | ▶ | if (exists $xml->{deviceList}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 19 | foreach my $device (@{$xml->{deviceList}->[0]->{device}}) { | |||||||||
| 54 | 29 | push @devices, Net::Fritz::Device->new( | |||||||||
| 55 | xmltree => $device, | ||||||||||
| 56 | fritz => $self->fritz | ||||||||||
| 57 | ); | ||||||||||
| 58 | } | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 32 | return \@devices; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | 5 | has attributes => ( is => 'lazy', init_arg => undef ); | |||||||||
| 66 | |||||||||||
| 67 | 5 | ▶ | use constant ATTRIBUTES => qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | deviceType | ||||||||||
| 69 | friendlyName | ||||||||||
| 70 | manufacturer | ||||||||||
| 71 | manufacturerURL | ||||||||||
| 72 | modelDescription | ||||||||||
| 73 | modelName | ||||||||||
| 74 | modelNumber | ||||||||||
| 75 | modelURL | ||||||||||
| 76 | UDN | ||||||||||
| 77 | presentationURL | ||||||||||
| 78 | 5 | ); | |||||||||
| 79 | |||||||||||
| 80 | 11 | ▶ | sub _build_attributes { | ||||||||
|
Subroutine
_build_attributes: called CC=3 SCAR=11.0
| |||||||||||
| 81 | 11 | my $self = shift; | |||||||||
| 82 | 11 | my $xml = $self->xmltree; | |||||||||
| 83 | 11 | my $attributes = {}; | |||||||||
| 84 | |||||||||||
| 85 | 11 | for my $attr (ATTRIBUTES) { | |||||||||
| 86 | 110 | ▶ | if (exists $xml->{$attr}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 21 | $attributes->{$attr} = $xml->{$attr}->[0]; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 11 | return $attributes; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | 8 | ▶ | sub get_service { | ||||||||
|
Subroutine
get_service: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 96 | 8 | my $self = shift; | |||||||||
| 97 | 8 | my $type = shift; | |||||||||
| 98 | |||||||||||
| 99 | 8 | foreach my $service (@{$self->service_list}) { | |||||||||
| 100 | 10 | ▶ | if ($service->serviceType eq $type) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 2 | return $service; | |||||||||
| 102 | } | ||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 6 | foreach my $device (@{$self->device_list}) { | |||||||||
| 106 | 5 | my $service = $device->get_service($type); | |||||||||
| 107 | 5 | ▶ | if (! $service->error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 2 | return $service; | |||||||||
| 109 | } | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 4 | return Net::Fritz::Error->new('service not found'); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | 11 | ▶ | sub find_service { | ||||||||
|
Subroutine
find_service: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 117 | 11 | my $self = shift; | |||||||||
| 118 | 11 | my $type = shift; | |||||||||
| 119 | |||||||||||
| 120 | 11 | foreach my $service (@{$self->service_list}) { | |||||||||
| 121 | 13 | ▶ | if ($service->serviceType =~ /$type/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 4 | return $service; | |||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 7 | foreach my $device (@{$self->device_list}) { | |||||||||
| 127 | 5 | my $service = $device->find_service($type); | |||||||||
| 128 | 5 | ▶ | if (! $service->error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 2 | return $service; | |||||||||
| 130 | } | ||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 5 | return Net::Fritz::Error->new('service not found'); | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | 8 | ▶ | sub find_service_names { | ||||||||
|
Subroutine
find_service_names: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 138 | 8 | my $self = shift; | |||||||||
| 139 | 8 | my $type = shift; | |||||||||
| 140 | |||||||||||
| 141 | 8 | my @found = (); | |||||||||
| 142 | |||||||||||
| 143 | 8 | foreach my $service (@{$self->service_list}) { | |||||||||
| 144 | 10 | ▶ | if ($service->serviceType =~ /$type/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 4 | push @found, $service; | |||||||||
| 146 | } | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 8 | foreach my $device (@{$self->device_list}) { | |||||||||
| 150 | 6 | my $data = $device->find_service_names($type); | |||||||||
| 151 | 6 | push @found, @{$data->data}; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 8 | return Net::Fritz::Data->new(\@found); | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | 7 | ▶ | sub find_device { | ||||||||
|
Subroutine
find_device: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 159 | 7 | my $self = shift; | |||||||||
| 160 | 7 | my $type = shift; | |||||||||
| 161 | |||||||||||
| 162 | 7 | foreach my $device (@{$self->device_list}) { | |||||||||
| 163 | 8 | ▶ | if ($device->attributes->{deviceType} eq $type) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 2 | return $device; | |||||||||
| 165 | } | ||||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | 5 | foreach my $device (@{$self->device_list}) { | |||||||||
| 169 | 4 | my $device = $device->find_device($type); | |||||||||
| 170 | 4 | ▶ | if (! $device->error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | 1 | return $device; | |||||||||
| 172 | } | ||||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | 4 | return Net::Fritz::Error->new( 'device not found' ); | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | |||||||||||
| 179 | 54 | ▶ | has _service_cache => ( is => 'ro', default => sub { return {} } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 180 | |||||||||||
| 181 | 5 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 182 | 5 | my $self = shift; | |||||||||
| 183 | 5 | my $type = shift; | |||||||||
| 184 | 5 | my $action = shift; | |||||||||
| 185 | 5 | my %call_args = (@_); | |||||||||
| 186 | |||||||||||
| 187 | 5 | my $service = $self->_service_cache->{$type}; | |||||||||
| 188 | 5 | ▶ | if (! defined $service) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 3 | $service = $self->find_service($type); | |||||||||
| 190 | 3 | $self->_service_cache->{$type} = $service; | |||||||||
| 191 | } | ||||||||||
| 192 | 5 | ▶ | return $service if $service->error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | |||||||||||
| 194 | 4 | return $service->call($action, %call_args); | |||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | |||||||||||
| 198 | 2 | ▶ | sub dump { | ||||||||
|
Subroutine
dump: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 199 | 2 | my $self = shift; | |||||||||
| 200 | |||||||||||
| 201 | 2 | my $indent = shift; | |||||||||
| 202 | 2 | ▶ | $indent = '' unless defined $indent; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | |||||||||||
| 204 | 2 | my $text = "${indent}Net::Fritz::Device:\n"; | |||||||||
| 205 | 2 | $indent .= ' '; | |||||||||
| 206 | 2 | $text .= "${indent}modelName = " . $self->attributes->{modelName} . "\n"; | |||||||||
| 207 | 2 | ▶ | $text .= "${indent}presentationURL = " . $self->attributes->{presentationURL} . "\n" if defined $self->attributes->{presentationURL}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 208 | |||||||||||
| 209 | 2 | my @service_list = @{$self->service_list}; | |||||||||
| 210 | 2 | ▶ | if (@service_list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | 1 | $text .= "${indent}subservices = {\n"; | |||||||||
| 212 | 1 | foreach my $service (@service_list) { | |||||||||
| 213 | 1 | $text .= $service->dump($indent . ' '); | |||||||||
| 214 | } | ||||||||||
| 215 | 1 | $text .= "${indent}}\n"; | |||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | 2 | my @device_list = @{$self->device_list}; | |||||||||
| 219 | 2 | ▶ | if (@device_list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 220 | 1 | $text .= "${indent}subdevices = {\n"; | |||||||||
| 221 | 1 | foreach my $device (@device_list) { | |||||||||
| 222 | 1 | $text .= $device->dump($indent . ' '); | |||||||||
| 223 | } | ||||||||||
| 224 | 1 | $text .= "${indent}}\n"; | |||||||||
| 225 | } | ||||||||||
| 226 | |||||||||||
| 227 | 2 | return $text; | |||||||||
| 228 | } | ||||||||||
| 229 | |||||||||||
| 230 | |||||||||||
| 231 | 5 | 1; | |||||||||
| 232 | |||||||||||
| 233 | __END__ | ||||||||||