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

118 lines
3.8 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
@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
) {
val CHILD = 0
val TEENAGE = 1
val ADULT = 2
val RESISTANTADULT = 3
val PREGNANT = 3
2020-03-14 16:47:48 +01:00
val MAXBOLUS = doubleArrayOf(5.0, 10.0, 17.0, 25.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 MINDIA = doubleArrayOf(5.0, 5.0, 5.0, 5.0)
val MAXDIA = doubleArrayOf(7.0, 7.0, 7.0, 10.0)
val MINIC = doubleArrayOf(2.0, 2.0, 2.0, 0.5)
val MAXIC = doubleArrayOf(100.0, 100.0, 100.0, 100.0)
2020-03-14 16:47:48 +01:00
val MINISF = 2.0 // mgdl
val MAXISF = 720.0 // mgdl
val MAXIOB_AMA = doubleArrayOf(3.0, 5.0, 7.0, 12.0)
val MAXIOB_SMB = doubleArrayOf(3.0, 7.0, 12.0, 25.0)
val MAXBASAL = doubleArrayOf(2.0, 5.0, 10.0, 12.0)
2020-05-01 14:07:24 +02:00
//LGS Hard limits
//No IOB at all
val MAXIOB_LGS = 0.0
2020-03-14 16:47:48 +01:00
private fun loadAge(): Int {
val sp_age = sp.getString(R.string.key_age, "")
val age: Int
2020-03-19 20:03:44 +01:00
age = if (sp_age == resourceHelper.gs(R.string.key_child)) CHILD
else if (sp_age == resourceHelper.gs(R.string.key_teenage)) TEENAGE
else if (sp_age == resourceHelper.gs(R.string.key_adult)) ADULT
else if (sp_age == resourceHelper.gs(R.string.key_resistantadult)) RESISTANTADULT
else if (sp_age == resourceHelper.gs(R.string.key_resistantadult)) PREGNANT
2020-03-19 20:03:44 +01:00
else ADULT
2020-03-14 16:47:48 +01:00
return age
}
fun maxBolus(): Double {
return MAXBOLUS[loadAge()]
}
fun maxIobAMA(): Double {
return MAXIOB_AMA[loadAge()]
}
fun maxIobSMB(): Double {
return MAXIOB_SMB[loadAge()]
}
fun maxBasal(): Double {
return MAXBASAL[loadAge()]
}
fun minDia(): Double {
return MINDIA[loadAge()]
}
fun maxDia(): Double {
return MAXDIA[loadAge()]
}
fun minIC(): Double {
return MINIC[loadAge()]
}
fun maxIC(): Double {
return MAXIC[loadAge()]
}
2020-03-14 16:47:48 +01:00
// safety checks
fun checkOnlyHardLimits(value: Double, valueName: String?, lowLimit: Double, highLimit: Double): Boolean {
return value == verifyHardLimits(value, valueName, lowLimit, highLimit)
}
fun verifyHardLimits(value: Double, valueName: String?, lowLimit: Double, highLimit: Double): Double {
var newvalue = value
if (newvalue < lowLimit || newvalue > highLimit) {
newvalue = Math.max(newvalue, lowLimit)
newvalue = Math.min(newvalue, highLimit)
var msg = String.format(resourceHelper.gs(R.string.valueoutofrange), valueName)
msg += ".\n"
msg += String.format(resourceHelper.gs(R.string.valuelimitedto), value, newvalue)
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
}
return newvalue
}
}