From f1d1f258f50f12801fd78fb245c7948a296779b0 Mon Sep 17 00:00:00 2001 From: betateilchen Date: Fri, 6 Feb 2015 11:50:35 +0000 Subject: [PATCH] contrib/InfoPanel: updated added: handling for bgimages added: commandref git-svn-id: svn://svn.code.sf.net/p/fhem/code/trunk@7891 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/contrib/InfoPanel/55_InfoPanel.pm | 285 +++++++++++++++++-------- 1 file changed, 196 insertions(+), 89 deletions(-) diff --git a/fhem/contrib/InfoPanel/55_InfoPanel.pm b/fhem/contrib/InfoPanel/55_InfoPanel.pm index 9277c7860..c4d37924b 100644 --- a/fhem/contrib/InfoPanel/55_InfoPanel.pm +++ b/fhem/contrib/InfoPanel/55_InfoPanel.pm @@ -61,6 +61,7 @@ sub btIP_itemTrash; sub btIP_color; sub btIP_xy; sub btIP_changeColor; +sub btIP_FileRead; sub btIP_ReturnSVG($); sub btIP_evalLayout($$@); @@ -83,21 +84,15 @@ sub InfoPanel_Initialize($) { $hash->{DefFn} = "btIP_Define"; $hash->{UndefFn} = "btIP_Undef"; #$hash->{AttrFn} = "btIP_Attr"; - $hash->{AttrList} = "disable:0,1 autoreload:1,0 bg bgcolor refresh size title tmin"; + $hash->{AttrList} = "autoreread:1,0 bgcenter:1,0 bgcolor bgdir refresh size title tmin"; $hash->{SetFn} = "btIP_Set"; $hash->{NotifyFn} = "btIP_Notify"; - - btIP_addExtension("btIP_CGI","btip","InfoPanel"); - return undef; } sub btIP_Define($$) { - my ($hash, $def) = @_; - my @a = split("[ \t]+", $def); - return "Usage: define InfoPanel filename" if(int(@a) != 3); my $name= $a[0]; my $filename= $a[2]; @@ -106,9 +101,11 @@ sub btIP_Define($$) { $hash->{fhem}{div} = ''; $hash->{LAYOUTFILE} = $filename; + btIP_addExtension("btIP_CGI","btip","InfoPanel"); btIP_readLayout($hash); - $hash->{STATE} = 'defined'; +readingsSingleUpdate($hash,'state','defined',1); +# $hash->{STATE} = 'defined'; return undef; } @@ -278,21 +275,8 @@ sub btIP_itemImg { my ($counter,$data,$info,$width,$height,$mimetype,$output); if($srctype eq 'file') { - Log3(undef,4,"InfoPanel: looking for img $arg"); - - if(configDBUsed()){ - Log3(undef,4,"InfoPanel: reading from configDB"); - ($data,$counter) = _cfgDB_Fileexport($arg,1); - } - - if(!$counter) { - Log3(undef,4,"InfoPanel: reading from filesystem"); - my $length = -s "$arg"; - open(GRAFIK, "<", $arg) or die("File not found $!"); - binmode(GRAFIK); - $counter = read(GRAFIK, $data, $length); - close(GRAFIK); - } + ($counter,$data) = btIP_FileRead($arg); + return unless $counter; } elsif ($srctype eq "url" || $srctype eq "urlq") { if($srctype eq "url") { $data= GetFileFromURL($arg,3,undef,1); @@ -302,7 +286,7 @@ sub btIP_itemImg { } elsif ($srctype eq 'data') { $data = $arg; } else { - Log3(undef,2,"InfoPanel: unknown sourcetype for image tag"); + Log3(undef,4,"InfoPanel: unknown sourcetype $srctype for image tag"); return ""; } @@ -563,22 +547,45 @@ sub btIP_xy { sub btIP_changeColor { my($file,$oldcolor,$newcolor) = @_; - my ($data,$readBytes); - my $length = -s "$file"; - open(GRAFIK, "<", $file) or die("File not found $!"); - binmode(GRAFIK); - $readBytes = read(GRAFIK, $data, $length); - close(GRAFIK); + Log3(undef,4,"InfoPanel: read file $file for changeColor"); + my ($counter,$data) = btIP_FileRead($file); + return unless $counter; if($newcolor =~ /[[:xdigit:]]{6}/) { - Log3(undef,4,"Infopanel: changing color from $oldcolor to $newcolor"); + Log3(undef,4,"InfoPanel: changing color from $oldcolor to $newcolor"); $data =~ s/fill="#$oldcolor"/fill="#$newcolor"/g; $data =~ s/fill:#$oldcolor/fill:#$newcolor/g; } else { - Log3(undef,4,"Infopanel: invalid rgb value for changeColor!"); + Log3(undef,4,"InfoPanel: invalid rgb value for changeColor!"); } return $data; } +sub btIP_FileRead { + my ($file) = @_; + my ($data,$counter); + + Log3(undef,4,"InfoPanel: looking for img $file"); + + if(configDBUsed()){ + Log3(undef,4,"InfoPanel: reading from configDB"); + ($data,$counter) = _cfgDB_Fileexport($file,1); + Log3(undef,4,"InfoPanel: file not found in database") unless $counter; + } + + if(!$counter) { + Log3(undef,4,"InfoPanel: reading from filesystem"); + my $length = -s "$file"; + open(GRAFIK, "<", $file) or die("File not found $!"); + binmode(GRAFIK); + $counter = read(GRAFIK, $data, $length); + close(GRAFIK); + Log3(undef,4,"InfoPanel: file not found in filesystem") unless $counter; + } + return "" unless $counter; + Log3(undef,4,"InfoPanel: file found."); + return ($counter,$data); +} + ################## # # create SVG content @@ -599,7 +606,7 @@ sub btIP_returnSVG($) { my ($width,$height)= split(/x/, AttrVal($name,"size","800x600")); my $bgcolor = AttrVal($name,'bgcolor','000000'); - + my $output = ""; our $svg = ""; eval { @@ -613,7 +620,7 @@ sub btIP_returnSVG($) { # set the background # check if background directory is set my $reason= "?"; # remember reason for undefined image - my $bgdir= AttrVal($name,"bg","undef"); + my $bgdir= AttrVal($name,"bgdir","undef"); if(defined($bgdir)){ my $bgnr; # item number if(defined($defs{$name}{fhem}) && defined($defs{$name}{fhem}{bgnr})) { @@ -632,63 +639,39 @@ sub btIP_returnSVG($) { $defs{$name}{fhem}{t}= $t1; $bgnr++; } - # detect pictures -# if(opendir(BGDIR, $bgdir)){ -# my @bgfiles= grep {$_ !~ /^\./} readdir(BGDIR); -# -# #foreach my $f (@bgfiles) { -# # Debug sprintf("File \"%s\"\n", $f); -# #} -# closedir(BGDIR); -# # get item number -# if($#bgfiles>=0) { -# if($bgnr > $#bgfiles) { $bgnr= 0; } -# $defs{$name}{fhem}{bgnr}= $bgnr; -# my $bgfile= $bgdir . "/" . $bgfiles[$bgnr]; -# my $filetype =(split(/\./,$bgfile))[-1]; -# my $bg; -# $bg= newFromGif GD::Image($bgfile) if $filetype =~ m/^gif$/i; -# $bg= newFromJpeg GD::Image($bgfile) if $filetype =~ m/^jpe?g$/i; -# $bg= newFromPng GD::Image($bgfile) if $filetype =~ m/^png$/i; -# if(defined($bg)) { -# my ($bgwidth,$bgheight)= $bg->getBounds(); -# if($bgwidth != $width or $bgheight != $height) { -# # we need to resize -# my ($w,$h); -# my ($u,$v)= ($bgwidth/$width, $bgheight/$height); -# if($u>$v) { -# $w= $width; -# $h= $bgheight/$u; -# } else { -# $h= $height; -# $w= $bgwidth/$v; -# } -# $svg->copyResized($bg,($width-$w)/2,($height-$h)/2,0,0,$w,$h,$bgwidth,$bgheight); -# } else { -# # size is as required -# # kill the predefined image and take the original -# undef $svg; -# $svg= $bg; -# } -# } else { -# undef $svg; -# $reason= "Something was wrong with background image \"$bgfile\"."; -# } -# } -# } # end opendir() + + if(opendir(BGDIR, $bgdir)){ + my @bgfiles= grep {$_ !~ /^\./} readdir(BGDIR); + closedir(BGDIR); + if($#bgfiles>=0) { + if($bgnr > $#bgfiles) { $bgnr= 0; } + $defs{$name}{fhem}{bgnr}= $bgnr; + my $bgfile = $bgdir . "/" . $bgfiles[$bgnr]; + my $info = image_info($bgfile); + my $bgwidth = $info->{width}; + my $bgheight = $info->{height}; + my ($u,$v) = ($bgwidth/$width, $bgheight/$height); + my $scale = ($u>$v) ? 1/$u : 1/$v; + my ($bgx,$bgy) = (0,0); + $bgx = ($width - $bgwidth/$u)/2 if AttrVal($name,'bgcenter',1); + $bgy = ($height - $bgheight/$u)/2 if AttrVal($name,'bgcenter',1); + $output = btIP_itemImg('-',$bgx,$bgy,$scale,'file',$bgfile,undef); + } + } # end opendir() } # end defined() - $svg .= "\" >\n\n"; + $svg .= "\" >\n"; + $svg .= "$output\n"; $svg = btIP_evalLayout($svg, $name, $defs{$name}{fhem}{layout}); - $defs{$name}{STATE} = localtime(); + readingsSingleUpdate($defs{$name},'state',localtime(),1); +# $defs{$name}{STATE} = localtime(); - }; #warn $@ if $@; if($@) { my $msg= $@; chomp $msg; - Log3 $name, 2, $msg; + Log3($name, 2, $msg); } $svg .= "Sorry, your browser does not support inline SVG.\n\n"; @@ -980,7 +963,7 @@ sub btIP_evalLayout($$@) { $params{ihalign}= $d unless($cmd eq "thalign"); $params{thalign}= $d unless($cmd eq "ihalign"); } else { - Log3 $name, 2, "InfoPanel: $name Illegal horizontal alignment $d"; + Log3($name, 2, "InfoPanel: $name Illegal horizontal alignment $d"); } } elsif($cmd ~~ @cmd_valign) { my $d = AnalyzePerlCommand(undef, $def); @@ -988,10 +971,10 @@ sub btIP_evalLayout($$@) { $params{ivalign}= $d unless($cmd eq "tvalign"); $params{tvalign}= $d unless($cmd eq "ivalign"); } else { - Log3 $name, 2, "InfoPanel: $name: Illegal vertical alignment $d"; + Log3($name, 2, "InfoPanel: $name: Illegal vertical alignment $d"); } } else { - Log3 $name, 2, "InfoPanel $name: Illegal command $cmd in layout definition."; + Log3($name, 2, "InfoPanel $name: Illegal command $cmd in layout definition."); } } # default } # given @@ -1031,6 +1014,9 @@ sub btIP_CGI{ if(!defined($defs{$name})) { return("text/plain; charset=utf-8", "Unknown InfoPanel device: $name"); } + if($ext eq "png") { + return btIP_returnPNG($name); + } if($ext eq "info" || $ext eq "html") { return btIP_returnHTML($name); } @@ -1051,13 +1037,34 @@ sub btIP_splitRequest($) { my $call= $request; $call =~ s/^.*\/btip\/([^\/]*)$/$1/; my $name= $call; - $name =~ s/^(.*)\.(svg|info|html)$/$1/; + $name =~ s/^(.*)\.(png|svg|info|html)$/$1/; my $ext= $call; $ext =~ s/^$name\.(.*)$/$1/; return ($name,$ext); } } +#################### +# +# HTML Stuff +# + +sub btIP_returnPNG($) { + my ($name) = @_; + my ($svgdata, $rsvg, $pngImg); + + $svgdata = btIP_returnSVG($name); + + eval { + require Image::LibRSVG; + $rsvg = new Image::LibRSVG(); + $rsvg->loadImageFromString($svgdata); + $pngImg = $rsvg->getImageBitmap(); + }; + Log3($FW_wname,1,"InfoPanel: Cannot create png image") if($@ or !defined($pngImg) or ($pngImg eq "")); + return $pngImg if $pngImg; + return undef; +} #################### # @@ -1129,7 +1136,7 @@ sub btIP_Overview { $name= $defs{$def}{NAME}; $url= btIP_getURL(); $html.= "$name
\n\n

\n"; } } @@ -1143,8 +1150,108 @@ sub btIP_getURL { return $proto."://$FW_httpheader{Host}$FW_ME"; } - - 1; # + +=pod +=begin html + + +

InfoPanel

+ + +
+ + + +=end html +=begin html_DE + + +

InfoPanel

+ +=end html_DE +=cut