Просмотр исходного кода

Fix cob decay; fix empty pump history JSON WIP

dnzxy 2 лет назад
Родитель
Сommit
c072e6b484

+ 6 - 6
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -99,11 +99,10 @@ final class OpenAPS {
         do {
             debugPrint("OpenAPS: \(#function) \(DebuggingIdentifiers.succeeded) fetched glucose")
             return try context.fetch(GlucoseStored.fetch(
-                NSPredicate.predicateFor30MinAgo,
+                NSPredicate.predicateForOneHourAgo,
                 ascending: false,
-                fetchLimit: 4
-            )) /// it only returns the last 4 values within the last 30 minutes, that means one reading can not be older than 5 minutes, otherwise the loop will fail
-            /// if we do not pass at least 4 values, cob cannot be calculated.
+                fetchLimit: 6
+            ))
         } catch {
             debugPrint("OpenAPS: \(#function) \(DebuggingIdentifiers.failed) failed to fetch glucose with error: \(error)")
             return []
@@ -135,7 +134,7 @@ final class OpenAPS {
     }
 
     private func parsePumpHistory(_ pumpHistory: [PumpEventStored]) -> String {
-        guard !pumpHistory.isEmpty else { return "" }
+        guard !pumpHistory.isEmpty else { return "{}" }
 
         let dtos: [PumpEventDTO] = pumpHistory.flatMap { event -> [PumpEventDTO] in
             var eventDTOs: [PumpEventDTO] = []
@@ -171,6 +170,7 @@ final class OpenAPS {
                 // carbs
                 let carbs = self.fetchCarbs()
                 let carbsString = self.jsonConverter.convertToJSON(carbs)
+                print(carbsString)
 
                 /// glucose
                 let glucose = self.fetchGlucose()
@@ -189,7 +189,7 @@ final class OpenAPS {
                     carbs: carbsString,
                     glucose: glucoseString
                 )
-
+                print(meal)
                 self.storage.save(meal, as: Monitor.meal)
 
                 // iob

+ 2 - 0
Model/Helper/CarbEntryStored+helper.swift

@@ -44,6 +44,8 @@ extension CarbEntryStored: Encodable {
         var container = encoder.container(keyedBy: CodingKeys.self)
 
         let dateFormatter = ISO8601DateFormatter()
+        dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
+
         let formattedDate = dateFormatter.string(from: date ?? Date())
         try container.encode(formattedDate, forKey: .actualDate)
         try container.encode(formattedDate, forKey: .created_at)

+ 9 - 0
Model/Helper/NSPredicates.swift

@@ -6,6 +6,10 @@ extension Date {
         Calendar.current.date(byAdding: .day, value: -1, to: Date())!
     }
 
+    static var oneHourAgo: Date {
+        Calendar.current.date(byAdding: .minute, value: -60, to: Date())!
+    }
+
     static var halfHourAgo: Date {
         Calendar.current.date(byAdding: .minute, value: -30, to: Date())!
     }
@@ -45,6 +49,11 @@ extension NSPredicate {
         return NSPredicate(format: "date >= %@", date as NSDate)
     }
 
+    static var predicateForOneHourAgo: NSPredicate {
+        let date = Date.oneHourAgo
+        return NSPredicate(format: "date >= %@", date as NSDate)
+    }
+
     static var predicateFor30MinAgo: NSPredicate {
         let date = Date.halfHourAgo
         return NSPredicate(format: "date >= %@", date as NSDate)