polscm32 2 лет назад
Родитель
Сommit
20ee7c2bdc

+ 22 - 19
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -140,9 +140,6 @@ final class OpenAPS {
                 /// glucose
                 let glucose = self.fetchGlucose()
                 let glucoseString = self.jsonConverter.convertToJSON(glucose)
-                print(glucoseString)
-                let glucoseFromFile = self.loadFileFromStorage(name: Monitor.glucose)
-                print(glucoseFromFile)
 
                 /// profile
                 let profile = self.loadFileFromStorage(name: Settings.profile)
@@ -416,7 +413,10 @@ final class OpenAPS {
             self.processQueue.async {
                 debug(.openAPS, "Start autosens")
                 let pumpHistory = self.loadFileFromStorage(name: OpenAPS.Monitor.pumpHistory)
-                let carbs = self.loadFileFromStorage(name: Monitor.carbHistory)
+
+                // carbs
+                let carbs = self.fetchCarbs()
+                let carbsString = self.jsonConverter.convertToJSON(carbs)
 
                 /// glucose
                 let glucose = self.fetchGlucose()
@@ -430,7 +430,7 @@ final class OpenAPS {
                     pumpHistory: pumpHistory,
                     basalprofile: basalProfile,
                     profile: profile,
-                    carbs: carbs,
+                    carbs: carbsString,
                     temptargets: tempTargets
                 )
 
@@ -458,14 +458,17 @@ final class OpenAPS {
 
                 let profile = self.loadFileFromStorage(name: Settings.profile)
                 let pumpProfile = self.loadFileFromStorage(name: Settings.pumpProfile)
-                let carbs = self.loadFileFromStorage(name: Monitor.carbHistory)
+
+                // carbs
+                let carbs = self.fetchCarbs()
+                let carbsString = self.jsonConverter.convertToJSON(carbs)
 
                 let autotunePreppedGlucose = self.autotunePrepare(
                     pumphistory: pumpHistory,
                     profile: profile,
                     glucose: glucoseString,
                     pumpprofile: pumpProfile,
-                    carbs: carbs,
+                    carbs: carbsString,
                     categorizeUamAsBasal: categorizeUamAsBasal,
                     tuneInsulinCurve: tuneInsulinCurve
                 )
@@ -774,12 +777,20 @@ final class OpenAPS {
     }
 
     func processAndSave(forecastData: [String: [Int]]) {
-        let context = self.context
-
         let currentDate = Date()
 
-        for (type, values) in forecastData {
-            createForecast(type: type, values: values, date: currentDate, context: context)
+        context.perform {
+            for (type, values) in forecastData {
+                self.createForecast(type: type, values: values, date: currentDate, context: self.context)
+            }
+
+            if self.context.hasChanges {
+                do {
+                    try self.context.save()
+                } catch {
+                    print("Failed to save forecast: \(error)")
+                }
+            }
         }
     }
 
@@ -795,13 +806,5 @@ final class OpenAPS {
             forecastValue.index = Int32(index)
             forecastValue.forecast = forecast
         }
-
-        if self.context.hasChanges {
-            do {
-                try context.save()
-            } catch {
-                print("Failed to save forecast: \(error)")
-            }
-        }
     }
 }

+ 0 - 19
FreeAPS/Sources/APS/Storage/GlucoseStorage.swift

@@ -7,7 +7,6 @@ import Swinject
 
 protocol GlucoseStorage {
     func storeGlucose(_ glucose: [BloodGlucose])
-    func removeGlucose(ids: [String])
     func syncDate() -> Date
     func filterTooFrequentGlucose(_ glucose: [BloodGlucose], at: Date) -> [BloodGlucose]
     func lastGlucoseDate() -> Date
@@ -149,24 +148,6 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
         }
     }
 
-    func removeGlucose(ids: [String]) {
-        processQueue.sync {
-            let file = OpenAPS.Monitor.glucose
-            self.storage.transaction { storage in
-                let bgInStorage = storage.retrieve(file, as: [BloodGlucose].self)
-                let filteredBG = bgInStorage?.filter { !ids.contains($0.id) } ?? []
-                guard bgInStorage != filteredBG else { return }
-                storage.save(filteredBG, as: file)
-
-                DispatchQueue.main.async {
-                    self.broadcaster.notify(GlucoseObserver.self, on: .main) {
-                        $0.glucoseDidUpdate(filteredBG.reversed())
-                    }
-                }
-            }
-        }
-    }
-
     func syncDate() -> Date {
         //  TODO: - proof logic here!
         fetchGlucose().first?.date ?? .distantPast