From 51b19c990cb5bda2306117ce67374626f8e9525a Mon Sep 17 00:00:00 2001 From: mfr69bs <> Date: Thu, 24 Jan 2013 02:12:28 +0000 Subject: [PATCH] some major changes in fheminfo git-svn-id: https://svn.fhem.de/fhem/trunk@2559 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/98_fheminfo.pm | 206 +++++++++++++++++++++++++++++++++++---- 1 file changed, 188 insertions(+), 18 deletions(-) diff --git a/fhem/FHEM/98_fheminfo.pm b/fhem/FHEM/98_fheminfo.pm index 29060bcba..42c0bf1bd 100644 --- a/fhem/FHEM/98_fheminfo.pm +++ b/fhem/FHEM/98_fheminfo.pm @@ -75,21 +75,90 @@ CommandFheminfo($$) my $os = $^O; my $arch = $Config{"archname"}; my $perl = $^V; - my $uniqueID = AttrVal("global","uniqueID",join "", map { unpack "H*", chr(rand(256)) } 1..16); + my $uniqueID = AttrVal("global","uniqueID",undef); my $sendStatistics = AttrVal("global","sendStatistics",1); + my $moddir = $attr{global}{modpath}."/FHEM"; + my $uidFile = $moddir."/FhemUtils/uniqueID"; - $attr{global}{uniqueID} = $uniqueID; + if(defined($uniqueID) && $uniqueID eq $uidFile) { + my $fh; + if(open($fh,"<".$uidFile)) { + Log 5, "fheminfo get uniqueID from $uidFile"; + while (my $line = <$fh>) { + chomp $line; + if($line =~ m/^uniqueID:[\da-fA-F]{32}$/) { + (undef,$uniqueID) = split(":",$line); + } + } + close $fh; + } + } + + if(!defined($uniqueID) || $uniqueID !~ m/^[\da-fA-F]{32}$/ || !-e $uidFile) { + my $fh; + if(!open($fh,">".$uidFile)) { + return "Can't open $uidFile: $!"; + } + if(!defined($uniqueID) || defined($uniqueID) && $uniqueID !~ m/^[\da-fA-F]{32}$/) { + $uniqueID = join "",map { unpack "H*", chr(rand(256)) } 1..16; + } + print $fh "################################################################\n"; + print $fh "# IMPORTANT NOTE:\n"; + print $fh "# This file is auto generated from the fheminfo command.\n"; + print $fh "# Please do not modify, move or delete this file!\n"; + print $fh "#\n"; + print $fh "# This file contains an unique ID for this installation. It is\n"; + print $fh "# to be used for statistical purposes only.\n"; + print $fh "# Based on this unique ID, no conclusions can be drawn to any\n"; + print $fh "# personal information of this installation.\n"; + print $fh "################################################################\n"; + print $fh "\n"; + print $fh "uniqueID:$uniqueID\n"; + close $fh; + Log 5, "fheminfo 'uniqueID' generated and stored in file '$uidFile'"; + } + + $attr{global}{uniqueID} = $uidFile; $attr{global}{sendStatistics} = $sendStatistics; - my $ret = checkConfigFile($uniqueID); + my $ret = checkConfigFile($uidFile); return $ret if($ret); + # get list of files + my $fail; + my $control_ref = {}; + + foreach my $pack (split(" ",uc($UPDATE{packages}))) { + $UPDATE{$pack}{control} = "controls_".lc($pack).".txt"; + } + + my $pack = "FHEM"; + + if(!-e "$moddir/$UPDATE{$pack}{control}") { + my $server = $UPDATE{server}; + my $BRANCH = ($DISTRIB_BRANCH eq "DEVELOPMENT") ? "SVN" : "STABLE"; + my $srcdir = $UPDATE{path}."/".lc($BRANCH); + Log 5, "fheminfo get $server/$srcdir/$UPDATE{$pack}{control}"; + my $controlFile = GetFileFromURL("$server/$srcdir/$UPDATE{$pack}{control}"); + return "Can't get '$UPDATE{$pack}{control}' from $server" if (!$controlFile); + # parse remote controlfile + ($fail,$control_ref) = parseControlFile($pack,$controlFile,$control_ref,0); + return "$fail\nfheminfo canceled..." if ($fail); + } else { + Log 5, "fheminfo get $moddir/$UPDATE{$pack}{control}"; + # parse local controlfile + ($fail,$control_ref) = parseControlFile($pack,"$moddir/$UPDATE{$pack}{control}",$control_ref,1); + } + foreach my $d (sort keys %defs) { my $n = $defs{$d}{NAME}; my $t = $defs{$d}{TYPE}; my $m = AttrVal($n,"model","unknown"); - $info{modules}{$t}{$n} = $m; + if(exists $control_ref->{$t}) { + Log 5, "fheminfo name:$n type:$t model:$m"; + $info{modules}{$t}{$n} = $m; + } } my $str; @@ -169,9 +238,65 @@ CommandFheminfo($$) return $ret; } +######################################## +sub parseControlFile($$$$) { + my ($pack,$controlFile,$control_ref,$local) = @_; + my %control = %$control_ref if ($control_ref && ref($control_ref) eq "HASH"); + my $from = ($local ? "local" : "remote"); + my $ret; + + if ($local) { + my $str = ""; + # read local controlfile in string + if (open FH, "$controlFile") { + $str = do { local $/; }; + } + close(FH); + $controlFile = $str + } + # parse file + if ($controlFile) { + foreach my $l (split("[\r\n]", $controlFile)) { + chomp($l); + Log 5, "fheminfo $from controls_".lc($pack).".txt: $l"; + my ($ctrl,$date,$size,$file,$move) = ""; + if ($l =~ m/^(UPD) (20\d\d-\d\d-\d\d_\d\d:\d\d:\d\d) (\d+) (\S+)$/) { + $ctrl = $1; + $date = $2; + $size = $3; + $file = $4; + } elsif ($l =~ m/^(DIR) (\S+)$/) { + $ctrl = $1; + $file = $2; + } elsif ($l =~ m/^(MOV) (\S+) (\S+)$/) { + $ctrl = $1; + $file = $2; + $move = $3; + } elsif ($l =~ m/^(DEL) (\S+)$/) { + $ctrl = $1; + $file = $2; + } else { + $ctrl = "ESC" + } + if ($ctrl eq "ESC") { + Log 1, "fheminfo File 'controls_".lc($pack).".txt' ($from) is corrupt"; + $ret = "File 'controls_".lc($pack).".txt' ($from) is corrupt"; + } + last if ($ret); + if ($l =~ m/^UPD/ && $file =~ m/^FHEM/) { + if ($file =~ m/^.*(\d\d_)(.*).pm$/) { + my $modName = $2; + $control{$modName} = $file; + } + } + } + } + return ($ret, \%control); +} + ######################################## sub checkConfigFile($) { - my $uniqueID = shift; + my $uidFile = shift; my $name = "fheminfo"; my $configFile = AttrVal("global","configfile",""); @@ -184,19 +309,32 @@ sub checkConfigFile($) { my @currentConfig = <$fh>; close $fh; - if(!grep {$_ =~ /uniqueID/} @currentConfig) { - my @newConfig; - my $done = 0; + my @newConfig; + my $done = 0; + if(grep {$_ =~ /uniqueID/} @currentConfig) { + Log 5, "fheminfo uniqueID in configfile"; + foreach my $line (@currentConfig) { + if($line =~ m/uniqueID/ && $line =~ m/[\da-fA-F]{32}/) { + Log 5, "fheminfo uniqueID in configfile and hex"; + $line = "attr global uniqueID $uidFile\n"; + $done = 1; + } + push(@newConfig,$line); + } + } else { + Log 5, "fheminfo uniqueID not in configfile"; foreach my $line (@currentConfig) { push(@newConfig,$line); if($line =~ /modpath/ && $done == 0) { - push(@newConfig,"attr global uniqueID $uniqueID\n"); + push(@newConfig,"attr global uniqueID $uidFile\n"); push(@newConfig,"attr global sendStatistics 1\n"); $done = 1; } } + } + if($done) { if(!open($fh,">".$configFile)) { return "Can't open $configFile: $!"; } @@ -211,6 +349,7 @@ sub checkConfigFile($) { } +######################################## sub checkModule($) { my $module = shift; eval("use $module"); @@ -250,7 +389,7 @@ sub checkModule($) {
  • Hardware architecture
  • Installed Perl version
  • Installed FHEM release and branch
  • -
  • Defined modules
  • +
  • Defined modules (only official FHEM Modules are counted)
  • Defined models per module

  • @@ -311,12 +450,28 @@ sub checkModule($) { 87c5cca38dc75a4f388ef87bdcbfbf6f which is assigned to the transmitted data to prevent duplicate entries.
    - The uniqueID is stored automatically in the configuration file of FHEM. + The uniqueID is stored automatically in a file named FhemUtils/uniqueID + in FHEM's modules path. +
    + IMPORTANT NOTE: +
    + Every installation of FHEM should have to have his own unique ID. +
    + Please do not modify, move or delete this file! You should always backup this file + (this is normally done by the update command automatically) and please restore + this file to the same path (FhemUtils in FHEM's modules path), if you plan to + reinstall your FHEM installation. This prevents duplicate entries for identical + installations on the same hardware in the statistics. +
    + Otherwise, please use different unique IDs for each installation of FHEM on different + hardware, e.g. one randomly generated unique ID for FRITZ!Box, another one for the first + Raspberry Pi, another one for the second Raspberry Pi, etc. +
    + Thanks for your support!
  • sendStatistics
    - This attribute is reserved for a future usage in conjunction with the - update command. + This attribute is used in conjunction with the update command.
    0: prevents transmission of data during an update.
    @@ -352,7 +507,7 @@ sub checkModule($) {
  • Hardware Architektur
  • Installierte Perl Version
  • Installierte FHEM release und "branch"
  • -
  • Definierte Module
  • +
  • Definierte Module (nur offizielle FHEM Module werden ermittelt)
  • Definierte Modelle je Modul

  • @@ -413,13 +568,28 @@ sub checkModule($) { 87c5cca38dc75a4f388ef87bdcbfbf6f welche den übertragenen Daten zur Vermeidung von doppelten Einträge zugewiesen wird.
    - Die uniqueID wird automatisch in der Konfigurationsdatei von FHEM - gespeichert. + Die uniqueID wird automatisch in einer Datei namens FHemUtils/uniqueID + im FHEM Modulverzeichnis gespeichert. +
    + WICHTIGER HINWEIS: +
    + Jede Installation von FHEM sollte seine eigene eindeutige ID haben. +
    + Bitte diese Datei nicht verändern, verschieben oder löschen! Diese Datei sollte + immer gesichert (wird normalerweise automatisch durch den update Befehl + erledigt) und bei einer Neuinstallation auf der gleichen Hardware im gleichen Verzeichnis + (FhemtUtils im FHEM Modulverzeichnis) wieder hergestellt werden. Dies verhindert + doppelte Einträge identischer Installationen auf der gleichen Hardware in der Statistik. +
    + Anderfalls, sollten bitte für jede Installation auf unterschiedlicher Hardware eigene + IDs genutzt werden, z.B. eine zufällig erzeugte ID für FRITZ!Box, eine weitere für + den ersten Raspberry Pi, eine weitere für einen zweiten Raspberry Pi, usw. +
    + Vielen Dank für die Unterstützung!
  • sendStatistics
    - Dieses Attribut ist für die künftige Verwendung in Verbindung mit dem - update Befehl reserviert. + Dieses Attribut wird in Verbindung mit dem update Befehl verwendet.
    0: verhindert die Übertragung der Daten während eines Updates.