diff --git a/modules/healthconnectonfhir/src/main/java/edu/stanford/healthconnectonfhir/HealthConnectToFHIR.kt b/modules/healthconnectonfhir/src/main/java/edu/stanford/healthconnectonfhir/HealthConnectToFHIR.kt index 1c853975b..747a4fd2b 100644 --- a/modules/healthconnectonfhir/src/main/java/edu/stanford/healthconnectonfhir/HealthConnectToFHIR.kt +++ b/modules/healthconnectonfhir/src/main/java/edu/stanford/healthconnectonfhir/HealthConnectToFHIR.kt @@ -51,7 +51,7 @@ fun T.createObservation( return observation } -inline fun StepsRecord.toObservation(): Observation { +fun StepsRecord.toObservation(): Observation { return this.createObservation( categories = listOf( Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("activity").setDisplay("Activity") @@ -65,7 +65,7 @@ inline fun StepsRecord.toObservation(): Observation { ) } -inline fun WeightRecord.toObservation(): Observation { +fun WeightRecord.toObservation(): Observation { return this.createObservation( categories = listOf( Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("vital-signs").setDisplay("Vital Signs") @@ -79,7 +79,7 @@ inline fun WeightRecord.toObservation(): Observation { ) } -inline fun HeightRecord.toObservation(): Observation { +fun HeightRecord.toObservation(): Observation { return this.createObservation( categories = listOf( Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("vital-signs").setDisplay("Vital Signs") @@ -93,7 +93,7 @@ inline fun HeightRecord.toObservation(): Observation { ) } -inline fun ActiveCaloriesBurnedRecord.toObservation(): Observation { +fun ActiveCaloriesBurnedRecord.toObservation(): Observation { return this.createObservation( categories = listOf( Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("activity").setDisplay("Activity") @@ -107,7 +107,7 @@ inline fun ActiveCaloriesBurnedRecord.toObservation(): Observation { ) } -inline fun BodyTemperatureRecord.toObservation(): Observation { +fun BodyTemperatureRecord.toObservation(): Observation { return createObservation( categories = listOf( Coding().setSystem("http://terminology.hl7.org/CodeSystem/observation-category").setCode("vital-signs").setDisplay("Vital Signs") @@ -121,7 +121,7 @@ inline fun BodyTemperatureRecord.toObservation(): Observation { ) } -inline fun BloodPressureRecord.toObservation(): Observation { +fun BloodPressureRecord.toObservation(): Observation { val observation = Observation() observation.status = Observation.ObservationStatus.FINAL @@ -167,7 +167,7 @@ inline fun BloodPressureRecord.toObservation(): Observation { return observation } -inline fun HeartRateRecord.toObservations(): List { +fun HeartRateRecord.toObservations(): List { return samples.map { sample -> val observation = Observation() observation.status = Observation.ObservationStatus.FINAL diff --git a/modules/healthconnectonfhir/src/test/java/edu/stanford/healthconnectonfhir/CreateObservationTests.kt b/modules/healthconnectonfhir/src/test/java/edu/stanford/healthconnectonfhir/CreateObservationTests.kt index 8b6308d00..42394f95b 100644 --- a/modules/healthconnectonfhir/src/test/java/edu/stanford/healthconnectonfhir/CreateObservationTests.kt +++ b/modules/healthconnectonfhir/src/test/java/edu/stanford/healthconnectonfhir/CreateObservationTests.kt @@ -6,6 +6,7 @@ import androidx.health.connect.client.records.BodyTemperatureRecord import androidx.health.connect.client.records.HeightRecord import androidx.health.connect.client.records.StepsRecord import androidx.health.connect.client.records.WeightRecord +import androidx.health.connect.client.records.HeartRateRecord import androidx.health.connect.client.units.Energy import androidx.health.connect.client.units.Length import androidx.health.connect.client.units.Mass @@ -205,4 +206,55 @@ class CreateObservationTests { assertEquals(37.5, (observation.value as Quantity).value.toDouble(), 0.0) assertEquals("°C", (observation.value as Quantity).unit) } + + @Test + fun heartRateRecord_toObservations_isCorrect() { + val heartRateRecord = HeartRateRecord( + samples = listOf( + HeartRateRecord.Sample( + time = Instant.parse("2023-05-18T10:15:30.00Z"), + beatsPerMinute = 72L + ), + HeartRateRecord.Sample( + time = Instant.parse("2023-05-18T10:16:30.00Z"), + beatsPerMinute = 75L + ) + ), + startTime = Instant.parse("2023-05-18T10:15:30.00Z"), + endTime = Instant.parse("2023-05-18T10:17:30.00Z"), + startZoneOffset = ZoneOffset.UTC, + endZoneOffset = ZoneOffset.UTC + ) + + val observations = heartRateRecord.toObservations() + + observations.forEach { observation -> + printObservationAsJson(observation) + assertEquals(Observation.ObservationStatus.FINAL, observation.status) + assertEquals("8867-4", observation.code.codingFirstRep.code) + assertEquals("vital-signs", observation.categoryFirstRep.codingFirstRep.code) + } + + assertEquals( + Date.from(Instant.parse("2023-05-18T10:15:30.00Z")), + (observations[0].effective as Period).start + ) + assertEquals( + Date.from(Instant.parse("2023-05-18T10:15:30.00Z")), + (observations[0].effective as Period).end + ) + assertEquals(72.0, (observations[0].value as Quantity).value.toDouble(), 0.0) + assertEquals("beats/minute", (observations[0].value as Quantity).unit) + + assertEquals( + Date.from(Instant.parse("2023-05-18T10:16:30.00Z")), + (observations[1].effective as Period).start + ) + assertEquals( + Date.from(Instant.parse("2023-05-18T10:16:30.00Z")), + (observations[1].effective as Period).end + ) + assertEquals(75.0, (observations[1].value as Quantity).value.toDouble(), 0.0) + assertEquals("beats/minute", (observations[1].value as Quantity).unit) + } } \ No newline at end of file