Skip to content

StanfordBDHG/OMHModels

Repository files navigation

OMHModels

Build and Test codecov

OMHModels is a Swift library for using Open mHealth and IEEE 1752 schemas to represent mobile health data in iOS projects.

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

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

About

Swift data models for IEEE 1752 Open mHealth schemas

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages