HealthKitOnFHIR 库提供扩展,可以使用封装在 FHIRModels 中的 ResourceProxy 将支持的 HealthKit 样本转换为相应的 FHIR 资源。
有关更多信息,请参阅 API 文档。
HealthKitOnFHIR 支持
请参阅 HKObject 支持表,获取完整支持类型列表。
注意
HealthKitOnFHIR 在从 HealthKit 样本创建 FHIR Observation 时,将使用 HKMetadataKeyTimeZone 中指定时区。 如果未指定时区,HealthKitOnFHIR 将使用设备的当前时区。
可以使用 Swift Package Manager 将 HealthKitOnFHIR 安装到您的 Xcode 项目中。
HealthKitOnFHIR
包。HealthKitOnFHIR 库提供扩展,可以使用封装在 FHIRModels 中的 ResourceProxy 将支持的 HealthKit 样本转换为相应的 FHIR 资源。
let sample: HKSample = // ...
let resource = try sample.resource
HKQuantitySample
、HKCategorySample
、HKCorrelationSample
、HKElectrocardiogram
和 HKWorkout
将被转换为 FHIR Observation 资源,并封装在 ResourceProxy 中。
let sample: HKQuantitySample = // ...
let observation = try sample.resource.get(if: Observation.self)
可以通过将自定义 HKSampleMapping
实例传递给 resource(withMapping:)
方法来自定义代码和单位。
let sample: HKQuantitySample = // ...
let sampleMapping: HKSampleMapping = // ...
let observation = try sample.resource(withMapping: sampleMapping).get(if: Observation.self)
HKClinicalRecord
将根据其底层数据的类型转换为 FHIR 资源。 目前仅支持以 FHIR R4 编码的记录。
let allergyRecord: HKClinicalRecord = // ...
let allergyIntolerance = try allergyRecord.resource.get(if: AllergyIntolerance.self)
在下面的示例中,我们将查询 HealthKit 存储中的心率数据,将生成的样本转换为 FHIR 观测,并将它们编码为 JSON。
import HealthKitOnFHIR
// Initialize an HKHealthStore instance and request permissions with it
// ...
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 results to FHIR observations
let observation: Observation?
do {
try observation = sample.resource.get(if: Observation.self)
} catch {
// Handle any mapping errors here.
// ...
}
// Encode FHIR observations as JSON
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes, .sortedKeys]
guard let observation,
let data = try? encoder.encode(observation) else {
// Handle any encoding errors here.
// ...
}
// Print the resulting JSON
let json = String(decoding: data, as: UTF8.self)
print(json)
以下示例生成以下 FHIR 观测
{
"code" : {
"coding" : [
{
"code" : "8867-4",
"display" : "Heart rate",
"system" : "http://loinc.org"
}
]
},
"effectiveDateTime" : "1885-11-11T00:00:00-08:00",
"identifier" : [
{
"id" : "8BA093D9-B99B-4A3C-8C9E-98C86F49F5D8"
}
],
"issued" : "2023-01-01T00:00:00-08:00",
"resourceType" : "Observation",
"status" : "final",
"valueQuantity" : {
"code": "/min",
"unit": "beats/minute",
"system": "http://unitsofmeasure.org",
"value" : 42
}
}
此项目已获得 MIT 许可证的许可。 有关更多信息,请参阅 Licenses。
此项目是在斯坦福大学的斯坦福生物设计数字健康项目的一部分开发的。 有关所有 HealthKitOnFHIR 贡献者的完整列表,请参见 CONTRIBUTORS.md。
HealthKit 是 Apple, Inc. 的注册商标。 FHIR 是 Health Level Seven International 的注册商标。