2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-03 10:46:53 +00:00

remove dependency on Google::Weather, major rewrite (Boris)

git-svn-id: https://svn.fhem.de/fhem/trunk@1310 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
borisneubert 2012-03-03 22:33:00 +00:00
parent c1b35f2290
commit 2b945f677b
2 changed files with 67 additions and 21 deletions

View File

@ -17,6 +17,8 @@
- feature: TRX modules by Willi
- feature: FHEMWEB icons (by Joerg)
- feature: FHEMWEB console (== inform timer)
- feature: remove dependency on Google::Weather, major rewrite (Boris)
- feature: started experimental interface implementation (fhem API v2) (Boris)
- 2011-12-31 (5.2)
- bugfix: applying smallscreen attributes to firefox/opera

View File

@ -8,10 +8,14 @@
# $Id$
package main;
my $UseWeatherGoogle= 0;
use strict;
use warnings;
use Time::HiRes qw(gettimeofday);
if($UseWeatherGoogle) {
use Weather::Google;
}
#####################################
@ -72,30 +76,50 @@ sub Weather_UpdateReading($$$$) {
}
###################################
sub Weather_GetUpdate($)
sub Weather_RetrieveDataDirectly($)
{
my ($hash)= @_;
my $location= $hash->{LOCATION};
#$location =~ s/([^\w()’*~!.-])/sprintf '%%%02x', ord $1/eg;
my $lang= $hash->{LANG};
if(!$hash->{LOCAL}) {
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "Weather_GetUpdate", $hash, 1);
my $fc = undef;
my $xml = GetHttpFile("www.google.com:80", "/ig/api?weather=" . $location . "&hl=" . $lang);
foreach my $l (split("<",$xml)) {
#Log 1, "DEBUG WEATHER: line=\"$l\"";
next if($l eq ""); # skip empty lines
$l =~ s/(\/|\?)?>$//; # strip off /> and >
my ($tag,$value)= split(" ", $l, 2); # split tag data=..... at the first blank
#Log 1, "DEBUG WEATHER: tag=\"$tag\" value=\"$value\"";
$fc= 0 if($tag eq "current_conditions");
$fc++ if($tag eq "forecast_conditions");
next if(!defined($value) || ($value !~ /^data=/));
my $prefix= $fc ? "fc" . $fc ."_" : "";
my $key= $tag;
$value=~ s/^data=\"(.*)\"$/$1/; # extract DATA from data="DATA"
#Log 1, "DEBUG WEATHER: prefix=\"$prefix\" tag=\"$tag\" value=\"$value\"";
Weather_UpdateReading($hash,$prefix,$key,$value);
}
}
my $name = $hash->{NAME};
readingsBeginUpdate($hash);
###################################
sub Weather_RetrieveDataViaWeatherGoogle($)
{
my ($hash)= @_;
# get weather information from Google weather API
# see http://search.cpan.org/~possum/Weather-Google-0.03/lib/Weather/Google.pm
my $location= $hash->{LOCATION};
my $lang= $hash->{LANG};
my $name = $hash->{NAME};
my $WeatherObj;
Log 4, "$name: Updating weather information for $location, language $lang.";
eval {
$WeatherObj= new Weather::Google($location, {language => $lang});
};
if($@) {
Log 1, "$name: Could not retrieve weather information.";
return 0;
@ -125,6 +149,26 @@ sub Weather_GetUpdate($)
}
}
}
###################################
sub Weather_GetUpdate($)
{
my ($hash) = @_;
if(!$hash->{LOCAL}) {
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "Weather_GetUpdate", $hash, 1);
}
readingsBeginUpdate($hash);
if($UseWeatherGoogle) {
Weather_RetrieveDataViaWeatherGoogle($hash);
} else {
Weather_RetrieveDataDirectly($hash);
}
readingsEndUpdate($hash, defined($hash->{LOCAL} ? 0 : 1)); # DoTrigger, because sub is called by a timer instead of dispatch
return 1;