Просмотр исходного кода

Update the name of the core enableSMB function, switch the type of
outUnits in Profile to GlucoseUnits

Sam King 9 месяцев назад
Родитель
Сommit
e08a33c96b

+ 1 - 1
Trio/Sources/APS/OpenAPSSwift/DetermineBasal/DetermineBasalGenerator.swift

@@ -211,7 +211,7 @@ enum DeterminationGenerator {
             targetLog: "" // Placeholder
         )
 
-        let smbDecision = try DosingEngine.shouldEnableSmb(
+        let smbDecision = try DosingEngine.makeSMBDosingDecision(
             profile: profile,
             meal: mealData,
             currentGlucose: currentGlucose,

+ 5 - 5
Trio/Sources/APS/OpenAPSSwift/DetermineBasal/DosingEngine.swift

@@ -95,10 +95,10 @@ enum DosingEngine {
 
     /// helper function for reason string glucose output
     private static func convertGlucose(profile: Profile, glucose: Decimal) -> Decimal {
-        if profile.outUnits == "mmol/L" {
-            return (glucose * 0.0555).jsRounded(scale: 1)
-        } else {
-            return glucose.jsRounded()
+        let units = profile.outUnits ?? .mgdL
+        switch units {
+        case .mgdL: return glucose.jsRounded()
+        case .mmolL: return glucose.asMmolL
         }
     }
 
@@ -106,7 +106,7 @@ enum DosingEngine {
     ///
     /// This function includes both the profile / customOrefVariable checks from JS `enable_smb` as
     /// well as some of the later checks from `determineBasal` that can disable SMB
-    static func shouldEnableSmb(
+    static func makeSMBDosingDecision(
         profile: Profile,
         meal: ComputedCarbs,
         currentGlucose: Decimal,

+ 1 - 1
Trio/Sources/APS/OpenAPSSwift/Models/Profile.swift

@@ -40,7 +40,7 @@ struct Profile: Codable {
     var temptargetSet: Bool?
     var autosensMax: Decimal = 1.2
     var autosensMin: Decimal = 0.7
-    var outUnits: String?
+    var outUnits: GlucoseUnits?
 
     // Additional properties
     var maxMealAbsorptionTime: Decimal = 6.0

+ 1 - 1
Trio/Sources/APS/OpenAPSSwift/Profile/ProfileGenerator.swift

@@ -168,7 +168,7 @@ enum ProfileGenerator {
             }
         }
 
-        profile.outUnits = bgTargets.userPreferredUnits.rawValue
+        profile.outUnits = bgTargets.userPreferredUnits
         let (updatedTargets, range) = try Targets.bgTargetsLookup(targets: bgTargets, tempTargets: tempTargets, profile: profile)
         profile.minBg = range.minBg?.rounded()
         profile.maxBg = range.maxBg?.rounded()

+ 19 - 19
TrioTests/OpenAPSSwiftTests/DetermineBasalEnableSmbTests.swift

@@ -89,7 +89,7 @@ import Testing
 
     @Test("Should return false by default with no enabling preferences") func defaultIsFalse() throws {
         let inputs = createDefaultInputs()
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -104,7 +104,7 @@ import Testing
         inputs.trioCustomOrefVariables.smbIsOff = true
         inputs.profile.enableSMBAlways = true // Ensure smbIsOff takes precedence
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -119,7 +119,7 @@ import Testing
         inputs.trioCustomOrefVariables.shouldProtectDueToHIGH = true
         inputs.profile.enableSMBAlways = true // Ensure protection takes precedence
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -135,7 +135,7 @@ import Testing
         inputs.profile.temptargetSet = true
         inputs.adjustedTargetGlucose = 120
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -151,7 +151,7 @@ import Testing
         inputs.minGuardGlucose = 65
         inputs.threshold = 70
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -181,7 +181,7 @@ import Testing
             device: "test"
         )
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -198,7 +198,7 @@ import Testing
         var inputs = createDefaultInputs()
         inputs.profile.enableSMBAlways = true
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -223,7 +223,7 @@ import Testing
             lastCarbTime: Date().timeIntervalSince1970
         )
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -248,7 +248,7 @@ import Testing
             lastCarbTime: Date().timeIntervalSince1970
         )
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -264,7 +264,7 @@ import Testing
         inputs.profile.temptargetSet = true
         inputs.adjustedTargetGlucose = 90
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -280,7 +280,7 @@ import Testing
         inputs.profile.enableSMBHighBgTarget = 140
         inputs.currentGlucose = 145
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -300,7 +300,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 17 // 5 PM
         inputs.clock = Calendar.current.date(bySettingHour: 14, minute: 0, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -318,7 +318,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 17 // 5 PM
         inputs.clock = Calendar.current.date(bySettingHour: 18, minute: 0, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -338,7 +338,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 6 // 6 AM
         inputs.clock = Calendar.current.date(bySettingHour: 2, minute: 0, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -358,7 +358,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 6 // 6 AM
         inputs.clock = Calendar.current.date(bySettingHour: 23, minute: 0, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -376,7 +376,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 6 // 6 AM
         inputs.clock = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -394,7 +394,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 0
         inputs.clock = Calendar.current.date(bySettingHour: 15, minute: 0, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -412,7 +412,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 11 // 11 AM
         inputs.clock = Calendar.current.date(bySettingHour: 11, minute: 30, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,
@@ -430,7 +430,7 @@ import Testing
         inputs.trioCustomOrefVariables.end = 11 // 11 AM
         inputs.clock = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date())!
 
-        let decision = try DosingEngine.shouldEnableSmb(
+        let decision = try DosingEngine.makeSMBDosingDecision(
             profile: inputs.profile, meal: inputs.meal, currentGlucose: inputs.currentGlucose,
             adjustedTargetGlucose: inputs.adjustedTargetGlucose, adjustedSensitivity: inputs.adjustedSensitivity,
             minGuardGlucose: inputs.minGuardGlucose, eventualGlucose: inputs.eventualGlucose,