|
| 1 | +#import "SSYSystemUptimer.h" |
| 2 | +#import "SSYShellTasker.h" |
| 3 | + |
| 4 | +NSString* const SSYSystemUptimerErrorDomain = @"SSYSystemUptimerErrorDomain"; |
| 5 | +NSInteger const SSYSystemUptimerSystemCommandFailedErrorCode = 214751; |
| 6 | +NSInteger const SSYSystemUptimerCouldNotParseSystemResponse = 214752; |
| 7 | + |
| 8 | +@implementation SSYSystemUptimer |
| 9 | + |
| 10 | ++ (NSDate*)lastWakeFromSleepError_p:(NSError**)error_p { |
| 11 | + NSDate* answer = nil; |
| 12 | + NSError* error = nil; |
| 13 | + |
| 14 | + /* I tested using "/usr/bin/pmset -g log", but that seemed to generate |
| 15 | + much more stdout than this: */ |
| 16 | + NSString* command = @"/usr/sbin/sysctl"; |
| 17 | + NSArray* arguments = [NSArray arrayWithObjects: |
| 18 | + @"-a", |
| 19 | + nil]; |
| 20 | + NSData* stdoutData = nil; |
| 21 | + NSInteger result = [SSYShellTasker doShellTaskCommand:command |
| 22 | + arguments:arguments |
| 23 | + inDirectory:nil |
| 24 | + stdinData:nil |
| 25 | + stdoutData_p:&stdoutData |
| 26 | + stderrData_p:NULL |
| 27 | + timeout:5.0 |
| 28 | + error_p:&error]; |
| 29 | + if (result != 0) { |
| 30 | + error = [NSError errorWithDomain:SSYSystemUptimerErrorDomain |
| 31 | + code:SSYSystemUptimerSystemCommandFailedErrorCode |
| 32 | + userInfo:@{ |
| 33 | + NSLocalizedDescriptionKey: NSLocalizedString(@"System command failed", nil), |
| 34 | + @"Command": command, |
| 35 | + @"Arguments": arguments, |
| 36 | + @"Result": [NSNumber numberWithInteger:result], |
| 37 | + NSUnderlyingErrorKey: error |
| 38 | + }]; |
| 39 | + } else { |
| 40 | + NSString* stdoutString = [[NSString alloc] initWithData:stdoutData |
| 41 | + encoding:NSUTF8StringEncoding] ; |
| 42 | + NSArray* lines = [stdoutString componentsSeparatedByString:@"\n"]; |
| 43 | + for (NSString* line in lines) { |
| 44 | + if ([line rangeOfString:@"waketime"].location != NSNotFound) { |
| 45 | + NSScanner* scanner = [[NSScanner alloc] initWithString:line]; |
| 46 | + [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] |
| 47 | + intoString:NULL]; |
| 48 | + NSString* timeString = nil; |
| 49 | + [scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] |
| 50 | + intoString:&timeString]; |
| 51 | + [scanner release]; |
| 52 | + NSTimeInterval timeSeconds = [timeString integerValue]; |
| 53 | + answer = [NSDate dateWithTimeIntervalSince1970:timeSeconds]; |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (!answer) { |
| 59 | + error = [NSError errorWithDomain:SSYSystemUptimerErrorDomain |
| 60 | + code:SSYSystemUptimerCouldNotParseSystemResponse |
| 61 | + userInfo:@{ |
| 62 | + NSLocalizedDescriptionKey: NSLocalizedString(@"Could not parse system response", nil) |
| 63 | + }]; |
| 64 | + } |
| 65 | + |
| 66 | + [stdoutString release]; |
| 67 | + } |
| 68 | + |
| 69 | + if (error && error_p) { |
| 70 | + *error_p = error ; |
| 71 | + } |
| 72 | + |
| 73 | + return answer; |
| 74 | +} |
| 75 | + |
| 76 | +@end |
0 commit comments