From c05e97c5f00e5c170172d4dedb2e6dc44a41d6ea Mon Sep 17 00:00:00 2001 From: markusbloch Date: Sat, 1 Dec 2012 14:09:24 +0000 Subject: [PATCH] added reverse cache file git-svn-id: https://fhem.svn.sourceforge.net/svnroot/fhem/trunk/fhem@2242 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- FHEM/72_FB_CALLMONITOR.pm | 126 +++++++++++++++++++++++++++++++++----- 1 file changed, 112 insertions(+), 14 deletions(-) diff --git a/FHEM/72_FB_CALLMONITOR.pm b/FHEM/72_FB_CALLMONITOR.pm index 8eba2e74e..967cb6b6d 100755 --- a/FHEM/72_FB_CALLMONITOR.pm +++ b/FHEM/72_FB_CALLMONITOR.pm @@ -87,7 +87,7 @@ FB_CALLMONITOR_Initialize($) $hash->{ReadyFn} = "FB_CALLMONITOR_Ready"; $hash->{DefFn} = "FB_CALLMONITOR_Define"; $hash->{UndefFn} = "FB_CALLMONITOR_Undef"; - $hash->{AttrList}= "do_not_notify:0,1 reverse-search:all,klicktel,dasoertliche,none reverse-search-cache:0,1 event-on-update-reading event-on-change-reading"; + $hash->{AttrList}= "do_not_notify:0,1 reverse-search-cache-file reverse-search:all,klicktel.de,dasoertliche.de,none reverse-search-cache:0,1 event-on-update-reading event-on-change-reading"; } @@ -97,7 +97,7 @@ FB_CALLMONITOR_Define($$) { my ($hash, $def) = @_; my @a = split("[ \t][ \t]*", $def); - + if(@a != 3) { my $msg = "wrong syntax: define FB_CALLMONITOR ip[:port]"; Log 2, $msg; @@ -109,7 +109,10 @@ FB_CALLMONITOR_Define($$) my $dev = $a[2]; $dev .= ":1012" if($dev !~ m/:/ && $dev ne "none" && $dev !~ m/\@/); + InternalTimer(gettimeofday()+3, "FB_CALLMONITOR_loadCacheFile", $hash, 0); + + $hash->{DeviceName} = $dev; @@ -217,14 +220,16 @@ if(AttrVal($name, "reverse-search-cache", "0") eq "1") Log GetLogLevel($name, 4), "FB_CALLMONITOR $name using cache for reverse search of $number"; if($hash->{helper}{CACHE}{$number} ne "timeout") { - return ($hash->{helper}{CACHE}{$number} ne "unknown" ? $hash->{helper}{CACHE}{$number} : undef) ; + return $hash->{helper}{CACHE}{$number}; } } } # Ask klicktel.de -if(AttrVal($name, "reverse-search", "none") eq "all" or AttrVal($name, "reverse-search", "none") eq "klicktel") -{ +if(AttrVal($name, "reverse-search", "none") eq "all" or AttrVal($name, "reverse-search", "none") eq "klicktel.de") +{ + Log GetLogLevel($name, 4), "FB_CALLMONITOR: $name using klicktel.de for reverse search of $number"; + $result = GetFileFromURL("http://www.klicktel.de/inverssuche/index/search?_dvform_posted=1&phoneNumber=".$number, 5); if(not defined($result)) { @@ -236,21 +241,23 @@ if(AttrVal($name, "reverse-search", "none") eq "all" or AttrVal($name, "reverse- } else { - #Log 2, $result; - if($result =~ /.*?(.+?)<\/span>.*?<\/a>/) + + if($result =~ /(.+?)<\/a>/) { $invert_match = $1; - # char replacing todo; - $hash->{helper}{CACHE}{$number} = $invert_match if(AttrVal($name, "reverse-search-cache", "0") eq "1"); + $invert_match = FB_CALLMONITOR_html2txt($invert_match); + FB_CALLMONITOR_writeToCache($hash, $number, $invert_match) if(AttrVal($name, "reverse-search-cache", "0") eq "1"); return $invert_match; } } } # Ask dasoertliche.de -if(AttrVal($name, "reverse-search", "none") eq "all" or AttrVal($name, "reverse-search", "none") eq "dasoertliche") +if(AttrVal($name, "reverse-search", "none") eq "all" or AttrVal($name, "reverse-search", "none") eq "dasoertliche.de") { - $result = GetFileFromURL("http://www1.dasoertliche.de/?form_name=search_inv&ph=".$number, 5); + Log GetLogLevel($name, 4), "FB_CALLMONITOR: $name using dasoertliche.de for reverse search of $number"; + + $result = GetFileFromURL("http://www1.dasoertliche.de/?form_name=search_inv&ph=".$number, 7); if(not defined($result)) { if(AttrVal($name, "reverse-search-cache", "0") eq "1") @@ -266,8 +273,8 @@ if(AttrVal($name, "reverse-search", "none") eq "all" or AttrVal($name, "reverse- if($result =~ /getItemData\('.*?', '.*?', '.*?', '.*?', '.*?', '(.*?)', '.*?', '.*?', '.*?'\);/) { $invert_match = $1; - # char replacing todo - $hash->{helper}{CACHE}{$number} = $invert_match if(AttrVal($name, "reverse-search-cache", "0") eq "1"); + $invert_match = FB_CALLMONITOR_html2txt($invert_match); + FB_CALLMONITOR_writeToCache($hash, $number, $invert_match) if(AttrVal($name, "reverse-search-cache", "0") eq "1"); return $invert_match; } } @@ -279,6 +286,93 @@ $hash->{helper}{CACHE}{$number} = "unknown"; return "unknown"; } +sub FB_CALLMONITOR_html2txt($) +{ + +my ($string) = @_; + +$string =~ s/ / /g; +$string =~ s/(\xe4|ä)/ä/g; +$string =~ s/(\xc4|Ä)/Ä/g; +$string =~ s/(\xf6|ö)/ö/g; +$string =~ s/(\xd6|Ö)/Ö/g; +$string =~ s/(\xfc|ü)/ü/g; +$string =~ s/(\xdc|Ü)/Ü/g; +$string =~ s/(\xdf|ß)/ß/g; +$string =~ s/<.+?>//g; +$string =~ s/(^\s+|\s+$)//g; + +return $string; + +} + + +sub FB_CALLMONITOR_writeToCache($$$) +{ + my ($hash, $number, $txt) = @_; + my $name = $hash->{NAME}; + my $file = AttrVal($name, "reverse-search-cache-file", ""); + + + $file =~ s/(^\s+|\s+$)//g; + + $hash->{helper}{CACHE}{$number} = $txt; + + if($file ne "") + { + Log GetLogLevel($name, 4), "FB_CALLMONITOR: $name opening cache file $file"; + if(open(CACHEFILE, ">>$file")) + { + print CACHEFILE "$number|$txt\n"; + close(CACHEFILE); + } + else + { + Log 2, "FB_CALLMONITOR: $name could not open cache file"; + } + } + + +} + +sub FB_CALLMONITOR_loadCacheFile($) +{ + my ($hash) = @_; + my $file = AttrVal($hash->{NAME}, "reverse-search-cache-file", ""); + my @cachefile; + my @tmpline; + + $file =~ s/(^\s+|\s+$)//g; + + if($file ne "") + { + Log 2, "FB_CALLMONITOR: loading cache file $file"; + if(open(CACHEFILE, "$file")) + { + @cachefile = ; + close(CACHEFILE); + + foreach my $line (@cachefile) + { + if(not $line =~ /^\s*$/) + { + $line =~ s/\n//g; + + @tmpline = split("\\|", $line); + + if(@tmpline == 2) + { + $hash->{helper}{CACHE}{$tmpline[0]} = $tmpline[1]; + } + } + } + } + else + { + Log 2, "FB_CALLMONITOR: could not open cache file"; + } + } +} 1; @@ -337,7 +431,7 @@ return "unknown";
  • do_not_notify
  • event-on-update-reading
  • event-on-change-reading

  • -
  • reverse-search (all|klicktel|dasoertliche|none)
  • +
  • reverse-search (all|klicktel.de|dasoertliche.de|none)
  • Activate the reverse searching of the external number (at dial and call receiving). It is possible to select a specific web service, which should be used for reverse searching. If the attribute is set to "all", the reverse search will reverse search on all websites until a valid answer is found on of them @@ -347,6 +441,9 @@ return "unknown"; and will be used instead of reverse searching again the same number.

    Possible values: 0 => off , 1 => on
    Default Value is 0 (off) +
  • reverse-search-cache-file <file>
  • + Write the internal reverse-search-cache to the given file and use it next time FHEM starts. + So all reverse search results are persistent written to disk and will be used instantly after FHEM starts.
    @@ -355,6 +452,7 @@ return "unknown";