Fix time in getHourlyBasalFromMedtrumProfileArray()

This commit is contained in:
jbr7rr 2023-05-30 13:05:32 +02:00
parent cd65639abb
commit aadf40b6fb
2 changed files with 36 additions and 19 deletions

View file

@ -19,6 +19,7 @@ import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.util.GregorianCalendar
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.round
@ -160,12 +161,13 @@ class MedtrumPump @Inject constructor(
var bolusDone = false // success end
// Last basal status update
// TODO maybe make basal parameters private? So we are forced to update trough handleBasalStatusUpdate
var lastBasalSequence = 0
var lastBasalPatchId = 0L
var lastBasalStartTime = 0L
val baseBasalRate: Double
get() = getCurrentHourlyBasalFromMedtrumProfileArray(actualBasalProfile)
get() = getHourlyBasalFromMedtrumProfileArray(actualBasalProfile, dateUtil.now())
// TBR status
val tempBasalInProgress: Boolean
@ -252,15 +254,17 @@ class MedtrumPump @Inject constructor(
return (list.size).toByteArray(1) + basals
}
fun getCurrentHourlyBasalFromMedtrumProfileArray(basalProfile: ByteArray): Double {
fun getHourlyBasalFromMedtrumProfileArray(basalProfile: ByteArray, timestamp: Long): Double {
val basalCount = basalProfile[0].toInt()
var basal = 0.0
if (basalProfile.size < 4 || (basalProfile.size - 1) % 3 != 0 || basalCount > 24) {
aapsLogger.debug(LTag.PUMP, "getCurrentHourlyBasalFromMedtrumProfileArray: No valid basal profile set")
aapsLogger.debug(LTag.PUMP, "getHourlyBasalFromMedtrumProfileArray: No valid basal profile set")
return basal
}
val hourOfDayMinutes = dateUtil.dateAndTimeString(dateUtil.now()).substring(11, 13).toInt() * 60 + dateUtil.dateAndTimeString(dateUtil.now()).substring(14, 16).toInt()
val date = GregorianCalendar()
date.timeInMillis = timestamp
val hourOfDayMinutes = date.get(GregorianCalendar.HOUR_OF_DAY) * 60 + date.get(GregorianCalendar.MINUTE)
for (index in 0 until basalCount) {
val currentIndex = 1 + (index * 3)
@ -278,10 +282,10 @@ class MedtrumPump @Inject constructor(
if (hourOfDayMinutes in startMinutes until endMinutes) {
basal = rate
aapsLogger.debug(LTag.PUMP, "getCurrentHourlyBasalFromMedtrumProfileArray: basal: $basal")
aapsLogger.debug(LTag.PUMP, "getHourlyBasalFromMedtrumProfileArray: basal: $basal")
break
}
// aapsLogger.debug(LTag.PUMP, "getCurrentHourlyBasalFromMedtrumProfileArray: rate: $rate, startMinutes: $startMinutes, endMinutes: $endMinutes")
// aapsLogger.debug(LTag.PUMP, "getHourlyBasalFromMedtrumProfileArray: rate: $rate, startMinutes: $startMinutes, endMinutes: $endMinutes")
}
return basal
}

View file

@ -2,14 +2,12 @@ package info.nightscout.pump.medtrum
import info.nightscout.core.extensions.pureProfileFromJson
import info.nightscout.core.profile.ProfileSealed
import info.nightscout.shared.utils.DateUtil
import org.json.JSONObject
import org.junit.jupiter.api.Test
import org.junit.Assert.*
import org.mockito.Mock
import org.mockito.Mockito.spy
import org.mockito.Mockito.`when`
import org.mockito.kotlin.any
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
class MedtrumPumpTest : MedtrumTestBase() {
@ -73,19 +71,34 @@ class MedtrumPumpTest : MedtrumTestBase() {
val profile = ProfileSealed.Pure(pureProfileFromJson(JSONObject(profileJSON), dateUtil)!!)
val profileArray = medtrumPump.buildMedtrumProfileArray(profile)
val localDate = LocalDate.of(2023, 1, 1)
// For 03:59
`when`(dateUtil.dateAndTimeString((any()))).thenReturn("2023-01-01T03:59:00.000Z")
val result = medtrumPump.getCurrentHourlyBasalFromMedtrumProfileArray(profileArray!!)
val localTime0399 = LocalTime.of(3, 59)
val zonedDateTime0399 = localDate.atTime(localTime0399).atZone(ZoneId.systemDefault())
val time0399 = zonedDateTime0399.toInstant().toEpochMilli()
val result = medtrumPump.getHourlyBasalFromMedtrumProfileArray(profileArray!!, time0399)
assertEquals(2.1, result, 0.01)
// For 22:30
val localTime2230 = LocalTime.of(22, 30)
val zonedDateTime2230 = localDate.atTime(localTime2230).atZone(ZoneId.systemDefault())
val time2230 = zonedDateTime2230.toInstant().toEpochMilli()
val result1 = medtrumPump.getHourlyBasalFromMedtrumProfileArray(profileArray!!, time2230)
assertEquals(1.7, result1, 0.01)
// For 23:59
`when`(dateUtil.dateAndTimeString((any()))).thenReturn("2023-01-01T23:59:59.999Z")
val result1 = medtrumPump.getCurrentHourlyBasalFromMedtrumProfileArray(profileArray!!)
assertEquals(2.0, result1, 0.01)
val localTime2359 = LocalTime.of(23, 59)
val zonedDateTime2359 = localDate.atTime(localTime2359).atZone(ZoneId.systemDefault())
val time2359 = zonedDateTime2359.toInstant().toEpochMilli()
val result2 = medtrumPump.getHourlyBasalFromMedtrumProfileArray(profileArray!!, time2359)
assertEquals(2.0, result2, 0.01)
// For 00:00
`when`(dateUtil.dateAndTimeString((any()))).thenReturn("2023-01-01T00:00:00.000Z")
val result2 = medtrumPump.getCurrentHourlyBasalFromMedtrumProfileArray(profileArray!!)
assertEquals(2.1, result2, 0.01)
val localTime0000 = LocalTime.of(0, 0)
val zonedDateTime0000 = localDate.atTime(localTime0000).atZone(ZoneId.systemDefault())
val time0000 = zonedDateTime0000.toInstant().toEpochMilli()
val result3 = medtrumPump.getHourlyBasalFromMedtrumProfileArray(profileArray!!, time0000)
assertEquals(2.1, result3, 0.01)
}
}