AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/HardLimits.kt

97 lines
3.9 KiB
Kotlin
Raw Normal View History

2020-03-14 16:47:48 +01:00
package info.nightscout.androidaps.utils
import android.content.Context
import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.AAPSLogger
2020-03-21 15:09:14 +01:00
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
2020-03-14 16:47:48 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import javax.inject.Inject
import javax.inject.Singleton
2021-03-07 19:26:08 +01:00
import kotlin.math.max
import kotlin.math.min
2020-03-14 16:47:48 +01:00
@Singleton
class HardLimits @Inject constructor(
2020-05-09 10:52:20 +02:00
private val aapsLogger: AAPSLogger,
private val rxBus: RxBusWrapper,
private val sp: SP,
private val resourceHelper: ResourceHelper,
private val context: Context,
private val nsUpload: NSUpload
2020-03-14 16:47:48 +01:00
) {
2021-03-07 19:26:08 +01:00
companion object {
private const val CHILD = 0
private const val TEENAGE = 1
private const val ADULT = 2
private const val RESISTANT_ADULT = 3
private const val PREGNANT = 4
private val MAX_BOLUS = doubleArrayOf(5.0, 10.0, 17.0, 25.0, 60.0)
// Very Hard Limits Ranges
// First value is the Lowest and second value is the Highest a Limit can define
val VERY_HARD_LIMIT_MIN_BG = intArrayOf(72, 180)
val VERY_HARD_LIMIT_MAX_BG = intArrayOf(90, 270)
val VERY_HARD_LIMIT_TARGET_BG = intArrayOf(80, 200)
// Very Hard Limits Ranges for Temp Targets
val VERY_HARD_LIMIT_TEMP_MIN_BG = intArrayOf(72, 180)
val VERY_HARD_LIMIT_TEMP_MAX_BG = intArrayOf(72, 270)
val VERY_HARD_LIMIT_TEMP_TARGET_BG = intArrayOf(72, 200)
val MIN_DIA = doubleArrayOf(5.0, 5.0, 5.0, 5.0, 5.0)
val MAX_DIA = doubleArrayOf(7.0, 7.0, 7.0, 7.0, 10.0)
val MIN_IC = doubleArrayOf(2.0, 2.0, 2.0, 2.0, 0.3)
val MAX_IC = doubleArrayOf(100.0, 100.0, 100.0, 100.0, 100.0)
const val MIN_ISF = 2.0 // mgdl
const val MAX_ISF = 720.0 // mgdl
val MAX_IOB_AMA = doubleArrayOf(3.0, 5.0, 7.0, 12.0, 25.0)
val MAX_IOB_SMB = doubleArrayOf(3.0, 7.0, 12.0, 25.0, 40.0)
val MAX_BASAL = doubleArrayOf(2.0, 5.0, 10.0, 12.0, 25.0)
//LGS Hard limits
//No IOB at all
const val MAX_IOB_LGS = 0.0
2020-03-14 16:47:48 +01:00
}
2021-03-07 19:26:08 +01:00
private fun loadAge(): Int = when (sp.getString(R.string.key_age, "")) {
resourceHelper.gs(R.string.key_child) -> CHILD
resourceHelper.gs(R.string.key_teenage) -> TEENAGE
resourceHelper.gs(R.string.key_adult) -> ADULT
resourceHelper.gs(R.string.key_resistantadult) -> RESISTANT_ADULT
resourceHelper.gs(R.string.key_pregnant) -> PREGNANT
else -> ADULT
2020-03-14 16:47:48 +01:00
}
2021-03-07 19:26:08 +01:00
fun maxBolus(): Double = MAX_BOLUS[loadAge()]
fun maxIobAMA(): Double = MAX_IOB_AMA[loadAge()]
fun maxIobSMB(): Double = MAX_IOB_SMB[loadAge()]
fun maxBasal(): Double = MAX_BASAL[loadAge()]
fun minDia(): Double = MIN_DIA[loadAge()]
fun maxDia(): Double = MAX_DIA[loadAge()]
fun minIC(): Double = MIN_IC[loadAge()]
fun maxIC(): Double = MAX_IC[loadAge()]
2020-03-14 16:47:48 +01:00
// safety checks
2021-03-07 19:26:08 +01:00
fun checkOnlyHardLimits(value: Double, valueName: Int, lowLimit: Double, highLimit: Double): Boolean =
value == verifyHardLimits(value, valueName, lowLimit, highLimit)
2020-03-14 16:47:48 +01:00
2021-03-03 00:07:02 +01:00
fun verifyHardLimits(value: Double, valueName: Int, lowLimit: Double, highLimit: Double): Double {
2021-03-07 19:26:08 +01:00
var newValue = value
if (newValue < lowLimit || newValue > highLimit) {
newValue = max(newValue, lowLimit)
newValue = min(newValue, highLimit)
2021-03-03 00:07:02 +01:00
var msg = String.format(resourceHelper.gs(R.string.valueoutofrange), resourceHelper.gs(valueName))
2020-03-14 16:47:48 +01:00
msg += ".\n"
2021-03-07 19:26:08 +01:00
msg += String.format(resourceHelper.gs(R.string.valuelimitedto), value, newValue)
2020-03-14 16:47:48 +01:00
aapsLogger.error(msg)
2020-05-09 10:52:20 +02:00
nsUpload.uploadError(msg)
2020-03-21 15:09:14 +01:00
ToastUtils.showToastInUiThread(context, rxBus, msg, R.raw.error)
2020-03-14 16:47:48 +01:00
}
2021-03-07 19:26:08 +01:00
return newValue
2020-03-14 16:47:48 +01:00
}
}