// // IdleTimeSensor.m // Pennyworth // // Created by Chris Karr on 12/24/07. // Copyright 2007 Chris J. Karr. All rights reserved. // #import "IdleTimeSensor.h" #import "IdleTime.h" #import "Context.h" #import "SensorsController.h" #define ACTIVITY_SENSOR_NAME @"User Activity Level" #define IDLE_SENSOR_NAME @"Idle Time" #define DAY 86400 #define HOUR 3600 #define MINUTE 60 @implementation IdleTimeSensor - (void) awakeFromNib { [[SensorsController sharedController] addSensor:ACTIVITY_SENSOR_NAME]; [[SensorsController sharedController] addSensor:IDLE_SENSOR_NAME]; active = 30.0; [[[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(update:) userInfo:nil repeats:YES] retain] fire]; } - (void) update: (NSTimer *) theTimer { long recentIdle = getSystemIdleTime (); if (![[SensorsController sharedController] canFire:ACTIVITY_SENSOR_NAME]) { NSMutableDictionary * note = [NSMutableDictionary dictionary]; [note setValue:ACTIVITY_SENSOR_NAME forKey:OBSERVATION_SENSOR]; [[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note]; } else { if (recentIdle < 5) active = active + 5.0; double load = active / 60.0; NSMutableDictionary * note = [NSMutableDictionary dictionary]; [note setValue:ACTIVITY_SENSOR_NAME forKey:OBSERVATION_SENSOR]; [note setValue:[NSNumber numberWithDouble:[[NSString stringWithFormat:@"%0.2f", load] doubleValue]] forKey:OBSERVATION_OBSERVATION]; [[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note]; active = load * 55.0; } if (![[SensorsController sharedController] canFire:IDLE_SENSOR_NAME]) { NSMutableDictionary * note = [NSMutableDictionary dictionary]; [note setValue:IDLE_SENSOR_NAME forKey:OBSERVATION_SENSOR]; [[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note]; return; } else { NSMutableDictionary * note = [NSMutableDictionary dictionary]; [note setValue:IDLE_SENSOR_NAME forKey:OBSERVATION_SENSOR]; [note setValue:[NSNumber numberWithLong:recentIdle] forKey:OBSERVATION_OBSERVATION]; [[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note]; } } @end