From 8f3f9a7a65006176bd84b361ebfeb7bcb34f7ef3 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 7 Nov 2021 22:50:34 +0100 Subject: [PATCH] always upload TBR absolute --- .../extensions/TemporaryBasalExtension.kt | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 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 8c10b1ac1b..b80ca45c65 100644 --- a/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt +++ b/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt @@ -74,7 +74,6 @@ fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil): .put("type", type.name) .put("isAbsolute", isAbsolute) .put("absolute", convertedToAbsolute(timestamp, profile)) - .put("percent", convertedToPercent(timestamp, profile) - 100) .also { if (interfaceIDs.pumpId != null) it.put("pumpId", interfaceIDs.pumpId) if (interfaceIDs.endId != null) it.put("endId", interfaceIDs.endId) @@ -83,19 +82,7 @@ fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil): if (isAdd && interfaceIDs.nightscoutId != null) it.put("_id", interfaceIDs.nightscoutId) } -/* - create fake object with nsID and isValid == false - */ -fun temporaryBasalFromNsIdForInvalidating(nsId: String): TemporaryBasal = - temporaryBasalFromJson( - JSONObject() - .put("mills", 1) - .put("absolute", 1.0) - .put("duration", 1.0) - .put("_id", nsId) - .put("isValid", false) - )!! - +@Suppress("IfThenToElvis") fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? { val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null var rate = JsonHelper.safeGetDoubleAllowNull(jsonObject, "rate") @@ -112,8 +99,16 @@ fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? { val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null)) val pumpSerial = JsonHelper.safeGetStringAllowNull(jsonObject, "pumpSerial", null) - rate = rate ?: if (percent != null) percent + 100 else absolute ?: return null - isAbsolute = isAbsolute ?: percent == 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 if (timestamp == 0L) return null