From 97495c482fe0c4e264444a9c3b9d585d4f8a476e Mon Sep 17 00:00:00 2001 From: borisneubert Date: Sun, 15 Feb 2015 11:50:48 +0000 Subject: [PATCH] 02_RSS.pm: new attribut autoreread; new NotifyFn and UndefFN; new layout commands textboxf, embed, push, pop; FWext is automatically deleted after deletion of last RSS device; RSS Overview and elements opened in new tab; refresh 0 to disable autorefresh; new function plotFromUrl git-svn-id: https://svn.fhem.de/fhem/trunk@7991 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 5 ++ fhem/FHEM/02_RSS.pm | 179 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 152 insertions(+), 32 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 186b742f9..25a1801a4 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,10 @@ # 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.pm: new attribut autoreread; new NotifyFn and UndefFN; + new layout commands textboxf, embed, push, pop; + FWext is automatically deleted after deletion of last + RSS device; RSS Overview and elements opened in new tab; + refresh 0 to disable autorefresh; new function plotFromUrl - bugfix: SOMFY: use correct attributename when calculating updatetime - feature: 95_FLOORPLAN.pm: arrange by drag&drop, copy, rename, delete processes userattr fp_ diff --git a/fhem/FHEM/02_RSS.pm b/fhem/FHEM/02_RSS.pm index 642077725..8953b1738 100644 --- a/fhem/FHEM/02_RSS.pm +++ b/fhem/FHEM/02_RSS.pm @@ -43,7 +43,7 @@ RSS_addExtension($$$) { my $url = "/" . $link; $data{FWEXT}{$url}{FUNC} = $func; - $data{FWEXT}{$url}{LINK} = $link; + $data{FWEXT}{$url}{LINK} = "+$link"; $data{FWEXT}{$url}{NAME} = $friendlyname; $data{FWEXT}{$url}{FORKABLE} = 0; } @@ -52,16 +52,15 @@ RSS_addExtension($$$) { sub RSS_Initialize($) { my ($hash) = @_; - $hash->{DefFn} = "RSS_Define"; + $hash->{DefFn} = "RSS_Define"; + $hash->{UndefFn} = "RSS_Undefine"; #$hash->{AttrFn} = "RSS_Attr"; - $hash->{AttrList}= "size bg bgcolor tmin refresh areas"; - $hash->{SetFn} = "RSS_Set"; - - RSS_addExtension("RSS_CGI","rss","RSS"); + $hash->{AttrList} = "size bg bgcolor tmin refresh areas autoreread:1,0"; + $hash->{SetFn} = "RSS_Set"; + $hash->{NotifyFn} = "RSS_Notify"; return undef; } - ################## sub @@ -100,6 +99,9 @@ RSS_Define($$) { $hash->{fhem}{hostname}= $hostname; $hash->{fhem}{filename}= $filename; $hash->{LAYOUTFILE} = $filename; + $hash->{NOTIFYDEV} = 'global'; + + RSS_addExtension("RSS_CGI","rss","RSS"); eval "use GD::Text::Align"; $hash->{fhem}{useTextAlign} = ($@ ? 0 : 1 ); @@ -119,6 +121,27 @@ RSS_Define($$) { return undef; } +sub RSS_Undefine($$) { + my ($hash, $arg) = @_; + # check if last device + my $url = '/rss'; + $data{FWEXT}{$url} = undef if int(devspec2array('TYPE=RSS')) == 1; + return undef; +} + +sub RSS_Notify { + my ($hash,$dev) = @_; + + my $name= $hash->{NAME}; + return unless AttrVal($name, 'autoreread',1); + return if($dev->{NAME} ne "global"); + return if(!grep(m/^FILEWRITE $hash->{LAYOUTFILE}$/, @{$dev->{CHANGED}})); + + Log3(undef, 4, "RSS: $name reread layout after edit."); + RSS_readLayout($hash); + return undef; +} + ################## sub RSS_Set() { @@ -178,10 +201,10 @@ RSS_Overview { $name= $defs{$def}{NAME}; $url= RSS_getURL($defs{$def}{fhem}{hostname}); $html.= "$name
\n\n

