stanfordbdhg/omhmodels
This source file is part of the OMHModels open source project
Installation
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.
Schemas
The following schemas are currently supported by this package:
IEEE 1752
- 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
Open mHealth (OMH)
Usage Example
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.
Initialization
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
)Encoding to JSON
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)")
}Decoding from JSON
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)")
}
}License
This project is licensed under the MIT License. See Licenses for more information.
Notice
This project is not officially endorsed by Open mHealth or IEEE.
Contributors
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.
[Stanford Byers Center for Biodesign Logo] [Stanford Byers Center for Biodesign Logo]
Package Metadata
Repository: stanfordbdhg/omhmodels
Default branch: main
README: README.md