polscm32 2 лет назад
Родитель
Сommit
bb8ba2d341
2 измененных файлов с 16 добавлено и 3 удалено
  1. 7 3
      FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift
  2. 9 0
      Model/Helper/NSPredicates.swift

+ 7 - 3
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -97,9 +97,14 @@ final class OpenAPS {
     // fetch glucose to pass it to the meal function and to determine basal
     private func fetchGlucose() -> [GlucoseStored]? {
         do {
-            return try context.fetch(GlucoseStored.fetch(ascending: false, fetchLimit: 3))
+            debugPrint("OpenAPS: \(#function) \(DebuggingIdentifiers.succeeded) fetched glucose")
+            return try context.fetch(GlucoseStored.fetch(
+                NSPredicate.predicateFor20MinAgo,
+                ascending: false,
+                fetchLimit: 3
+            )) /// it only returns the last 3 values within the last 20 minutes, that means one reading can not be older than 5 minutes, otherwise the loop will fail
         } catch {
-            print("failed")
+            debugPrint("OpenAPS: \(#function) \(DebuggingIdentifiers.failed) failed to fetch glucose with error: \(error)")
             return []
         }
     }
@@ -121,7 +126,6 @@ final class OpenAPS {
 
                 /// glucose
                 let glucose = self.fetchGlucose()
-//                    let glucoseString = self.convertToJSON(glucose)
                 let glucoseString = self.jsonConverter.convertToJSON(glucose)
 
                 /// profile

+ 9 - 0
Model/Helper/NSPredicates.swift

@@ -10,6 +10,10 @@ extension Date {
         Calendar.current.date(byAdding: .minute, value: -30, to: Date())!
     }
 
+    static var twentyMinutesAgo: Date {
+        Calendar.current.date(byAdding: .minute, value: -20, to: Date())!
+    }
+
     static var twoHoursAgo: Date {
         Calendar.current.date(byAdding: .hour, value: -2, to: Date())!
     }
@@ -46,6 +50,11 @@ extension NSPredicate {
         return NSPredicate(format: "date >= %@", date as NSDate)
     }
 
+    static var predicateFor20MinAgo: NSPredicate {
+        let date = Date.twentyMinutesAgo
+        return NSPredicate(format: "date >= %@", date as NSDate)
+    }
+
     static var predicateFor30MinAgoForDetermination: NSPredicate {
         let date = Date.halfHourAgo
         return NSPredicate(format: "deliverAt >= %@", date as NSDate)