98_weblink.pm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ##############################################
  2. # $Id: 98_weblink.pm 13143 2017-01-18 16:20:05Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use vars qw($FW_subdir); # Sub-path in URL for extensions, e.g. 95_FLOORPLAN
  7. use vars qw($FW_ME); # webname (default is fhem), used by 97_GROUP/weblink
  8. use IO::File;
  9. #####################################
  10. sub
  11. weblink_Initialize($)
  12. {
  13. my ($hash) = @_;
  14. $hash->{DefFn} = "weblink_Define";
  15. $hash->{AttrList} =
  16. "disable:0,1 disabledForIntervals htmlattr nodetaillink:1,0";
  17. $hash->{FW_summaryFn} = "weblink_FwFn";
  18. $hash->{FW_detailFn} = "weblink_FwFn";
  19. $hash->{FW_atPageEnd} = 1;
  20. }
  21. #####################################
  22. sub
  23. weblink_Define($$)
  24. {
  25. my ($hash, $def) = @_;
  26. my ($name, $type, $wltype, $link) = split("[ \t]+", $def, 4);
  27. my %thash = ( link=>1, image=>1, iframe=>1, htmlCode=>1,
  28. cmdList=>1,
  29. fileplot=>1, dbplot=>1);
  30. if(!$link || !$thash{$wltype}) {
  31. return "Usage: define <name> weblink [" .
  32. join("|",sort keys %thash) . "] <arg>";
  33. }
  34. if($wltype eq "fileplot" || $wltype eq "dbplot") {
  35. Log3 $name, 1, "Converting weblink $name ($wltype) to SVG";
  36. my $newm = LoadModule("SVG");
  37. return "Cannot load module SVG" if($newm eq "UNDEFINED");
  38. $hash->{TYPE} = "SVG";
  39. $hash->{DEF} = $link;
  40. return CallFn($name, "DefFn", $hash, "$name $type $link");
  41. }
  42. $hash->{WLTYPE} = $wltype;
  43. $hash->{LINK} = $link;
  44. $hash->{STATE} = "initialized";
  45. return undef;
  46. }
  47. #####################################
  48. # FLOORPLAN compat
  49. sub
  50. FW_showWeblink($$$$)
  51. {
  52. my ($d,undef,undef,$buttons) = @_;
  53. if($buttons !~ m/HASH/) {
  54. my %h = (); $buttons = \%h;
  55. }
  56. FW_pO(weblink_FwFn(undef, $d, "", $buttons));
  57. return $buttons;
  58. }
  59. ##################
  60. sub
  61. weblink_FwDetail($@)
  62. {
  63. my ($d, $text, $nobr)= @_;
  64. return "" if(AttrVal($d, "group", "") || AttrVal($d, "nodetaillink", ""));
  65. my $alias = AttrVal($d, "alias", $d);
  66. my $ret = ($nobr ? "" : "<br>");
  67. $ret .= "$text " if($text);
  68. $ret .= FW_pHPlain("detail=$d", $alias) if(!$FW_subdir);
  69. $ret .= "<br>";
  70. return $ret;
  71. }
  72. sub
  73. weblink_FwFn($$$$)
  74. {
  75. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  76. my $hash = $defs{$d};
  77. my $link = $hash->{LINK};
  78. my $wltype = $hash->{WLTYPE};
  79. my $ret = "";
  80. return "" if(IsDisabled($d));
  81. my $attr = AttrVal($d, "htmlattr", "");
  82. if($wltype eq "htmlCode") {
  83. $link = AnalyzePerlCommand(undef, $link) if($link =~ m/^{(.*)}$/s);
  84. $ret = $link;
  85. } elsif($wltype eq "link") {
  86. my $alias = AttrVal($d, "alias", $d);
  87. $ret = "<a href=\"$link\" $attr>$alias</a>"; # no FW_pH, open extra browser
  88. } elsif($wltype eq "image") {
  89. $ret = "<img src=\"$link\" $attr><br>" .
  90. weblink_FwDetail($d);
  91. } elsif($wltype eq "iframe") {
  92. $ret = "<iframe src=\"$link\" $attr>Iframes disabled</iframe>" .
  93. weblink_FwDetail($d);
  94. } elsif($wltype eq "cmdList") {
  95. my @lines = split(" ", $link);
  96. my $row = 1;
  97. $ret = "<table>";
  98. $ret .= "<tr><td><div class='devType'><a href='$FW_ME?detail=$d'>"
  99. . AttrVal($d, "alias", $d)."</a></div></td></tr>";
  100. $ret .= "<tr><td><table class=\"block wide\">";
  101. foreach my $line (@lines) {
  102. my @args = split(":", $line, 3);
  103. $ret .= "<tr class='".(($row++&1)?"odd":"even")."'>";
  104. $ret .= "<td><a href='$FW_ME?cmd=$args[2]'><div class='col1'>".
  105. "<img src='$FW_ME/icons/$args[0]' width='19' height='19' ".
  106. "align='center' alt='$args[0]' title='$args[0]'>".
  107. "$args[1]</div></a></td></td>";
  108. $ret .= "</tr>";
  109. }
  110. $ret .= "</table></td></tr>";
  111. $ret .= "</table><br>";
  112. }
  113. return $ret;
  114. }
  115. 1;
  116. =pod
  117. =item helper
  118. =item summary define a HTTP link for the FHEMWEB frontend
  119. =item summary_DE HTTP Link fuer das FHEMWEB Frontend
  120. =begin html
  121. <a name="weblink"></a>
  122. <h3>weblink</h3>
  123. <ul>
  124. <a name="weblinkdefine"></a>
  125. <b>Define</b>
  126. <ul>
  127. <code>define &lt;name&gt; weblink [link|image|iframe|htmlCode|cmdList]
  128. &lt;argument&gt;</code>
  129. <br><br>
  130. This is a placeholder device used with FHEMWEB to be able to add user
  131. defined links.
  132. Examples:
  133. <ul>
  134. <code>
  135. define homepage weblink link http://www.fhem.de<br>
  136. define webcam_picture weblink image http://w.x.y.z/current.jpg<br>
  137. define interactive_webcam weblink iframe http://w.x.y.z/webcam.cgi<br>
  138. define hr weblink htmlCode &lt;hr&gt<br>
  139. define w_Frlink weblink htmlCode { WeatherAsHtml("w_Frankfurt") }<br>
  140. define systemCommands weblink cmdList
  141. pair:Pair:set+cul2+hmPairForSec+60
  142. restart:Restart:shutdown+restart
  143. update:UpdateCheck:update+check
  144. </code>
  145. </ul>
  146. <br>
  147. Notes:
  148. <ul>
  149. <li>For cmdList &lt;argument&gt; consists of a list of space
  150. separated icon:label:cmd triples.</li>
  151. </ul>
  152. </ul>
  153. <a name="weblinkset"></a>
  154. <b>Set</b> <ul>N/A</ul><br>
  155. <a name="weblinkget"></a>
  156. <b>Get</b> <ul>N/A</ul><br>
  157. <a name="weblinkattr"></a>
  158. <b>Attributes</b>
  159. <ul>
  160. <a name="htmlattr"></a>
  161. <li>htmlattr<br>
  162. HTML attributes to be used for link, image and iframe type of links.
  163. E.g.:<br>
  164. <ul>
  165. <code>
  166. define yw weblink iframe http://weather.yahooapis.com/forecastrss?w=650272&u=c<br>
  167. attr yw htmlattr width="480" height="560"<br>
  168. </code>
  169. </ul></li>
  170. <a name="nodetaillink"></a>
  171. <li>nodetaillink<br>
  172. Show no detail link for the types image and iframe.
  173. </li>
  174. <li><a href="#disable">disable</a></li>
  175. <li><a href="#disabledForIntervals">disabledForIntervals</a></li>
  176. </ul>
  177. <br>
  178. </ul>
  179. =end html
  180. =cut