Skip to content

Commit

Permalink
Merge pull request #752 from hplato/master
Browse files Browse the repository at this point in the history
Nanoleaf v1.1.01 and ical2vsdb v5
  • Loading branch information
hplato authored Feb 20, 2018
2 parents 432a87a + 812a260 commit 05d12f0
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 59 deletions.
71 changes: 53 additions & 18 deletions bin/ical2vsdb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use strict;
## NOTE: v4 requires the update 2014 vsdb calendar schema (adding a control attribute).
## Common module organizer 2014 and vsdb calendar.pl 1.6.0-3 required.
##
## v5 02-2018: Some ALARM DATA has wonky dates (ie BEGIN:VALARM TRIGGER;VALUE=DATE-TIME:19760401T005545Z)
## ignore these entries and use 15 minutes for notification
## also enable a override_alarms to set a standard for all notifications ignoring what's in the ical data
## set to 'all' to remove all user specified alarm settings

use lib '../lib', '../lib/site';
use iCal::Parser;
Expand All @@ -40,20 +44,21 @@ use vsLock;
# verify locking works as expected

my $progname = "ical2vsdb";
my $progver = "v4 01-2014";
my $progver = "v5 02-2018";
my $DB = 0;

my $days_before = -180; # defaults to avoid large vsdb databases, can be overriden
my $days_after = 180; #

my $config_file = "";
my $config_version = 2;
my $vsdb_cal_file = "calendar.tab";
my $vsdb_todo_file = "tasks.tab";
my $sleep_time = 900; #poll cycle default is 15 minutes
my $md5file = "";
my $local_cache = "";
my $data = "";
my $config_file = "";
my $config_version = 2;
my $vsdb_cal_file = "calendar.tab";
my $vsdb_todo_file = "tasks.tab";
my $sleep_time = 900; #poll cycle default is 15 minutes
my $md5file = "";
my $local_cache = "";
my $data = "";
my $override_alarms = "0"; #ignore ical alarms and always do $override_alarms in minutes speech events

$config_file = $ARGV[0] if $ARGV[0];

