From 41b1d1f709d7797c46cb84c244df8d045e7e8f0f Mon Sep 17 00:00:00 2001 From: borisneubert <> Date: Tue, 24 Sep 2013 17:37:49 +0000 Subject: [PATCH] feature: Calendar can read from file and limit number of calendar events retrieved in get command git-svn-id: https://svn.fhem.de/fhem/trunk@3955 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 2 ++ fhem/FHEM/57_Calendar.pm | 58 +++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index e72aec523..8c24c9630 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,4 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. + - feature: Calendar can read from file and limit number of calendar events + retrieved in get command - feature: new module 70_ENIGMA2.pm added (by loredo) - change: CustomGetFileFromURL() in HttpUtils.pm now reacts to "301 Moved Permanently" return code diff --git a/fhem/FHEM/57_Calendar.pm b/fhem/FHEM/57_Calendar.pm index 6424b056b..8c8899e2d 100644 --- a/fhem/FHEM/57_Calendar.pm +++ b/fhem/FHEM/57_Calendar.pm @@ -792,10 +792,29 @@ sub Calendar_GetUpdate($) { $hash->{fhem}{lastUpdate}= FmtDateTime($t); Log3 $hash, 4, "Calendar " . $hash->{NAME} . ": Updating..."; + my $type = $hash->{fhem}{type}; my $url= $hash->{fhem}{url}; - my $ics= GetFileFromURLQuiet($url); - #my $ics= CustomGetFileFromURL(0, $url, undef, undef, 1); + my $ics; + + if($type eq "url"){ + $ics= GetFileFromURLQuiet($url) if($type eq "url"); + } elsif($type eq "file") { + if(open(ICSFILE, $url)) { + while() { + $ics .= $_; + } + close(ICSFILE); + } else { + Log3 $hash, 1, "Calendar " . $hash->{NAME} . ": Could not open file $url"; + return 0; + } + } else { + # this case never happens by virtue of _Define, so just + die "Software Error"; + } + + if(!defined($ics)) { Log3 $hash, 1, "Calendar " . $hash->{NAME} . ": Could not retrieve file at URL"; return 0; @@ -888,7 +907,7 @@ sub Calendar_Get($@) { my $cmd= $a[1]; if(grep(/^$cmd$/, ("text","full","summary","location","alarm","start","end"))) { - return "argument is missing" if($#a != 2); + return "argument is missing" if($#a < 2); my $reading= $a[2]; # $reading is alarmed, all, changed, deleted, new, started, updated @@ -912,7 +931,12 @@ sub Calendar_Get($@) { push @texts, $event->startTime() if $cmd eq "start"; push @texts, $event->endTime() if $cmd eq "end"; } - } + } + if(defined($a[3])) { + my $keep= $a[3]; + return "Argument $keep is not a number." unless($keep =~ /\d+/); + splice @texts, $keep if($#texts>= 0); + } return join("\n", @texts); } elsif($cmd eq "find") { @@ -940,17 +964,20 @@ sub Calendar_Define($$) { my @a = split("[ \t][ \t]*", $def); - return "syntax: define Calendar ical url [interval]" - if(($#a < 4 && $#a > 5) || ($a[2] ne 'ical') || ($a[3] ne 'url')); + return "syntax: define Calendar ical url [interval]\n".\ + " define Calendar ical file [interval]" + if(($#a < 4 && $#a > 5) || ($a[2] ne 'ical') || (($a[3] ne 'url') && ($a[3] ne 'file'))); $hash->{STATE} = "Initialized"; my $name = $a[0]; + my $type = $a[3]; my $url = $a[4]; my $interval = 3600; $interval= $a[5] if($#a==5); + $hash->{fhem}{type}= $type; $hash->{fhem}{url}= $url; $hash->{fhem}{interval}= $interval; $hash->{fhem}{events}= Calendar::Events->new(); @@ -991,21 +1018,24 @@ sub Calendar_Undef($$) { Define
    define <name> Calendar ical url <URL> [<interval>]
    + define <name> Calendar ical file <FILENAME> [<interval>]

    Defines a calendar device.

    - A calendar device periodically gathers calendar events from the source calendar at the given URL. The file at the given URL - must be in ICal format.

    + A calendar device periodically gathers calendar events from the source calendar at the given URL or from a file. + The file must be in ICal format.

    If the URL starts with https://, the perl module IO::Socket::SSL must be installed (use cpan -i IO::Socket::SSL).

    Note for users of Google Calendar: You can literally use the private ICal URL from your Google Calendar. - Google App accounts do not work since requests to the URL - get redirected first and the fhem mechanism for retrieving data via http/https cannot handle this. If your Google Calendar + + If your Google Calendar URL starts with https:// and the perl module IO::Socket::SSL is not installed on your system, you can - replace it by http://.

    + replace it by http:// if and only if there is no redirection to the https:// URL. + Check with your browser first if unsure.

    The optional parameter interval is the time between subsequent updates in seconds. It defaults to 3600 (1 hour).

    @@ -1014,6 +1044,7 @@ sub Calendar_Undef($$) {
           define MyCalendar Calendar ical url https://www.google.com­/calendar/ical/john.doe%40example.com­/private-foo4711/basic.ics
           define YourCalendar Calendar ical url http://www.google.com­/calendar/ical/jane.doe%40example.com­/private-bar0815/basic.ics 86400
    +      define SomeCalendar Calendar ical file /home/johndoe/calendar.ics
           

@@ -1032,12 +1063,13 @@ sub Calendar_Undef($$) { Get
    - get <name> full|text|summary|location|alarm|start|end <reading>|<uid>

    + get <name> full|text|summary|location|alarm|start|end <reading>|<uid> [max]

    Returns, line by line, the full state or a textual representation or the summary (subject, title) or the location or the alarm time or the start time or the end time of the calendar event(s) listed in the - reading <reading> or identified by the UID <uid>.

    + reading <reading> or identified by the UID <uid>. The optional parameter max limits + the number of returned lines.

    get <name> find <regexp>