From 2eeb56e093bce917aae0bb7561aa5447ffe8088e Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Sat, 2 Sep 2017 22:48:58 -0500 Subject: [PATCH] Prevent repeate ICE values --- Loop/Managers/LoopDataManager.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Loop/Managers/LoopDataManager.swift b/Loop/Managers/LoopDataManager.swift index 2981699635..e3f66cb473 100644 --- a/Loop/Managers/LoopDataManager.swift +++ b/Loop/Managers/LoopDataManager.swift @@ -800,6 +800,13 @@ final class LoopDataManager { insulinEffect.filterDateRange(startDate, endDate) ) + // Ensure we're not repeating effects + if let lastEffect = insulinCounteractionEffects.last { + guard startDate >= lastEffect.endDate else { + return + } + } + // Compare that retrospective, insulin-driven prediction to the actual glucose change to // calculate the effect of all insulin counteraction guard let lastGlucose = prediction.last else { return } @@ -807,7 +814,7 @@ final class LoopDataManager { let velocityUnit = glucoseUnit.unitDivided(by: HKUnit.second()) let discrepancy = change.end.quantity.doubleValue(for: glucoseUnit) - lastGlucose.quantity.doubleValue(for: glucoseUnit) // mg/dL let averageVelocity = HKQuantity(unit: velocityUnit, doubleValue: discrepancy / change.end.endDate.timeIntervalSince(change.start.endDate)) - let effect = GlucoseEffectVelocity(startDate: change.start.startDate, endDate: change.end.startDate, quantity: averageVelocity) + let effect = GlucoseEffectVelocity(startDate: startDate, endDate: change.end.startDate, quantity: averageVelocity) insulinCounteractionEffects.append(effect) // For now, only keep the last 24 hours of values