| 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.2'; | |||||||||
| 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 | sub _build_service_list { | ||||||||||
| 28 | 28 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_service_list: called CC=3 SCAR=11.0
| |||||||||||
| 29 | 28 | my $xml = $self->xmltree; | |||||||||
| 30 | 28 | my @services; | |||||||||
| 31 | |||||||||||
| 32 | 28 | ▶ | if (exists $xml->{serviceList}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 26 | foreach my $service (@{$xml->{serviceList}->[0]->{service}}) { | |||||||||
| 34 | 35 | push @services, Net::Fritz::Service->new( | |||||||||
| 35 | xmltree => $service, | ||||||||||
| 36 | fritz => $self->fritz | ||||||||||
| 37 | ); | ||||||||||
| 38 | } | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 28 | return \@services; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | 5 | has device_list => ( is => 'lazy', init_arg => undef ); | |||||||||
| 46 | |||||||||||
| 47 | sub _build_device_list { | ||||||||||
| 48 | 31 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_device_list: called CC=3 SCAR=11.0
| |||||||||||
| 49 | 31 | my $xml = $self->xmltree; | |||||||||
| 50 | 31 | my @devices; | |||||||||
| 51 | |||||||||||
| 52 | 31 | ▶ | 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 | 31 | return \@devices; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | 5 | has attributes => ( is => 'lazy', init_arg => undef ); | |||||||||
| 66 | |||||||||||
| 67 | 5 | use constant ATTRIBUTES => qw( | |||||||||
| 68 | deviceType | ||||||||||
| 69 | friendlyName | ||||||||||
| 70 | manufacturer | ||||||||||
| 71 | manufacturerURL | ||||||||||
| 72 | modelDescription | ||||||||||
| 73 | modelName | ||||||||||
| 74 | modelNumber | ||||||||||
| 75 | modelURL | ||||||||||
| 76 | UDN | ||||||||||
| 77 | presentationURL | ||||||||||
| 78 | 5 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 79 | |||||||||||
| 80 | sub _build_attributes { | ||||||||||
| 81 | 11 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_attributes: called CC=3 SCAR=11.0
| |||||||||||
| 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 | sub get_service { | ||||||||||
| 96 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_service: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 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 | sub find_service { | ||||||||||
| 117 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
find_service: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 118 | 8 | my $type = shift; | |||||||||
| 119 | |||||||||||
| 120 | 8 | foreach my $service (@{$self->service_list}) { | |||||||||
| 121 | 10 | ▶ | if ($service->serviceType =~ /$type/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 2 | return $service; | |||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 6 | 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 | 4 | return Net::Fritz::Error->new('service not found'); | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | sub find_service_names { | ||||||||||
| 138 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
find_service_names: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 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 | sub find_device { | ||||||||||
| 159 | 7 | ▶ | my $self = shift; | ||||||||
|
Subroutine
find_device: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 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 | sub dump { | ||||||||||
| 180 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
dump: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 181 | |||||||||||
| 182 | 2 | my $indent = shift; | |||||||||
| 183 | 2 | ▶ | $indent = '' unless defined $indent; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | |||||||||||
| 185 | 2 | my $text = "${indent}Net::Fritz::Device:\n"; | |||||||||
| 186 | 2 | $indent .= ' '; | |||||||||
| 187 | 2 | $text .= "${indent}modelName = " . $self->attributes->{modelName} . "\n"; | |||||||||
| 188 | 2 | ▶ | $text .= "${indent}presentationURL = " . $self->attributes->{presentationURL} . "\n" if defined $self->attributes->{presentationURL}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | |||||||||||
| 190 | 2 | my @service_list = @{$self->service_list}; | |||||||||
| 191 | 2 | ▶ | if (@service_list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | 1 | $text .= "${indent}subservices = {\n"; | |||||||||
| 193 | 1 | foreach my $service (@service_list) { | |||||||||
| 194 | 1 | $text .= $service->dump($indent . ' '); | |||||||||
| 195 | } | ||||||||||
| 196 | 1 | $text .= "${indent}}\n"; | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 2 | my @device_list = @{$self->device_list}; | |||||||||
| 200 | 2 | ▶ | if (@device_list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 201 | 1 | $text .= "${indent}subdevices = {\n"; | |||||||||
| 202 | 1 | foreach my $device (@device_list) { | |||||||||
| 203 | 1 | $text .= $device->dump($indent . ' '); | |||||||||
| 204 | } | ||||||||||
| 205 | 1 | $text .= "${indent}}\n"; | |||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | 2 | return $text; | |||||||||
| 209 | } | ||||||||||
| 210 | |||||||||||
| 211 | |||||||||||
| 212 | 5 | 1; | |||||||||
| 213 | |||||||||||
| 214 | __END__ | ||||||||||