public
Description: My OS X Thermometer client.
Clone URL: git://github.com/dustin/osx-thermometer.git
osx-thermometer / Readings.m
100644 140 lines (123 sloc) 3.348 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// arch-tag: ACD5D879-7A7E-11D9-9C8D-000A957659CC
#import "Readings.h"
#import "Thermometer.h"
#import "TempReading.h"
#import "SparklineCell.h"
 
@implementation Readings
 
-(void)awakeFromNib
{
  therms=[[NSArray alloc] initWithObjects: nil];
  dateFormat=[[NSDateFormatter alloc] initWithDateFormat:@"%A, %H:%M:%S"
    allowNaturalLanguage:YES];
 
  // Get the default units
  NSString *ustring=[[NSUserDefaults standardUserDefaults]
    objectForKey:@"units"];
  [self setUnits: ustring];
 
  [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(gotThermometerList:)
        name:THERM_LIST
        object:nil];
 
  [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(dataUpdated:)
        name:DATA_UPDATED
        object:nil];
 
  [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(unitChanged:)
        name:UNIT_CHANGE
        object:nil];
}
 
-(void)setUnits:(NSString *)to
{
  if([to isEqual:@"c"]) {
    celsius=YES;
  } else {
    celsius=NO;
  }
}
 
-(void)unitChanged:(id)ob
{
  [self setUnits:[ob object]];
}
 
-(void)gotThermometerList:(id)notification
{
  // NSLog(@"Got a thermometer list: %@", notification);
  [therms release];
  therms=[notification object];
  [therms retain];
  NSLog(@"Got my thermometers: %@", therms);
}
 
-(void)dataUpdated:(id)anObject
{
  // NSLog(@"Data was updated");
}
 
- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
{
  id rv=nil;
  if(item == nil) {
    rv=[therms objectAtIndex: index];
  } else {
    // NSLog(@"Getting reading at index %d", index);
    rv=[[item lastReadings] objectAtIndex: index];
  }
  return(rv);
}
 
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
  BOOL rv=NO;
  if([item isKindOfClass: [Thermometer class]]) {
    rv=YES;
  }
  return(rv);
}
 
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
  int rv=0;
  if(item == nil) {
    rv=[therms count];
  } else if([item isKindOfClass: [Thermometer class]]) {
    rv=[[item lastReadings] count];
  }
  return(rv);
}
 
- (id)outlineView:(NSOutlineView *)outlineView
  objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
  // NSLog(@"Asking for object value of %@ of item %@", [tableColumn identifier], item);
  id rv=nil;
  if([@"reading" isEqual:[tableColumn identifier]]) {
    float reading=[item reading];
    if(!celsius) {
      reading=CTOF(reading);
    }
    rv=[NSString stringWithFormat: @"%.2f", reading];
  } else if([@"graph" isEqual:[tableColumn identifier]]) {
    if([item isKindOfClass: [Thermometer class]]) {
      rv=[[NSMutableArray alloc]
        initWithCapacity: [[item lastReadings] count]];
      [rv autorelease];
      NSEnumerator *e=[[item lastReadings] objectEnumerator];
      TempReading *tr=nil;
      while( (tr = [e nextObject]) != nil) {
        SparklineDatum *d=[[SparklineDatum alloc]
          initWithTimestamp: [[tr readingTimestamp]
                    timeIntervalSince1970]
          value: [NSNumber numberWithFloat: [tr floatValue]]];
        [rv addObject: d];
 
        [d release];
      }
    }
  } else {
    if([item isKindOfClass: [Thermometer class]]) {
      NSString *label=[NSString stringWithFormat: @"%@ (%d)",
        [item name], [[item lastReadings] count]];
      rv=label;
    } else {
      rv=[dateFormat stringForObjectValue: [item readingTimestamp]];
    }
  }
  return(rv);
}
 
@end