OMHModels is a Swift library for using Open mHealth and IEEE 1752 schemas to represent mobile health data in iOS projects.
The project can be added to your Xcode project or Swift Package using the Swift Package Manager.
Xcode: For an Xcode project, follow the instructions on adding package dependencies to your app.
Swift Package: You can follow the Swift Package Manager documentation about defining dependencies to add this project as a dependency to your Swift Package.
The following schemas are currently supported by this package:
- body-posture
- data-point
- date-time
- descriptive-statistic
- descriptive-statistic-denominator
- duration-unit-value
- duration-unit-value-range
- frequency-unit-value
- schema-id
- header
- physical-activity
- time-frame
- time-interval
- total-sleep-time
- unit-value
- unit-value-range
- blood-glucose
- blood-pressure
- body-fat-percentage -body-height
- body-location
- body-mass-index
- body-temperature
- body-weight
- heart-rate
- oxygen-saturation
- part-of-day
- respiratory-rate
- specimen-source
- step-count
- temporal-relationship-to-meal
- temporal-relationship-to-physical-activity
- temporal-relationship-to-sleep
The OMHModels
package can be used to represent mobile health data in accordance with the above schemas within your iOS application.
Below is an example using TotalSleepTime
, although the same instructions apply to any schema in the package.
To create an instance of the TotalSleepTime
struct:
let sleepTime = TotalSleepTime(
totalSleepTime: DurationUnitValue(value: 8, unit: .hour),
effectiveTimeFrame: TimeFrame(timeInterval:
TimeInterval(
startDateTime: // Date,
endDateTime: // Date
),
isMainSleep: true,
descriptiveStatistic: .average,
descriptiveStatisticDenominator: .week
)
To encode the TotalSleepTime
instance to JSON:
let json: String
do {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes, .sortedKeys]
// Note that both IEEE 1752 and Open mHealth use snake case for its properties when represneted in JSON
encoder.keyCodingStrategy = .convertToSnakeCase
let data = try encoder.encode(sleepTime)
json = String(data: jsonData, encoding: .utf8)
} catch {
print("Error encoding to JSON: \(error)")
}
To decode a JSON string back to a TotalSleepTime
instance:
let jsonString = /* your JSON string here */
if let jsonData = jsonString.data(using: .utf8) {
do {
let decoder = JSONDecoder()
let decodedSleepTime = try decoder.decode(TotalSleepTime.self, from: jsonData)
print(decodedSleepTime)
} catch {
print("Error decoding from JSON: \(error)")
}
}
This project is licensed under the MIT License. See Licenses for more information.
This project is not officially endorsed by Open mHealth or IEEE.
This project is developed as part of the Stanford Byers Center for Biodesign at Stanford University. See CONTRIBUTORS.md for a full list of all OMHModels contributors.