Add LocalInsulin class and update BolusExtension with iobCalc done with LocalInsulin
This commit is contained in:
parent
50d60e1302
commit
a708c45bfb
|
@ -0,0 +1,45 @@
|
|||
package info.nightscout.androidaps.data
|
||||
|
||||
import info.nightscout.androidaps.database.entities.Bolus
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.pow
|
||||
|
||||
class LocalInsulin constructor(val name:String?, val peak:Int = DEFAULT_PEAK, private val userDefinedDia: Double = DEFAULT_DIA) {
|
||||
val dia
|
||||
get(): Double {
|
||||
val dia = userDefinedDia
|
||||
return if (dia >= MIN_DIA) {
|
||||
dia
|
||||
} else {
|
||||
MIN_DIA
|
||||
}
|
||||
}
|
||||
|
||||
val duration
|
||||
get() = (60 * 60 * 1000L * dia).toLong()
|
||||
|
||||
fun iobCalcForTreatment(bolus: Bolus, time: Long): Iob {
|
||||
val result = Iob()
|
||||
if (bolus.amount != 0.0) {
|
||||
val bolusTime = bolus.timestamp
|
||||
val t = (time - bolusTime) / 1000.0 / 60.0
|
||||
val td = dia * 60 //getDIA() always >= MIN_DIA
|
||||
val tp = peak.toDouble()
|
||||
// force the IOB to 0 if over DIA hours have passed
|
||||
if (t < td) {
|
||||
val tau = tp * (1 - tp / td) / (1 - 2 * tp / td)
|
||||
val a = 2 * tau / td
|
||||
val S = 1 / (1 - a + (1 + a) * exp(-td / tau))
|
||||
result.activityContrib = bolus.amount * (S / tau.pow(2.0)) * t * (1 - t / td) * exp(-t / tau)
|
||||
result.iobContrib = bolus.amount * (1 - S * (1 - a) * ((t.pow(2.0) / (tau * td * (1 - a)) - t / tau - 1) * Math.exp(-t / tau) + 1))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val MIN_DIA = 5.0
|
||||
private const val DEFAULT_DIA = 6.0
|
||||
private const val DEFAULT_PEAK = 75
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package info.nightscout.androidaps.extensions
|
||||
|
||||
import info.nightscout.androidaps.data.Iob
|
||||
import info.nightscout.androidaps.data.LocalInsulin
|
||||
import info.nightscout.androidaps.database.embedments.InterfaceIDs
|
||||
import info.nightscout.androidaps.database.entities.Bolus
|
||||
import info.nightscout.androidaps.database.entities.TherapyEvent
|
||||
|
@ -16,6 +17,12 @@ fun Bolus.iobCalc(activePlugin: ActivePlugin, time: Long, dia: Double): Iob {
|
|||
return insulinInterface.iobCalcForTreatment(this, time, dia)
|
||||
}
|
||||
|
||||
// Add specific calculation for Autotune (reference localInsulin for Peak/dia)
|
||||
fun Bolus.iobCalc(time: Long, localInsulin: LocalInsulin): Iob {
|
||||
if (!isValid || type == Bolus.Type.PRIMING ) return Iob()
|
||||
return localInsulin.iobCalcForTreatment(this, time)
|
||||
}
|
||||
|
||||
fun Bolus.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
|
||||
JSONObject()
|
||||
.put("eventType", if (type == Bolus.Type.SMB) TherapyEvent.Type.CORRECTION_BOLUS.text else TherapyEvent.Type.MEAL_BOLUS.text)
|
||||
|
|
Loading…
Reference in a new issue