\n"; } } @@ -257,7 +280,7 @@ RSS_HTMLHead($$) { my $doctype= ''; my $xmlns= 'xmlns="http://www.w3.org/1999/xhtml"'; - my $r= defined($refresh) ? "\n" : ""; + my $r= (defined($refresh) && $refresh > 0) ? "\n" : ""; # css and js header output should be coded only in one place my $css= ""; #my $cssTemplate = "\n"; @@ -282,8 +305,10 @@ RSS_returnHTML($) { my $img= "$url/rss/$name.$type"; my $refresh= AttrVal($name, 'refresh', 60); my $areas= AttrVal($name, 'areas', ""); + my $embed= $defs{$name}{".embed"}; + $defs{$name}{".embed"} = undef; my $code= RSS_HTMLHead($name, $refresh) . - "\n\n\n$areas\n\n\n" . + "\n

\n\n\n$areas\n\n
\n$embed\n" . RSS_HTMLTail(); return ("text/html; charset=utf-8", $code); } @@ -338,7 +363,7 @@ RSS_itemText { sub RSS_itemTextBox { - my ($S,$x,$y,$boxwidth,$text,%params)= @_; + my ($S,$x,$y,$boxwidth,$bgcolor,$text,%params)= @_; return unless(defined($text)); if($params{useTextWrap}) { @@ -350,7 +375,10 @@ RSS_itemTextBox { ); $wrapbox->set_font($params{font}, $params{pt}); $wrapbox->set(align => $params{thalign}, width => $boxwidth); - my ($left, $top, $right, $bottom) = $wrapbox->draw($x, $y); + my ($left, $top, $right, $bottom); + ($left, $top, $right, $bottom) = $wrapbox->get_bounds($x,$y); + $S->filledRectangle($left,$top,$right,$bottom,RSS_color($S,$bgcolor)) if(defined($bgcolor)); + ($left, $top, $right, $bottom) = $wrapbox->draw($x, $y); return $bottom; } else { RSS_itemText($S,$x,$y,$text,%params); @@ -487,6 +515,8 @@ RSS_evalLayout($$@) { my @layout= split("\n", $layout); + my %pstack; + my $pstackcount = 0; my %params; $params{font}= "Arial"; $params{pt}= 12; @@ -507,8 +537,8 @@ RSS_evalLayout($$@) { $params{y}= 0; - my ($x,$y,$x1,$y1,$x2,$y2,$scale,$boxwidth,$text,$imgtype,$srctype,$arg,$format); - + my ($x,$y,$z,$x1,$y1,$x2,$y2,$scale,$bgcolor,$boxwidth,$text,$imgtype,$srctype,$arg,$format); + my $cont= ""; foreach my $line (@layout) { # kill trailing newline @@ -588,8 +618,15 @@ RSS_evalLayout($$@) { ($x,$y,$boxwidth,$text)= split("[ \t]+", $def, 4); ($x,$y)= RSS_xy($S, $x,$y,%params); my $txt= AnalyzePerlCommand(undef, $text); - #Debug "$name: ($x,$y) $txt"; - $y= RSS_itemTextBox($S,$x,$y,$boxwidth,$txt,%params); + $y= RSS_itemTextBox($S,$x,$y,$boxwidth,undef,$txt,%params); + $params{x} = $x; + $params{y} = $y; + } elsif($cmd eq "textboxf") { + ($x,$y,$boxwidth,$bgcolor,$text)= split("[ \t]+", $def, 5); + ($x,$y)= RSS_xy($S, $x,$y,%params); + $bgcolor = ($bgcolor ne "") ? AnalyzePerlCommand(undef,$bgcolor) : undef; + my $txt= AnalyzePerlCommand(undef, $text); + $y= RSS_itemTextBox($S,$x,$y,$boxwidth,$bgcolor,$txt,%params); $params{x} = $x; $params{y} = $y; } elsif($cmd eq "line") { @@ -629,9 +666,28 @@ RSS_evalLayout($$@) { $params{y} = $y; my $arg= AnalyzePerlCommand(undef, $arg); RSS_itemImg($S,$x,$y,$scale,$imgtype,$srctype,$arg,%params); - } else { + } elsif($cmd eq "push") { + $pstackcount++; + while ( my ($key, $value) = each(%params) ) { + $pstack{$pstackcount}{$key} = $value; + } + } elsif($cmd eq "pop") { + return unless $pstackcount; + while ( my ($key, $value) = each($pstack{$pstackcount}) ) { + $params{$key} = $value; + } + delete $pstack{$pstackcount}; + $pstackcount--; + } elsif($cmd eq "embed") { + ($x,$y,$z,$format,$text,$arg) = split("[ \t]+", $def,6); + ($x,$y)= RSS_xy($S, $x,$y,%params); + my $arg= AnalyzePerlCommand(undef, $arg); + $defs{$name}{".embed"} .= "
\n"; + $defs{$name}{".embed"} .= $arg."\n"; + $defs{$name}{".embed"} .= "
\n"; + } else { Log3 $name, 2, "$name: Illegal command $cmd in layout definition."; - } + } #Debug "after command $line: x= " . $params{x} . ", y= " . $params{y}; }; @@ -803,6 +859,38 @@ RSS_CGI(){ } +sub +plotFromUrl(@) +{ + my (@plotName) = @_; + my @webs; + + @webs=devspec2array("TYPE=FHEMWEB"); + foreach(@webs) { + if(!InternalVal($_,'TEMPORARY',undef)) { + $FW_wname=InternalVal($_,'NAME',''); + last; + } + } + + my ($w, $h) = split(",", AttrVal($plotName[0],"plotsize","800,160")); + + my $url; + $url = ""; + return $url; +} + # @@ -893,6 +981,8 @@ RSS_CGI(){ Attributes

