diff --git a/CHANGED b/CHANGED index 09f034071..6aa8c5b58 100644 --- a/CHANGED +++ b/CHANGED @@ -1,20 +1,21 @@ # 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. + - change: 59_Weather completely reworked - bugfix: 98_Verkehrsinfo.pm: internalTimer - change: 32_withings: improve Aura handling - - bugfix: 49_SSCam: fix blocking sscam operation if snap was executed with - arguments and aatribute snapEmailTxt is not set, + - bugfix: 49_SSCam: fix blocking sscam operation if snap was executed with + arguments and aatribute snapEmailTxt is not set, (Forum:#45671 #msg885475) - feature: 89_FULLY: Non blocking commands, expert mode - bugfix: 73_AutoShuttersControl: little fixes in commandref - feature: 49_SSCam: trigger several number of snapshots with alternative - text and send it alltogether by email + text and send it alltogether by email (execute "get versionNotes" for informations) - bugfix: 98_DOIFtools: fixed line break in DOIFtoolsLog - feature: 72_FB_CALLMONITOR: support for contact images and download of all available contact images from FritzBox phonebooks via TR-064 API. - (new attributes: contactImageDirectory, + (new attributes: contactImageDirectory, contactDefaultImage and contactImageViaTR064) - feature: 72_FB_CALLLIST: support for contact images (new attributes contactImageDirectory and contactImageDirectory) @@ -28,7 +29,7 @@ - bugfix: 88_HMCCU: Bug fixes - change: 10_MYSENSORS_DEVICE: make OTA feature available, change battery name convention, - support MySensors API 2.0 features like + support MySensors API 2.0 features like heartbeat and smartSleep - bugfix: 49_SSCam: fix problem with some older SMTP SSL module - bugfix: 73_AutoShuttersControl: fix typo in commandref @@ -47,22 +48,22 @@ - feature: 73_AutoShuttersControl: support for shading, fix bugs - feature: 37_harmony: use websockets if xmpp is not available fixes issues with firmware 4.15.206 - - bugfix: 97_TrashCal: changed to new format + - bugfix: 97_TrashCal: changed to new format - bugfix: 42_AptToDate: fix state reading after getDistribution - feature: 49_SSCam: tooltip hints in camera devices, commandref revised - feature: 36_WMBUS: new attribute ignoreUnknownDataBlocks - - feature: 49_SSCam: V8.0.0, new type "hls" of streaming devices, hls - playback now available for all common browsers, + - feature: 49_SSCam: V8.0.0, new type "hls" of streaming devices, hls + playback now available for all common browsers, tooltip hints in streaming devices, minor fixes - feature: 30_HUEBridge, 31_HUEDevice: added get startup command - change: 55_DWD_OpenData: maintenance release (forum #83097 msg #869647) - change: mqtt2.template: change name convention + cleanup - feature: 72_FB_CALLLIST: new readings/events "count-..." implemented to - count different call types - - change: 72_FB_CALLMONITOR: removed klicktel.de for reverse search, as + count different call types + - change: 72_FB_CALLMONITOR: removed klicktel.de for reverse search, as the service is not available anymore - bugfix: 49_SSCam: fix in autocreateCams and take snapshot - - feature: 98_MediaList.pm: new readings: sortby and status for better + - feature: 98_MediaList.pm: new readings: sortby and status for better integration in FTUI - change: 49_SSCamSTRM: V2.1.0, change get popupStream to set popupStream - feature: 49_SSCam: V7.7.0, autocreateCams command added to SVS device, @@ -73,7 +74,7 @@ - change: 20_FRM_IN: only update on change (Forum #81815) - bugfix: 73_AutoShuttersControl: fix absent Event and ModeDown absent bug - bugfix: 73_AutoShuttersControl: fix gone then absent bug - - feature: 49_SSCam: V7.6.0, The PTZ panel is completed by "Preset" and + - feature: 49_SSCam: V7.6.0, The PTZ panel is completed by "Preset" and "Patrol" dropdown lists (for PTZ cams only) - feature: 49_SSCamSTRM: click on content in stream device or snap gallery opens a popup window @@ -97,7 +98,7 @@ attention to a drive command - bugfix: 73_AutoShuttersControl: fix many bugs, change window event logic - change: 93_DbRep: fix warnings and minor changes - - change: 93_DbLog: V3.13.0, addLog considers DbLogInclude (Forum:#92854), + - change: 93_DbLog: V3.13.0, addLog considers DbLogInclude (Forum:#92854), new attributes traceFlag, traceLevel (only for support !) - feature: attrTemplate set command added - bugfix: 73_AutoShuttersControl: fix logical bugs, @@ -135,8 +136,8 @@ and new attributes with direct attribute help - feature: 74_Unifi: Added readings for wan_ip and results of speedtest - feature: 77_UWZ: new language support, french and hollandisch - - change: 93_DbRep: V8.6.0, devices and readings can be excluded by - EXCLUDE-option in attributes "reading/device", + - change: 93_DbRep: V8.6.0, devices and readings can be excluded by + EXCLUDE-option in attributes "reading/device", reduceLog use attributes "device/reading" - see commandref for further information - change: 73_AMADCommBridge 74_AMADDevice: change commandref, change version @@ -149,11 +150,11 @@ - bugfix: 74_XiaomiBTLESens: fix humidity code - feature: 98_Verkehrsinfo.pm: add attribut disable - feature: allowed: multi-allowed changes (Forum #92423) - - feature: 93_DbRep: V8.4.0, reduceLog from DbLog integrated into DbRep, - sqlCmd/dbValue with textField-long as default, both + - feature: 93_DbRep: V8.4.0, reduceLog from DbLog integrated into DbRep, + sqlCmd/dbValue with textField-long as default, both attributes timeOlderThan / timeDiffToNow can be set, get versionNotes changed to support en/de, new attribute - "countEntriesDetail", countEntries separate count of + "countEntriesDetail", countEntries separate count of every reading if attribute "countEntriesDetail" is set - change: 49_SSCam: COMPATIBILITY changed to 8.2.1 - bugfix: 74_XiaomiBTLESens : fix warning Illegal hexadecimal digit @@ -182,7 +183,7 @@ - bugfix: 10_IT fix usage with multible io devices - bugfix: 73_GardenaSmartBridge/Device fix typo oK to ok, change to package System - - change: 93_DbRep: V8.2.3, check availability of DbLog-device at + - change: 93_DbRep: V8.2.3, check availability of DbLog-device at definition time of DbRep-device - change: 93_Log2Syslog: send BSD-format changed, commandref revised - bugfix: 93_DbRep: fix don't get the real min timestamp in special cases @@ -196,12 +197,12 @@ - bugfix: 72_XiaomiDevice: set incorrect fan speed to 0 - bugfix: 38_netatmo: increased timeouts to counter server issues, logging - bugfix: 88_HMCCU: Detection of non standard addresses - - feature: 93_Log2Syslog: TCP-Server in Collector-mode / SSL-support and a - lot more, pls. see new get versionNotes command + - feature: 93_Log2Syslog: TCP-Server in Collector-mode / SSL-support and a + lot more, pls. see new get versionNotes command for further Information - feature: 93_DbRep: 8.1.0, new get versionNotes command - bugfix: 88_HMCCU: Temporary CCU programs are ignored now. - - change: 19_Revolt: allow adjustment of energy value, filtering of + - change: 19_Revolt: allow adjustment of energy value, filtering of implausible values - bugfix: 72_XiaomiDevice: better handling of definition w/ missing token - change: 32_withings: add in_bed for sleep trackers, ignore inactive users diff --git a/FHEM/59_Weather.pm b/FHEM/59_Weather.pm index 5e691ab5a..1bd17b036 100755 --- a/FHEM/59_Weather.pm +++ b/FHEM/59_Weather.pm @@ -31,6 +31,8 @@ use Time::HiRes qw(gettimeofday); use HttpUtils; use vars qw($FW_ss); +# use Data::Dumper; # for Debug only + my %pressure_trend_txt_en = ( 0 => "steady", 1 => "rising", 2 => "falling" ); my %pressure_trend_txt_de = ( 0 => "gleichbleibend", 1 => "steigend", 2 => "fallend" ); my %pressure_trend_txt_nl = ( 0 => "stabiel", 1 => "stijgend", 2 => "dalend" ); @@ -136,7 +138,7 @@ sub Weather_Initialize($) { $hash->{UndefFn} = "Weather_Undef"; $hash->{GetFn} = "Weather_Get"; $hash->{SetFn} = "Weather_Set"; - $hash->{AttrList}= "disable " . $readingFnAttributes; + $hash->{AttrList}= "disable:0,1 " . $readingFnAttributes; $hash->{NotifyFn}= "Weather_Notify"; #Weather_DebugCodes('de'); @@ -150,43 +152,19 @@ sub degrees_to_direction($@) { return $directions_txt_i18n[$mod]; } -################################### -sub Weather_RetrieveData($$) { - my ($name, $blocking) = @_; - my $hash = $defs{$name}; - # WOEID [WHERE-ON-EARTH-ID], go to http://weather.yahoo.com to find out - my $location= $hash->{LOCATION}; - my $units= $hash->{UNITS}; - - my %args= ( - woeid => $location, - format => "json", - blocking => $blocking, - callbackFnRef => \&Weather_RetrieveDataFinished, - hash => $hash, - ); - - # this needs to be finalized to use the APIOPTIONS - my $maxage= $hash->{fhem}{allowCache} ? 600 : 0; # use cached data if allowed - $hash->{fhem}{allowCache}= 1; - YahooWeatherAPI_RetrieveDataWithCache($maxage, \%args); -} - - -sub Weather_ReturnWithError($$$$$) { - my ($hash, $doTrigger, $err, $pubDate, $pubDateComment)= @_; +sub Weather_ReturnWithError($$) { + my ($hash, $responseRef)= @_; my $name= $hash->{NAME}; - $hash->{fhem}{allowCache}= 0; # do not use cache on next try - - Log3 $hash, 3, "$name: $err"; readingsBeginUpdate($hash); - readingsBulkUpdate($hash, "lastError", $err); - readingsBulkUpdate($hash, "pubDateComment", $pubDateComment) if(defined($pubDateComment)); - readingsBulkUpdate($hash, "pubDateRemote", $pubDate) if(defined($pubDate)); - readingsBulkUpdate($hash, "validity", "stale"); - readingsEndUpdate($hash, $doTrigger); + readingsBulkUpdate($hash, 'lastError', $responseRef->{status}); + + foreach my $r (keys %{$responseRef} ) { + readingsBulkUpdate($hash, $r, $responseRef->{$r}) if ( ref($responseRef->{$r}) ne 'HASH' ); + } + readingsBulkUpdate($hash, 'state', 'API Maintainer: ' . $responseRef->{apiMaintainer} . ' ErrorMsg: ' . $responseRef->{status}); + readingsEndUpdate($hash, 1); my $next= 60; # $next= $hash->{INTERVAL}; Weather_RearmTimer($hash, gettimeofday()+$next); @@ -194,126 +172,129 @@ sub Weather_ReturnWithError($$$$$) { return; } -sub Weather_RetrieveDataFinished($$$) { +sub Weather_RetrieveCallbackFn($) { - my ($argsRef, $err, $response)= @_; + my $name = shift; + my $hash = $defs{$name}; + my $responseRef = $hash->{fhem}->{api}->getWeather; - my $hash= $argsRef->{hash}; - my $name= $hash->{NAME}; - my $doTrigger= $argsRef->{blocking} ? 0 : 1; - - # check for error from retrieving data - return Weather_ReturnWithError($hash, $doTrigger, $err, undef, undef) if($err); - - # decode JSON data from Weather Channel - my $data; - ($err, $data)= YahooWeatherAPI_JSONReturnChannelData($response); - return Weather_ReturnWithError($hash, $doTrigger, $err, undef, undef) if($err); - - # check if up-to-date - my ($pubDateComment, $pubDate, $pubDateTs)= YahooWeatherAPI_pubDate($data); - return Weather_ReturnWithError($hash, $doTrigger, $pubDateComment, $pubDate, $pubDateComment) - unless(defined($pubDateTs)); - my $ts= defined($hash->{READINGS}{pubDateTs}) ? $hash->{READINGS}{pubDateTs}{VAL} : 0; - return Weather_ReturnWithError($hash, $doTrigger, "stale data received", $pubDate, $pubDateComment) - if($ts> $pubDateTs); - - - # - # from here on we assume that $data is complete and correct - # - my $lang= $hash->{LANG}; - - my @YahooCodes_i18n= YahooWeatherAPI_getYahooCodes($lang); - - my $item= $data->{item}; + if ( $responseRef->{status} eq 'ok' ) { + Weather_WriteReadings($hash,$responseRef); + } + else { + Weather_ReturnWithError($hash,$responseRef); + } +} +sub Weather_WriteReadings($$) { + my ($hash,$dataRef) = @_; + my $name = $hash->{NAME}; readingsBeginUpdate($hash); # delete some unused readings - delete($hash->{READINGS}{temp_f}) if(defined($hash->{READINGS}{temp_f})); - delete($hash->{READINGS}{unit_distance}) if(defined($hash->{READINGS}{unit_distance})); - delete($hash->{READINGS}{unit_speed}) if(defined($hash->{READINGS}{unit_speed})); - delete($hash->{READINGS}{unit_pressuree}) if(defined($hash->{READINGS}{unit_pressuree})); - delete($hash->{READINGS}{unit_temperature}) if(defined($hash->{READINGS}{unit_temperature})); - - # convert to metric units as far as required - my $isConverted= YahooWeatherAPI_ConvertChannelData($data); + delete($hash->{READINGS}->{temp_f}) if(defined($hash->{READINGS}->{temp_f})); + delete($hash->{READINGS}->{unit_distance}) if(defined($hash->{READINGS}->{unit_distance})); + delete($hash->{READINGS}->{unit_speed}) if(defined($hash->{READINGS}->{unit_speed})); + delete($hash->{READINGS}->{unit_pressuree}) if(defined($hash->{READINGS}->{unit_pressuree})); + delete($hash->{READINGS}->{unit_temperature}) if(defined($hash->{READINGS}->{unit_temperature})); # housekeeping information - readingsBulkUpdate($hash, "lastError", ""); - readingsBulkUpdate($hash, "pubDateComment", $pubDateComment); - readingsBulkUpdate($hash, "pubDate", $pubDate); - readingsBulkUpdate($hash, "pubDateRemote", $pubDate); - readingsBulkUpdate($hash, "pubDateTs", $pubDateTs); - readingsBulkUpdate($hash, "isConverted", $isConverted); - readingsBulkUpdate($hash, "validity", "up-to-date"); - - # description - readingsBulkUpdate($hash, "description", $data->{description}); - - # location - readingsBulkUpdate($hash, "city", $data->{location}{city}); - readingsBulkUpdate($hash, "region", $data->{location}{region}); - readingsBulkUpdate($hash, "country", $data->{location}{country}); - readingsBulkUpdate($hash, "lat", $item->{lat}); - readingsBulkUpdate($hash, "long", $item->{long}); - - # wind - my $windspeed= int($data->{wind}{speed}+0.5); - readingsBulkUpdate($hash, "wind", $windspeed); - readingsBulkUpdate($hash, "wind_speed", $windspeed); - readingsBulkUpdate($hash, "wind_chill", $data->{wind}{chill}); - my $winddir= $data->{wind}{direction}; - readingsBulkUpdate($hash, "wind_direction", $winddir); - my $wdir= degrees_to_direction($winddir, @directions_txt_i18n); - readingsBulkUpdate($hash, "wind_condition", "Wind: $wdir $windspeed km/h"); - - # atmosphere - my $humidity= $data->{atmosphere}{humidity}; - readingsBulkUpdate($hash, "humidity", $humidity); - my $pressure= $data->{atmosphere}{pressure}; - readingsBulkUpdate($hash, "pressure", $pressure); - readingsBulkUpdate($hash, "visibility", int($data->{atmosphere}{visibility}+0.5)); - my $pressure_trend= $data->{atmosphere}{rising}; - readingsBulkUpdate($hash, "pressure_trend", $pressure_trend); - readingsBulkUpdate($hash, "pressure_trend_txt", $pressure_trend_txt_i18n{$pressure_trend}); - readingsBulkUpdate($hash, "pressure_trend_sym", $pressure_trend_sym{$pressure_trend}); - - # condition - my $date= $item->{condition}{date}; - readingsBulkUpdate($hash, "current_date_time", $date); - readingsBulkUpdate($hash, "day_of_week", $wdays_txt_i18n{substr($date,0,3)}); - my $code= $item->{condition}{code}; - readingsBulkUpdate($hash, "code", $code); - readingsBulkUpdate($hash, "condition", $YahooCodes_i18n[$code]); - readingsBulkUpdate($hash, "icon", $iconlist[$code]); - my $temp= $item->{condition}{temp}; - readingsBulkUpdate($hash, "temp_c", $temp); - readingsBulkUpdate($hash, "temperature", $temp); - - # forecast - my $forecast= $item->{forecast}; - my $i= 0; - foreach my $fc (@{$forecast}) { - $i++; - my $f= "fc" . $i ."_"; - readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}}); - readingsBulkUpdate($hash, $f . "date", $fc->{date}); - readingsBulkUpdate($hash, $f . "low_c", $fc->{low}); - readingsBulkUpdate($hash, $f . "high_c", $fc->{high}); - my $fccode= $fc->{code}; - readingsBulkUpdate($hash, $f . "code", $fccode); - readingsBulkUpdate($hash, $f . "condition", $YahooCodes_i18n[$fccode]); - readingsBulkUpdate($hash, $f . "icon", $iconlist[$fccode]); + readingsBulkUpdate($hash, 'lastError', ''); + foreach my $r (keys %{$dataRef} ) { + readingsBulkUpdate($hash, $r, $dataRef->{$r}) + if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' ); + readingsBulkUpdate($hash, '.license', $dataRef->{license}->{text}); } - #my $val= "T:$temp°C " . substr($status_items_txt_i18n{1}, 0, 1) .":$humidity% " . substr($status_items_txt_i18n{0}, 0, 1) . ":$windspeed km/h P:$pressure mbar"; - my $val= "T: $temp H: $humidity W: $windspeed P: $pressure"; - Log3 $hash, 4, "$name: $val"; - readingsBulkUpdate($hash, "state", $val); + readingsBulkUpdate($hash, "validity", "up-to-date"); - readingsEndUpdate($hash, $doTrigger); + + ### current + if ( defined($dataRef->{current}) and ref( $dataRef->{current} ) eq 'HASH' ) { + while( my ($r,$v) = each %{$dataRef->{current}} ) { + readingsBulkUpdate($hash, $r, $v) + if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' ); + } + + readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}->{code}]); + if ( defined($dataRef->{current}->{wind_direction}) + and $dataRef->{current}->{wind_direction} + and defined($dataRef->{current}->{wind_speed}) + and $dataRef->{current}->{wind_speed} + ) + { + my $wdir= degrees_to_direction($dataRef->{current}->{wind_direction}, @directions_txt_i18n); + readingsBulkUpdate($hash, 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{current}->{wind_speed} . ' km/h'); + } + } + + ### forecast + if ( ref( $dataRef->{forecast} ) eq 'HASH' ) { + ## hourly + if ( defined($dataRef->{forecast}->{hourly}) + and ref( $dataRef->{forecast}->{hourly} ) eq 'ARRAY' + and scalar( @{ $dataRef->{forecast}->{hourly} } ) > 0 ) + { + my $i= 0; + foreach my $fc (@{$dataRef->{forecast}->{hourly}}) { + $i++; + my $f= "hfc" . $i ."_"; + + while( my ($r,$v) = each %{$fc} ) { + readingsBulkUpdate($hash, $f.$r, $v) + if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' ); + } + readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{hourly}[$i-1]{code}]); + + if ( defined($dataRef->{forecast}->{hourly}[$i-1]{wind_direction}) + and $dataRef->{forecast}->{hourly}[$i-1]{wind_direction} + and defined($dataRef->{forecast}->{hourly}[$i-1]{wind_speed}) + and $dataRef->{forecast}->{hourly}[$i-1]{wind_speed} + ) + { + my $wdir= degrees_to_direction($dataRef->{forecast}->{hourly}[$i-1]{wind_direction}, @directions_txt_i18n); + readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{hourly}[$i-1]{wind_speed} . ' km/h'); + } + } + } + + ## daily + if ( defined($dataRef->{forecast}->{daily}) and ref( $dataRef->{forecast}->{daily} ) eq 'ARRAY' + and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 ) + { + my $i= 0; + foreach my $fc (@{$dataRef->{forecast}->{daily}}) { + $i++; + my $f= "fc" . $i ."_"; + + while( my ($r,$v) = each %{$fc} ) { + readingsBulkUpdate($hash, $f.$r, $v) + if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' ); + } + readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{daily}[$i-1]{code}]); + + if ( defined($dataRef->{forecast}->{daily}[$i-1]{wind_direction}) + and $dataRef->{forecast}->{daily}[$i-1]{wind_direction} + and defined($dataRef->{forecast}->{daily}[$i-1]{wind_speed}) + and $dataRef->{forecast}->{daily}[$i-1]{wind_speed} + ) + { + my $wdir= degrees_to_direction($dataRef->{forecast}->{daily}[$i-1]{wind_direction}, @directions_txt_i18n); + readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{daily}[$i-1]{wind_speed} . ' km/h'); + } + } + } + } + + my $val= 'T:' . $dataRef->{current}->{temperature} . '°C' + .' ' . substr($status_items_txt_i18n{1}, 0, 1) . ':' . $dataRef->{current}->{humidity} . '%' + .' ' . substr($status_items_txt_i18n{0}, 0, 1) . ':' . $dataRef->{current}->{wind} . 'km/h' + .' P:' . $dataRef->{current}->{pressure} . 'mbar'; + + Log3 $hash, 4, "$name: $val"; + readingsBulkUpdate($hash, 'state', $val); + + readingsEndUpdate($hash, 1); Weather_RearmTimer($hash, gettimeofday()+$hash->{INTERVAL}); return; @@ -326,7 +307,7 @@ sub Weather_GetUpdate($) { my ($hash) = @_; my $name = $hash->{NAME}; - if($attr{$name} && $attr{$name}{disable}) { + if($attr{$name} && $attr{$name}->{disable}) { Log3 $hash, 5, "Weather $name: retrieval of weather data is disabled by attribute."; readingsBeginUpdate($hash); readingsBulkUpdate($hash, "pubDateComment", "disabled by attribute"); @@ -334,7 +315,8 @@ sub Weather_GetUpdate($) { readingsEndUpdate($hash, 1); Weather_RearmTimer($hash, gettimeofday()+$hash->{INTERVAL}); } else { - Weather_RetrieveData($name, 0); +# Weather_RetrieveData($name, 0); + $hash->{fhem}->{api}->setRetrieveData; } return 1; @@ -350,13 +332,14 @@ sub Weather_Get($@) { my $reading= $a[1]; my $value; - if(defined($hash->{READINGS}{$reading})) { - $value= $hash->{READINGS}{$reading}{VAL}; + if(defined($hash->{READINGS}->{$reading})) { + $value = $hash->{READINGS}->{$reading}->{VAL}; } else { - my $rt= ""; + my $rt = ''; if(defined($hash->{READINGS})) { - $rt= join(" ", sort keys %{$hash->{READINGS}}); + $rt = join(":noArg ", sort keys %{$hash->{READINGS}}); } + return "Unknown reading $reading, choose one of " . $rt; } @@ -375,7 +358,7 @@ sub Weather_Set($@) { Weather_GetUpdate($hash); return undef; } else { - return "Unknown argument $cmd, choose one of update"; + return "Unknown argument $cmd, choose one of update:noArg"; } } @@ -401,7 +384,7 @@ sub Weather_Notify($$) { return if($dev->{NAME} ne "global"); return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}})); - # return if($attr{$name} && $attr{$name}{disable}); + # return if($attr{$name} && $attr{$name}->{disable}); # update weather after initialization or change of configuration # wait 10 to 29 seconds to avoid congestion due to concurrent activities @@ -421,47 +404,29 @@ sub Weather_Define($$) { my ($hash, $def) = @_; - # define Weather [interval] - # define MyWeather Weather "Maintal,HE" 3600 + my $usage= "syntax: define Weather [API=] [apikey=] [location=] [interval=] [lang=]"; - # define Weather location= [API=] [interval=] [lang=] - - my $name; - my $API="YahooWeatherAPI,transport:https,cachemaxage:600"; - my $location; + # defaults + my $API="DarkSkyAPI,cachemaxage:600"; my $interval = 3600; - my $lang = "en"; - if($def =~ /=/) { + # parse parameters + my ($arrayref, $hashref)= parseParams($def); + my @a= @{$arrayref}; + my %h= %{$hashref}; - my $usage= "syntax: define Weather location= [API=] [lang=]"; + # check minimum syntax + return $usage unless(scalar @a == 2); + my $name= $a[0]; - my ($arrayref, $hashref)= parseParams($def); - my @a= @{$arrayref}; - my %h= %{$hashref}; - return $usage unless(scalar @a == 2); - $name= $a[0]; - - return $usage unless exists $h{location}; - $location= $h{location}; - $lang= $h{lang} if exists $h{lang}; - $interval= $h{interval} if exists $h{interval}; - $API= $h{API} if exists $h{API}; - - } else { - my @a = split("[ \t][ \t]*", $def); - - return "syntax: define Weather [interval [en|de|nl|fr|pl|it]]" - if(int(@a) < 3 && int(@a) > 5); - - $name = $a[0]; - $location = $a[2]; - if(int(@a)>=4) { $interval= $a[3]; } - if(int(@a)==5) { $lang= $a[4]; } - - } + my $location= $h{location} if exists $h{location}; + my $apikey = $h{apikey} if exists $h{apikey}; + my $lang= $h{lang} if exists $h{lang}; + $interval= $h{interval} if exists $h{interval}; + $API = $h{API} if exists $h{API}; + # evaluate API options my ($api,$apioptions)= split(',', $API, 2); $apioptions= "" unless(defined($apioptions)); eval { @@ -470,21 +435,24 @@ sub Weather_Define($$) { return "$name: cannot load API $api: $@" if($@); $hash->{NOTIFYDEV} = "global"; - $hash->{STATE} = "Initialized"; - $hash->{fhem}{interfaces}= "temperature;humidity;wind"; - - $hash->{LOCATION} = $location; + $hash->{fhem}->{interfaces}= "temperature;humidity;wind"; + $hash->{LOCATION} = ( (defined($location) and $location) ? $location : AttrVal( 'global', 'latitude', 'error' ).','.AttrVal( 'global', 'longitude', 'error' ) ); $hash->{INTERVAL} = $interval; - $hash->{LANG} = $lang; + $hash->{LANG} = ( (defined($lang) and $lang) ? $lang : lc(AttrVal('global','language','de')) ); $hash->{API} = $api; + $hash->{APIKEY} = $apikey; $hash->{APIOPTIONS} = $apioptions; - $hash->{UNITS} = "c"; # hardcoded to use degrees centigrade (Celsius) - $hash->{READINGS}{current_date_time}{TIME}= TimeNow(); - $hash->{READINGS}{current_date_time}{VAL}= "none"; + #$hash->{UNITS} = "c"; # hardcoded to use degrees centigrade (Celsius) + $hash->{READINGS}->{current_date_time}->{TIME}= TimeNow(); + $hash->{READINGS}->{current_date_time}->{VAL}= "none"; - $hash->{fhem}{allowCache}= 1; + $hash->{fhem}->{allowCache}= 1; - Weather_LanguageInitialize($lang); + readingsSingleUpdate($hash,'state','Initialized',1); + Weather_LanguageInitialize($hash->{LANG}); + + my $apistring = $api . '::Weather'; + $hash->{fhem}->{api} = $apistring->new( { devName => $hash->{NAME}, apikey => $hash->{APIKEY}, location => $hash->{LOCATION}, apioptions => $hash->{APIOPTIONS}, language => $hash->{LANG} } ); Weather_GetUpdate($hash) if($init_done); @@ -510,8 +478,7 @@ use constant ICONSCALE => 0.5; ##################################### -sub -WeatherIconIMGTag($) { +sub WeatherIconIMGTag($) { my $width= int(ICONSCALE*ICONWIDTH); my ($icon)= @_; @@ -523,16 +490,16 @@ WeatherIconIMGTag($) { ##################################### -sub -WeatherAsHtmlV($;$) +sub WeatherAsHtmlV($;$) { my ($d,$items) = @_; $d = "" if(!$d); - $items = 10 if( !$items ); + $items = 9 if( !$items ); return "$d is not a Weather instance
" - if(!$defs{$d} || $defs{$d}{TYPE} ne "Weather"); + if(!$defs{$d} || $defs{$d}->{TYPE} ne "Weather"); + my $h = $defs{$d}; my $width= int(ICONSCALE*ICONWIDTH); my $ret = ''; @@ -543,36 +510,36 @@ WeatherAsHtmlV($;$) ReadingsVal($d, "temp_c", ""), ReadingsVal($d, "humidity", ""), ReadingsVal($d, "wind_condition", "")); + my $fc = ( (defined($h->{READINGS}->{fc1_day_of_week}) and $h->{READINGS}->{fc1_day_of_week}) ? 'fc' : 'hfc' ); for(my $i=1; $i<$items; $i++) { $ret .= sprintf('', $width, - WeatherIconIMGTag(ReadingsVal($d, "fc${i}_icon", "")), - ReadingsVal($d, "fc${i}_day_of_week", ""), - ReadingsVal($d, "fc${i}_condition", ""), - ReadingsVal($d, "fc${i}_low_c", ""), ReadingsVal($d, "fc${i}_high_c", "")); + WeatherIconIMGTag(ReadingsVal($d, "${fc}${i}_icon", "")), + ReadingsVal($d, "${fc}${i}_day_of_week", ""), + ReadingsVal($d, "${fc}${i}_condition", ""), + ReadingsVal($d, "${fc}${i}_low_c", ""), ReadingsVal($d, "${fc}${i}_high_c", "")); } $ret .= "
%s%s: %s
min %s°C max %s°C
"; return $ret; } -sub -WeatherAsHtml($;$) +sub WeatherAsHtml($;$) { my ($d,$i) = @_; WeatherAsHtmlV($d,$i); } -sub -WeatherAsHtmlH($;$) +sub WeatherAsHtmlH($;$) { my ($d,$items) = @_; $d = "" if(!$d); - $items = 10 if( !$items ); + $items = 9 if( !$items ); return "$d is not a Weather instance
" - if(!$defs{$d} || $defs{$d}{TYPE} ne "Weather"); + if(!$defs{$d} || $defs{$d}->{TYPE} ne "Weather"); + my $h = $defs{$d}; my $width= int(ICONSCALE*ICONWIDTH); @@ -580,41 +547,41 @@ WeatherAsHtmlH($;$) my $format= '
%s
%s
%s°C %s%%
%s
'; my $ret = ''; + my $fc = ( (defined($h->{READINGS}->{fc1_day_of_week}) and $h->{READINGS}->{fc1_day_of_week}) ? 'fc' : 'hfc' ); # icons $ret .= sprintf('', $width, WeatherIconIMGTag(ReadingsVal($d, "icon", ""))); for(my $i=1; $i<$items; $i++) { - $ret .= sprintf('', $width, WeatherIconIMGTag(ReadingsVal($d, "fc${i}_icon", ""))); + $ret .= sprintf('', $width, WeatherIconIMGTag(ReadingsVal($d, "${fc}${i}_icon", ""))); } $ret .= ''; # condition $ret .= sprintf('', ReadingsVal($d, "condition", "")); for(my $i=1; $i<$items; $i++) { - $ret .= sprintf('', ReadingsVal($d, "fc${i}_day_of_week", ""), - ReadingsVal($d, "fc${i}_condition", "")); + $ret .= sprintf('', ReadingsVal($d, "${fc}${i}_day_of_week", ""), + ReadingsVal($d, "${fc}${i}_condition", "")); } $ret .= ''; # temp/hum | min $ret .= sprintf('', ReadingsVal($d, "temp_c", ""), ReadingsVal($d, "humidity", "")); for(my $i=1; $i<$items; $i++) { - $ret .= sprintf('', ReadingsVal($d, "fc${i}_low_c", "")); + $ret .= sprintf('', ReadingsVal($d, "${fc}${i}_low_c", "")); } $ret .= ''; # wind | max $ret .= sprintf('', ReadingsVal($d, "wind_condition", "")); for(my $i=1; $i<$items; $i++) { - $ret .= sprintf('', ReadingsVal($d, "fc${i}_high_c", "")); + $ret .= sprintf('', ReadingsVal($d, "${fc}${i}_high_c", "")); } $ret .= "
%s%s%s
%s%s: %s%s: %s
%s°C %s%%min %s°Cmin %s°C
%smax %s°Cmax %s°C
"; return $ret; } -sub -WeatherAsHtmlD($;$) +sub WeatherAsHtmlD($;$) { my ($d,$i) = @_; if($FW_ss) { @@ -631,52 +598,95 @@ WeatherAsHtmlD($;$) =pod =item device -=item summary provides current weather condition and forecast (source: Yahoo Weather API) -=item summary_DE stellt Wetterbericht und -vorhersage bereit (Quelle: Yahoo Weather API) +=item summary provides current weather condition and forecast +=item summary_DE stellt Wetterbericht und -vorhersage bereit =begin html

