Jelajahi Sumber

replace currentTemp in apsManager/openAPS, fix predictions not showing up

polscm32 1 tahun lalu
induk
melakukan
c98e066982

+ 28 - 12
FreeAPS/Sources/APS/APSManager.swift

@@ -416,8 +416,8 @@ final class BaseAPSManager: APSManager, Injectable {
             }
 
             let now = Date()
-            let temp = currentTemp(date: now)
-
+            let temp = fetchCurrentTempBasal(date: now)
+        
             let mainPublisher = makeProfiles()
                 .flatMap { _ in self.autosens() }
                 .flatMap { _ in self.dailyAutotune() }
@@ -713,17 +713,33 @@ final class BaseAPSManager: APSManager, Injectable {
         }
     }
 
-    private func currentTemp(date: Date) -> TempBasal {
-        let defaultTemp = { () -> TempBasal in
-            guard let temp = storage.retrieve(OpenAPS.Monitor.tempBasal, as: TempBasal.self) else {
-                return TempBasal(duration: 0, rate: 0, temp: .absolute, timestamp: Date())
+    private func fetchCurrentTempBasal(date: Date) -> TempBasal {
+        var fetchedTempBasal = TempBasal(duration: 0, rate: 0, temp: .absolute, timestamp: Date())
+
+        privateContext.performAndWait {
+            let results = CoreDataStack.shared.fetchEntities(
+                ofType: PumpEventStored.self,
+                onContext: privateContext,
+                predicate: NSPredicate.recentPumpHistory,
+                key: "timestamp",
+                ascending: false,
+                fetchLimit: 1
+            )
+
+            guard let tempBasalEvent = results.first,
+                  let tempBasal = tempBasalEvent.tempBasal,
+                  let eventTimestamp = tempBasalEvent.timestamp
+            else {
+                return
             }
-            let delta = Int((date.timeIntervalSince1970 - temp.timestamp.timeIntervalSince1970) / 60)
-            let duration = max(0, temp.duration - delta)
-            return TempBasal(duration: duration, rate: temp.rate, temp: .absolute, timestamp: date)
-        }()
 
-        guard let state = pumpManager?.status.basalDeliveryState else { return defaultTemp }
+            let delta = Int((date.timeIntervalSince1970 - eventTimestamp.timeIntervalSince1970) / 60)
+            let duration = max(0, Int(tempBasal.duration) - delta)
+            let rate = tempBasal.rate as? Decimal ?? 0
+            fetchedTempBasal = TempBasal(duration: duration, rate: rate, temp: .absolute, timestamp: date)
+        }
+
+        guard let state = pumpManager?.status.basalDeliveryState else { return fetchedTempBasal }
         switch state {
         case .active:
             return TempBasal(duration: 0, rate: 0, temp: .absolute, timestamp: date)
@@ -732,7 +748,7 @@ final class BaseAPSManager: APSManager, Injectable {
             let durationMin = max(0, Int((dose.endDate.timeIntervalSince1970 - date.timeIntervalSince1970) / 60))
             return TempBasal(duration: durationMin, rate: rate, temp: .absolute, timestamp: date)
         default:
-            return defaultTemp
+            return fetchedTempBasal
         }
     }
 

+ 1 - 2
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -197,7 +197,6 @@ final class OpenAPS {
 
                 // temp_basal
                 let tempBasal = currentTemp.rawJSON
-                self.storage.save(tempBasal, as: Monitor.tempBasal)
 
                 let pumpHistoryObjectIDs = self.fetchPumpHistoryObjectIDs() ?? []
                 let pumpHistoryJSON = self.parsePumpHistory(pumpHistoryObjectIDs)
@@ -208,6 +207,7 @@ final class OpenAPS {
                 /// glucose
                 let glucoseAsJSON = self.fetchAndProcessGlucose()
 
+                // TODO: - Save and fetch profile/basalProfile in/from UserDefaults
                 /// profile
                 let profile = self.loadFileFromStorage(name: Settings.profile)
                 let basalProfile = self.loadFileFromStorage(name: Settings.basalProfile)
@@ -221,7 +221,6 @@ final class OpenAPS {
                     carbs: carbsAsJSON,
                     glucose: glucoseAsJSON
                 )
-                self.storage.save(meal, as: Monitor.meal)
 
                 // iob
                 let autosens = self.loadFileFromStorage(name: Settings.autosense)

+ 18 - 5
FreeAPS/Sources/Models/Determination.swift

@@ -80,13 +80,26 @@ extension Determination {
     }
 }
 
-protocol DeterminationObserver {
-    func determinationDidUpdate(_ determination: Determination)
+extension Predictions {
+    private enum CodingKeys: String, CodingKey {
+        case iob = "IOB"
+        case zt = "ZT"
+        case cob = "COB"
+        case uam = "UAM"
+    }
 }
 
-// needed?
-protocol EnactedDeterminationObserver {
-    func enactedDeterminationDidUpdate(_ determination: Determination)
+extension Insulin {
+    private enum CodingKeys: String, CodingKey {
+        case TDD
+        case bolus
+        case temp_basal
+        case scheduled_basal
+    }
+}
+
+protocol DeterminationObserver {
+    func determinationDidUpdate(_ determination: Determination)
 }
 
 extension Determination {

+ 0 - 2
FreeAPS/Sources/Modules/Settings/View/SettingsRootView.swift

@@ -123,8 +123,6 @@ extension Settings {
                                 .navigationLink(to: .configEditor(file: OpenAPS.Settings.bgTargets), from: self)
                             Text("Temp targets")
                                 .navigationLink(to: .configEditor(file: OpenAPS.Settings.tempTargets), from: self)
-                            Text("Meal")
-                                .navigationLink(to: .configEditor(file: OpenAPS.Monitor.meal), from: self)
                         }
 
                         Group {