Skip to content

Commit

Permalink
fix parsing of year (#114)
Browse files Browse the repository at this point in the history
# *fix parsing of year*

## ♻️ Current situation & Problem
Timeyear parsing didn't seem to work correctly.


## ⚙️ Release Notes 
Expand timestampYear to 16 bits by combining data[7] and data[8] for
accurate year representation

## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).

---------

Signed-off-by: Basler182 <[email protected]>
  • Loading branch information
Basler182 authored Oct 1, 2024
1 parent 9f0ed5d commit 19283a4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:Suppress("MagicNumber")

package edu.stanford.bdh.engagehf.bluetooth.service.mapper

import android.bluetooth.BluetoothGattCharacteristic
Expand All @@ -19,7 +20,8 @@ internal class BloodPressureMapper @Inject constructor() : MeasurementMapper.Chi
*/
override fun recognises(characteristic: BluetoothGattCharacteristic?): Boolean {
return with(BLEServiceType.BLOOD_PRESSURE) {
characteristic?.let { service == it.service.uuid && this.characteristic == it.uuid } ?: false
characteristic?.let { service == it.service.uuid && this.characteristic == it.uuid }
?: false
}
}

Expand All @@ -30,8 +32,19 @@ internal class BloodPressureMapper @Inject constructor() : MeasurementMapper.Chi
* @param data The byte array representing the data of the characteristic.
* @return The blood pressure measurement, or null if the characteristic is not recognized or mapping fails.
*/
override suspend fun map(characteristic: BluetoothGattCharacteristic?, data: ByteArray): Measurement? {
return if (recognises(characteristic).not()) null else runCatching { interpretBloodPressureMeasurement(data = data) }.getOrNull()
override suspend fun map(
characteristic: BluetoothGattCharacteristic?,
data: ByteArray,
): Measurement? {
return if (recognises(characteristic).not()) {
null
} else {
runCatching {
interpretBloodPressureMeasurement(
data = data
)
}.getOrNull()
}
}

private fun interpretBloodPressureMeasurement(data: ByteArray): Measurement.BloodPressure {
Expand All @@ -46,7 +59,7 @@ internal class BloodPressureMapper @Inject constructor() : MeasurementMapper.Chi
val systolic = (data[1].toInt() and 0xFF).toFloat()
val diastolic = (data[3].toInt() and 0xFF).toFloat()
val meanArterialPressure = (data[5].toInt() and 0xFF).toFloat()
val timestampYear = (data[7].toInt() and 0xFF)
val timestampYear = (data[7].toInt() and 0xFF) or ((data[8].toInt() and 0xFF) shl 8)
val timestampMonth = (data[9].toInt() and 0xFF)
val timestampDay = (data[10].toInt() and 0xFF)
val timeStampHour = (data[11].toInt() and 0xFF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,50 @@ class BloodPressureMapperTest {
}

@Test
fun `it should map characteristic and data to blood pressure measurement`() = runTestUnconfined {
// given
val sut = mapper
val data = byteArrayOf(
0b00101111, // Flags: bloodPressureUnitsFlag, timeStampFlag, pulseRateFlag, userIdFlag, measurementStatusFlag
120, // Systolic
0,
80, // Diastolic
0,
70, // Mean Arterial Pressure
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
)

// when
val result = sut.map(characteristic, data) as Measurement.BloodPressure

// then
with(result) {
val expectedFlags = Measurement.BloodPressure.Flags(
bloodPressureUnitsFlag = true,
timeStampFlag = true,
pulseRateFlag = true,
userIdFlag = true,
measurementStatusFlag = true
fun `it should map characteristic and data to blood pressure measurement`() =
runTestUnconfined {
// given
val sut = mapper
val data = byteArrayOf(
0b00101111, // Flags: bloodPressureUnitsFlag, timeStampFlag, pulseRateFlag, userIdFlag, measurementStatusFlag
120, // Systolic
0,
80, // Diastolic
0,
70, // Mean Arterial Pressure
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
)
assertThat(flags).isEqualTo(expectedFlags)
assertThat(systolic).isEqualTo(120.0f)
assertThat(diastolic).isEqualTo(80.0f)
assertThat(meanArterialPressure).isEqualTo(70.0f)

// when
val result = sut.map(characteristic, data) as Measurement.BloodPressure

// then
with(result) {
val expectedFlags = Measurement.BloodPressure.Flags(
bloodPressureUnitsFlag = true,
timeStampFlag = true,
pulseRateFlag = true,
userIdFlag = true,
measurementStatusFlag = true
)
assertThat(flags).isEqualTo(expectedFlags)
assertThat(systolic).isEqualTo(120.0f)
assertThat(diastolic).isEqualTo(80.0f)
assertThat(meanArterialPressure).isEqualTo(70.0f)
}
}
}

@Test
fun `it should return null when characteristic is not recognised`() = runTestUnconfined {
Expand Down Expand Up @@ -118,4 +119,23 @@ class BloodPressureMapperTest {
// then
assertThat(result).isNull()
}

@Test
fun `it should return correct date when map from byteArray`() = runTestUnconfined {
// given
val data = byteArrayOf(30, -126, 0, 83, 0, 98, 0, -24, 7, 9, 30, 22, 11, 23, 80, 0, 1, 0, 0)

// when
val result = mapper.map(characteristic, data) as Measurement.BloodPressure

// then
with(result) {
assertThat(timestampYear).isEqualTo(2024)
assertThat(timestampMonth).isEqualTo(9)
assertThat(timestampDay).isEqualTo(30)
assertThat(timeStampHour).isEqualTo(22)
assertThat(timeStampMinute).isEqualTo(11)
assertThat(timeStampSecond).isEqualTo(23)
}
}
}

0 comments on commit 19283a4

Please sign in to comment.