-
Notifications
You must be signed in to change notification settings - Fork 0
/
FitbitJSONData.m
64 lines (45 loc) · 1.76 KB
/
FitbitJSONData.m
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
//
// FitbitJSONData.m
// PulseBeat
//
// Created by Omar Metwally on 10/9/13.
// Copyright (c) 2013 Logisome. All rights reserved.
//
#import "FitbitJSONData.h"
#import "MetasomeDataPointStore.h"
@implementation FitbitJSONData
@synthesize name, jsonData, numberOfPoints;
-(id)initWithDictionary:(NSDictionary *)dict dataName:(NSString *)dn
{
self = [super init];
if (self) {
[self setName:dn];
jsonData = dict;
[self setNumberOfPoints:[[jsonData objectForKey:[self name]] count]];
}
return self;
}
-(void)saveToDataPointStore:(NSString *)parameterName
{
if ( [[jsonData objectForKey:[self name]] count] < 1 )
return;
for (NSDictionary *d in [jsonData objectForKey:[self name]] ) {
NSString *dateString = [d objectForKey:@"dateTime"];
NSString *valueString = [d objectForKey:@"value"];
// NSLog(@"dateString = %@, valueString = %@", dateString, valueString);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateString];
float valueToSave = [valueString floatValue];
// in case of sleep, convert minutes asleep to hours
if ( [parameterName isEqualToString:@"Sleep hours"] ) {
valueToSave = (valueToSave / 60.0);
}
[[MetasomeDataPointStore sharedStore] addPointWithName:parameterName value:valueToSave date:date.timeIntervalSince1970 options:noOptions fromApi:@"Fitbit"];
BOOL result = [[MetasomeDataPointStore sharedStore] saveChanges];
if (!result) {
NSLog(@"Error saving Fitbit data to CoreData!");
}
}
}
@end