Forráskód Böngészése

Fix bug in forecast blending

Sam King 6 hónapja
szülő
commit
260c3b94eb

+ 10 - 8
Trio/Sources/APS/OpenAPSSwift/Forecasts/ForecastGenerator.swift

@@ -220,7 +220,8 @@ enum ForecastGenerator {
                 forecasts: iobForecast.forecasts,
                 minGuardGlucose: iobForecast.minGuardGlucose,
                 minForecastGlucose: minIobForecastGlucose,
-                maxForecastGlucose: maxIobForecastGlucose
+                maxForecastGlucose: maxIobForecastGlucose,
+                lastForecastGlucose: iobForecast.rawForecasts.last ?? currentGlucose
             ),
             zt: ZTForecast(
                 forecasts: ztForecast.forecasts,
@@ -230,14 +231,16 @@ enum ForecastGenerator {
                 forecasts: cobForecast.forecasts,
                 minGuardGlucose: cobForecast.minGuardGlucose,
                 minForecastGlucose: minCobForecastGlucose,
-                maxForecastGlucose: maxCobForecastGlucose
+                maxForecastGlucose: maxCobForecastGlucose,
+                lastForecastGlucose: cobForecast.rawForecasts.last ?? currentGlucose
             ),
             uam: UAMForecast(
                 forecasts: uamForecast.forecasts,
                 minGuardGlucose: uamForecast.minGuardGlucose,
                 minForecastGlucose: minUamForecastGlucose,
                 maxForecastGlucose: maxUamForecastGlucose,
-                duration: uamForecast.duration!
+                duration: uamForecast.duration!,
+                lastForecastGlucose: uamForecast.rawForecasts.last ?? currentGlucose
             ) // I don't love the force unwrap here but it should always be set
         )
     }
@@ -309,19 +312,18 @@ enum ForecastGenerator {
         let avgerageForecastGlucose: Decimal
         if uamResult.minForecastGlucose < 999, cobResult.minForecastGlucose < 999 {
             avgerageForecastGlucose = (
-                (1 - fractionCarbsLeft) * (uamResult.forecasts.last ?? currentGlucose) + fractionCarbsLeft *
-                    (cobResult.forecasts.last ?? currentGlucose)
+                (1 - fractionCarbsLeft) * uamResult.lastForecastGlucose + fractionCarbsLeft * cobResult.lastForecastGlucose
             ).rounded()
         } else if cobResult.minForecastGlucose < 999 {
             avgerageForecastGlucose =
-                (((iobResult.forecasts.last ?? currentGlucose) + (cobResult.forecasts.last ?? currentGlucose)) / 2)
+                ((iobResult.lastForecastGlucose + cobResult.lastForecastGlucose) / 2)
                     .rounded()
         } else if uamResult.minForecastGlucose < 999 {
             avgerageForecastGlucose =
-                (((iobResult.forecasts.last ?? currentGlucose) + (uamResult.forecasts.last ?? currentGlucose)) / 2)
+                ((iobResult.lastForecastGlucose + uamResult.lastForecastGlucose) / 2)
                     .rounded()
         } else {
-            avgerageForecastGlucose = (iobResult.forecasts.last ?? currentGlucose).rounded()
+            avgerageForecastGlucose = iobResult.lastForecastGlucose.rounded()
         }
         let adjustedAverageForecastGlucose = max(avgerageForecastGlucose, ztResult.minGuardGlucose)
 

+ 3 - 0
Trio/Sources/APS/OpenAPSSwift/Forecasts/ForecastResults.swift

@@ -5,6 +5,7 @@ struct IOBForecast {
     let minGuardGlucose: Decimal // The absolute min of the untrimmed array
     let minForecastGlucose: Decimal // The min after the initial 90-min peak
     let maxForecastGlucose: Decimal // The absolute max of the untrimmed array
+    let lastForecastGlucose: Decimal // The last forecast (IOBPredBG in JS)
 }
 
 struct COBForecast {
@@ -12,6 +13,7 @@ struct COBForecast {
     let minGuardGlucose: Decimal // The absolute min of the untrimmed array
     let minForecastGlucose: Decimal // The min after the initial 90-min peak
     let maxForecastGlucose: Decimal // The absolute max of the untrimmed array
+    let lastForecastGlucose: Decimal // The last forecast (COBPredBG in JS)
 }
 
 struct UAMForecast {
@@ -20,6 +22,7 @@ struct UAMForecast {
     let minForecastGlucose: Decimal // The min after the initial 60-min peak
     let maxForecastGlucose: Decimal // The absolute max of the untrimmed array
     let duration: Decimal // The calculated UAM duration in hours
+    let lastForecastGlucose: Decimal // The last forecast (UAMPredBG in JS)
 }
 
 struct ZTForecast {