GDSweblink.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # $Id: GDSweblink.pm 9432 2015-10-11 10:38:18Z betateilchen $
  2. package main;
  3. use strict;
  4. use warnings;
  5. use POSIX;
  6. ####################################################################################################
  7. #
  8. # create weblinks
  9. # provided and maintained by jensb
  10. #
  11. ####################################################################################################
  12. # weather description to icon name mapping
  13. my %GDSDayWeatherIconMap = (
  14. 'bedeckt' => 'overcast',
  15. 'bewölkt' => 'mostlycloudy',
  16. 'Dunst oder flacher Nebel' => 'haze',
  17. 'gefrierender Nebel' => 'icy',
  18. 'gering bewölkt' => 'partlycloudy',
  19. 'Gewitter' => 'thunderstorm',
  20. 'Glatteisbildung' => 'icy',
  21. 'Graupelschauer' => 'snow',
  22. 'Hagelschauer' => 'snow',
  23. 'heiter' => 'partlycloudy',
  24. 'in Wolken' => 'mostlycloudy',
  25. 'kein signifikantes Wetter' => 'na',
  26. 'kräftiger Graupelschauer' => 'heavysnow',
  27. 'kräftiger Hagelschauer' => 'heavysnow',
  28. 'kräftiger Regen' => 'heavyrain',
  29. 'kräftiger Regenschauer' => 'scatteredshowers',
  30. 'kräftiger Schneefall' => 'heavysnow',
  31. 'kräftiger Schneeregen' => 'rainsnow',
  32. 'kräftiger Schneeregenschauer' => 'rainsnow',
  33. 'kräftiger Schneeschauer' => 'heavysnow',
  34. 'leicht bewölkt' => 'partlycloudy',
  35. 'leichter Regen' => 'mist',
  36. 'leichter Schneefall' => 'snow',
  37. 'leichter Schneeregen' => 'rainsnow',
  38. 'Nebel' => 'fog',
  39. 'Regen' => 'rain',
  40. 'Regenschauer' => 'scatteredshowers',
  41. 'Sandsturm' => 'dust',
  42. 'Schneefall' => 'snow',
  43. 'Schneefegen' => 'snow',
  44. 'Schneeregen' => 'rainsnow',
  45. 'Schneeregenschauer' => 'rainsnow',
  46. 'Schneeschauer' => 'snow',
  47. 'schweres Gewitter' => 'thunderstorm',
  48. 'stark bewölkt' => 'mostlycloudy',
  49. 'starkes Gewitter' => 'thunderstorm',
  50. 'wolkenlos' => 'sunny',
  51. '---' => 'mostlycloudy',
  52. );
  53. my %GDSNightWeatherIconMap = (
  54. 'bedeckt' => 'overcast',
  55. 'bewölkt' => 'mostlycloudy_night',
  56. 'Dunst oder flacher Nebel' => 'haze_night',
  57. 'gefrierender Nebel' => 'icy',
  58. 'gering bewölkt' => 'partlycloudy_night',
  59. 'Gewitter' => 'thunderstorm',
  60. 'Glatteisbildung' => 'icy',
  61. 'Graupelschauer' => 'snow',
  62. 'Hagelschauer' => 'snow',
  63. 'heiter' => 'partlycloudy_night',
  64. 'in Wolken' => 'mostlycloudy_night',
  65. 'kein signifikantes Wetter' => 'na',
  66. 'kräftiger Graupelschauer' => 'heavysnow',
  67. 'kräftiger Hagelschauer' => 'heavysnow',
  68. 'kräftiger Regen' => 'heavyrain',
  69. 'kräftiger Regenschauer' => 'scatteredshowers_night',
  70. 'kräftiger Schneefall' => 'heavysnow',
  71. 'kräftiger Schneeregen' => 'rainsnow',
  72. 'kräftiger Schneeregenschauer' => 'rainsnow',
  73. 'kräftiger Schneeschauer' => 'heavysnow',
  74. 'leicht bewölkt' => 'partlycloudy_night',
  75. 'leichter Regen' => 'mist',
  76. 'leichter Schneefall' => 'snow',
  77. 'leichter Schneeregen' => 'rainsnow',
  78. 'Nebel' => 'fog',
  79. 'Regen' => 'rain',
  80. 'Regenschauer' => 'scatteredshowers_night',
  81. 'Sandsturm' => 'dust',
  82. 'Schneefall' => 'snow',
  83. 'Schneefegen' => 'snow',
  84. 'Schneeregen' => 'rainsnow',
  85. 'Schneeregenschauer' => 'rainsnow',
  86. 'Schneeschauer' => 'snow',
  87. 'schweres Gewitter' => 'thunderstorm',
  88. 'stark bewölkt' => 'mostlycloudy_night',
  89. 'starkes Gewitter' => 'thunderstorm',
  90. 'wolkenlos' => 'sunny_night',
  91. '---' => 'mostlycloudy_night',
  92. );
  93. # icon parameters
  94. use constant ICONHIGHT => 120;
  95. use constant ICONWIDTH => 175;
  96. use constant ICONSCALE => 0.5;
  97. sub GDSIsDay($$) {
  98. # check if it is day at given time
  99. #
  100. # @param: time
  101. # @param: altitude, see documentation of module SUNRISE_EL
  102. my ($time, $altitude) = @_;
  103. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
  104. my $t = ($hour*60 + $min) + $sec;
  105. my (undef, $srHour, $srMin, $srSec, undef) = GetTimeSpec(sunrise_abs_dat($time, $altitude));
  106. my $sunrise = ($srHour*60 + $srMin) + $srSec;
  107. my (undef, $ssHour, $ssMin, $ssSec, undef) = GetTimeSpec(sunset_abs_dat($time, $altitude));
  108. my $sunset = ($ssHour*60 + $ssMin) + $ssSec;
  109. return $t >= $sunrise && $t <= $sunset;
  110. }
  111. sub GDSIconIMGTag($;$) {
  112. # get FHEM weather icon
  113. #
  114. # @param: weather description
  115. # @param: time of weather description or 1 for night, optional, defaults to daytime icons
  116. my $width = int(ICONSCALE*ICONWIDTH);
  117. my ($weather, $time) = @_;
  118. my $icon;
  119. if (!defined($time) || (defined($time) && $time > 1 && GDSIsDay($time, "REAL"))) {
  120. $icon = $GDSDayWeatherIconMap{$weather};
  121. } else {
  122. $icon = $GDSNightWeatherIconMap{$weather};
  123. }
  124. if (defined($icon)) {
  125. my $url= FW_IconURL("weather/$icon");
  126. my $style= " width=$width";
  127. return "<img src=\"$url\"$style alt=\"$icon\">";
  128. } else {
  129. return "";
  130. }
  131. }
  132. sub GDSAsHtmlV($;$) {
  133. # create forecast in a vertical HTML table
  134. #
  135. # @param: device name
  136. # @param: number of icons, optional, default 8
  137. my ($d,$items) = @_;
  138. $d = "<none>" if(!$d);
  139. $items = $items? $items - 1 : 7;
  140. return "$d is not a GDS instance<br>"
  141. if(!$defs{$d} || $defs{$d}{TYPE} ne "GDS");
  142. my $width = int(ICONSCALE*ICONWIDTH);
  143. my $ret = sprintf('<table class="weather"><tr><th width=%d></th><th></th></tr>', $width);
  144. $ret .= sprintf('<tr><td class="weatherIcon" width=%d>%s</td><td class="weatherValue"><span class="weatherDay">Aktuell: </span><span class="weatherCondition">%s</span><br><span class="weatherValue">%s°C</span><br><span class="weatherWind">Wind %s km/h %s</span></td></tr>',
  145. $width,
  146. GDSIconIMGTag(ReadingsVal($d, "c_weather", "?"), time_str2num(ReadingsTimestamp($d, "c_weather", TimeNow()))),
  147. ReadingsVal($d, "c_weather", "?"),
  148. ReadingsVal($d, "c_temperature", "?"),
  149. ReadingsVal($d, "c_windSpeed", "?"), ReadingsVal($d, "c_windDir", "?"));
  150. # get time of last forecast
  151. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time_str2num(ReadingsTimestamp($d, "fc3_weather24", TimeNow())));
  152. for(my $i=0; $i<$items; $i++) {
  153. my $day = int(($i + 1)/2);
  154. my $timeLabel = $i == 0? ($hour < 17? '18' : '24') : ($i - 1)%2 == 0? '12' : '24';
  155. my $weekday = $i == 0? ($hour < 17? 'Spät' : 'Nachts') : ($i - 1)%2 == 0? ReadingsVal($d, "fc".$day."_weekday", "?").' früh' : ReadingsVal($d, "fc".$day."_weekday", "?").' spät';
  156. if (($i - 1)%2 == 0) {
  157. $ret .= sprintf('<tr><td class="weatherIcon" width=%d>%s</td><td class="weatherValue"><span class="weatherDay">%s: </span><span class="weatherCondition">%s</span><br><span class="weatherMin">min %s°C</span><br><span class="weatherWind">%s</span></span></td></tr>',
  158. $width,
  159. GDSIconIMGTag(ReadingsVal($d, "fc".$day."_weather".$timeLabel, "?")),
  160. $weekday,
  161. ReadingsVal($d, "fc".$day."_weather".$timeLabel, "?"),
  162. ReadingsVal($d, "fc".$day."_tMinAir", "?"),
  163. ReadingsVal($d, "fc".$day."_windGust".$timeLabel, ""));
  164. } else {
  165. if ($i == 0 && $hour >= 17) {
  166. $ret .= sprintf('<tr><td class="weatherIcon" width=%d>%s</td><td class="weatherValue"><span class="weatherDay">%s: </span><span class="weatherCondition">%s</span><br><span class="weatherValue">%s°C</span><br><span class="weatherWind">%s</span></td></tr>',
  167. $width,
  168. GDSIconIMGTag(ReadingsVal($d, "fc".$day."_weather".$timeLabel, "?"), 1),
  169. $weekday,
  170. ReadingsVal($d, "fc".$day."_weather".$timeLabel, "?"),
  171. ReadingsVal($d, "fc".$day."_tAvgAir".$timeLabel, "?"),
  172. ReadingsVal($d, "fc".$day."_windGust".$timeLabel, ""));
  173. } else {
  174. $ret .= sprintf('<tr><td class="weatherIcon" width=%d>%s</td><td class="weatherValue"><span class="weatherDay">%s: </span><span class="weatherCondition">%s</span><br><span class="weatherMax">max %s°C</span><br><span class="weatherWind">%s</span></td></tr>',
  175. $width,
  176. GDSIconIMGTag(ReadingsVal($d, "fc".$day."_weather".$timeLabel, "?")),
  177. $weekday,
  178. ReadingsVal($d, "fc".$day."_weather".$timeLabel, "?"),
  179. ReadingsVal($d, "fc".$day."_tMaxAir", "?"),
  180. ReadingsVal($d, "fc".$day."_windGust".$timeLabel, ""));
  181. }
  182. }
  183. }
  184. $ret .= "</table>";
  185. return $ret;
  186. }
  187. sub GDSAsHtmlH($;$) {
  188. # create forecast in a horizontal HTML table
  189. #
  190. # @param: device name
  191. # @param: number of icons, optional, default 8
  192. my ($d, $items) = @_;
  193. $d = "<none>" if(!$d);
  194. $items = $items? $items - 1 : 7;
  195. return "$d is not a GDS instance<br>"
  196. if(!$defs{$d} || $defs{$d}{TYPE} ne "GDS");
  197. my $width = 110;
  198. my $ret = '<table class="weather">';
  199. # get time of last forecast
  200. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time_str2num(ReadingsTimestamp($d, "fc3_weather24", TimeNow())));
  201. # weekday / time
  202. $ret .= sprintf('<tr><td align="center" class="weatherDay">Aktuell</td>');
  203. for(my $i=0; $i<$items; $i++) {
  204. my $day = int(($i + 1)/2);
  205. my $timeLabel = $i == 0? ($hour < 17? '18' : '24') : ($i - 1)%2 == 0? '12' : '24';
  206. my $weekday = $i == 0? ($hour < 17? 'Spät' : 'Nachts') : ($i - 1)%2 == 0? ReadingsVal($d, "fc".$day."_weekday", "").' früh' : ReadingsVal($d, "fc".$day."_weekday", "").' spät';
  207. $ret .= sprintf('<td align="center" class="weatherDay">%s</td>', $weekday);
  208. }
  209. $ret .= '</tr>';
  210. # condition icon
  211. $ret .= sprintf('<tr><td align="center" class="weatherIcon" width=%d>%s</td>', $width, GDSIconIMGTag(ReadingsVal($d, "c_weather", "na"), time_str2num(ReadingsTimestamp($d, "c_weather", TimeNow()))));
  212. for(my $i=0; $i<$items; $i++) {
  213. my $day = int(($i + 1)/2);
  214. my $timeLabel = $i == 0? ($hour < 17? '18' : '24') : ($i - 1)%2 == 0? '12' : '24';
  215. $ret .= sprintf('<td align="center" class="weatherIcon" width=%d>%s</td>', $width, GDSIconIMGTag(ReadingsVal($d, "fc".$day."_weather".$timeLabel, "na"), $i==0 && $hour >= 17? 1 : undef));
  216. }
  217. $ret .= '</tr>';
  218. # condition text
  219. $ret .= sprintf('<tr><td align="center" class="weatherCondition">%s</td>', ReadingsVal($d, "c_weather", "?"));
  220. for(my $i=0; $i<$items; $i++) {
  221. my $day = int(($i + 1)/2);
  222. my $timeLabel = $i == 0? ($hour < 17? '18' : '24') : ($i - 1)%2 == 0? '12' : '24';
  223. $ret .= sprintf('<td align="center" class="weatherCondition">%s</td>', ReadingsVal($d, "fc".$day."_weather".$timeLabel, "?"));
  224. }
  225. $ret .= '</tr>';
  226. # temperature / min temperature
  227. $ret .= sprintf('<tr><td align="center" class="weatherValue">%s°C</td>', ReadingsVal($d, "c_temperature", "?"));
  228. for(my $i=0; $i<$items; $i++) {
  229. my $day = int(($i + 1)/2);
  230. my $timeLabel = $i == 0? ($hour < 17? '18' : '24') : ($i - 1)%2 == 0? '12' : '24';
  231. if (($i - 1)%2 == 0) {
  232. $ret .= sprintf('<td align="center" class="weatherMin">min %s°C</td>', ReadingsVal($d, "fc".$day."_tMinAir", "?"));
  233. } else {
  234. if ($i == 0 && $hour >= 17) {
  235. $ret .= sprintf('<td align="center" class="weatherValue">%s°C</td>', ReadingsVal($d, "fc".$day."_tAvgAir".$timeLabel, "?"));
  236. } else {
  237. $ret .= sprintf('<td align="center" class="weatherMax">max %s°C</td>', ReadingsVal($d, "fc".$day."_tMaxAir", "?"));
  238. }
  239. }
  240. }
  241. $ret .= '</tr>';
  242. # wind
  243. $ret .= sprintf('<tr><td align="center" class="weatherWind">%s km/h %s</td>', ReadingsVal($d, "c_windSpeed", "?"), ReadingsVal($d, "c_windDir", "?"));
  244. for(my $i=0; $i<$items; $i++) {
  245. my $day = int(($i + 1)/2);
  246. my $timeLabel = $i == 0? ($hour < 17? '18' : '24') : ($i - 1)%2 == 0? '12' : '24';
  247. $ret .= sprintf('<td align="center" class="weatherWind">%s</td>', ReadingsVal($d, "fc".$day."_windGust".$timeLabel, ""));
  248. }
  249. $ret .= "</tr></table>";
  250. return $ret;
  251. }
  252. sub GDSAsHtmlD($;$) {
  253. # create forecast in a horizontal or vertical HTML table
  254. # depending on the display orientation
  255. # @param: device name
  256. # @param: number of icons, optional, default 8
  257. my ($d,$i) = @_;
  258. if(defined($FW_ss) && $FW_ss) {
  259. GDSAsHtmlV($d,$i);
  260. } else {
  261. GDSAsHtmlH($d,$i);
  262. }
  263. }
  264. 1;
  265. =pod
  266. =begin html
  267. <a name="gdsUtils"></a>
  268. <h3>gdsUtils</h3>
  269. <ul>
  270. <li> This module provides three additional functions:<br/>
  271. <code>GDSAsHtmlV</code>, <code>GDSAsHtmlH</code> and <code>GDSAsHtmlD</code>. <br/>
  272. The first function returns the HTML code for a vertically arranged weather forecast. <br/>
  273. The second function returns the HTML code for a horizontally arranged weather forecast. <br/>
  274. The third function dynamically picks the orientation depending on whether a <br/>
  275. smallscreen style is set (vertical layout) or not (horizontal layout).<br/>
  276. The attributes gdsSetCond and gdsSetForecast must be configured for the functions to work.<br/>
  277. Each of these functions accepts an additional parameter to limit the number of icons to display (1...8). <br/>
  278. If the attribute gdsSetForecast is not configured this parameter should be set to 1.<br/>
  279. <br/>
  280. Example: <code>define MyForecastWeblink weblink htmlCode { GDSAsHtml("MyWeather") }</code> <br/>
  281. where "MyWeather" is the name of your GDS device.<br/>
  282. </li>
  283. </ul>
  284. =end html
  285. =cut