Weather

    - You need the JSON perl module. Use apt-get install libjson-perl on Debian and derivatives.

    + Note: you need the JSON perl module. Use apt-get install + libjson-perl on Debian and derivatives.

    + + The Weather module works with various weather APIs: +

    +
    + Such a virtual Weather device periodically gathers current and forecast + weather conditions from the chosen weather API.

    - Define -
      - define <name> Weather <location> [<interval> [<language>]]
      -
      - Defines a virtual device for weather forecasts.

      + Define

      +
        + define <name> Weather [API=<API>[,<apiotions>]] [apikey=<apikey>] + [location=<location>] [interval=<interval>] [lang=<lang>]

        - A Weather device periodically gathers current and forecast weather conditions - from the Yahoo Weather API.

        - The parameter location is the WOEID (WHERE-ON-EARTH-ID), go to - http://weather.yahoo.com to find it out for your location.

        + The parameters have the following meanings:
        - The optional parameter interval is the time between subsequent updates - in seconds. It defaults to 3600 (1 hour).

        + + + + + + + + +

        - The optional language parameter may be one of - de, - en, - pl, - fr, - nl, - it, - - It determines the natural language in which the forecast information appears. - It defaults to en. If you want to set the language you also have to set the interval.

        + A very simple definition is:

        + define <name> Weather apikey=<DarkSkyAPISecretKey>

        + This uses the Dark Sky API with an individual key that you need to + retrieve from the Dark Sky web site.

        Examples:

        -      define MyWeather Weather 673513
        -      define Forecast Weather 673513 1800
        -     
        + define Forecast Weather apikey=987498ghjgf864 + define MyWeather Weather api=OpenWeatherMapAPI,cachemaxage:600 apikey=09878945fdskv876 location=52.4545,13.4545 interval=3600 language=de + - The module provides four additional functions WeatherAsHtml, WeatherAsHtmlV, WeatherAsHtmlH and - WeatherAsHtmlD. The former two functions are identical: they return the HTML code for a - vertically arranged weather forecast. The third function returns the HTML code for a horizontally arranged weather forecast. The - 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.

        + + API-specific documentation follows.

        + + Dark Sky

        + +

        APIname of the weather API, e.g. DarkSkyAPI
        apioptionsindivual options for the chosen API
        apikeykey for the chosen API
        locationlocation for the weather forecast; + e.g. coordinates, a town name or an ID, depending on the chosen API
        intervalduration in seconds between updates
        langlanguage of the forecast: de, + en, pl, fr, it or nl
        + + + + +

        + + OpenWeatherMap

        + +

        APIDarkSkyAPI
        apioptionscachemaxage=<cachemaxage>
        duration + in seconds to retrieve the forecast from the cache instead from the API
        location<latitude,longitude>
        + geographic coordinates in degrees of the location for which the + weather is forecast; if missing, the values of the attributes + of the global device are taken, if these exist.
        + + + + +

        + + The module provides four additional functions WeatherAsHtml, + WeatherAsHtmlV, WeatherAsHtmlH and + WeatherAsHtmlD. The former two functions are identical: + they return the HTML code for a vertically arranged weather forecast. + The third function returns the HTML code for a horizontally arranged + weather forecast. The 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.

        Example:

               define MyWeatherWeblink weblink htmlCode { WeatherAsHtmlH("MyWeather") }
        @@ -704,6 +714,7 @@ WeatherAsHtmlD($;$)
             Valid readings and their meaning (? can be one of 1, 2, 3, 4, 5 and stands
             for today, tomorrow, etc.):
        APIOpenWeatherMapAPI
        apioptionscachemaxage=<cachemaxage>
        duration + in seconds to retrieve the forecast from the cache instead from the API
        location<latitude,longitude>
        + geographic coordinates in degrees of the location for which the + weather is forecast; if missing, the values of the attributes + of the global device are taken, if these exist.
        + @@ -731,13 +742,9 @@ WeatherAsHtmlD($;$)
        .locenselicense of the API provider, if available
        cityname of town returned for location
        codecurrent condition code
        conditioncurrent condition
        wind_speedsame as wind

        - The following readings help to identify whether a workaround has kicked in to avoid the retrieval of - stale data from the remote server: - - - - -
        pubDatepublication time of forecast for current set of readings
        pubDateRemotepublication time of forecast as seen on remote server
        validitystale, if publication time as seen on remote server is before that of current set of readings
        + Depending on the chosen API, other readings can be shown as well. + The meaning of these readings can be determined from the API provider's + documentation.

      @@ -759,37 +766,101 @@ WeatherAsHtmlD($;$)

      Weather

        - Es wird das Perl-Modul JSON benötigt. Mit apt-get install libjson-perl kann es unter Debian und Derivaten installiert werden.

        + Hinweis: es wird das Perl-Modul JSON benötigt. Mit apt-get install + libjson-perl kann es unter Debian und Derivaten installiert + werden.

        + + Das Weather-Modul arbeitet mit verschiedenen Wetter-APIs zusammen: +

        +
        + Eine solche virtuelle Wetterstation sammelt periodisch aktuelle Wetterdaten + und Wettervorhersagen aus dem verwendeten API.

        + - Define + Define

          - define <name> Weather <location> [<interval> [<language>]]
          -
          - Bezechnet ein virtuelles Gerät für Wettervorhersagen.

          + define <name> Weather [API=<API>[,<apiotions>]] [apikey=<apikey>] + [location=<location>] [interval=<interval>] [lang=<lang>]

          - Eine solche virtuelle Wetterstation sammelt periodisch aktuelle und zukünftige Wetterdaten aus der Yahoo-Wetter-API.

          + Die Parameter haben die folgende Bedeutung:
          - Der Parameter location entspricht der sechsstelligen WOEID (WHERE-ON-EARTH-ID). Die WOEID für den eigenen Standort kann auf http://weather.yahoo.com gefunden werden.

          + + + + + + + + +

          - Der optionale Parameter interval 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.

          - - Der optionale Parameter für die möglichen Sprachen darf einen der folgende Werte annehmen: de, en, pl, fr oder nl. Er bezeichnet die natürliche Sprache, in der die Wetterinformationen dargestellt werden. Der Standardwert ist en. 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.

          + Eine ganz einfache Definition ist:

          + define <name> Weather apikey=<DarkSkyAPISecretKey>

          + Bei dieser Definition wird die API von Dark Sky verwendet mit einem + individuellen Schlüssel, den man sich auf der Webseite von Dark Sky + beschaffen muss.

          Beispiele:

          -      define MyWeather Weather 673513
          -      define Forecast Weather 673513 1800
          -     
          + define Forecast Weather apikey=987498ghjgf864 + define MyWeather Weather api=OpenWeatherMapAPI,cachemaxage:600 apikey=09878945fdskv876 location=52.4545,13.4545 interval=3600 language=de + - Das Modul unterstützt zusätzlich vier verschiedene Funktionen WeatherAsHtml, WeatherAsHtmlV, WeatherAsHtmlH und WeatherAsHtmlD. 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.

          + Es folgt die API-spezifische Dokumentation.

          + + Dark Sky

          + +

          APIName des Wetter-APIs, z.B. DarkSkyAPI
          apioptionsIndividuelle Optionen für das gewählte API
          apikeySchlüssel für das gewählte API
          locationOrt, für den das Wetter vorhergesagt wird. + Abhängig vom API z.B. die Koordinaten, ein Ortsname oder eine ID.
          intervalDauer in Sekunden zwischen den einzelnen + Aktualisierungen der Wetterdaten
          langSprache der Wettervorhersage: de, + en, pl, fr, it oder nl
          + + + + +

          + + OpenWeatherMap

          + +

          APIDarkSkyAPI
          apioptionscachemaxage=<cachemaxage>
          Zeitdauer in + Sekunden, innerhalb derer die Wettervorhersage nicht neu abgerufen + sondern aus dem Cache zurück geliefert wird.
          location<latitude,longitude>
          Geographische Breite + und Länge des Ortes in Grad, für den das Wetter vorhergesagt wird. + Bei fehlender Angabe werden die Werte aus den gleichnamigen Attributen + des global-Device genommen, sofern vorhanden.
          + + + + +

          + + Das Modul unterstützt zusätzlich vier verschiedene Funktionen + WeatherAsHtml, WeatherAsHtmlV, + WeatherAsHtmlH und WeatherAsHtmlD. + Die ersten beiden Funktionen sind identisch: sie erzeugen + den HTML-Kode 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.

          Beispiel:

                 define MyWeatherWeblink weblink htmlCode { WeatherAsHtmlH("MyWeather") }
               
          -
          @@ -798,7 +869,9 @@ WeatherAsHtmlD($;$)
            set <name> update

            - Erzwingt eine Abfrage der Wetterdaten. Die darauffolgende Abfrage wird gemäß dem eingestellten Intervall interval Sekunden später durchgeführt.

            + Erzwingt eine Abfrage der Wetterdaten. Die darauffolgende Abfrage + wird gemäß dem eingestellten + Intervall interval Sekunden später durchgeführt.


          @@ -807,50 +880,44 @@ WeatherAsHtmlD($;$)
            get <name> <reading>

            - 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.):

            + 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.):

          APIOpenWeatherMapAPI
          apioptionscachemaxage=<cachemaxage> Zeitdauer in + Sekunden, innerhalb derer die Wettervorhersage nicht neu abgerufen + sondern aus dem Cache zurück geliefert wird.
          location<latitude,longitude> Geographische Breite + und Länge des Ortes in Grad, für den das Wetter vorhergesagt wird. + Bei fehlender Angabe werden die Werte aus den gleichnamigen Attributen + des global-Device genommen, sofern vorhanden.
          - - - + + + + - - + + - + - - + + - - - - - - + + + - -
          cityName der Stadt, der aufgrund der WOEID übermittelt wird
          codeCode für die aktuellen Wetterverhältnisse
          conditionaktuelle Wetterverhältnisse
          .licenseLizenz des jeweiligen API-Anbieters, sofern vorhanden
          cityName der Stadt, der für die location übermittelt wird
          codeCode für die aktuellen Wetterverhältnisse
          conditionaktuelle Wetterverhältnisse
          current_date_timeZeitstempel der letzten Aktualisierung der Wetterdaten vom Server
          fc?_codeCode für die vorhergesagten Wetterverhältnisse
          fc?_conditionvorhergesagte Wetterverhältnisse
          fc?_codeCode für die vorhergesagten Wetterverhältnisse
          fc?_conditionvorhergesagte Wetterverhältnisse
          fc?_day_of_weekWochentag des Tages, der durch ? dargestellt wird
          fc?_high_cvorhergesagte maximale Tagestemperatur in Grad Celsius
          fc?_iconIcon für Vorhersage
          fc?_iconIcon für Vorhersage
          fc?_low_cvorhergesagte niedrigste Tagestemperatur in Grad Celsius
          humiditygegenwärtige Luftfeuchtgkeit in %
          iconrelativer Pfad für das aktuelle Icon
          humiditygegenwärtige Luftfeuchtgkeit in %
          iconrelativer Pfad für das aktuelle Icon
          pressureLuftdruck in hPa
          pressure_trendLuftdrucktendenz (0= gleichbleibend, 1= steigend, 2= fallend)
          pressure_trend_txttextliche Darstellung der Luftdrucktendenz
          pressure_trend_symsymbolische Darstellung der Luftdrucktendenz
          temperaturegegenwärtige Temperatur in Grad Celsius
          temp_cgegenwärtige Temperatur in Grad Celsius
          temp_fgegenwärtige Temperatur in Grad Celsius
          temperaturegegenwärtige Temperatur in Grad Celsius
          temp_cgegenwärtige Temperatur in Grad Celsius
          temp_fgegenwärtige Temperatur in Grad Celsius
          visibilitySichtweite in km
          windWindgeschwindigkeit in km/h
          wind_chillgefühlte Temperatur in Grad Celsius
          wind_conditionWindrichtung und -geschwindigkeit
          wind_directionGradangabe der Windrichtung (0 = Nordwind)
          wind_speedWindgeschwindigkeit in km/h (mit wind identisch)
          -
          - Die folgenden Daten helfen zu identifizieren, ob ein Workaround angeschlagen hat, der die Verwendung von - veralteten Daten auf dem entfernten Server verhindert: - - -
          pubDateVeröffentlichungszeitpunkt der Wettervorhersage in den aktuellen Daten (readings)
          pubDateRemoteVeröffentlichungszeitpunkt der Wettervorhersage auf dem entfernten Server
          validitystale, wenn der Veröffentlichungszeitpunkt auf dem entfernten Server vor dem Zeitpunkt der aktuellen Daten (readings) liegt
          - +
          + Je nach verwendeter API ist es durchaus möglich, dass weitere + Readings geschrieben werden. Die Bedeutung dieser Readings kann man + der API-Beschreibung des Anbieters entnehmen.

        Attribute
          -
        • disable: stellt die Abfrage der Wetterdaten ab - der Timer läft gemäß Plan doch es werden keine Daten vom +
        • disable: stellt die Abfrage der Wetterdaten ab - der Timer läft + gemäß Plan doch es werden keine Daten vom API angefordert.
        • readingFnAttributes
        diff --git a/FHEM/DarkSkyAPI.pm b/FHEM/DarkSkyAPI.pm new file mode 100644 index 000000000..380a75cbd --- /dev/null +++ b/FHEM/DarkSkyAPI.pm @@ -0,0 +1,524 @@ +# $Id$ +############################################################################### +# +# Developed with Kate +# +# (c) 2019 Copyright: Marko Oldenburg (leongaultier at gmail dot com) +# All rights reserved +# +# Special thanks goes to: +# +# +# This script is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License,or +# any later version. +# +# The GNU General Public License can be found at +# http://www.gnu.org/copyleft/gpl.html. +# A copy is found in the textfile GPL.txt and important notices to the license +# from the author is found in LICENSE.txt distributed with these scripts. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# +############################################################################### + +package DarkSkyAPI::Weather; +use strict; +use warnings; + +use POSIX; +use HttpUtils; + +my $missingModul = ''; +eval "use JSON;1" + or $missingModul .= + "JSON "; # apt-get install libperl-JSON on Debian and derivatives +eval "use Encode qw(encode_utf8);1" or $missingModul .= "Encode "; + +# use Data::Dumper; # for Debug only +## API URL +use constant URL => 'https://api.darksky.net/forecast/'; + +my %codes = ( + 'clear-day' => 32, + 'clear-night' => 31, + 'rain' => 11, + 'snow' => 16, + 'sleet' => 18, + 'wind' => 24, + 'fog' => 20, + 'cloudy' => 26, + 'partly-cloudy-day' => 30, + 'partly-cloudy-night' => 29, + 'hail' => 17, + 'thunderstorm' => 4, + 'tornado' => 0, +); + +sub new { + ### geliefert wird ein Hash + my ( $class, $argsRef ) = @_; + + my $self = { + devName => $argsRef->{devName}, + key => ( + ( defined( $argsRef->{apikey} ) and $argsRef->{apikey} ) + ? $argsRef->{apikey} + : 'none' + ), + cachemaxage => ( + ( defined( $argsRef->{apioptions} ) and $argsRef->{apioptions} ) + ? ( + ( split( ':', $argsRef->{apioptions} ) )[0] eq 'cachemaxage' + ? ( split( ':', $argsRef->{apioptions} ) )[1] + : 900 + ) + : 900 + ), + lang => $argsRef->{language}, + lat => ( split( ',', $argsRef->{location} ) )[0], + long => ( split( ',', $argsRef->{location} ) )[1], + fetchTime => 0, + }; + + $self->{cached} = _CreateForecastRef($self); + + bless $self, $class; + return $self; +} + +sub setFetchTime { + my $self = shift; + + $self->{fetchTime} = time(); + return 0; +} + +sub setRetrieveData { + my $self = shift; + + _RetrieveDataFromDarkSky($self); + return 0; +} + +sub getFetchTime { + my $self = shift; + + return $self->{fetchTime}; +} + +sub getWeather { + my $self = shift; + + return $self->{cached}; +} + +sub _RetrieveDataFromDarkSky($) { + my $self = shift; + + # retrieve data from cache + if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) { + return _CallWeatherCallbackFn($self); + } + + my $paramRef = { + timeout => 15, + self => $self, + callback => \&_RetrieveDataFinished, + }; + + if ( $self->{lat} eq 'error' + or $self->{long} eq 'error' + or $self->{key} eq 'none' + or $missingModul ) + { + _RetrieveDataFinished( + $paramRef, +'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]', + undef + ) if ( $self->{lat} eq 'error' or $self->{long} eq 'error' ); + + _RetrieveDataFinished( $paramRef, + 'No given api key. (define myWeather Weather apikey=[KEY])', + undef ) + if ( $self->{key} eq 'none' ); + + _RetrieveDataFinished( $paramRef, + 'Perl modul ' . $missingModul . ' is missing.', undef ) + if ($missingModul); + } + else { + $paramRef->{url} = + URL + . $self->{key} . '/' + . $self->{lat} . ',' + . $self->{long} + . '?lang=' + . $self->{lang} + . '&units=auto'; + + main::HttpUtils_NonblockingGet($paramRef); + } +} + +sub _RetrieveDataFinished($$$) { + my ( $paramRef, $err, $response ) = @_; + my $self = $paramRef->{self}; + + if ( !$err ) { + $self->{cached}->{status} = 'ok'; + $self->{cached}->{validity} = 'up-to-date', $self->{fetchTime} = time(); + _ProcessingRetrieveData( $self, $response ); + } + else { + $self->{fetchTime} = time() if ( not defined( $self->{fetchTime} ) ); + _ErrorHandling( $self, $err ); + _ProcessingRetrieveData( $self, $response ); + } +} + +sub _ProcessingRetrieveData($$) { + my ( $self, $response ) = @_; + + if ( $self->{cached}->{status} eq 'ok' + and defined($response) + and $response ) + { + my $data = eval { decode_json($response) }; + + if ($@) { + _ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ ); + } + elsif ( defined( $data->{code} ) + and $data->{code} + and defined( $data->{error} ) + and $data->{error} ) + { + _ErrorHandling( $self, + 'Code: ' . $data->{code} . ' Error: ' . $data->{error} ); + } + else { + # print Dumper $data; ## für Debugging + + $self->{cached}->{current_date_time} = + strftime( "%a, %e %b %Y %H:%M %p", + localtime( $self->{fetchTime} ) ); + $self->{cached}->{timezone} = $data->{timezone}; + $self->{cached}->{license}{text} = + $data->{flags}->{'meteoalarm-license'}; + $self->{cached}->{current} = { + 'temperature' => int( + sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 + ), + 'temp_c' => int( + sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 + ), + 'dewPoint' => int( + sprintf( "%.1f", $data->{currently}->{dewPoint} ) + 0.5 + ), + 'humidity' => $data->{currently}->{humidity} * 100, + 'condition' => encode_utf8( $data->{currently}->{summary} ), + 'pressure' => int( + sprintf( "%.1f", $data->{currently}->{pressure} ) + 0.5 + ), + 'wind' => int( + sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 + ), + 'wind_speed' => int( + sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 + ), + 'wind_direction' => $data->{currently}->{windBearing}, + 'windGust' => int( + sprintf( "%.1f", $data->{currently}->{windGust} ) + 0.5 + ), + 'cloudCover' => $data->{currently}->{cloudCover} * 100, + 'uvIndex' => $data->{currently}->{uvIndex}, + 'visibility' => int( + sprintf( "%.1f", $data->{currently}->{visibility} ) + 0.5 + ), + 'ozone' => $data->{currently}->{ozone}, + 'code' => $codes{ $data->{currently}->{icon} }, + 'iconAPI' => $data->{currently}->{icon}, + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{currently}->{'time'} ) + ), + 'precipProbability' => $data->{currently}->{precipProbability}, + 'apparentTemperature' => int( + sprintf( + "%.1f", $data->{currently}->{apparentTemperature} + ) + 0.5 + ), + 'precipIntensity' => $data->{currently}->{precipIntensity}, + }; + + if ( ref( $data->{daily}->{data} ) eq "ARRAY" + and scalar( @{ $data->{daily}->{data} } ) > 0 ) + { + ### löschen des alten Datensatzes + delete $self->{cached}->{forecast}; + + my $i = 0; + foreach ( @{ $data->{daily}->{data} } ) { + push( + @{ $self->{cached}->{forecast}->{daily} }, + { + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{'time'} + ) + ), + 'day_of_week' => strftime( + "%a", + localtime( + $data->{daily}->{data}->[$i]->{'time'} + ) + ), + 'low_c' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureLow} ) + 0.5 + ), + 'high_c' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureHigh} ) + 0.5 + ), + 'tempMin' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureMin} ) + 0.5 + ), + 'tempMinTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureMinTime} + ) + ), + 'tempMax' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureMax} ) + 0.5 + ), + 'tempMaxTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureMaxTime} + ) + ), + 'tempLow' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureLow} ) + 0.5 + ), + 'tempLowTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureLowTime} + ) + ), + 'tempHigh' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureHigh} ) + 0.5 + ), + 'tempHighTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureHighTime} + ) + ), + 'apparentTempLow' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureLow} ) + 0.5 + ), + 'apparentTempLowTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureLowTime} + ) + ), + 'apparentTempHigh' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureHigh} ) + 0.5 + ), + 'apparentTempHighTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureHighTime} + ) + ), + 'apparenttempMin' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMin} ) + 0.5 + ), + 'apparenttempMinTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMinTime} + ) + ), + 'apparenttempMax' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMax} ) + 0.5 + ), + 'apparenttempMaxTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMaxTime} + ) + ), + 'code' => + $codes{ $data->{daily}->{data}->[$i]->{icon} }, + 'iconAPI' => $data->{daily}->{data}->[$i]->{icon}, + 'condition' => encode_utf8( + $data->{daily}->{data}->[$i]->{summary} + ), + 'ozone' => $data->{daily}->{data}->[$i]->{ozone}, + 'uvIndex' => + $data->{daily}->{data}->[$i]->{uvIndex}, + 'uvIndexTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{uvIndexTime} + ) + ), + 'precipIntensity' => + $data->{daily}->{data}->[$i]->{precipIntensity}, + 'precipIntensityMax' => + $data->{daily}->{data}->[$i] + ->{precipIntensityMax}, + 'precipIntensityMaxTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{precipIntensityMaxTime} + ) + ), + 'dewPoint' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{dewPoint} ) + + 0.5 + ), + 'humidity' => + $data->{daily}->{data}->[$i]->{humidity} * 100, + 'cloudCover' => + $data->{daily}->{data}->[$i]->{cloudCover} * 100, + 'precipType' => + $data->{daily}->{data}->[$i]->{precipType}, + + 'wind_direction' => + $data->{daily}->{data}->[$i]->{windBearing}, + 'wind' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{windSpeed} ) + + 0.5 + ), + 'wind_speed' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{windSpeed} ) + + 0.5 + ), + 'windGust' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{windGust} ) + + 0.5 + ), + 'windGustTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{windGustTime} + ) + ), + 'moonPhase' => + $data->{daily}->{data}->[$i]->{moonPhase}, + 'sunsetTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{sunsetTime} + ) + ), + 'sunriseTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{sunriseTime} + ) + ), + + 'precipProbability' => + $data->{daily}->{data}->[$i]->{precipProbability}, + 'pressure' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{pressure} ) + + 0.5 + ), + 'visibility' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{visibility} ) + + 0.5 + ), + } + ); + + $i++; + } + } + } + } + + ## Aufruf der callbackFn + _CallWeatherCallbackFn($self); +} + +sub _CallWeatherCallbackFn($) { + my $self = shift; + + # ## Aufruf der callbackFn + main::Weather_RetrieveCallbackFn( $self->{devName} ); +} + +sub _ErrorHandling($$) { + my ( $self, $err ) = @_; + + $self->{cached}->{current_date_time} = + strftime( "%a, %e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), + $self->{cached}->{status} = $err; + $self->{cached}->{validity} = 'stale'; +} + +sub _CreateForecastRef($) { + my $self = shift; + + my $forecastRef = ( + { + lat => $self->{lat}, + long => $self->{long}, + apiMaintainer => +'Leon Gaultier (CoolTux)', + } + ); + + return $forecastRef; +} + +############################################################################## + +1; diff --git a/FHEM/OpenWeatherMapAPI.pm b/FHEM/OpenWeatherMapAPI.pm new file mode 100644 index 000000000..74ccda52f --- /dev/null +++ b/FHEM/OpenWeatherMapAPI.pm @@ -0,0 +1,484 @@ +# $Id$ +############################################################################### +# +# Developed with Kate +# +# (c) 2019 Copyright: Marko Oldenburg (leongaultier at gmail dot com) +# All rights reserved +# +# Special thanks goes to: +# +# +# This script is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License,or +# any later version. +# +# The GNU General Public License can be found at +# http://www.gnu.org/copyleft/gpl.html. +# A copy is found in the textfile GPL.txt and important notices to the license +# from the author is found in LICENSE.txt distributed with these scripts. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# +############################################################################### + +### Beispielaufruf +# https://api.openweathermap.org/data/2.5/weather?lat=[lat]&lon=[long]&APPID=[API] Current +# https://api.openweathermap.org/data/2.5/forecast?lat=[lat]&lon=[long]&APPID=[API] Forecast +# https://openweathermap.org/weather-conditions Icons und Conditions ID's + +package OpenWeatherMapAPI::Weather; +use strict; +use warnings; + +use POSIX; +use HttpUtils; + +my $missingModul = ''; +eval "use JSON;1" + or $missingModul .= + "JSON "; # apt-get install libperl-JSON on Debian and derivatives +eval "use Encode qw(encode_utf8);1" or $missingModul .= "Encode "; + +# use Data::Dumper; # for Debug only +## API URL +use constant URL => 'https://api.openweathermap.org/data/2.5/'; +## URL . 'weather?' for current data +## URL . 'forecast?' for forecast data + +my %codes = ( + 200 => 45, + 201 => 45, + 202 => 45, + 210 => 4, + 211 => 4, + 212 => 3, + 221 => 4, + 230 => 45, + 231 => 45, + 232 => 45, + 300 => 9, + 301 => 9, + 302 => 9, + 310 => 9, + 311 => 9, + 312 => 9, + 313 => 9, + 314 => 9, + 321 => 9, + 500 => 35, + 501 => 35, + 502 => 35, + 503 => 35, + 504 => 35, + 511 => 35, + 520 => 35, + 521 => 35, + 522 => 35, + 531 => 35, + 600 => 14, + 601 => 16, + 602 => 13, + 611 => 46, + 612 => 46, + 615 => 5, + 616 => 5, + 620 => 14, + 621 => 46, + 622 => 42, + 701 => 19, + 711 => 22, + 721 => 19, + 731 => 23, + 741 => 20, + 751 => 23, + 761 => 19, + 762 => 3200, + 771 => 1, + 781 => 0, + 800 => 32, + 801 => 30, + 802 => 26, + 803 => 26, + 804 => 28, +); + +sub new { + ### geliefert wird ein Hash + my ( $class, $argsRef ) = @_; + + my $self = { + devName => $argsRef->{devName}, + key => ( + ( defined( $argsRef->{apikey} ) and $argsRef->{apikey} ) + ? $argsRef->{apikey} + : 'none' + ), + cachemaxage => ( + ( defined( $argsRef->{apioptions} ) and $argsRef->{apioptions} ) + ? ( + ( split( ':', $argsRef->{apioptions} ) )[0] eq 'cachemaxage' + ? ( split( ':', $argsRef->{apioptions} ) )[1] + : 900 + ) + : 900 + ), + lang => $argsRef->{language}, + lat => ( split( ',', $argsRef->{location} ) )[0], + long => ( split( ',', $argsRef->{location} ) )[1], + fetchTime => 0, + endpoint => 'none', + }; + + $self->{cached} = _CreateForecastRef($self); + + bless $self, $class; + return $self; +} + +sub setFetchTime { + my $self = shift; + + $self->{fetchTime} = time(); + return 0; +} + +sub setRetrieveData { + my $self = shift; + + _RetrieveDataFromOpenWeatherMap($self); + return 0; +} + +sub getFetchTime { + my $self = shift; + + return $self->{fetchTime}; +} + +sub getWeather { + my $self = shift; + + return $self->{cached}; +} + +sub _RetrieveDataFromOpenWeatherMap($) { + my $self = shift; + + # retrieve data from cache + if ( $self->{endpoint} eq 'none' ) { + if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) { + return _CallWeatherCallbackFn($self); + } + } + + my $paramRef = { + timeout => 15, + self => $self, + endpoint => ( $self->{endpoint} eq 'none' ? 'weather' : 'forecast' ), + callback => \&_RetrieveDataFinished, + }; + + $self->{endpoint} = $paramRef->{endpoint}; + + if ( $self->{lat} eq 'error' + or $self->{long} eq 'error' + or $self->{key} eq 'none' + or $missingModul ) + { + _RetrieveDataFinished( + $paramRef, +'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]', + undef + ) if ( $self->{lat} eq 'error' or $self->{long} eq 'error' ); + + _RetrieveDataFinished( $paramRef, + 'No given api key. (define myWeather Weather apikey=[KEY])', + undef ) + if ( $self->{key} eq 'none' ); + + _RetrieveDataFinished( $paramRef, + 'Perl modul ' . $missingModul . ' is missing.', undef ) + if ($missingModul); + } + else { + $paramRef->{url} = + URL + . $paramRef->{endpoint} . '?' . 'lat=' + . $self->{lat} . '&' . 'lon=' + . $self->{long} . '&' + . 'APPID=' + . $self->{key} . '&' . 'lang=' + . $self->{lang}; + + main::HttpUtils_NonblockingGet($paramRef); + } +} + +sub _RetrieveDataFinished($$$) { + my ( $paramRef, $err, $response ) = @_; + my $self = $paramRef->{self}; + + if ( !$err ) { + $self->{cached}->{status} = 'ok'; + $self->{cached}->{validity} = 'up-to-date', $self->{fetchTime} = time(); + _ProcessingRetrieveData( $self, $response ); + } + else { + $self->{fetchTime} = time() if ( not defined( $self->{fetchTime} ) ); + _ErrorHandling( $self, $err ); + _ProcessingRetrieveData( $self, $response ); + } +} + +sub _ProcessingRetrieveData($$) { + my ( $self, $response ) = @_; + + if ( $self->{cached}->{status} eq 'ok' + and defined($response) + and $response ) + { + my $data = eval { decode_json($response) }; + + if ($@) { + _ErrorHandling( $self, + 'OpenWeatherMap Weather decode JSON err ' . $@ ); + } + elsif ( defined( $data->{cod} ) + and $data->{cod} + and $data->{cod} != 200 + and defined( $data->{message} ) + and $data->{message} ) + { + _ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} ); + } + else { + + ###### Ab hier wird die ResponseHash Referenze für die Rückgabe zusammen gestellt + $self->{cached}->{current_date_time} = + strftime( "%a, %e %b %Y %H:%M %p", + localtime( $self->{fetchTime} ) ); + + if ( $self->{endpoint} eq 'weather' ) { + $self->{cached}->{country} = $data->{sys}->{country}; + $self->{cached}->{city} = $data->{name}; + $self->{cached}->{license}{text} = 'none'; + $self->{cached}->{current} = { + 'temperature' => int( + sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + + 0.5 + ), + 'temp_c' => int( + sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + + 0.5 + ), + 'low_c' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 + ), + 'high_c' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5 + ), + 'tempLow' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 + ), + 'tempHigh' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5 + ), + 'humidity' => $data->{main}->{humidity}, + 'condition' => + encode_utf8( $data->{weather}->[0]->{description} ), + 'pressure' => + int( sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5 ), + 'wind' => + int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ), + 'wind_speed' => + int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ), + 'wind_direction' => $data->{wind}->{deg}, + 'cloudCover' => $data->{clouds}->{all}, + 'visibility' => + int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ), + 'code' => $codes{ $data->{weather}->[0]->{id} }, + 'iconAPI' => $data->{weather}->[0]->{icon}, + 'sunsetTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{sys}->{sunset} ) + ), + 'sunriseTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{sys}->{sunrise} ) + ), + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{dt} ) + ), + }; + } + + if ( $self->{endpoint} eq 'forecast' ) { + if ( ref( $data->{list} ) eq "ARRAY" + and scalar( @{ $data->{list} } ) > 0 ) + { + ## löschen des alten Datensatzes + delete $self->{cached}->{forecast}; + + my $i = 0; + foreach ( @{ $data->{list} } ) { + push( + @{ $self->{cached}->{forecast}->{hourly} }, + { + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + ( $data->{list}->[$i]->{dt} ) - 3600 + ) + ), + 'day_of_week' => strftime( + "%a", + localtime( + ( $data->{list}->[$i]->{dt} ) - 3600 + ) + ), + 'temperature' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main}->{temp} + - 273.15 + ) + ) + 0.5 + ), + 'temp_c' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main}->{temp} + - 273.15 + ) + ) + 0.5 + ), + 'low_c' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_min} - 273.15 + ) + ) + 0.5 + ), + 'high_c' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_max} - 273.15 + ) + ) + 0.5 + ), + 'tempLow' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_min} - 273.15 + ) + ) + 0.5 + ), + 'tempHigh' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_max} - 273.15 + ) + ) + 0.5 + ), + 'humidity' => + $data->{list}->[$i]->{main}->{humidity}, + 'condition' => encode_utf8( + $data->{list}->[$i]->{weather}->[0] + ->{description} + ), + 'pressure' => int( + sprintf( "%.1f", + $data->{list}->[$i]->{main}->{pressure} + ) + 0.5 + ), + 'wind' => int( + sprintf( "%.1f", + $data->{list}->[$i]->{wind}->{speed} ) + + 0.5 + ), + 'wind_speed' => int( + sprintf( "%.1f", + $data->{list}->[$i]->{wind}->{speed} ) + + 0.5 + ), + 'cloudCover' => + $data->{list}->[$i]->{clouds}->{all}, + 'code' => + $codes{ $data->{list}->[$i]->{weather}->[0] + ->{id} }, + 'iconAPI' => + $data->{list}->[$i]->{weather}->[0]->{icon}, + }, + ); + + $i++; + } + } + } + } + } + + $self->{endpoint} = 'none' if ( $self->{endpoint} eq 'forecast' ); + + _RetrieveDataFromOpenWeatherMap($self) + if ( $self->{endpoint} eq 'weather' ); + + _CallWeatherCallbackFn($self) if ( $self->{endpoint} eq 'none' ); +} + +sub _CallWeatherCallbackFn($) { + my $self = shift; + + # print 'Dumperausgabe: ' . Dumper $self; + ### Aufruf der callbackFn + main::Weather_RetrieveCallbackFn( $self->{devName} ); +} + +sub _ErrorHandling($$) { + my ( $self, $err ) = @_; + + $self->{cached}->{current_date_time} = + strftime( "%a, %e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), + $self->{cached}->{status} = $err; + $self->{cached}->{validity} = 'stale'; +} + +sub _CreateForecastRef($) { + my $self = shift; + + my $forecastRef = ( + { + lat => $self->{lat}, + long => $self->{long}, + apiMaintainer => +'Leon Gaultier (CoolTux)', + } + ); + + return $forecastRef; +} + +############################################################################## + +1; diff --git a/MAINTAINER.txt b/MAINTAINER.txt index ea9a62071..74b8fcbeb 100644 --- a/MAINTAINER.txt +++ b/MAINTAINER.txt @@ -480,7 +480,7 @@ FHEM/98_expandJSON.pm dev0 Unterstuetzende Dienste FHEM/98_feels_like.pm hotbso Wettermodule FHEM/98_fheminfo.pm betateilchen Sonstiges FHEM/98_fhemdebug.pm rudolfkoenig Sonstiges -FHEM/98_freezemon KernSani Unterstuetzende Dienste +FHEM/98_freezemon KernSani Unterstuetzende Dienste FHEM/98_GoogleAuth.pm betateilchen Unterstuetzende Dienste FHEM/98_GOOGLECAST.pm dominikkarall Multimedia FHEM/98_help.pm betateilchen Sonstiges @@ -538,7 +538,9 @@ FHEM/TimeSeries.pm neubert /jensb FHEM Development FHEM/TR064Utils.pm rudolfkoenig Automatisierung FHEM/UConv.pm loredo FHEM Development FHEM/Unit.pm loredo FHEM Development -FHEM/YahooWeatherAPI.pm neubert Unterstuetzende Dienste/Wettermodule +FHEM/DarkSkyAPI.pm CoolTux Unterstuetzende Dienste/Wettermodule +FHEM/OpenWeatherAPI.pm CoolTux Unterstuetzende Dienste/Wettermodule +FHEM/YahooWeatherAPI.pm neubert (deprecated) FHEM/lib/Device/Firmata/* jensb Sonstige Systeme FHEM/lib/Device/MySensors/* Hauswart/Beta-User Bastelecke/MySensors FHEM/lib/MP3/* Reinerlein Multimedia