From 58f5bcc12cc52c20ed906fe6addbfcf715e25bfc Mon Sep 17 00:00:00 2001 From: borisneubert Date: Sat, 8 Aug 2015 06:54:33 +0000 Subject: [PATCH] 57_Calendar.pm: made download from URL non-blocking git-svn-id: svn://svn.code.sf.net/p/fhem/code/trunk@9026 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/57_Calendar.pm | 46 ++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index c00b5767a..d5bd970ef 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # 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. + - feature: 57_Calendar: made download from URL non-blocking - bugfix: 95_Dashboard: fixed problem with smallscreen styles that caused devices to be shown in wrong tabs - bugfix: 34_NUT: versucht bei disabled=1 nicht mehr, eine Verbindung aufzubauen diff --git a/fhem/FHEM/57_Calendar.pm b/fhem/FHEM/57_Calendar.pm index 457c7ebe1..ca4495c7c 100644 --- a/fhem/FHEM/57_Calendar.pm +++ b/fhem/FHEM/57_Calendar.pm @@ -44,17 +44,11 @@ no if $] >= 5.017011, warnings => 'experimental::smartmatch'; # modeStarted triggered on calendar update # http://forum.fhem.de/index.php?topic=28516 # -# take care of unitialized value warnings -# http://forum.fhem.de/index.php?topic=28409 -# # *** Potential future extensions: # # add support for EXDATE # http://forum.fhem.de/index.php?topic=24485 # -# nonblocking retrieval -# http://forum.fhem.de/index.php?topic=29622 -# # sequence of events fired sorted by time # http://forum.fhem.de/index.php?topic=29112 # @@ -932,14 +926,30 @@ sub Calendar_GetUpdate($$) { my $ics; if($type eq "url"){ - #$ics= GetFileFromURLQuiet($url,10,undef,0,5) if($type eq "url"); - ($errmsg, $ics)= HttpUtils_BlockingGet( { url => $url, hideurl => 1, timeout => 10, } ); + + HttpUtils_NonblockingGet({ + url => $url, + noshutdown => 1, + hash => $hash, + type => 'caldata', + removeall => $removeall, + callback => \&Calendar_ParseUpdate, + }); + Log3 $hash, 4, "Calendar: Getting data from $url"; + } elsif($type eq "file") { if(open(ICSFILE, $url)) { while() { $ics .= $_; } close(ICSFILE); + + my $paramhash; + $paramhash->{type} = 'caldata'; + $paramhash->{removeall} = $removeall; + Calendar_ParseUpdate($paramhash,'',$ics); + return undef; + } else { Log3 $hash, 1, "Calendar " . $hash->{NAME} . ": Could not open file $url"; return 0; @@ -948,12 +958,30 @@ sub Calendar_GetUpdate($$) { # this case never happens by virtue of _Define, so just die "Software Error"; } - + + +} + +################################### +sub Calendar_ParseUpdate($$$) { + + my ($param, $errmsg, $data) = @_; + my $hash = $param->{hash}; + my $name = $hash->{NAME}; + my $removeall = $param->{removeall}; + my $ics = $data; + if( $errmsg ) + { + Log3 $name, 1, "$name: URL error: ".$errmsg; + $hash->{STATE} = "error"; + return undef; + } if(!defined($ics) or ("$ics" eq "") or ($errmsg ne "")) { Log3 $hash, 1, "Calendar " . $hash->{NAME} . ": Could not retrieve file at URL. $errmsg"; return 0; } + Log3 $hash, 4, "Calendar: Parsing data"; # we parse the calendar into a recursive ICal::Entry structure my $ical= ICal::Entry->new("root");