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