2007-12-31 14:43:02 +00:00
##############################################
2011-11-12 07:51:08 +00:00
# $Id$
2007-12-31 14:43:02 +00:00
package main ;
use strict ;
use warnings ;
2013-04-22 18:27:46 +00:00
use vars qw( $FW_subdir ) ; # Sub-path in URL for extensions, e.g. 95_FLOORPLAN
2013-04-29 13:35:49 +00:00
use IO::File ;
2007-12-31 14:43:02 +00:00
#####################################
sub
weblink_Initialize ( $ )
{
my ( $ hash ) = @ _ ;
$ hash - > { DefFn } = "weblink_Define" ;
2013-08-14 15:08:15 +00:00
$ hash - > { AttrList } = "htmlattr" ;
2013-04-22 18:27:46 +00:00
$ hash - > { FW_summaryFn } = "weblink_FwFn" ;
$ hash - > { FW_detailFn } = "weblink_FwFn" ;
$ hash - > { FW_atPageEnd } = 1 ;
2007-12-31 14:43:02 +00:00
}
#####################################
sub
weblink_Define ( $$ )
{
my ( $ hash , $ def ) = @ _ ;
2013-08-14 15:08:15 +00:00
my ( $ name , $ type , $ wltype , $ link ) = split ( "[ \t]+" , $ def , 4 ) ;
my % thash = ( link = > 1 , image = > 1 , iframe = > 1 , htmlCode = > 1 ,
cmdList = > 1 , readings = > 1 ,
fileplot = > 1 , dbplot = > 1 ) ;
2007-12-31 14:43:02 +00:00
if ( ! $ link || ! $ thash { $ wltype } ) {
2009-12-21 18:03:56 +00:00
return "Usage: define <name> weblink [" .
join ( "|" , sort keys % thash ) . "] <arg>" ;
2007-12-31 14:43:02 +00:00
}
2013-08-14 15:08:15 +00:00
if ( $ wltype eq "fileplot" || $ wltype eq "dbplot" ) {
2013-08-18 14:13:59 +00:00
Log3 $ name , 1 , "Converting weblink $name ($wltype) to SVG" ;
2013-08-14 15:08:15 +00:00
my $ newm = LoadModule ( "SVG" ) ;
return "Cannot load module SVG" if ( $ newm eq "UNDEFINED" ) ;
$ hash - > { TYPE } = "SVG" ;
$ hash - > { DEF } = $ link ;
return CallFn ( $ name , "DefFn" , $ hash , "$name $type $link" ) ;
2013-04-29 13:35:49 +00:00
}
2013-08-14 15:08:15 +00:00
$ hash - > { WLTYPE } = $ wltype ;
$ hash - > { LINK } = $ link ;
$ hash - > { STATE } = "initialized" ;
2013-04-29 13:35:49 +00:00
return undef ;
}
2013-04-22 18:27:46 +00:00
#####################################
# FLOORPLAN compat
sub
FW_showWeblink ( $$ $$ )
{
my ( $ d , undef , undef , $ buttons ) = @ _ ;
if ( $ buttons !~ m/HASH/ ) {
my % h = ( ) ; $ buttons = \ % h ;
}
2013-04-23 09:38:28 +00:00
FW_pO ( weblink_FwFn ( undef , $ d , "" , $ buttons ) ) ;
2013-04-22 18:27:46 +00:00
return $ buttons ;
}
##################
sub
2013-04-28 12:40:28 +00:00
weblink_FwDetail ( $@ )
2013-04-22 18:27:46 +00:00
{
2013-07-25 08:59:13 +00:00
my ( $ d , $ text , $ nobr ) = @ _ ;
2013-05-05 08:39:11 +00:00
return "" if ( AttrVal ( $ d , "group" , "" ) ) ;
2013-04-22 18:27:46 +00:00
my $ alias = AttrVal ( $ d , "alias" , $ d ) ;
2013-07-25 08:59:13 +00:00
my $ ret = ( $ nobr ? "" : "<br>" ) ;
2013-04-28 12:40:28 +00:00
$ ret . = "$text " if ( $ text ) ;
2013-04-22 18:27:46 +00:00
$ ret . = FW_pHPlain ( "detail=$d" , $ alias ) if ( ! $ FW_subdir ) ;
2013-04-23 09:38:28 +00:00
$ ret . = "<br>" ;
2013-04-22 18:27:46 +00:00
return $ ret ;
}
sub
weblink_FwFn ( $$ $$ )
{
2013-04-23 10:55:08 +00:00
my ( $ FW_wname , $ d , $ room , $ pageHash ) = @ _ ; # pageHash is set for summaryFn.
2013-04-22 18:27:46 +00:00
my $ hash = $ defs { $ d } ;
my $ link = $ hash - > { LINK } ;
my $ wltype = $ hash - > { WLTYPE } ;
my $ ret = "" ;
my $ attr = AttrVal ( $ d , "htmlattr" , "" ) ;
if ( $ wltype eq "htmlCode" ) {
$ link = AnalyzePerlCommand ( undef , $ link ) if ( $ link =~ m/^{(.*)}$/ ) ;
$ ret = $ link ;
} elsif ( $ wltype eq "link" ) {
$ ret = "<a href=\"$link\" $attr>$d</a>" ; # no FW_pH, open extra browser
} elsif ( $ wltype eq "image" ) {
$ ret = "<img src=\"$link\" $attr><br>" .
weblink_FwDetail ( $ d ) ;
} elsif ( $ wltype eq "iframe" ) {
$ ret = "<iframe src=\"$link\" $attr>Iframes disabled</iframe>" .
weblink_FwDetail ( $ d ) ;
2013-05-06 06:29:47 +00:00
} elsif ( $ wltype eq "cmdList" ) {
my @ lines = split ( " " , $ link ) ;
2013-07-06 09:45:31 +00:00
my $ row = 1 ;
2013-08-15 14:43:43 +00:00
$ ret = "<table>" ;
2013-07-06 09:45:31 +00:00
$ ret . = "<tr><td><div class=\"devType\"><a href=\"/fhem?detail=$d\">" . AttrVal ( $ d , "alias" , $ d ) . "</a></div></td></tr>" ;
2013-05-06 06:29:47 +00:00
$ ret . = "<tr><td><table class=\"block wide\">" ;
foreach my $ line ( @ lines ) {
my @ args = split ( ":" , $ line , 3 ) ;
$ ret . = "<tr class=\"" . ( ( $ row + + & 1 ) ? "odd" : "even" ) . "\">" ;
$ ret . = "<td><a href=\"/fhem?cmd=$args[2]\"><div class=\col1\"><img src=\"/fhem/icons/$args[0]\" width=19 height=19 align=\"center\" alt=\"$args[0]\" title=\"$args[0]\"> $args[1]</div></a></td></td>" ;
$ ret . = "</tr>" ;
}
$ ret . = "</table></td></tr>" ;
$ ret . = "</table><br>" ;
2013-07-06 09:45:31 +00:00
} elsif ( $ wltype eq "readings" ) {
my @ params = split ( " " , $ link ) ;
my @ devices ;
my $ mapping ;
my $ show_heading = 1 ;
my $ show_state = 1 ;
my $ show_time = 1 ;
while ( @ params ) {
my $ param = shift ( @ params ) ;
if ( $ param eq '*noheading' ) {
$ show_heading = 0 ;
} elsif ( $ param eq '*notime' ) {
$ show_time = 0 ;
}
elsif ( $ param eq '*nostate' ) {
$ show_state = 0 ;
} elsif ( $ param =~ m/^{/ ) {
$ mapping = eval $ param . " " . join ( " " , @ params ) ;
last ;
} else {
my @ device = split ( ":" , $ param ) ;
if ( defined ( $ defs { $ device [ 0 ] } ) ) {
push @ devices , [ @ device ] ;
} else {
foreach my $ d ( sort keys % defs ) {
2013-07-15 17:35:17 +00:00
next if ( IsIgnored ( $ d ) ) ;
2013-07-06 09:45:31 +00:00
next if ( $ d !~ m/$device[0]/ ) ;
push @ devices , [ $ d , $ device [ 1 ] ] ;
}
}
}
}
my $ row = 1 ;
$ ret . = "<table>" ;
$ ret . = "<tr><td><div class=\"devType\"><a href=\"/fhem?detail=$d\">" . AttrVal ( $ d , "alias" , $ d ) . "</a></div></td></tr>" if ( $ show_heading ) ;
$ ret . = "<tr><td><table class=\"block wide\">" ;
foreach my $ device ( @ devices ) {
my $ h = $ defs { @ { $ device } [ 0 ] } ;
my $ regex = @ { $ device } [ 1 ] ;
my $ name = $ h - > { NAME } ;
next if ( ! $ h ) ;
foreach my $ n ( sort keys % { $ h - > { READINGS } } ) {
next if ( $ n =~ m/^\./ ) ;
next if ( $ n eq "state" && ! $ show_state ) ;
next if ( defined ( $ regex ) && $ n !~ m/$regex/ ) ;
my $ val = $ h - > { READINGS } - > { $ n } ;
if ( ref ( $ val ) ) {
my ( $ v , $ t ) = ( $ val - > { VAL } , $ val - > { TIME } ) ;
$ v = FW_htmlEscape ( $ v ) ;
$ t = "" if ( ! $ t ) ;
$ ret . = sprintf ( "<tr class=\"%s\">" , ( $ row & 1 ) ? "odd" : "even" ) ;
$ row + + ;
my $ m = $ n ;
$ m = $ mapping - > { $ n } if ( defined ( $ mapping ) && defined ( $ mapping - > { $ n } ) ) ;
$ m = $ mapping - > { $ name . "." . $ n } if ( defined ( $ mapping ) && defined ( $ mapping - > { $ name . "." . $ n } ) ) ;
$ m =~ s/\$NAME/$name/g ;
$ ret . = "<td><div class=\"dname\"><a href=\"/fhem?detail=$name\">$m</a></div></td>" ; ;
2013-07-15 17:35:17 +00:00
$ ret . = "<td><div informId=\"$name-$n\">$v</div></td>" ;
$ ret . = "<td><div informId=\"$name-$n-ts\">$t</div></td>" if ( $ show_time ) ;
2013-07-06 09:45:31 +00:00
}
}
}
$ ret . = "</table></td></tr>" ;
$ ret . = "</table>" ;
$ ret . = "</br>" ;
2013-04-29 13:35:49 +00:00
}
2013-08-14 15:08:15 +00:00
return $ ret ;
2013-04-29 13:35:49 +00:00
}
2007-12-31 14:43:02 +00:00
1 ;
2012-11-04 13:49:43 +00:00
= pod
= begin html
< a name = "weblink" > </a>
<h3> weblink </h3>
<ul>
< a name = "weblinkdefine" > </a>
<b> Define </b>
<ul>
2013-08-14 15:08:15 +00:00
<code> define & lt ; name & gt ; weblink [ link | image | iframe | htmlCode | cmdList | readings ]
2012-11-04 13:49:43 +00:00
& lt ; argument & gt ; </code>
<br> <br>
2013-08-14 15:08:15 +00:00
This is a placeholder device used with FHEMWEB to be able to add user
defined links .
2012-11-04 13:49:43 +00:00
Examples:
<ul>
<code> define homepage weblink link http: // www . fhem . de </code> <br>
<code> define webcam_picture weblink image http: // w . x . y . z /current.jpg</co de > <br>
<code> define interactive_webcam weblink iframe http: // w . x . y . z /webcam.cgi</co de > <br>
<code> define hr weblink htmlCode & lt ; hr & gt </code> <br>
<code> define w_Frlink weblink htmlCode { WeatherAsHtml ( "w_Frankfurt" ) } </code> <br>
2013-05-06 06:29:47 +00:00
<code> define systemCommands weblink cmdList pair:Pair:set + cul2 + hmPairForSec + 60 restart:Restart:shutdown + restart update:UpdateCheck:update + check </code> <br>
2013-07-15 17:35:17 +00:00
<code> define wl_SystemStatus weblink readings sysstat * nostate * notime { { 'load' = > 'Systemauslastung' , 'temperature' = > 'Systemtemperatur in &deg;;C' } } </code> <br>
2013-07-06 09:45:31 +00:00
<code> define wlHeizung weblink readings t1:temperature t2:temperature t3:temperature * notime { { 't1.temperature' = > 'Vorlauf' , 't2.temperature' = > 'R&uuml;;cklauf' , 't3.temperature' = > 'Zirkulation' } } </code>
2012-11-04 13:49:43 +00:00
</ul>
<br>
Notes:
<ul>
2013-05-06 06:29:47 +00:00
<li> For cmdList & lt ; argument & gt ; consist of a list of space separated icon:label:cmd triples . </li>
2013-08-14 15:08:15 +00:00
<li> For readings & lt ; argument & gt ; consist of one or more & lt ; device & gt ; [ : regex ] pairs ,
2013-07-06 09:45:31 +00:00
zero or more of the modifiers * noheading , * notime and * nostate and as the last argument an optional { } expression that should return
a perl hash to map reading names to display names .
<ul>
<li> & lt ; device & gt ; <br>
the device ( s ) from which the readings should be taken </li>
<li> regex <br>
an optional regex to select the readings that should be displayed . </li>
<li> * noheading <br> don ' t display the table heading </li>
<li> * notime <br> don ' t display the reading timestamp </li>
<li> * nostate <br> don ' t include the state reading , other readings can be excluded with a regex of the form: <code> ^ ( ( ? ! reading ) . ) * $< /code></ li >
<li> { ... } <br>
a perl expression that returns a hash that maps the reading name to the display name . keys can be either the name of the reading or & lt ; device & gt ; . & lt ; reading & gt ; . $ NAME is replaced by the device name </li>
</li>
</ul>
2013-05-16 08:03:55 +00:00
</ul>
2012-11-04 13:49:43 +00:00
</ul>
< a name = "weblinkset" > </a>
2013-08-14 15:08:15 +00:00
<b> Set </b> <ul> N /A</ ul > <br>
2012-11-04 13:49:43 +00:00
< a name = "weblinkget" > </a>
<b> Get </b> <ul> N /A</ ul > <br>
< a name = "weblinkattr" > </a>
<b> Attributes </b>
<ul>
< a name = "htmlattr" > </a>
<li> htmlattr <br>
2013-05-16 08:03:55 +00:00
HTML attributes to be used for link , image and iframe type of links .
E . g . : <br>
2012-11-04 13:49:43 +00:00
<ul>
<code>
define yw weblink wl_im1 iframe http: // weather . yahooapis . com / forecastrss ? w = 650272 & u = c <br>
attr yw weblink htmlattr width = "480" height = "560" <br>
</code>
2013-05-16 08:03:55 +00:00
</ul> </li>
2012-11-04 13:49:43 +00:00
</ul>
<br>
</ul>
= end html
= cut