Procházet zdrojové kódy

Fixes a bug in the Swift implementation when currentDeviation should be null for meal

Sam King před 10 měsíci
rodič
revize
f556f7421c

+ 3 - 3
Trio/Sources/APS/OpenAPSSwift/Forecasts/ForecastGenerator.swift

@@ -16,10 +16,10 @@ enum ForecastGenerator {
         threshold: Decimal,
         currentTime: Date
     ) -> ForecastResult {
-        let carbImpact = mealData
-            .currentDeviation * (profile.carbRatio ?? profile.carbRatioFor(time: currentTime)) /
+        let currentDeviation = mealData.currentDeviation ?? 0
+        let carbImpact = currentDeviation * (profile.carbRatio ?? profile.carbRatioFor(time: currentTime)) /
             (profile.sens ?? profile.sensitivityFor(time: currentTime))
-        let deviation = mealData.currentDeviation
+        let deviation = currentDeviation
 
         // JS oref initializes all xxxPredBGs array with current glucose, we do the same, then generate
         let iobForecast = [glucose] + forecastIOB(

+ 34 - 2
Trio/Sources/APS/OpenAPSSwift/Meal/MealTotal.swift

@@ -3,13 +3,43 @@ import Foundation
 struct ComputedCarbs: Codable {
     var carbs: Decimal
     var mealCOB: Decimal
-    var currentDeviation: Decimal
+    var currentDeviation: Decimal?
     var maxDeviation: Decimal
     var minDeviation: Decimal
     var slopeFromMaxDeviation: Decimal
     var slopeFromMinDeviation: Decimal
     var allDeviations: [Decimal]
     var lastCarbTime: TimeInterval
+
+    enum CodingKeys: String, CodingKey {
+        case carbs
+        case mealCOB
+        case currentDeviation
+        case maxDeviation
+        case minDeviation
+        case slopeFromMaxDeviation
+        case slopeFromMinDeviation
+        case allDeviations
+        case lastCarbTime
+    }
+
+    func encode(to encoder: Encoder) throws {
+        var container = encoder.container(keyedBy: CodingKeys.self)
+        try container.encode(carbs, forKey: .carbs)
+        try container.encode(mealCOB, forKey: .mealCOB)
+        try container.encode(maxDeviation, forKey: .maxDeviation)
+        try container.encode(minDeviation, forKey: .minDeviation)
+        try container.encode(slopeFromMaxDeviation, forKey: .slopeFromMaxDeviation)
+        try container.encode(slopeFromMinDeviation, forKey: .slopeFromMinDeviation)
+        try container.encode(allDeviations, forKey: .allDeviations)
+        try container.encode(lastCarbTime, forKey: .lastCarbTime)
+
+        if let currentDeviation = currentDeviation {
+            try container.encode(currentDeviation, forKey: .currentDeviation)
+        } else {
+            try container.encodeNil(forKey: .currentDeviation)
+        }
+    }
 }
 
 struct IOBInput {
@@ -165,10 +195,12 @@ enum MealTotal {
             mealCOB = 0
         }
 
+        let currentDeviation = finalCobResult.allDeviations.isEmpty ? nil : finalCobResult.currentDeviation.rounded(scale: 2)
+
         return ComputedCarbs(
             carbs: carbs,
             mealCOB: mealCOB,
-            currentDeviation: finalCobResult.currentDeviation.rounded(scale: 2),
+            currentDeviation: currentDeviation,
             maxDeviation: finalCobResult.maxDeviation.rounded(scale: 2),
             minDeviation: finalCobResult.minDeviation.rounded(scale: 2),
             slopeFromMaxDeviation: finalCobResult.slopeFromMaxDeviation.rounded(scale: 3),

+ 5 - 5
TrioTests/OpenAPSSwiftTests/MealTotalTests.swift

@@ -97,7 +97,7 @@ import Testing
         #expect(result!.mealCOB.isWithin(0.5, of: 10) == true, "mealCOB: \(result!.mealCOB.description)")
         #expect(
             result!.currentDeviation == 3.6,
-            "currentDeviation: \(result!.currentDeviation.description)"
+            "currentDeviation: \(result!.currentDeviation!.description)"
         )
     }
 
@@ -172,8 +172,8 @@ import Testing
         #expect(result != nil)
         #expect(result!.carbs == 20)
         #expect(
-            result!.currentDeviation.isWithin(0.02, of: 0.67) == true,
-            "currentDeviation: \(result!.currentDeviation.description)"
+            result!.currentDeviation!.isWithin(0.02, of: 0.67) == true,
+            "currentDeviation: \(result!.currentDeviation!.description)"
         )
         #expect(result!.mealCOB.isWithin(0.25, of: 14) == true, "mealCOB: \(result!.mealCOB.description)")
     }
@@ -226,8 +226,8 @@ import Testing
         #expect(result?.carbs == 0)
         #expect(result?.mealCOB == 0)
         #expect(
-            result?.currentDeviation.isWithin(0.02, of: 0.67) == true,
-            "currentDeviation: \(result!.currentDeviation.description)"
+            result?.currentDeviation!.isWithin(0.02, of: 0.67) == true,
+            "currentDeviation: \(result!.currentDeviation!.description)"
         )
     }