Expand Down Expand Up @@ -89,13 +94,19 @@ if ( ( $count == 0 ) or ( $output_dir eq "" ) ) {

&read_md5file if ( $md5file ne "" );
print "Writing http requests to local cache: $local_cache\n" if $local_cache;
if ($override_alarms) {
if (lc $override_alarms eq "all") {
print "** Removing custom alarms for all events\n";
} else {
print "Using a standard " .$override_alarms . "m alarm for all events\n";
}
}
print "Processing $count iCals";
print ", $days_after days in the future" if $days_after;
my $abs_days_before = abs $days_before;
print ", $abs_days_before in the past" if $abs_days_before;
print "\nTarget vsdb directory is :$output_dir\n" if ($DB);
print "MD5 temp file is :$md5file\n" if ( $md5file ne "" and $DB );

#------------- Main Processing Loop -----------------
#BEGIN
while (1) {
Expand Down Expand Up @@ -208,7 +219,6 @@ while (1) {
else {
$parser = $parser->calendar
unless $ical_data[$loop]->{method} ne "dir";
print ".";

#print Dumper $parser;
$ical_data[$loop]->{hash} = $digest;
Expand All @@ -217,10 +227,9 @@ while (1) {
push( @changed_icals, @$data_info[0] );
push( @master_cal, @$data_cals );
push( @master_todo, @$data_todos );
print ".";
&cache_local( $data, $ical_data[$loop]->{options}->{name} )
if $local_cache;
print "done\n";
print "Processing Complete\n";
print "Calendar Info:" if $DB;
print @$data_info[0] . "," . @$data_info[1] . "\n" if $DB;
}
Expand All @@ -239,7 +248,7 @@ while (1) {
#print Dumper @master_cal;
update_db( "$output_dir/$vsdb_cal_file", \@changed_icals, \@master_cal );
update_db( "$output_dir/$vsdb_todo_file", \@changed_icals, \@master_todo );
print "done\n";
print "DataBases Updated\n";
}

#--------------- Empty data structures and sleep ------------
Expand Down Expand Up @@ -364,12 +373,13 @@ sub parse_cal {
push @out_info, $calname;
push @out_info, $caldesc;
my $count = 0;

my $ical_messages = "";

#print Dumper $cal;
while ( my $year = each %{ $cal->{events} } ) {
while ( my $month = each %{ $cal->{events}->{$year} } ) {
while ( my $day = each %{ $cal->{events}->{$year}->{$month} } ) {
print "."; #give some progress
# print "."; #give some progress
while ( my $uid = each %{ $cal->{events}->{$year}->{$month}->{$day} } ) {
my $delta;

Expand Down Expand Up @@ -405,10 +415,14 @@ sub parse_cal {
}

my $reminder = '';
my $bad_alarm = 0;
if ( $event_ref->{VALARM} ) {
foreach my $alarm ( @{ $event_ref->{VALARM} } ) {
if ( $alarm->{when} && $event_ref->{DTSTART} ) {
$bad_alarm = $alarm->{when} if ($alarm->{when}->{local_c}->{year} < $event_ref->{DTSTART}->{local_c}->{year});
my $duration = $event_ref->{DTSTART} - $alarm->{when};
print "db: D[" . $event_ref->{DESCRIPTION}. " S[" . $event_ref->{SUMMARY}. "]" if ($DB > 2);
print "db: [dt=" . $event_ref->{DTSTART} . " alarm=" , $alarm->{when} . " rem=$reminder dur=$duration]\n" if ($DB > 2);
$reminder .= ($reminder) ? ',' : '';
if ( $duration->delta_days ) {
$reminder .= $duration->delta_days . 'd';
Expand All @@ -418,6 +432,20 @@ sub parse_cal {
}
}
}
if (lc $override_alarms eq 'all') {
$reminder = '';
print ":";
} elsif ($override_alarms) {
$reminder = $override_alarms . 'm';
print ".";
} elsif ($bad_alarm) {
$reminder = '15m';
$ical_messages .= "ical2vsdb: WARNING event " . $event_ref->{SUMMARY} . " on " . $event_ref->{DTSTART} . " has bad alarm date " . $bad_alarm . ". Using 15 minute default\n";
print "!";
} else {
print ".";
}
print "db2: rem2=$reminder\n" if ($DB > 2);
}

#MH Specific attributes
Expand Down Expand Up @@ -458,6 +486,8 @@ sub parse_cal {
}
}
}
print ".done\n";
print $ical_messages if ($ical_messages);
print "db: $count calendar records processed\n" if $DB;

my @todos = $cal->{todos};
Expand Down Expand Up @@ -564,6 +594,7 @@ sub init {

my $count = 0;
my $version = 0;
my $dcsicals = "";
open( CFGFILE, $config_file )
|| die "Error: Cannot open config file $config_file!";

Expand Down Expand Up @@ -601,7 +632,9 @@ sub init {
}
elsif ( lc $type eq "local_cache_dir" ) {
$local_cache = $url;

}
elsif ( lc $type eq "override_alarms" ) {
$override_alarms = $url;
}
else {
$ical_data[$count]->{type} = $type;
Expand All @@ -611,7 +644,7 @@ sub init {
$ical_data[$count]->{options}->{$key} = $value;
}
$ical_data[$count]->{hash} = "none";
print "Applying second level ical parsing (set nodscfix if this breaks anything) ...\n"
$dcsicals .= $ical_data[$count]->{options}->{name} . " "
unless ( exists $ical_data[$count]->{options}->{nodcsfix} );

my ( $method, $loc ) = split( /:\/\//, $url );
Expand All @@ -636,6 +669,8 @@ sub init {
}
}
close(CFGFILE);

print "Applied second level ical parsing to ($dcsicals).\n" if ($dcsicals);

die "Incompatible configuration file version!"
if ( $version != $config_version );
Expand Down
Loading

0 comments on commit 05d12f0

Please sign in to comment.