HealthKitOnOMH 库提供扩展,可以将支持的 HealthKit 样本转换为相应的 IEEE 标准 1752.1 和 Open mHealth (OMH) 模式。
可以使用 Swift Package Manager 将 HealthKitOnOMH 安装到您的 Xcode 项目中。
HealthKitOnOMH
包进行安装。HealthKitOnOMH 库提供扩展,可以将支持的 HealthKit 样本转换为相应的 IEEE 标准 1752.1 和 Open mHealth (OMH) 模式。
let sample: HKQuantitySample = // ...
let dataPoint: DataPoint<HealthKitQuantitySample> = try sample.omhDataPoint
在以下示例中,我们将查询 HealthKit 存储以获取步数数据,然后根据 omh:heart-rate 模式将结果样本转换为 Open mHealth 数据点。
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.
// ...
}
上面的代码将生成符合 Open mHealth 的 heart-rate 模式的以下 JSON
{
"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"
}
}
}
HKQuantityType | 支持 | Open mHealth / IEEE 1752 模式 |
---|---|---|
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 |
该项目根据 MIT 许可证获得许可。 有关更多信息,请参阅许可证。
该项目是斯坦福大学生物设计数字健康项目的一部分。 有关所有 HealthKitOnOMH 贡献者的完整列表,请参阅 CONTRIBUTORS.md。
HealthKit 是 Apple, Inc. 的注册商标。