Skip to content

DRAFT: add safety features: maxCarbEntry, maxAutoIOB #1870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
B4E96D5D248A82A2002DABAD /* StatusBarHUDView.xib in Resources */ = {isa = PBXBuildFile; fileRef = B4E96D5C248A82A2002DABAD /* StatusBarHUDView.xib */; };
B4F3D25124AF890C0095CE44 /* BluetoothStateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4F3D25024AF890C0095CE44 /* BluetoothStateManager.swift */; };
B4FEEF7D24B8A71F00A8DF9B /* DeviceDataManager+DeviceStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4FEEF7C24B8A71F00A8DF9B /* DeviceDataManager+DeviceStatus.swift */; };
B633A70E291011EE00A39E1D /* Settings.bundle in Resources */ = {isa = PBXBuildFile; fileRef = B633A70D291011EE00A39E1D /* Settings.bundle */; };
C10B28461EA9BA5E006EA1FC /* far_future_high_bg_forecast.json in Resources */ = {isa = PBXBuildFile; fileRef = C10B28451EA9BA5E006EA1FC /* far_future_high_bg_forecast.json */; };
C11B9D5B286778A800500CF8 /* SwiftCharts in Frameworks */ = {isa = PBXBuildFile; productRef = C11B9D5A286778A800500CF8 /* SwiftCharts */; };
C11B9D5E286778D000500CF8 /* LoopKitUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C11B9D5D286778D000500CF8 /* LoopKitUI.framework */; };
Expand Down Expand Up @@ -1438,6 +1439,7 @@
B4E96D5C248A82A2002DABAD /* StatusBarHUDView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = StatusBarHUDView.xib; sourceTree = "<group>"; };
B4F3D25024AF890C0095CE44 /* BluetoothStateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothStateManager.swift; sourceTree = "<group>"; };
B4FEEF7C24B8A71F00A8DF9B /* DeviceDataManager+DeviceStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DeviceDataManager+DeviceStatus.swift"; sourceTree = "<group>"; };
B633A70D291011EE00A39E1D /* Settings.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Settings.bundle; sourceTree = "<group>"; };
C101947127DD473C004E7EB8 /* MockKitUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = MockKitUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C10B28451EA9BA5E006EA1FC /* far_future_high_bg_forecast.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = far_future_high_bg_forecast.json; sourceTree = "<group>"; };
C11AA5C7258736CF00BDE12F /* DerivedAssetsBase.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = DerivedAssetsBase.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1883,6 +1885,7 @@
43776F831B8022E90074EA36 = {
isa = PBXGroup;
children = (
B633A70D291011EE00A39E1D /* Settings.bundle */,
C18A491122FCC20B00FDA733 /* Scripts */,
4FF4D0FA1E1834BD00846527 /* Common */,
43776F8E1B8022E90074EA36 /* Loop */,
Expand Down Expand Up @@ -3295,6 +3298,7 @@
A966152723EA5A26005D8B29 /* DerivedAssets.xcassets in Resources */,
7D70764F1FE06EE1004AC8EA /* InfoPlist.strings in Resources */,
7D7076631FE06EE4004AC8EA /* Localizable.strings in Resources */,
B633A70E291011EE00A39E1D /* Settings.bundle in Resources */,
43776F971B8022E90074EA36 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
44 changes: 37 additions & 7 deletions Loop/Managers/DoseMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ extension Collection where Element: GlucoseValue {
at date: Date,
suspendThreshold: HKQuantity,
sensitivity: HKQuantity,
model: InsulinModel
model: InsulinModel,
maxAutoIOB: Double?,
insulinOnBoard: Double?
) -> InsulinCorrection? {
var minGlucose: GlucoseValue?
var eventualGlucose: GlucoseValue?
Expand Down Expand Up @@ -322,14 +324,32 @@ extension Collection where Element: GlucoseValue {
return nil
}

// Question: this is the entirelyBelow section of code
// I did not add maxAutoIOB check here as I don't think it is needed

return .entirelyBelowRange(
min: min,
minTarget: minGlucoseTargets.lowerBound,
units: units
)
} else if eventual.quantity > eventualGlucoseTargets.upperBound,
let minCorrectionUnits = minCorrectionUnits, let correctingGlucose = correctingGlucose
var minCorrectionUnits = minCorrectionUnits, let correctingGlucose = correctingGlucose
{
// Limit automatic dosing to prevent insulinOnBoard > maxAutoIOB
if minCorrectionUnits > 0 &&
insulinOnBoard != nil &&
maxAutoIOB != nil &&
maxAutoIOB! > 0 {
let checkMaxAutoIOB = maxAutoIOB! - (insulinOnBoard! + minCorrectionUnits)
if checkMaxAutoIOB < 0 {
print("*** maxAutoIOB Feature enacted in aboveRange")
print("*** Initial minCorrectionUnits ", round(100*minCorrectionUnits)/100)
print("*** maxAutoIOB, insulinOnBoard, checkMaxAutoIOB ", round(100*maxAutoIOB!)/100, round(100*insulinOnBoard!)/100, round(100*checkMaxAutoIOB)/100)
minCorrectionUnits = Swift.max(minCorrectionUnits+checkMaxAutoIOB, 0)
print("*** Adjusted minCorrectionUnits ", round(100*minCorrectionUnits)/100)
}
}

return .aboveRange(
min: min,
correcting: correctingGlucose,
Expand Down Expand Up @@ -371,14 +391,18 @@ extension Collection where Element: GlucoseValue {
rateRounder: ((Double) -> Double)? = nil,
isBasalRateScheduleOverrideActive: Bool = false,
duration: TimeInterval = .minutes(30),
continuationInterval: TimeInterval = .minutes(11)
continuationInterval: TimeInterval = .minutes(11),
insulinOnBoard: Double?,
maxAutoIOB: Double?
) -> TempBasalRecommendation? {
let correction = self.insulinCorrection(
to: correctionRange,
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
model: model
model: model,
maxAutoIOB: maxAutoIOB,
insulinOnBoard: insulinOnBoard
)

let scheduledBasalRate = basalRates.value(at: date)
Expand Down Expand Up @@ -439,14 +463,18 @@ extension Collection where Element: GlucoseValue {
rateRounder: ((Double) -> Double)? = nil,
isBasalRateScheduleOverrideActive: Bool = false,
duration: TimeInterval = .minutes(30),
continuationInterval: TimeInterval = .minutes(11)
continuationInterval: TimeInterval = .minutes(11),
insulinOnBoard: Double?,
maxAutoIOB: Double?
) -> AutomaticDoseRecommendation? {
guard let correction = self.insulinCorrection(
to: correctionRange,
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
model: model
model: model,
maxAutoIOB: maxAutoIOB,
insulinOnBoard: insulinOnBoard
) else {
return nil
}
Expand Down Expand Up @@ -516,7 +544,9 @@ extension Collection where Element: GlucoseValue {
at: date,
suspendThreshold: suspendThreshold ?? correctionRange.quantityRange(at: date).lowerBound,
sensitivity: sensitivity.quantity(at: date),
model: model
model: model,
maxAutoIOB: nil,
insulinOnBoard: nil
) else {
return ManualBolusRecommendation(amount: 0, pendingInsulin: pendingInsulin)
}
Expand Down
12 changes: 10 additions & 2 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,10 @@ extension LoopDataManager {

let dosingRecommendation: AutomaticDoseRecommendation?

// DRAFT - cheat with Settings Bundle: later - add maxAutoIOB properly to Loop:
// if not entered in iOS Settings, maxAutoIOB is 0.0
let maxAutoIOB = UserDefaults.standard.double(forKey: "maxAutoIOB")

switch settings.automaticDosingStrategy {
case .automaticBolus:
let volumeRounder = { (_ units: Double) in
Expand All @@ -1634,7 +1638,9 @@ extension LoopDataManager {
lastTempBasal: lastTempBasal,
volumeRounder: volumeRounder,
rateRounder: rateRounder,
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true,
insulinOnBoard: dosingDecision.insulinOnBoard?.value,
maxAutoIOB: maxAutoIOB
)
case .tempBasalOnly:
let temp = predictedGlucose.recommendedTempBasal(
Expand All @@ -1647,7 +1653,9 @@ extension LoopDataManager {
maxBasalRate: maxBasal!,
lastTempBasal: lastTempBasal,
rateRounder: rateRounder,
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true
isBasalRateScheduleOverrideActive: settings.scheduleOverride?.isBasalRateScheduleOverriden(at: startDate) == true,
insulinOnBoard: dosingDecision.insulinOnBoard?.value,
maxAutoIOB: maxAutoIOB
)
dosingRecommendation = AutomaticDoseRecommendation(basalAdjustment: temp)
}
Expand Down
6 changes: 6 additions & 0 deletions Loop/View Controllers/CarbEntryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ final class CarbEntryViewController: LoopChartsTableViewController, Identifiable
}

guard let quantity = quantity, quantity.doubleValue(for: preferredCarbUnit) > 0 else { return false }

// DRAFT - cheat with Settings Bundle: later - add maxCarbEntry properly to Loop:
let maxCarbEntry = UserDefaults.standard.double(forKey: "maxCarbEntry")
if (maxCarbEntry > 0) {
maxQuantity = HKQuantity(unit: .gram(), doubleValue: maxCarbEntry)
}
guard quantity.compare(maxQuantity) != .orderedDescending else {
navigationDelegate.showMaxQuantityValidationWarning(for: self, maxQuantityGrams: maxQuantity.doubleValue(for: .gram()))
return false
Expand Down
61 changes: 61 additions & 0 deletions Settings.bundle/Root.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Max Allowed Carb Entry</string>
</dict>
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Max Allowed Carb Entry</string>
<key>Key</key>
<string>maxCarbEntry</string>
<key>DefaultValue</key>
<string>250g</string>
<key>Values</key>
<array>
<real>50</real>
<real>99</real>
<real>150</real>
<real>200</real>
<real>250</real>
<real>300</real>
</array>
<key>Titles</key>
<array>
<string>50g</string>
<string>99g</string>
<string>150g</string>
<string>200g</string>
<string>250g</string>
<string>300g</string>
</array>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>max Auto IOB</string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>maxAutoIOB</string>
<key>Key</key>
<string>maxAutoIOB</string>
<key>KeyboardType</key>
<string>NumbersAndPunctuation</string>
</dict>
</array>
</dict>
</plist>
Binary file added Settings.bundle/en.lproj/Root.strings
Binary file not shown.