diff --git a/Pod/Classes/OMHSerializer.h b/Pod/Classes/OMHSerializer.h index 33471ce..6449317 100644 --- a/Pod/Classes/OMHSerializer.h +++ b/Pod/Classes/OMHSerializer.h @@ -45,5 +45,13 @@ */ - (NSString*)jsonForSample:(HKSample*)sample error:(NSError**)error; +/** + Serializes HealthKit samples into Open mHealth compliant json data points. + @param sample the HealthKit sample to be serialized + @param error an NSError that is passed by reference and can be checked to identify specific errors + @return an NSDictionary object containing the sample's data in a format that adheres to the appropriate Open mHealth schema. + */ +- (NSDictionary*)dictForSample:(HKSample*)sample error:(NSError**)error; + @end diff --git a/Pod/Classes/OMHSerializer.m b/Pod/Classes/OMHSerializer.m index b1f2ea3..57930e4 100644 --- a/Pod/Classes/OMHSerializer.m +++ b/Pod/Classes/OMHSerializer.m @@ -125,6 +125,53 @@ - (NSString*)jsonForSample:(HKSample*)sample error:(NSError**)error { return jsonString; } +/** + Serializes HealthKit samples into Open mHealth compliant json data points. + @param sample the HealthKit sample to be serialized + @param error an NSError that is passed by reference and can be checked to identify specific errors + @return an NSDictionary object containing the sample's data in a format that adheres to the appropriate Open mHealth schema + */ +- (NSDictionary*)dictForSample:(HKSample*)sample error:(NSError**)error { + NSParameterAssert(sample); + // first, verify we support the sample type + NSArray* supportedTypeIdentifiers = [[self class] supportedTypeIdentifiers]; + NSString* sampleTypeIdentifier = sample.sampleType.identifier; + NSString* serializerClassName; + if ([supportedTypeIdentifiers includes:sampleTypeIdentifier]){ + serializerClassName = [OMHHealthKitConstantsMapper allSupportedTypeIdentifiersToClasses][sampleTypeIdentifier]; + } + else{ + if (error) { + NSString* errorMessage = + [NSString stringWithFormat: @"Unsupported HKSample type: %@", + sampleTypeIdentifier]; + NSDictionary* userInfo = @{ NSLocalizedDescriptionKey : errorMessage }; + *error = [NSError errorWithDomain: OMHErrorDomain + code: OMHErrorCodeUnsupportedType + userInfo: userInfo]; + } + return nil; + } + // if we support it, select appropriate subclass for sample + + //For sleep analysis, the OMH schema does not capture an 'inBed' state, so if that value is set we need to use a generic category serializer + //otherwise, it defaults to using the OMH schema for the 'asleep' state. + if ([sampleTypeIdentifier isEqualToString:HKCategoryTypeIdentifierSleepAnalysis]){ + HKCategorySample* categorySample = (HKCategorySample*)sample; + if(categorySample.value == 0){ + serializerClassName = @"OMHSerializerGenericCategorySample"; + } + } + Class serializerClass = NSClassFromString(serializerClassName); + // subclass verifies it supports sample's values + if (![serializerClass canSerialize:sample error:error]) { + return nil; + } + // instantiate a serializer + OMHSerializer* serializer = [[serializerClass alloc] initWithSample:sample]; + return [serializer data]; +} + + (NSString*)parseUnitFromQuantity:(HKQuantity*)quantity { NSArray *arrayWithSplitUnitAndValue = [quantity.description componentsSeparatedByCharactersInSet:[NSCharacterSet