From 682628e84543c3a8e5553c81b49832d8ce1cbc89 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 3 Dec 2021 09:33:17 +0100 Subject: [PATCH] NSC: better TBR sync --- .../extensions/TemporaryBasalExtension.kt | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt b/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt index 1e2ba381a7..660a9d6ced 100644 --- a/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt +++ b/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt @@ -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