HealthKitOnOMH

0.1.0

Converts HealthKit data to Open mHealth/IEEE 1752 schemas
StanfordBDHG/HealthKitOnOMH

What's New

0.1.0

2023-10-14T13:44:15Z

What's Changed

New Contributors

Full Changelog: https://github.com/StanfordBDHG/HealthKitOnOMH/commits/0.1.0

HealthKitOnOMH

Build and Test codecov

The HealthKitonOMH library provides extensions that convert supported HealthKit samples into corresponding IEEE Standard 1752.1 and Open mHealth (OMH) schemas.

Installation

HealthKitOnOMH can be installed into your Xcode project using Swift Package Manager.

  1. In Xcode 14 and newer (requires Swift 5.7), go to “File” » “Add Packages...”
  2. Enter the URL to this GitHub repository, then select the HealthKitOnOMH package to install.

Usage

The HealthKitonOMH library provides extensions that convert supported HealthKit samples into corresponding IEEE Standard 1752.1 and Open mHealth (OMH) schemas.

let sample: HKQuantitySample = // ...
let dataPoint: DataPoint<HealthKitQuantitySample> = try sample.omhDataPoint

Example

In the following example, we will query the HealthKit store for step count data, convert the resulting samples to Open mHealth data points based on the omh:heart-rate schema.

import HealthKitOnOMH

// Initialize an HKHealthStore instance and request permissions with it
// ...

// Or create a HealthKit sample
let date = ISO8601DateFormatter().date(from: "1885-11-11T00:00:00-08:00") ?? .now
let sample = HKQuantitySample(
    type: HKQuantityType(.heartRate),
    quantity: HKQuantity(unit: HKUnit.count().unitDivided(by: .minute()), doubleValue: 42.0),
    start: date,
    end: date
)

// Convert the sample into an Open mHealth (OMH) Data Point
let json: String
do {
    guard let omhDataPoint = try sample.omhDataPoint as? any DataPoint<HeartRate> else {
        return
    }
            
    // Encode the data point as JSON
    let encoder = JSONEncoder()
    encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes, .sortedKeys]
    
    // Note that Open mHealth uses snake case for its properties when represented in JSON
    encoder.keyEncodingStrategy = .convertToSnakeCase
            
    let data = try encoder.encode(omhDataPoint)
            
    json = String(decoding: data, as: UTF8.self)
} catch {
    // Handle any errors here.
    // ...
}

The above code will produce the following JSON in conformance with Open mHealth's heart-rate schema:

{
  "body" : {
    "effective_time_frame" : {
      "time_interval" : {
        "end_date_time" : {
          "value" : "1885-11-11T08:00:00Z"
        },
        "start_date_time" : {
          "value" : "1885-11-11T08:00:00Z"
        }
      }
    },
    "heart_rate" : {
      "unit" : "beats/min",
      "value" : 42
    }
  },
  "header" : {
    "creation_date_time" : {
      "value" : "2023-10-11T11:53:30Z"
    },
    "id" : "FF7F647D-8757-4926-871A-3D61DDCD0900",
    "schema_id" : {
      "name" : "heart-rate",
      "namespace" : "omh",
      "version" : "2.0"
    }
  }
}

Supported Types

HKQuantityType Supported Open mHealth / IEEE 1752 schema
HKQuantityTypeIdentifierBodyTemperature omh:body-temperature:3.x
HKCorrelationTypeIdentifierBloodPressure omh:blood-pressure:3.x
HKQuantityTypeIdentifierBloodGlucose omh:blood-glucose:3.x
HKQuantityTypeIdentifierBodyFatPercentage omh:body-fat-percentage:1.x
HKQuantityTypeIdentifierBodyMass omh:body-weight:2.x
HKQuantityTypeIdentifierBodyMassIndex omh:body-mass-index:1.x
HKQuantityTypeIdentifierBodyTemperature omh:body-temperature:3.x
HKQuantityTypeIdentifierHeartRate omh:heart-rate:2.x
HKQuantityTypeIdentifierHeight omh:body-height:1.x
HKQuantityTypeIdentifierOxygenSaturation omh:oxygen-saturation:2.x
HKQuantityTypeIdentifierRespiratoryRate omh:respiratory-rate:2.x
HKQuantityTypeIdentifierStepCount omh:step-count:3.x

License

This project is licensed under the MIT License. See Licenses for more information.

Contributors

This project is developed as part of the Stanford Biodesign for Digital Health projects at Stanford. See CONTRIBUTORS.md for a full list of all HealthKitOnOMH contributors.

Notices

HealthKit is a registered trademark of Apple, Inc.

Stanford Byers Center for Biodesign Logo Stanford Byers Center for Biodesign Logo

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Wed Nov 20 2024 22:30:29 GMT-1000 (Hawaii-Aleutian Standard Time)