From b27f6797363a529c8c9ef8546410fd30e20d74f6 Mon Sep 17 00:00:00 2001 From: tpoitzsch <> Date: Sun, 12 Jul 2015 13:16:25 +0000 Subject: [PATCH] FRITZBOX: preparation for AVM API GAU: M3U_LOCAL & M3U_URL git-svn-id: https://svn.fhem.de/fhem/trunk@8945 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/72_FRITZBOX.pm | 74 ++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/fhem/FHEM/72_FRITZBOX.pm b/fhem/FHEM/72_FRITZBOX.pm index 472ee9ea4..082663f63 100644 --- a/fhem/FHEM/72_FRITZBOX.pm +++ b/fhem/FHEM/72_FRITZBOX.pm @@ -276,7 +276,7 @@ sub FRITZBOX_Set($$@) # . " convertMOH" # . " convertRingTone" - my $forceShell = ( AttrVal( $name, "forceTelnetConnection", 0 ) == 1 || $hash->{REMOTE} == 0 ); + my $forceShell = ( AttrVal( $name, "forceTelnetConnection", 0 ) == 1 || defined $hash->{REMOTE} && $hash->{REMOTE} == 0 ); # set alarm if ( lc $cmd eq 'alarm') { @@ -630,7 +630,7 @@ sub FRITZBOX_API_Check_Run($) } # Check for remote APIs else { - my $agent = LWP::UserAgent->new( env_proxy => 1, keep_alive => 1, protocols_allowed => ['http'], timeout => 1); + my $agent = LWP::UserAgent->new( env_proxy => 1, keep_alive => 1, protocols_allowed => ['http'], timeout => 10); # Check if webcm exists $response = $agent->get( "http://".$host."/cgi-bin/webcm" ); @@ -667,6 +667,51 @@ sub FRITZBOX_API_Check_Run($) FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->TR064", 0; FRITZBOX_Log $hash, 4, "API TR-064 does not exist: ".$response->status_line; } + + # Check if m3u can be created and the URL tested + my $m3uFileLocal = AttrVal( $name, "m3uFileLocal", "./www/images/".$name.".m3u" ); + if (open my $fh, '>', $m3uFileLocal) { + print $fh "http://translate.google.com/translate_tts?ie=UTF-8&tl=fr&q=Lirumlaruml%C3%B6ffelstielwerdasnichtkannderkannnichtviel"; + close $fh; + FRITZBOX_Log $hash, 4, "Created m3u file '$m3uFileLocal'."; + FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->M3U_LOCAL", $m3uFileLocal; + + # Get the m3u-URL + my $m3uFileURL = AttrVal( $name, "m3uFileURL", "undefined" ); + # if not defined then try to build the correct URL for the file + if ( $m3uFileURL eq "undefined" ) { + # Getting IP of FHEM host + FRITZBOX_Log $hash, 4, "Try to get my IP address."; + my $socket = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => $host, PeerPort => 'http(80)' ); + my $ip = $socket->sockhost; #A side-effect of making a socket connection is that our IP address is available from the 'sockhost' method + FRITZBOX_Log $hash, 4, "Could not determine my ip address" unless $ip; + # Get a web port + my $port; + FRITZBOX_Log $hash, 4, "Try to get a FHEMWEB port."; + foreach( keys %defs ) { + if ( $defs{$_}->{TYPE} eq "FHEMWEB" && defined $defs{$_}->{PORT} ) { + $port = $defs{$_}->{PORT}; + last; + } + } + FRITZBOX_Log $hash, 4, "Could not find a FHEMWEB device." unless $port; + $m3uFileURL = "http://$ip:$port/fhem/images/$name.m3u" if defined $ip && defined $port; + } + # Check if m3u can be accessed + unless ( $m3uFileURL eq "undefined" ) { + FRITZBOX_Log $hash, 4, "Try to get '$m3uFileURL'"; + $response = $agent->get( $m3uFileURL ); + if ($response->is_error) { + FRITZBOX_Log $hash, 4, "Failed to get '$m3uFileURL': ".$response->status_line; + $m3uFileURL = "undefined" ; + } + } + FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->M3U_URL", $m3uFileURL; + } + else { + FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->M3U_LOCAL", "undefined"; + FRITZBOX_Log $hash, 4, "Error: Cannot create save file '$m3uFileLocal' because $!\n"; + } } # Check if telnet modul exists @@ -688,27 +733,6 @@ sub FRITZBOX_API_Check_Run($) } } -# Check if m3u can be created and the URL tested - # my $m3uFileLocal = AttrVal( $name, "m3uFileLocal", "./www/images/".$name.".m3u" ); - # if (open my $fh, '>', $m3uFileLocal) { - # print "http://translate.google.com/translate_tts?ie=UTF-8&tl=fr&q=Lirumlaruml%C3%B6ffelstielwerdasnichtkannderkannnichtviel"; - # close $fh; - # FRITZBOX_Log $hash, 4, "Created m3u file '$m3uFileLocal'."; - # FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->M3U_LOCAL", $m3uFileLocal; - # my $m3uFileURL = AttrVal( $name, "m3uFileURL", "" ); - # unless ( $m3uFileURL ) { - # my $port = 8083; - # my $ip = "192.169.178.23"; - # my $m3uFileName ="fhem/images/".$name.".m3u"; - # $m3uFileURL = "http://$ip:$port/$m3uFileName"; - # FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->M3U_URL", $m3uFileURL; - # } - # } - # else { - # FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->M3U_LOCAL", ""; - # FRITZBOX_Log $hash, 4, "Error: Cannot create save file '$m3uFileLocal' because $!\n"; - # } - FRITZBOX_Readout_Add_Reading $hash, \@roReadings, "->APICHECKED", 1; push @roReadings, "readoutTime", sprintf( "%.2f", time()-$startTime); @@ -4567,7 +4591,7 @@ sub FRITZBOX_fritztris($)