59_Weather.pm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. # $Id: 59_Weather.pm 12559 2016-11-13 08:54:54Z borisneubert $
  2. ##############################################################################
  3. #
  4. # 59_Weather.pm
  5. # Copyright by Dr. Boris Neubert
  6. # e-mail: omega at online dot de
  7. #
  8. # This file is part of fhem.
  9. #
  10. # Fhem is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # Fhem is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. package main;
  25. use strict;
  26. use warnings;
  27. use Time::HiRes qw(gettimeofday);
  28. use HttpUtils;
  29. use vars qw($FW_ss);
  30. my %pressure_trend_txt_en = ( 0 => "steady", 1 => "rising", 2 => "falling" );
  31. my %pressure_trend_txt_de = ( 0 => "gleichbleibend", 1 => "steigend", 2 => "fallend" );
  32. my %pressure_trend_txt_nl = ( 0 => "stabiel", 1 => "stijgend", 2 => "dalend" );
  33. my %pressure_trend_txt_fr = ( 0 => "stable", 1 => "croissant", 2 => "décroissant" );
  34. my %pressure_trend_txt_pl = ( 0 => "stabilne", 1 => "rośnie", 2 => "spada" );
  35. my %pressure_trend_sym = ( 0 => "=", 1 => "+", 2 => "-" );
  36. my @directions_txt_en = ('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW');
  37. my @directions_txt_de = ('N', 'NNO', 'NO', 'ONO', 'O', 'OSO', 'SO', 'SSO', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW');
  38. my @directions_txt_nl = ('N', 'NNO', 'NO', 'ONO', 'O', 'OZO', 'ZO', 'ZZO', 'Z', 'ZZW', 'ZW', 'WZW', 'W', 'WNW', 'NW', 'NNW');
  39. my @directions_txt_fr = ('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSO', 'SO', 'OSO', 'O', 'ONO', 'NO', 'NNO');
  40. my @directions_txt_pl = ('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW');
  41. my %wdays_txt_en = ('Mon' => 'Mon', 'Tue' => 'Tue', 'Wed'=> 'Wed', 'Thu' => 'Thu', 'Fri' => 'Fri', 'Sat' => 'Sat', 'Sun' => 'Sun');
  42. my %wdays_txt_de = ('Mon' => 'Mo', 'Tue' => 'Di', 'Wed'=> 'Mi', 'Thu' => 'Do', 'Fri' => 'Fr', 'Sat' => 'Sa', 'Sun' => 'So');
  43. my %wdays_txt_nl = ('Mon' => 'Maa', 'Tue' => 'Din', 'Wed'=> 'Woe', 'Thu' => 'Don', 'Fri' => 'Vri', 'Sat' => 'Zat', 'Sun' => 'Zon');
  44. my %wdays_txt_fr= ('Mon' => 'Lun', 'Tue' => 'Mar', 'Wed'=> 'Mer', 'Thu' => 'Jeu', 'Fri' => 'Ven', 'Sat' => 'Sam', 'Sun' => 'Dim');
  45. my %wdays_txt_pl = ('Mon' => 'Pon', 'Tue' => 'Wt', 'Wed'=> 'Śr', 'Thu' => 'Czw', 'Fri' => 'Pt', 'Sat' => 'Sob', 'Sun' => 'Nie');
  46. my @iconlist = (
  47. 'storm', 'storm', 'storm', 'thunderstorm', 'thunderstorm', 'rainsnow',
  48. 'sleet', 'snow', 'drizzle', 'drizzle', 'icy' ,'chance_of_rain',
  49. 'chance_of_rain', 'snowflurries', 'chance_of_snow', 'heavysnow', 'snow', 'sleet',
  50. 'sleet', 'dust', 'fog', 'haze', 'smoke', 'flurries',
  51. 'windy', 'icy', 'cloudy', 'mostlycloudy_night', 'mostlycloudy', 'partly_cloudy_night',
  52. 'partly_cloudy', 'sunny', 'sunny', 'mostly_clear_night', 'mostly_sunny', 'heavyrain',
  53. 'sunny', 'scatteredthunderstorms', 'scatteredthunderstorms', 'scatteredthunderstorms', 'scatteredshowers', 'heavysnow',
  54. 'chance_of_snow', 'heavysnow', 'partly_cloudy', 'heavyrain', 'chance_of_snow', 'scatteredshowers');
  55. ###################################
  56. sub Weather_DebugCodes($) {
  57. my ($lang)= @_;
  58. my @YahooCodes_i18n= YahooWeatherAPI_getYahooCodes($lang);
  59. Debug "Weather Code List, see http://developer.yahoo.com/weather/#codes";
  60. for(my $c= 0; $c<= 47; $c++) {
  61. Debug sprintf("%2d %30s %30s", $c, $iconlist[$c], $YahooCodes_i18n[$c]);
  62. }
  63. }
  64. #####################################
  65. sub Weather_Initialize($) {
  66. my ($hash) = @_;
  67. $hash->{DefFn} = "Weather_Define";
  68. $hash->{UndefFn} = "Weather_Undef";
  69. $hash->{GetFn} = "Weather_Get";
  70. $hash->{SetFn} = "Weather_Set";
  71. $hash->{AttrList}= "disable " . $readingFnAttributes;
  72. $hash->{NotifyFn}= "Weather_Notify";
  73. #Weather_DebugCodes('de');
  74. }
  75. ###################################
  76. sub degrees_to_direction($@) {
  77. my ($degrees,@directions_txt_i18n) = @_;
  78. my $mod = int((($degrees + 11.25) % 360) / 22.5);
  79. return $directions_txt_i18n[$mod];
  80. }
  81. ###################################
  82. sub Weather_RetrieveData($$) {
  83. my ($name, $blocking) = @_;
  84. my $hash = $defs{$name};
  85. # WOEID [WHERE-ON-EARTH-ID], go to http://weather.yahoo.com to find out
  86. my $location= $hash->{LOCATION};
  87. my $units= $hash->{UNITS};
  88. my %args= (
  89. woeid => $location,
  90. format => "json",
  91. blocking => $blocking,
  92. callbackFnRef => \&Weather_RetrieveDataFinished,
  93. hash => $hash,
  94. );
  95. # this needs to be finalized to use the APIOPTIONS
  96. my $maxage= $hash->{fhem}{allowCache} ? 600 : 0; # use cached data if allowed
  97. $hash->{fhem}{allowCache}= 1;
  98. YahooWeatherAPI_RetrieveDataWithCache($maxage, \%args);
  99. }
  100. sub Weather_ReturnWithError($$$$$) {
  101. my ($hash, $doTrigger, $err, $pubDate, $pubDateComment)= @_;
  102. my $name= $hash->{NAME};
  103. $hash->{fhem}{allowCache}= 0; # do not use cache on next try
  104. Log3 $hash, 3, "$name: $err";
  105. readingsBeginUpdate($hash);
  106. readingsBulkUpdate($hash, "lastError", $err);
  107. readingsBulkUpdate($hash, "pubDateComment", $pubDateComment) if(defined($pubDateComment));
  108. readingsBulkUpdate($hash, "pubDateRemote", $pubDate) if(defined($pubDate));
  109. readingsBulkUpdate($hash, "validity", "stale");
  110. readingsEndUpdate($hash, $doTrigger);
  111. my $next= 60; # $next= $hash->{INTERVAL};
  112. Weather_RearmTimer($hash, gettimeofday()+$next);
  113. return;
  114. }
  115. sub Weather_RetrieveDataFinished($$$) {
  116. my ($argsRef, $err, $response)= @_;
  117. my $hash= $argsRef->{hash};
  118. my $name= $hash->{NAME};
  119. my $doTrigger= $argsRef->{blocking} ? 0 : 1;
  120. # check for error from retrieving data
  121. return Weather_ReturnWithError($hash, $doTrigger, $err, undef, undef) if($err);
  122. # decode JSON data from Weather Channel
  123. my $data;
  124. ($err, $data)= YahooWeatherAPI_JSONReturnChannelData($response);
  125. return Weather_ReturnWithError($hash, $doTrigger, $err, undef, undef) if($err);
  126. # check if up-to-date
  127. my ($pubDateComment, $pubDate, $pubDateTs)= YahooWeatherAPI_pubDate($data);
  128. return Weather_ReturnWithError($hash, $doTrigger, $pubDateComment, $pubDate, $pubDateComment)
  129. unless(defined($pubDateTs));
  130. my $ts= defined($hash->{READINGS}{pubDateTs}) ? $hash->{READINGS}{pubDateTs}{VAL} : 0;
  131. return Weather_ReturnWithError($hash, $doTrigger, "stale data received", $pubDate, $pubDateComment)
  132. if($ts> $pubDateTs);
  133. #
  134. # from here on we assume that $data is complete and correct
  135. #
  136. my $lang= $hash->{LANG};
  137. my %wdays_txt_i18n;
  138. my @directions_txt_i18n;
  139. my %pressure_trend_txt_i18n;
  140. if($lang eq "de") {
  141. %wdays_txt_i18n= %wdays_txt_de;
  142. @directions_txt_i18n= @directions_txt_de;
  143. %pressure_trend_txt_i18n= %pressure_trend_txt_de;
  144. } elsif($lang eq "nl") {
  145. %wdays_txt_i18n= %wdays_txt_nl;
  146. @directions_txt_i18n= @directions_txt_nl;
  147. %pressure_trend_txt_i18n= %pressure_trend_txt_nl;
  148. } elsif($lang eq "fr") {
  149. %wdays_txt_i18n= %wdays_txt_fr;
  150. @directions_txt_i18n= @directions_txt_fr;
  151. %pressure_trend_txt_i18n= %pressure_trend_txt_fr;
  152. } elsif($lang eq "pl") {
  153. %wdays_txt_i18n= %wdays_txt_pl;
  154. @directions_txt_i18n= @directions_txt_pl;
  155. %pressure_trend_txt_i18n= %pressure_trend_txt_pl;
  156. } else {
  157. %wdays_txt_i18n= %wdays_txt_en;
  158. @directions_txt_i18n= @directions_txt_en;
  159. %pressure_trend_txt_i18n= %pressure_trend_txt_en;
  160. }
  161. my @YahooCodes_i18n= YahooWeatherAPI_getYahooCodes($lang);
  162. my $item= $data->{item};
  163. readingsBeginUpdate($hash);
  164. # delete some unused readings
  165. delete($hash->{READINGS}{temp_f}) if(defined($hash->{READINGS}{temp_f}));
  166. delete($hash->{READINGS}{unit_distance}) if(defined($hash->{READINGS}{unit_distance}));
  167. delete($hash->{READINGS}{unit_speed}) if(defined($hash->{READINGS}{unit_speed}));
  168. delete($hash->{READINGS}{unit_pressuree}) if(defined($hash->{READINGS}{unit_pressuree}));
  169. delete($hash->{READINGS}{unit_temperature}) if(defined($hash->{READINGS}{unit_temperature}));
  170. # convert to metric units as far as required
  171. my $isConverted= YahooWeatherAPI_ConvertChannelData($data);
  172. # housekeeping information
  173. readingsBulkUpdate($hash, "lastError", "");
  174. readingsBulkUpdate($hash, "pubDateComment", $pubDateComment);
  175. readingsBulkUpdate($hash, "pubDate", $pubDate);
  176. readingsBulkUpdate($hash, "pubDateRemote", $pubDate);
  177. readingsBulkUpdate($hash, "pubDateTs", $pubDateTs);
  178. readingsBulkUpdate($hash, "isConverted", $isConverted);
  179. readingsBulkUpdate($hash, "validity", "up-to-date");
  180. # description
  181. readingsBulkUpdate($hash, "description", $data->{description});
  182. # location
  183. readingsBulkUpdate($hash, "city", $data->{location}{city});
  184. readingsBulkUpdate($hash, "region", $data->{location}{region});
  185. readingsBulkUpdate($hash, "country", $data->{location}{country});
  186. readingsBulkUpdate($hash, "lat", $item->{lat});
  187. readingsBulkUpdate($hash, "long", $item->{long});
  188. # wind
  189. my $windspeed= int($data->{wind}{speed}+0.5);
  190. readingsBulkUpdate($hash, "wind", $windspeed);
  191. readingsBulkUpdate($hash, "wind_speed", $windspeed);
  192. readingsBulkUpdate($hash, "wind_chill", $data->{wind}{chill});
  193. my $winddir= $data->{wind}{direction};
  194. readingsBulkUpdate($hash, "wind_direction", $winddir);
  195. my $wdir= degrees_to_direction($winddir, @directions_txt_i18n);
  196. readingsBulkUpdate($hash, "wind_condition", "Wind: $wdir $windspeed km/h");
  197. # atmosphere
  198. my $humidity= $data->{atmosphere}{humidity};
  199. readingsBulkUpdate($hash, "humidity", $humidity);
  200. my $pressure= $data->{atmosphere}{pressure};
  201. readingsBulkUpdate($hash, "pressure", $pressure);
  202. readingsBulkUpdate($hash, "visibility", int($data->{atmosphere}{visibility}+0.5));
  203. my $pressure_trend= $data->{atmosphere}{rising};
  204. readingsBulkUpdate($hash, "pressure_trend", $pressure_trend);
  205. readingsBulkUpdate($hash, "pressure_trend_txt", $pressure_trend_txt_i18n{$pressure_trend});
  206. readingsBulkUpdate($hash, "pressure_trend_sym", $pressure_trend_sym{$pressure_trend});
  207. # condition
  208. my $date= $item->{condition}{date};
  209. readingsBulkUpdate($hash, "current_date_time", $date);
  210. readingsBulkUpdate($hash, "day_of_week", $wdays_txt_i18n{substr($date,0,3)});
  211. my $code= $item->{condition}{code};
  212. readingsBulkUpdate($hash, "code", $code);
  213. readingsBulkUpdate($hash, "condition", $YahooCodes_i18n[$code]);
  214. readingsBulkUpdate($hash, "icon", $iconlist[$code]);
  215. my $temp= $item->{condition}{temp};
  216. readingsBulkUpdate($hash, "temp_c", $temp);
  217. readingsBulkUpdate($hash, "temperature", $temp);
  218. # forecast
  219. my $forecast= $item->{forecast};
  220. my $i= 0;
  221. foreach my $fc (@{$forecast}) {
  222. $i++;
  223. my $f= "fc" . $i ."_";
  224. readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}});
  225. readingsBulkUpdate($hash, $f . "date", $fc->{date});
  226. readingsBulkUpdate($hash, $f . "low_c", $fc->{low});
  227. readingsBulkUpdate($hash, $f . "high_c", $fc->{high});
  228. my $fccode= $fc->{code};
  229. readingsBulkUpdate($hash, $f . "code", $fccode);
  230. readingsBulkUpdate($hash, $f . "condition", $YahooCodes_i18n[$fccode]);
  231. readingsBulkUpdate($hash, $f . "icon", $iconlist[$fccode]);
  232. }
  233. my $val= "T: $temp H: $humidity W: $windspeed P: $pressure";
  234. Log3 $hash, 4, "$name: $val";
  235. readingsBulkUpdate($hash, "state", $val);
  236. readingsEndUpdate($hash, $doTrigger);
  237. Weather_RearmTimer($hash, gettimeofday()+$hash->{INTERVAL});
  238. return;
  239. }
  240. ###################################
  241. sub Weather_GetUpdate($) {
  242. my ($hash) = @_;
  243. my $name = $hash->{NAME};
  244. if($attr{$name} && $attr{$name}{disable}) {
  245. Log3 $hash, 5, "Weather $name: retrieval of weather data is disabled by attribute.";
  246. readingsBeginUpdate($hash);
  247. readingsBulkUpdate($hash, "pubDateComment", "disabled by attribute");
  248. readingsBulkUpdate($hash, "validity", "stale");
  249. readingsEndUpdate($hash, 1);
  250. Weather_RearmTimer($hash, gettimeofday()+$hash->{INTERVAL});
  251. } else {
  252. Weather_RetrieveData($name, 0);
  253. }
  254. return 1;
  255. }
  256. ###################################
  257. sub Weather_Get($@) {
  258. my ($hash, @a) = @_;
  259. return "argument is missing" if(int(@a) != 2);
  260. my $reading= $a[1];
  261. my $value;
  262. if(defined($hash->{READINGS}{$reading})) {
  263. $value= $hash->{READINGS}{$reading}{VAL};
  264. } else {
  265. my $rt= "";
  266. if(defined($hash->{READINGS})) {
  267. $rt= join(" ", sort keys %{$hash->{READINGS}});
  268. }
  269. return "Unknown reading $reading, choose one of " . $rt;
  270. }
  271. return "$a[0] $reading => $value";
  272. }
  273. ###################################
  274. sub Weather_Set($@) {
  275. my ($hash, @a) = @_;
  276. my $cmd= $a[1];
  277. # usage check
  278. if((@a == 2) && ($a[1] eq "update")) {
  279. Weather_DisarmTimer($hash);
  280. Weather_GetUpdate($hash);
  281. return undef;
  282. } else {
  283. return "Unknown argument $cmd, choose one of update";
  284. }
  285. }
  286. ###################################
  287. sub Weather_RearmTimer($$) {
  288. my ($hash, $t) = @_;
  289. InternalTimer($t, "Weather_GetUpdate", $hash, 0) ;
  290. }
  291. sub Weather_DisarmTimer($) {
  292. my ($hash)= @_;
  293. RemoveInternalTimer($hash);
  294. }
  295. sub Weather_Notify($$) {
  296. my ($hash,$dev) = @_;
  297. my $name = $hash->{NAME};
  298. my $type = $hash->{TYPE};
  299. return if($dev->{NAME} ne "global");
  300. return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}}));
  301. # return if($attr{$name} && $attr{$name}{disable});
  302. # update weather after initialization or change of configuration
  303. # wait 10 to 29 seconds to avoid congestion due to concurrent activities
  304. Weather_DisarmTimer($hash);
  305. my $delay= 10+int(rand(20));
  306. #$delay= 3; # delay removed until further notice
  307. Log3 $hash, 5, "Weather $name: FHEM initialization or rereadcfg triggered update, delay $delay seconds.";
  308. Weather_RearmTimer($hash, gettimeofday()+$delay) ;
  309. return undef;
  310. }
  311. #####################################
  312. sub Weather_Define($$) {
  313. my ($hash, $def) = @_;
  314. # define <name> Weather <location> [interval]
  315. # define MyWeather Weather "Maintal,HE" 3600
  316. # define <name> Weather location=<location> [API=<API>] [interval=<interval>] [lang=<lang>]
  317. my $name;
  318. my $API="YahooWeatherAPI,transport:https,cachemaxage:600";
  319. my $location;
  320. my $interval = 3600;
  321. my $lang = "en";
  322. if($def =~ /=/) {
  323. my $usage= "syntax: define <name> Weather location=<location> [API=<API>] [lang=<lang>]";
  324. my ($arrayref, $hashref)= parseParams($def);
  325. my @a= @{$arrayref};
  326. my %h= %{$hashref};
  327. return $usage unless(scalar @a == 2);
  328. $name= $a[0];
  329. return $usage unless exists $h{location};
  330. $location= $h{location};
  331. $lang= $h{lang} if exists $h{lang};
  332. $interval= $h{interval} if exists $h{interval};
  333. $API= $h{API} if exists $h{API};
  334. } else {
  335. my @a = split("[ \t][ \t]*", $def);
  336. return "syntax: define <name> Weather <location> [interval [en|de|nl]]"
  337. if(int(@a) < 3 && int(@a) > 5);
  338. $name = $a[0];
  339. $location = $a[2];
  340. if(int(@a)>=4) { $interval= $a[3]; }
  341. if(int(@a)==5) { $lang= $a[4]; }
  342. }
  343. my ($api,$apioptions)= split(',', $API, 2);
  344. $apioptions= "" unless(defined($apioptions));
  345. eval {
  346. require "$api.pm";
  347. };
  348. return "$name: cannot load API $api: $@" if($@);
  349. $hash->{NOTIFYDEV} = "global";
  350. $hash->{STATE} = "Initialized";
  351. $hash->{fhem}{interfaces}= "temperature;humidity;wind";
  352. $hash->{LOCATION} = $location;
  353. $hash->{INTERVAL} = $interval;
  354. $hash->{LANG} = $lang;
  355. $hash->{API} = $api;
  356. $hash->{APIOPTIONS} = $apioptions;
  357. $hash->{UNITS} = "c"; # hardcoded to use degrees centigrade (Celsius)
  358. $hash->{READINGS}{current_date_time}{TIME}= TimeNow();
  359. $hash->{READINGS}{current_date_time}{VAL}= "none";
  360. $hash->{fhem}{allowCache}= 1;
  361. Weather_GetUpdate($hash) if($init_done);
  362. return undef;
  363. }
  364. #####################################
  365. sub Weather_Undef($$) {
  366. my ($hash, $arg) = @_;
  367. RemoveInternalTimer($hash);
  368. return undef;
  369. }
  370. #####################################
  371. # Icon Parameter
  372. use constant ICONHIGHT => 120;
  373. use constant ICONWIDTH => 175;
  374. use constant ICONSCALE => 0.5;
  375. #####################################
  376. sub
  377. WeatherIconIMGTag($) {
  378. my $width= int(ICONSCALE*ICONWIDTH);
  379. my ($icon)= @_;
  380. my $url= FW_IconURL("weather/$icon");
  381. my $style= " width=$width";
  382. return "<img src=\"$url\"$style alt=\"$icon\">";
  383. }
  384. #####################################
  385. sub
  386. WeatherAsHtmlV($;$)
  387. {
  388. my ($d,$items) = @_;
  389. $d = "<none>" if(!$d);
  390. $items = 10 if( !$items );
  391. return "$d is not a Weather instance<br>"
  392. if(!$defs{$d} || $defs{$d}{TYPE} ne "Weather");
  393. my $width= int(ICONSCALE*ICONWIDTH);
  394. my $ret = '<table class="weather">';
  395. $ret .= sprintf('<tr><td class="weatherIcon" width=%d>%s</td><td class="weatherValue">%s<br>%s°C %s%%<br>%s</td></tr>',
  396. $width,
  397. WeatherIconIMGTag(ReadingsVal($d, "icon", "")),
  398. ReadingsVal($d, "condition", ""),
  399. ReadingsVal($d, "temp_c", ""), ReadingsVal($d, "humidity", ""),
  400. ReadingsVal($d, "wind_condition", ""));
  401. for(my $i=1; $i<$items; $i++) {
  402. $ret .= sprintf('<tr><td class="weatherIcon" width=%d>%s</td><td class="weatherValue"><span class="weatherDay">%s: %s</span><br><span class="weatherMin">min %s°C</span> <span class="weatherMax">max %s°C</span></td></tr>',
  403. $width,
  404. WeatherIconIMGTag(ReadingsVal($d, "fc${i}_icon", "")),
  405. ReadingsVal($d, "fc${i}_day_of_week", ""),
  406. ReadingsVal($d, "fc${i}_condition", ""),
  407. ReadingsVal($d, "fc${i}_low_c", ""), ReadingsVal($d, "fc${i}_high_c", ""));
  408. }
  409. $ret .= "</table>";
  410. return $ret;
  411. }
  412. sub
  413. WeatherAsHtml($;$)
  414. {
  415. my ($d,$i) = @_;
  416. WeatherAsHtmlV($d,$i);
  417. }
  418. sub
  419. WeatherAsHtmlH($;$)
  420. {
  421. my ($d,$items) = @_;
  422. $d = "<none>" if(!$d);
  423. $items = 10 if( !$items );
  424. return "$d is not a Weather instance<br>"
  425. if(!$defs{$d} || $defs{$d}{TYPE} ne "Weather");
  426. my $width= int(ICONSCALE*ICONWIDTH);
  427. my $format= '<td><table border=1><tr><td class="weatherIcon" width=%d>%s</td></tr><tr><td class="weatherValue">%s</td></tr><tr><td class="weatherValue">%s°C %s%%</td></tr><tr><td class="weatherValue">%s</td></tr></table></td>';
  428. my $ret = '<table class="weather">';
  429. # icons
  430. $ret .= sprintf('<tr><td class="weatherIcon" width=%d>%s</td>', $width, WeatherIconIMGTag(ReadingsVal($d, "icon", "")));
  431. for(my $i=1; $i<$items; $i++) {
  432. $ret .= sprintf('<td class="weatherIcon" width=%d>%s</td>', $width, WeatherIconIMGTag(ReadingsVal($d, "fc${i}_icon", "")));
  433. }
  434. $ret .= '</tr>';
  435. # condition
  436. $ret .= sprintf('<tr><td class="weatherDay">%s</td>', ReadingsVal($d, "condition", ""));
  437. for(my $i=1; $i<$items; $i++) {
  438. $ret .= sprintf('<td class="weatherDay">%s: %s</td>', ReadingsVal($d, "fc${i}_day_of_week", ""),
  439. ReadingsVal($d, "fc${i}_condition", ""));
  440. }
  441. $ret .= '</tr>';
  442. # temp/hum | min
  443. $ret .= sprintf('<tr><td class="weatherMin">%s°C %s%%</td>', ReadingsVal($d, "temp_c", ""), ReadingsVal($d, "humidity", ""));
  444. for(my $i=1; $i<$items; $i++) {
  445. $ret .= sprintf('<td class="weatherMin">min %s°C</td>', ReadingsVal($d, "fc${i}_low_c", ""));
  446. }
  447. $ret .= '</tr>';
  448. # wind | max
  449. $ret .= sprintf('<tr><td class="weatherMax">%s</td>', ReadingsVal($d, "wind_condition", ""));
  450. for(my $i=1; $i<$items; $i++) {
  451. $ret .= sprintf('<td class="weatherMax">max %s°C</td>', ReadingsVal($d, "fc${i}_high_c", ""));
  452. }
  453. $ret .= "</tr></table>";
  454. return $ret;
  455. }
  456. sub
  457. WeatherAsHtmlD($;$)
  458. {
  459. my ($d,$i) = @_;
  460. if($FW_ss) {
  461. WeatherAsHtmlV($d,$i);
  462. } else {
  463. WeatherAsHtmlH($d,$i);
  464. }
  465. }
  466. #####################################
  467. 1;
  468. =pod
  469. =item device
  470. =item summary provides current weather condition and forecast (source: Yahoo Weather API)
  471. =item summary_DE stellt Wetterbericht und -vorhersage bereit (Quelle: Yahoo Weather API)
  472. =begin html
  473. <a name="Weather"></a>
  474. <h3>Weather</h3>
  475. <ul>
  476. You need the JSON perl module. Use <code>apt-get install libjson-perl</code> on Debian and derivatives.<br><br>
  477. <a name="Weatherdefine"></a>
  478. <b>Define</b>
  479. <ul>
  480. <code>define &lt;name&gt; Weather &lt;location&gt; [&lt;interval&gt; [&lt;language&gt;]]</code><br>
  481. <br>
  482. Defines a virtual device for weather forecasts.<br><br>
  483. A Weather device periodically gathers current and forecast weather conditions
  484. from the Yahoo Weather API.<br><br>
  485. The parameter <code>location</code> is the WOEID (WHERE-ON-EARTH-ID), go to
  486. <a href="http://weather.yahoo.com">http://weather.yahoo.com</a> to find it out for your location.<br><br>
  487. The optional parameter <code>interval</code> is the time between subsequent updates
  488. in seconds. It defaults to 3600 (1 hour).<br><br>
  489. The optional language parameter may be one of
  490. <code>de</code>,
  491. <code>en</code>,
  492. <code>pl</code>,
  493. <code>fr</code>,
  494. <code>nl</code>,
  495. It determines the natural language in which the forecast information appears.
  496. It defaults to <code>en</code>. If you want to set the language you also have to set the interval.<br><br>
  497. Examples:
  498. <pre>
  499. define MyWeather Weather 673513
  500. define Forecast Weather 673513 1800
  501. </pre>
  502. The module provides four additional functions <code>WeatherAsHtml</code>, <code>WeatherAsHtmlV</code>, <code>WeatherAsHtmlH</code> and
  503. <code>WeatherAsHtmlD</code>. The former two functions are identical: they return the HTML code for a
  504. vertically arranged weather forecast. The third function returns the HTML code for a horizontally arranged weather forecast. The
  505. latter function dynamically picks the orientation depending on wether a smallscreen style is set (vertical layout) or not (horizontal layout). Each version accepts an additional paramter to limit the numer of icons to display.<br><br>
  506. Example:
  507. <pre>
  508. define MyWeatherWeblink weblink htmlCode { WeatherAsHtmlH("MyWeather") }
  509. </pre>
  510. </ul>
  511. <br>
  512. <a name="Weatherset"></a>
  513. <b>Set </b>
  514. <ul>
  515. <code>set &lt;name&gt; update</code><br><br>
  516. Forces the retrieval of the weather data. The next automatic retrieval is scheduled to occur
  517. <code>interval</code> seconds later.<br><br>
  518. </ul>
  519. <br>
  520. <a name="Weatherget"></a>
  521. <b>Get</b>
  522. <ul>
  523. <code>get &lt;name&gt; &lt;reading&gt;</code><br><br>
  524. Valid readings and their meaning (? can be one of 1, 2, 3, 4, 5 and stands
  525. for today, tomorrow, etc.):<br>
  526. <table>
  527. <tr><td>city</td><td>name of town returned for location</td></tr>
  528. <tr><td>code</td><td>current condition code</td></tr>
  529. <tr><td>condition</td><td>current condition</td></tr>
  530. <tr><td>current_date_time</td><td>last update of forecast on server</td></tr>
  531. <tr><td>fc?_code</td><td>forecast condition code</td></tr>
  532. <tr><td>fc?_condition</td><td>forecast condition</td></tr>
  533. <tr><td>fc?_day_of_week</td><td>day of week for day +?</td></tr>
  534. <tr><td>fc?_high_c</td><td>forecasted daily high in degrees centigrade</td></tr>
  535. <tr><td>fc?_icon</td><td>forecast icon</td></tr>
  536. <tr><td>fc?_low_c</td><td>forecasted daily low in degrees centigrade</td></tr>
  537. <tr><td>humidity</td><td>current humidity in %</td></tr>
  538. <tr><td>icon</td><td>relative path for current icon</td></tr>
  539. <tr><td>pressure</td><td>air pressure in hPa</td></tr>
  540. <tr><td>pressure_trend</td><td>air pressure trend (0= steady, 1= rising, 2= falling)</td></tr>
  541. <tr><td>pressure_trend_txt</td><td>textual representation of air pressure trend</td></tr>
  542. <tr><td>pressure_trend_sym</td><td>symbolic representation of air pressure trend</td></tr>
  543. <tr><td>temperature</td><td>current temperature in degrees centigrade</td></tr>
  544. <tr><td>temp_c</td><td>current temperature in degrees centigrade</td></tr>
  545. <tr><td>temp_f</td><td>current temperature in degrees Fahrenheit</td></tr>
  546. <tr><td>visibility</td><td>visibility in km</td></tr>
  547. <tr><td>wind</td><td>wind speed in km/h</td></tr>
  548. <tr><td>wind_chill</td><td>wind chill in degrees centigrade</td></tr>
  549. <tr><td>wind_condition</td><td>wind direction and speed</td></tr>
  550. <tr><td>wind_direction</td><td>direction wind comes from in degrees (0 = north wind)</td></tr>
  551. <tr><td>wind_speed</td><td>same as wind</td></tr>
  552. </table>
  553. <br>
  554. The following readings help to identify whether a workaround has kicked in to avoid the retrieval of
  555. stale data from the remote server:
  556. <table>
  557. <tr><td>pubDate</td><td>publication time of forecast for current set of readings</td></tr>
  558. <tr><td>pubDateRemote</td><td>publication time of forecast as seen on remote server</td></tr>
  559. <tr><td>validity</td><td>stale, if publication time as seen on remote server is before that of current set of readings</td></tr>
  560. </table>
  561. </ul>
  562. <br>
  563. <a name="Weatherattr"></a>
  564. <b>Attributes</b>
  565. <ul>
  566. <li>disable: disables the retrieval of weather data - the timer runs according to schedule,
  567. though no data is requested from the API.</li>
  568. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  569. </ul>
  570. <br>
  571. </ul>
  572. =end html
  573. =begin html_DE
  574. <a name="Weather"></a>
  575. <h3>Weather</h3>
  576. <ul>
  577. Es wird das Perl-Modul JSON ben&ouml;tigt. Mit <code>apt-get install libjson-perl</code> kann es unter Debian und Derivaten installiert werden.<br><br>
  578. <a name="Weatherdefine"></a>
  579. <b>Define</b>
  580. <ul>
  581. <code>define &lt;name&gt; Weather &lt;location&gt; [&lt;interval&gt; [&lt;language&gt;]]</code><br>
  582. <br>
  583. Bezechnet ein virtuelles Gerät für Wettervorhersagen.<br><br>
  584. Eine solche virtuelle Wetterstation sammelt periodisch aktuelle und zukünftige Wetterdaten aus der Yahoo-Wetter-API.<br><br>
  585. Der Parameter <code>location</code> entspricht der sechsstelligen WOEID (WHERE-ON-EARTH-ID). Die WOEID für den eigenen Standort kann auf <a href="http://weather.yahoo.com">http://weather.yahoo.com</a> gefunden werden.<br><br>
  586. Der optionale Parameter <code>interval</code> gibt die Dauer in Sekunden zwischen den einzelnen Aktualisierungen der Wetterdaten an. Der Standardwert ist 3600 (1 Stunde). Wird kein Wert angegeben, gilt der Standardwert.<br><br>
  587. Der optionale Parameter für die möglichen Sprachen darf einen der folgende Werte annehmen: <code>de</code>, <code>en</code>, <code>pl</code>, <code>fr</code> oder <code>nl</code>. Er bezeichnet die natürliche Sprache, in der die Wetterinformationen dargestellt werden. Der Standardwert ist <code>en</code>. Wird für die Sprache kein Wert angegeben, gilt der Standardwert. Wird allerdings der Parameter für die Sprache gesetzt, muss ebenfalls ein Wert für das Abfrageintervall gesetzt werden.<br><br>
  588. Beispiele:
  589. <pre>
  590. define MyWeather Weather 673513
  591. define Forecast Weather 673513 1800
  592. </pre>
  593. Das Modul unterstützt zusätzlich vier verschiedene Funktionen <code>WeatherAsHtml</code>, <code>WeatherAsHtmlV</code>, <code>WeatherAsHtmlH</code> und <code>WeatherAsHtmlD</code>. Die ersten beiden Funktionen sind identisch: sie erzeugen den HTML-Code für eine vertikale Darstellung des Wetterberichtes. Die dritte Funktion liefert den HTML-Code für eine horizontale Darstellung des Wetterberichtes. Die letztgenannte Funktion wählt automatisch eine Ausrichtung, die abhängig davon ist, ob ein Smallcreen Style ausgewählt ist (vertikale Darstellung) oder nicht (horizontale Darstellung). Alle vier Funnktionen akzeptieren einen zusätzlichen optionalen Paramter um die Anzahl der darzustellenden Icons anzugeben.<br><br>
  594. Beispiel:
  595. <pre>
  596. define MyWeatherWeblink weblink htmlCode { WeatherAsHtmlH("MyWeather") }
  597. </pre>
  598. </ul>
  599. <br>
  600. <a name="Weatherset"></a>
  601. <b>Set </b>
  602. <ul>
  603. <code>set &lt;name&gt; update</code><br><br>
  604. Erzwingt eine Abfrage der Wetterdaten. Die darauffolgende Abfrage wird gemäß dem eingestellten Intervall <code>interval</code> Sekunden später durchgeführt.<br><br>
  605. </ul>
  606. <br>
  607. <a name="Weatherget"></a>
  608. <b>Get</b>
  609. <ul>
  610. <code>get &lt;name&gt; &lt;reading&gt;</code><br><br>
  611. Gültige ausgelesene Daten (readings) und ihre Bedeutung (das ? kann einen der Werte 1, 2, 3 , 4 oder 5 annehmen und steht für heute, morgen, übermorgen etc.):<br><br>
  612. <table>
  613. <tr><td>city</td><td>Name der Stadt, der aufgrund der WOEID übermittelt wird</td></tr>
  614. <tr><td>code</td><td>Code für die aktuellen Wetterverhältnisse</td></tr>
  615. <tr><td>condition</td><td>aktuelle Wetterverhältnisse</td></tr>
  616. <tr><td>current_date_time</td><td>Zeitstempel der letzten Aktualisierung der Wetterdaten vom Server</td></tr>
  617. <tr><td>fc?_code</td><td>Code für die vorhergesagten Wetterverhältnisse</td></tr>
  618. <tr><td>fc?_condition</td><td>vorhergesagte Wetterverhältnisse</td></tr>
  619. <tr><td>fc?_day_of_week</td><td>Wochentag des Tages, der durch ? dargestellt wird</td></tr>
  620. <tr><td>fc?_high_c</td><td>vorhergesagte maximale Tagestemperatur in Grad Celsius</td></tr>
  621. <tr><td>fc?_icon</td><td>Icon für Vorhersage</td></tr>
  622. <tr><td>fc?_low_c</td><td>vorhergesagte niedrigste Tagestemperatur in Grad Celsius</td></tr>
  623. <tr><td>humidity</td><td>gegenwärtige Luftfeuchtgkeit in %</td></tr>
  624. <tr><td>icon</td><td>relativer Pfad für das aktuelle Icon</td></tr>
  625. <tr><td>pressure</td><td>Luftdruck in hPa</td></tr>
  626. <tr><td>pressure_trend</td><td>Luftdrucktendenz (0= gleichbleibend, 1= steigend, 2= fallend)</td></tr>
  627. <tr><td>pressure_trend_txt</td><td>textliche Darstellung der Luftdrucktendenz</td></tr>
  628. <tr><td>pressure_trend_sym</td><td>symbolische Darstellung der Luftdrucktendenz</td></tr>
  629. <tr><td>temperature</td><td>gegenwärtige Temperatur in Grad Celsius</td></tr>
  630. <tr><td>temp_c</td><td>gegenwärtige Temperatur in Grad Celsius</td></tr>
  631. <tr><td>temp_f</td><td>gegenwärtige Temperatur in Grad Celsius</td></tr>
  632. <tr><td>visibility</td><td>Sichtweite in km</td></tr>
  633. <tr><td>wind</td><td>Windgeschwindigkeit in km/h</td></tr>
  634. <tr><td>wind_chill</td><td>gefühlte Temperatur in Grad Celsius</td></tr>
  635. <tr><td>wind_condition</td><td>Windrichtung und -geschwindigkeit</td></tr>
  636. <tr><td>wind_direction</td><td>Gradangabe der Windrichtung (0 = Nordwind)</td></tr>
  637. <tr><td>wind_speed</td><td>Windgeschwindigkeit in km/h (mit wind identisch)</td></tr>
  638. </table>
  639. <br>
  640. Die folgenden Daten helfen zu identifizieren, ob ein Workaround angeschlagen hat, der die Verwendung von
  641. veralteten Daten auf dem entfernten Server verhindert:
  642. <table>
  643. <tr><td>pubDate</td><td>Ver&ouml;ffentlichungszeitpunkt der Wettervorhersage in den aktuellen Daten (readings)</td></tr>
  644. <tr><td>pubDateRemote</td><td>Ver&ouml;ffentlichungszeitpunkt der Wettervorhersage auf dem entfernten Server</td></tr>
  645. <tr><td>validity</td><td>stale, wenn der Ver&ouml;ffentlichungszeitpunkt auf dem entfernten Server vor dem Zeitpunkt der aktuellen Daten (readings) liegt</td></tr>
  646. </table>
  647. </ul>
  648. <br>
  649. <a name="Weatherattr"></a>
  650. <b>Attribute</b>
  651. <ul>
  652. <li>disable: stellt die Abfrage der Wetterdaten ab - der Timer l&auml;ft gem&auml;&szlig Plan doch es werden keine Daten vom
  653. API angefordert.</li>
  654. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  655. </ul>
  656. <br>
  657. </ul>
  658. =end html_DE
  659. =cut