Procházet zdrojové kódy

address dans feedback

polscm32 aka Marvout před 1 rokem
rodič
revize
f7bf0a5036

+ 1 - 1
FreeAPS/Sources/APS/APSManager.swift

@@ -402,7 +402,7 @@ final class BaseAPSManager: APSManager, Injectable {
     func simulateDetermineBasal(carbs: Decimal, iob: Decimal) async -> Determination? {
         do {
             let temp = await fetchCurrentTempBasal(date: Date.now)
-            return try await openAPS.simulateDetermineBasal(currentTemp: temp, clock: Date(), carbs: carbs, iob: iob)
+            return try await openAPS.determineBasal(currentTemp: temp, clock: Date(), carbs: carbs, iob: iob, simulation: true)
         } catch {
             debugPrint(
                 "\(DebuggingIdentifiers.failed) \(#file) \(#function) Error occurred in invokeDummyDetermineBasalSync: \(error)"

+ 10 - 107
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -264,11 +264,12 @@ final class OpenAPS {
         return .bolus(bolusDTO)
     }
 
-    func simulateDetermineBasal(
+    func determineBasal(
         currentTemp: TempBasal,
         clock: Date = Date(),
-        carbs: Decimal,
-        iob: Decimal
+        carbs: Decimal? = nil,
+        iob: Decimal? = nil,
+        simulation: Bool = false
     ) async throws -> Determination? {
         debug(.openAPS, "Start determineBasal")
 
@@ -281,7 +282,7 @@ final class OpenAPS {
 
         // Perform asynchronous calls in parallel
         async let pumpHistoryObjectIDs = fetchPumpHistoryObjectIDs() ?? []
-        async let carbs = fetchAndProcessCarbs(additionalCarbs: carbs)
+        async let carbs = fetchAndProcessCarbs(additionalCarbs: carbs ?? 0)
         async let glucose = fetchAndProcessGlucose()
         async let oref2 = oref2()
         async let profileAsync = loadFileFromStorageAsync(name: Settings.profile)
@@ -312,7 +313,6 @@ final class OpenAPS {
             reservoirAsync,
             preferencesAsync
         )
-//        print("carbs: \(carbsAsJSON)")
 
         // Meal
         let meal = try await self.meal(
@@ -331,106 +331,10 @@ final class OpenAPS {
             clock: dateFormattedAsString,
             autosens: autosens.isEmpty ? .null : autosens
         )
-//        print("pumphistory : \(pumpHistoryJSON)")
-        print("iob: \(iob)")
-//        print("profile: \(profile)")
 
-        // Determine basal
-        let orefDetermination = try await determineBasal(
-            glucose: glucoseAsJSON,
-            currentTemp: tempBasal,
-            iob: iob,
-            profile: profile,
-            autosens: autosens.isEmpty ? .null : autosens,
-            meal: meal,
-            microBolusAllowed: true,
-            reservoir: reservoir,
-            pumpHistory: pumpHistoryJSON,
-            preferences: preferences,
-            basalProfile: basalProfile,
-            oref2_variables: oref2_variables
-        )
-
-//        debug(.openAPS, "oref 2 scheiß: \(oref2_variables)")
-        debug(.openAPS, "Determinated: \(orefDetermination)")
-
-        if var determination = Determination(from: orefDetermination), let deliverAt = determination.deliverAt {
-            // set both timestamp and deliverAt to the SAME date; this will be updated for timestamp once it is enacted
-            // AAPS does it the same way! we'll follow their example!
-            determination.timestamp = deliverAt
-
-            return determination
-        } else {
-            return nil
+        if !simulation {
+            storage.save(iob, as: Monitor.iob)
         }
-    }
-
-    func determineBasal(currentTemp: TempBasal, clock: Date = Date()) async throws -> Determination? {
-        debug(.openAPS, "Start determineBasal")
-
-        // clock
-        let dateFormatted = OpenAPS.dateFormatter.string(from: clock)
-        let dateFormattedAsString = "\"\(dateFormatted)\""
-
-        // temp_basal
-        let tempBasal = currentTemp.rawJSON
-
-        // Perform asynchronous calls in parallel
-        async let pumpHistoryObjectIDs = fetchPumpHistoryObjectIDs() ?? []
-        async let carbs = fetchAndProcessCarbs()
-        async let glucose = fetchAndProcessGlucose()
-        async let oref2 = oref2()
-        async let profileAsync = loadFileFromStorageAsync(name: Settings.profile)
-        async let basalAsync = loadFileFromStorageAsync(name: Settings.basalProfile)
-        async let autosenseAsync = loadFileFromStorageAsync(name: Settings.autosense)
-        async let reservoirAsync = loadFileFromStorageAsync(name: Monitor.reservoir)
-        async let preferencesAsync = loadFileFromStorageAsync(name: Settings.preferences)
-
-        // Await the results of asynchronous tasks
-        let (
-            pumpHistoryJSON,
-            carbsAsJSON,
-            glucoseAsJSON,
-            oref2_variables,
-            profile,
-            basalProfile,
-            autosens,
-            reservoir,
-            preferences
-        ) = await (
-            parsePumpHistory(await pumpHistoryObjectIDs),
-            carbs,
-            glucose,
-            oref2,
-            profileAsync,
-            basalAsync,
-            autosenseAsync,
-            reservoirAsync,
-            preferencesAsync
-        )
-
-        // TODO: - Save and fetch profile/basalProfile in/from UserDefaults!
-
-        // Meal
-        let meal = try await self.meal(
-            pumphistory: pumpHistoryJSON,
-            profile: profile,
-            basalProfile: basalProfile,
-            clock: dateFormattedAsString,
-            carbs: carbsAsJSON,
-            glucose: glucoseAsJSON
-        )
-
-        // IOB
-        let iob = try await self.iob(
-            pumphistory: pumpHistoryJSON,
-            profile: profile,
-            clock: dateFormattedAsString,
-            autosens: autosens.isEmpty ? .null : autosens
-        )
-
-        // TODO: refactor this to core data
-        storage.save(iob, as: Monitor.iob)
 
         // Determine basal
         let orefDetermination = try await determineBasal(
@@ -451,12 +355,11 @@ final class OpenAPS {
         debug(.openAPS, "Determinated: \(orefDetermination)")
 
         if var determination = Determination(from: orefDetermination), let deliverAt = determination.deliverAt {
-            // set both timestamp and deliverAt to the SAME date; this will be updated for timestamp once it is enacted
-            // AAPS does it the same way! we'll follow their example!
             determination.timestamp = deliverAt
 
-            // save to core data asynchronously
-            await processDetermination(determination)
+            if !simulation {
+                await processDetermination(determination)
+            }
 
             return determination
         } else {

+ 0 - 21
FreeAPS/Sources/Modules/Bolus/View/BolusRootView.swift

@@ -18,18 +18,12 @@ extension Bolus {
 
         @StateObject var state = StateModel()
 
-//        @State private var showAlert = false
         @State private var showPresetSheet = false
         @State private var autofocus: Bool = true
         @State private var calculatorDetent = PresentationDetent.medium
         @State private var pushed: Bool = false
-//        @State private var isPromptPresented: Bool = false
-//        @State private var dish: String = ""
-//        @State private var saved: Bool = false
         @State private var debounce: DispatchWorkItem?
 
-//        @Environment(\.managedObjectContext) var moc
-
         private enum Config {
             static let dividerHeight: CGFloat = 2
             static let spacing: CGFloat = 3
@@ -37,11 +31,6 @@ extension Bolus {
 
         @Environment(\.colorScheme) var colorScheme
 
-//        @FetchRequest(
-//            entity: MealPresetStored.entity(),
-//            sortDescriptors: [NSSortDescriptor(key: "dish", ascending: true)]
-//        ) var carbPresets: FetchedResults<MealPresetStored>
-
         private var formatter: NumberFormatter {
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
@@ -88,10 +77,6 @@ extension Bolus {
                 )
         }
 
-//        private var empty: Bool {
-//            state.useFPUconversion ? (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) : (state.carbs <= 0)
-//        }
-
         /// Handles macro input (carb, fat, protein) in a debounced fashion.
         func handleDebouncedInput() {
             debounce?.cancel()
@@ -182,12 +167,6 @@ extension Bolus {
                             }
                         }.listRowBackground(Color.chart)
 
-//                        if state.displayPresets {
-//                            Section {
-//                                mealPresets
-//                            }.listRowBackground(Color.chart)
-//                        }
-
                         Section {
                             HStack {
                                 Button(action: {

+ 0 - 1
FreeAPS/Sources/Modules/Bolus/View/ForeCastChart.swift

@@ -49,7 +49,6 @@ struct ForeCastChart: View {
         Chart {
             drawGlucose()
             drawCurrentTimeMarker()
-//            drawForecasts()
             drawForecastArea()
         }
         .chartXAxis { forecastChartXAxis }

+ 0 - 46
FreeAPS/Sources/Modules/Bolus/View/MealPresetView.swift

@@ -321,52 +321,6 @@ struct MealPresetView: View {
         }
     }
 
-//    @ViewBuilder private func dishInfos() -> some View {
-//        if !state.summation.isEmpty {
-//            let presetSummary = generatePresetSummary()
-//
-//            Section(header: Text("Summary")) {
-//                presetSummary
-//                    .lineLimit(nil) // In case the text is too long, allow it to wrap to the next line
-//
-//                VStack(alignment: .leading) {
-//                    HStack {
-//                        Text("Carbs: ")
-//                            .font(.footnote)
-//                            .foregroundStyle(.secondary)
-//                        Text("\(carbs as NSNumber, formatter: mealFormatter)")
-//                            .font(.footnote)
-//                        Text(" g")
-//                            .font(.footnote)
-//                            .foregroundStyle(.secondary)
-//                    }
-//
-//                    HStack {
-//                        Text("Fat: ")
-//                            .font(.footnote)
-//                            .foregroundStyle(.secondary)
-//                        Text("\(fat as NSNumber, formatter: mealFormatter)")
-//                            .font(.footnote)
-//                        Text(" g")
-//                            .font(.footnote)
-//                            .foregroundStyle(.secondary)
-//                    }
-//
-//                    HStack {
-//                        Text("Protein: ")
-//                            .font(.footnote)
-//                            .foregroundStyle(.secondary)
-//                        Text("\(protein as NSNumber, formatter: mealFormatter)")
-//                            .font(.footnote)
-//                        Text(" g")
-//                            .font(.footnote)
-//                            .foregroundStyle(.secondary)
-//                    }
-//                }
-//            }
-//        }
-//    }
-
     private func generatePresetSummary() -> some View {
         var counts = [String: Int]()