NSC: better TBR sync

This commit is contained in:
Milos Kozak 2021-12-03 09:33:17 +01:00
parent d7a4740e7d
commit 682628e845

View file

@ -70,11 +70,11 @@ fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil):
.put("isValid", isValid)
.put("duration", T.msecs(duration).mins())
.put("durationInMilliseconds", duration) // rounded duration leads to different basal IOB
.put("rate", rate)
.put("type", type.name)
.put("isAbsolute", isAbsolute)
.put("absolute", convertedToAbsolute(timestamp, profile))
.put("rate", convertedToAbsolute(timestamp, profile)) // generated by OpenAPS, for compatibility
.also {
if (isAbsolute) it.put("absolute", rate)
else it.put("percent", rate - 100)
if (interfaceIDs.pumpId != null) it.put("pumpId", interfaceIDs.pumpId)
if (interfaceIDs.endId != null) it.put("endId", interfaceIDs.endId)
if (interfaceIDs.pumpType != null) it.put("pumpType", interfaceIDs.pumpType!!.name)
@ -82,11 +82,9 @@ fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil):
if (isAdd && interfaceIDs.nightscoutId != null) it.put("_id", interfaceIDs.nightscoutId)
}
@Suppress("IfThenToElvis")
@Suppress("IfThenToElvis", "CascadeIf")
fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
var rate = JsonHelper.safeGetDoubleAllowNull(jsonObject, "rate")
var isAbsolute = JsonHelper.safeGetBooleanAllowNull(jsonObject, "isAbsolute")
val percent = JsonHelper.safeGetDoubleAllowNull(jsonObject, "percent")
val absolute = JsonHelper.safeGetDoubleAllowNull(jsonObject, "absolute")
val duration = JsonHelper.safeGetLongAllowNull(jsonObject, "duration") ?: return null
@ -99,17 +97,16 @@ fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? {
val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null))
val pumpSerial = JsonHelper.safeGetStringAllowNull(jsonObject, "pumpSerial", null)
if (rate == null) {
if (absolute != null) {
rate = absolute
isAbsolute = true
} else if (percent != null) {
rate = percent + 100
isAbsolute = false
} else return null
}
if (isAbsolute == null) return null
if (duration == 0L) return null
val rate: Double
val isAbsolute: Boolean
if (absolute != null) {
rate = absolute
isAbsolute = true
} else if (percent != null) {
rate = percent + 100
isAbsolute = false
} else return null
if (duration == 0L && durationInMilliseconds == null) return null
if (timestamp == 0L) return null
return TemporaryBasal(
@ -173,7 +170,15 @@ fun TemporaryBasal.iobCalc(time: Long, profile: Profile, insulinInterface: Insul
return result
}
fun TemporaryBasal.iobCalc(time: Long, profile: Profile, lastAutosensResult: AutosensResult, exercise_mode: Boolean, half_basal_exercise_target: Int, isTempTarget: Boolean, insulinInterface: Insulin): IobTotal {
fun TemporaryBasal.iobCalc(
time: Long,
profile: Profile,
lastAutosensResult: AutosensResult,
exercise_mode: Boolean,
half_basal_exercise_target: Int,
isTempTarget: Boolean,
insulinInterface: Insulin
): IobTotal {
val result = IobTotal(time)
val realDuration = getPassedDurationToTimeInMinutes(time)
var netBasalAmount = 0.0