Item placement commands

@@ -1023,21 +1119,37 @@ RSS_CGI(){ You can use { <perl special> } for <text> to fully access device readings and do some programming on the fly. See below for examples.
-

  • textbox <x> <y> <boxwidth> <text>
    Same as before but text is rendered in a box of horizontal width <boxwidth>.

  • + +
  • textbox <x> <y> <boxwidth> <text>
    Same as before but text is rendered + in a box of horizontal width <boxwidth>.

  • + +
  • textboxf <x> <y> <boxwidth> <bgcolor> <text>
    Same as before but + the textbox will be filled with the given background color <bgcolor> before drawing the text. + <bgcolor> can be used with { <perl special> } to evalute RGB value.

  • +
  • time <x> <y>
    Renders the current time in HH:MM format.

  • +
  • seconds <x> <y> <format>
    Renders the curent seconds. Maybe useful for a RSS Clock.

  • -
  • date <x> <y>
    Renders the current date in DD:MM:YYY format.

  • + +
  • date <x> <y>
    Renders the current date in DD.MM.YYYY format.

  • +
  • line <x1> <y1> <x2> <y2> [<thickness>]
    Draws a line from position (<x1>, <y1>) to position (<x2>, <y2>) with optional thickness (default=1).

  • rect <x1> <y1> <x2> <y2> [<filled>]
    Draws a rectangle with corners at positions (<x1>, <y1>) and (<x2>, <y2>), which is filled if the <filled> parameter is set and not zero.

  • +
  • img <x> <y> <['w' or 'h']s> <imgtype> <srctype> <arg>
    Renders a picture at the position (<x>, <y>). The <imgtype> is one of gif, jpeg, png. The picture is scaled by the factor <s> (a decimal value). If 'w' or 'h' is in front of scale-value the value is used to set width or height to the value in pixel. If <srctype> is file, the picture is loaded from the filename <arg>, if <srctype> is url or urlq, the picture is loaded from the URL <arg> (with or without logging the URL), if <srctype> is data, the picture is piped in from data <arg>. You can use - { <perl special> } for <arg>. See below for example. - Notice: do not load the image from URL that is served by fhem as it leads to a deadlock.
  • -
    + { <perl special> } for <arg>. See below for example.
    + Warning: do not load the image from URL that is served by fhem as it leads to a deadlock.
    + +
  • embed <x> <y> <z> <position> <id> <element>
    + For HTML output: embeds a div element into the HTML page at (<x>,<y>) with z-order <z> and positioning <position> (use absolute). <id> is the id attribute of the + div element and <element> is its content.
    + Note: There are several issues with different browsers when using this.

  • + Example

    @@ -1052,6 +1164,8 @@ RSS_CGI(){ moveby 0 -25
    text x y "Another text"
    img 20 530 0.5 png file { "/usr/share/fhem/www/images/weather/" . ReadingsVal("MyWeather","icon","") . ".png" }
    + embed 0 0 2 absolute plot1 { plotFromUrl('mySVG') } + embed 10 200 2 absolute iframe1 "<iframe width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/9HShl_ufOFI\" frameborder=\"0\" allowfullscreen></iframe>"

    @@ -1063,8 +1177,9 @@ RSS_CGI(){ img 20 30 0.6 png data { plotAsPng("mySVGPlot","qday",-1) }

    - This requires the perl module Image::LibRSVG and librsvg. Debian-based systems can install these with apt-get install libimage-librsvg-perl. - + This requires the perl module Image::LibRSVG and librsvg. Debian-based systems can install these with apt-get install libimage-librsvg-perl.

    + + For HTML output, you can use plotFromURL(<name>[,<zoom>[,<offset>]]) instead.