Skip to content

Commit

Permalink
Use TemporaryBasalExtension in Garmin /sgv.json endpoint, resolves ni…
Browse files Browse the repository at this point in the history
  • Loading branch information
suside committed Jan 27, 2025
1 parent 5b80a0b commit fb0602f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import app.aaps.core.data.ue.ValueWithUnit
import app.aaps.core.interfaces.aps.Loop
import app.aaps.core.interfaces.constraints.ConstraintsChecker
import app.aaps.core.interfaces.db.PersistenceLayer
import app.aaps.core.interfaces.db.ProcessedTbrEbData
import app.aaps.core.interfaces.iob.IobCobCalculator
import app.aaps.core.interfaces.logging.AAPSLogger
import app.aaps.core.interfaces.logging.LTag
Expand All @@ -24,6 +25,7 @@ import app.aaps.core.interfaces.queue.CommandQueue
import app.aaps.core.keys.Preferences
import app.aaps.core.keys.StringKey
import app.aaps.core.keys.UnitDoubleKey
import app.aaps.core.objects.extensions.convertedToPercent
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import java.time.Clock
Expand All @@ -45,7 +47,8 @@ class LoopHubImpl @Inject constructor(
private val profileUtil: ProfileUtil,
private val persistenceLayer: PersistenceLayer,
private val userEntryLogger: UserEntryLogger,
private val preferences: Preferences
private val preferences: Preferences,
private val processedTbrEbData: ProcessedTbrEbData
) : LoopHub {

val disposable = CompositeDisposable()
Expand Down Expand Up @@ -89,8 +92,10 @@ class LoopHubImpl @Inject constructor(
/** Returns the factor by which the basal rate is currently raised (> 1) or lowered (< 1). */
override val temporaryBasal: Double
get() {
val apsResult = loop.lastRun?.constraintsProcessed
return if (apsResult == null) Double.NaN else apsResult.percent / 100.0
return currentProfile?.let {
val tb = processedTbrEbData.getTempBasalIncludingConvertedExtended(clock.millis())
tb?.convertedToPercent(clock.millis(), it)?.div(100.0)
} ?: Double.NaN
}

override val lowGlucoseMark get() = profileUtil.convertToMgdl(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package app.aaps.plugins.sync.garmin

import app.aaps.core.data.iob.CobInfo
import app.aaps.core.interfaces.aps.IobTotal
import app.aaps.core.data.model.EPS
import app.aaps.core.data.model.GV
import app.aaps.core.data.model.GlucoseUnit
import app.aaps.core.data.model.HR
import app.aaps.core.data.model.ICfg
import app.aaps.core.data.model.OE
import app.aaps.core.data.model.SourceSensor
import app.aaps.core.data.model.TB
import app.aaps.core.data.model.TE
import app.aaps.core.data.model.TrendArrow
import app.aaps.core.data.ue.Action
import app.aaps.core.data.ue.Sources
import app.aaps.core.data.ue.ValueWithUnit
import app.aaps.core.interfaces.aps.APSResult
import app.aaps.core.interfaces.aps.IobTotal
import app.aaps.core.interfaces.aps.Loop
import app.aaps.core.interfaces.constraints.Constraint
import app.aaps.core.interfaces.constraints.ConstraintsChecker
import app.aaps.core.interfaces.db.PersistenceLayer
import app.aaps.core.interfaces.db.ProcessedTbrEbData
import app.aaps.core.interfaces.iob.IobCobCalculator
import app.aaps.core.interfaces.logging.UserEntryLogger
import app.aaps.core.interfaces.profile.Profile
Expand Down Expand Up @@ -63,6 +64,7 @@ class LoopHubTest : TestBase() {
@Mock lateinit var persistenceLayer: PersistenceLayer
@Mock lateinit var userEntryLogger: UserEntryLogger
@Mock lateinit var preferences: Preferences
@Mock lateinit var processedTbrEbData: ProcessedTbrEbData

private lateinit var loopHub: LoopHubImpl
private val clock = Clock.fixed(Instant.ofEpochMilli(10_000), ZoneId.of("UTC"))
Expand All @@ -76,7 +78,7 @@ class LoopHubTest : TestBase() {
}
loopHub = LoopHubImpl(
aapsLogger, commandQueue, constraints, iobCobCalculator, loop,
profileFunction, profileUtil, persistenceLayer, userEntryLogger, preferences
profileFunction, profileUtil, persistenceLayer, userEntryLogger, preferences, processedTbrEbData
)
loopHub.clock = clock
}
Expand All @@ -92,7 +94,7 @@ class LoopHubTest : TestBase() {
verifyNoMoreInteractions(userEntryLogger)
}

@Test
@Test
fun testCurrentProfile() {
val profile = mock<Profile>()
whenever(profileFunction.getProfile()).thenReturn(profile)
Expand Down Expand Up @@ -204,19 +206,39 @@ class LoopHubTest : TestBase() {

@Test
fun testTemporaryBasal() {
val apsResult = mock<APSResult>()
whenever(apsResult.percent).thenReturn(45)
val lastRun = Loop.LastRun().apply { constraintsProcessed = apsResult }
whenever(loop.lastRun).thenReturn(lastRun)
val profile = mock<Profile>()
whenever(profileFunction.getProfile()).thenReturn(profile)
val tb = mock<TB> {
on { isAbsolute }.thenReturn(false)
on { rate }.thenReturn(45.0)
}
whenever(processedTbrEbData.getTempBasalIncludingConvertedExtended(clock.millis())).thenReturn(tb)
assertEquals(0.45, loopHub.temporaryBasal, 1e-6)
verify(profileFunction, times(1)).getProfile()
}

@Test
fun testTemporaryBasalAbsolute() {
val profile = mock<Profile> {
onGeneric { getBasal(clock.millis()) }.thenReturn(2.0)
}
whenever(profileFunction.getProfile()).thenReturn(profile)
val tb = mock<TB> {
on { isAbsolute }.thenReturn(true)
on { rate }.thenReturn(0.9)
}
whenever(processedTbrEbData.getTempBasalIncludingConvertedExtended(clock.millis())).thenReturn(tb)
assertEquals(0.45, loopHub.temporaryBasal, 1e-6)
verify(loop).lastRun
verify(profileFunction, times(1)).getProfile()
}

@Test
fun testTemporaryBasalNoRun() {
whenever(loop.lastRun).thenReturn(null)
val profile = mock<Profile>()
whenever(profileFunction.getProfile()).thenReturn(profile)
whenever(processedTbrEbData.getTempBasalIncludingConvertedExtended(clock.millis())).thenReturn(null)
assertTrue(loopHub.temporaryBasal.isNaN())
verify(loop, times(1)).lastRun
verify(profileFunction, times(1)).getProfile()
}

@Test
Expand Down

0 comments on commit fb0602f

Please sign in to comment.