From 1fffcad8c43a9fc87fa0bb351abb6a568fc0820a Mon Sep 17 00:00:00 2001 From: neubert Date: Wed, 29 Sep 2021 11:52:41 +0000 Subject: [PATCH] 02_RSS: support for more than one directory for background images git-svn-id: https://svn.fhem.de/fhem/trunk@25031 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 2 + fhem/FHEM/02_RSS.pm | 118 +++++++++++++++++++++++++------------------- 2 files changed, 68 insertions(+), 52 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 81784155c..bb76ce90d 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - feature: 02_RSS: support for more than one directory for background + images - new: 50_Signalbot: Signal Messenger integration (official release) - feature: 57_Calender: limit specification when=... amended - change: 10_MQTT_BRIDGE: Move to deprecated diff --git a/fhem/FHEM/02_RSS.pm b/fhem/FHEM/02_RSS.pm index 2fabed285..c29fe24be 100644 --- a/fhem/FHEM/02_RSS.pm +++ b/fhem/FHEM/02_RSS.pm @@ -59,7 +59,7 @@ sub RSS_Initialize($) { #$hash->{AttrFn} = "RSS_Attr"; $hash->{AttrList} = -"size itemtitle bg bgcolor tmin refresh areas autoreread:1,0 viewport noscroll urlOverride"; +"size itemtitle bgroot bg bgcolor tmin refresh areas autoreread:1,0 viewport noscroll urlOverride"; $hash->{SetFn} = "RSS_Set"; $hash->{NotifyFn} = "RSS_Notify"; @@ -931,8 +931,10 @@ sub RSS_returnIMG($$) { # # check if background directory is set my $reason = "?"; # remember reason for undefined image - my $bgdir = AttrVal( $name, "bg", "undef" ); - if ( defined($bgdir) ) { + my $bgroot = AttrVal( $name, "bgroot", ""); + my $bgdirs = AttrVal( $name, "bg", "undef" ); # comma-separated list of directories + if ( defined($bgdirs) ) { + my $bgnr; # item number if ( defined( $defs{$name}{fhem} ) && defined( $defs{$name}{fhem}{bgnr} ) ) @@ -958,67 +960,73 @@ sub RSS_returnIMG($$) { } # detect pictures - if ( opendir( BGDIR, $bgdir ) ) { - my @bgfiles = grep { $_ !~ /^\./ } readdir(BGDIR); + my @bgfiles; + for my $bgdiritem (split(/,/, $bgdirs)) { + my $bgdir= $bgroot . $bgdiritem; + if ( opendir( BGDIR, $bgdir ) ) { + push(@bgfiles, map { $bgdir . "/" . $_ } grep { $_ !~ /^\./ } readdir(BGDIR)); + closedir(BGDIR); + } else { + Log3 $name, 2, "$name: Cannot open directory $bgdiritem"; + } + } + #foreach my $f (@bgfiles) { + # Debug sprintf("File \"%s\"\n", $f); + #} - #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 = $bgfiles[$bgnr]; + my $filetype = ( split( /\./, $bgfile ) )[-1]; + readingsSingleUpdate($defs{$name}, "bgFilename", $bgfile, 1); + readingsSingleUpdate($defs{$name}, "bgFiletype", $filetype, 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; - # 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]; - readingsSingleUpdate($defs{$name}, "bgFilename", $bgfile, 1); - readingsSingleUpdate($defs{$name}, "bgFiletype", $filetype, 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 ) { - 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; - } - $S->copyResized( - $bg, - ( $width - $w ) / 2, - ( $height - $h ) / 2, - 0, 0, $w, $h, $bgwidth, $bgheight - ); + # we need to resize + my ( $w, $h ); + my ( $u, $v ) = + ( $bgwidth / $width, $bgheight / $height ); + if ( $u > $v ) { + $w = $width; + $h = $bgheight / $u; } else { - # size is as required - # kill the predefined image and take the original - undef $S; - $S = $bg; + $h = $height; + $w = $bgwidth / $v; } + $S->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 $S; - $reason = "Something was wrong with background image \"$bgfile\"."; + $S = $bg; } } + else { + undef $S; + $reason = "Something was wrong with background image \"$bgfile\"."; + } } } + # # evaluate layout # @@ -1219,8 +1227,14 @@ sub plotFromUrl(@) { by FHEMWEB file editor.
  • size
    The dimensions of the picture in the format <width>x<height>.

  • -
  • bg
    The directory that contains the background pictures (must be in JPEG, GIF or PNG format, file +
  • bg
    A comma-separated list of directories that contain the background pictures (must be in JPEG, GIF or PNG format, file format is guessed from file name extension).

  • +
  • bgroot
    A common prefix for all directories in the bg attribute. Needs to include a trailing path delimiter. Useful for + setting subdirectories of a common root directory as background directories by only listing their names in the bg attributes + and not the full paths.

    + Example 1: attr myRSS bg /opt/fhem/PictureFrame/Bergansichten,/data/Files/Pictures
    + Example 2: attr myRSS bgroot /opt/fhem/PictureFrame/; attr myRSS bg Bergansichten,Waldbilder
    +

  • bgcolor <color>
    Sets the background color.

  • tmin
    The background picture is shown at least tmin seconds, no matter how frequently the RSS feed consumer accesses the page.