// // CalendarSensor.m // Pennyworth // // Created by Chris Karr on 6/12/08. // Copyright 2008 Chris Karr. All rights reserved. // #import "CalendarSensor.h" #import #import "Context.h" #import "SensorsController.h" #define SENSOR_NAME @"Calendar" #define CALENDAR_SCAN @"Calendar_Scan" @implementation CalendarSensor - (void) awakeFromNib { [[SensorsController sharedController] addSensor:SENSOR_NAME]; [[[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(update:) userInfo:nil repeats:YES] retain] fire]; } - (void) update:(NSTimer *) theTimer { if (![[SensorsController sharedController] canFire:SENSOR_NAME]) return; NSCalendarDate * now = [NSCalendarDate date]; NSCalendarDate * startDate = [NSCalendarDate dateWithYear:[now yearOfCommonEra] month:[now monthOfYear] day:[now dayOfMonth] hour:0 minute:0 second:0 timeZone:nil]; NSCalendarDate * endDate = [NSCalendarDate dateWithYear:[now yearOfCommonEra] month:[now monthOfYear] day:[now dayOfMonth] hour:24 minute:59 second:59 timeZone:nil]; for (CalCalendar * calendar in [[CalCalendarStore defaultCalendarStore] calendars]) { NSPredicate * today = [CalCalendarStore eventPredicateWithStartDate:startDate endDate:endDate calendars:[NSArray arrayWithObject:calendar]]; NSArray * events = [[CalCalendarStore defaultCalendarStore] eventsWithPredicate:today]; NSMutableArray * titles = [NSMutableArray array]; for (CalEvent * event in events) { if ([[event endDate] timeIntervalSinceNow] >= 0 && [[event startDate] timeIntervalSinceNow] <= 0) [titles addObject:event.title]; } NSMutableDictionary * note = [NSMutableDictionary dictionary]; [note setValue:[NSString stringWithFormat:@"Calendar Sensor (%@)", calendar.title] forKey:OBSERVATION_SENSOR]; [note setValue:titles forKey:OBSERVATION_OBSERVATION]; [note setValue:[NSNumber numberWithInteger:60] forKey:OBSERVATION_LIFESPAN]; [[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note]; note = [NSMutableDictionary dictionary]; [note setValue:[NSString stringWithFormat:@"Active Event Count (%@)", calendar.title] forKey:OBSERVATION_SENSOR]; [note setValue:[NSNumber numberWithInteger:[titles count]] forKey:OBSERVATION_OBSERVATION]; [note setValue:[NSNumber numberWithInteger:60] forKey:OBSERVATION_LIFESPAN]; [[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note]; } } @end