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

Fix cob calculation by passing correct fields and # of glucose

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

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

@@ -99,10 +99,11 @@ final class OpenAPS {
         do {
             debugPrint("OpenAPS: \(#function) \(DebuggingIdentifiers.succeeded) fetched glucose")
             return try context.fetch(GlucoseStored.fetch(
-                NSPredicate.predicateFor20MinAgo,
+                NSPredicate.predicateFor30MinAgo,
                 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
+                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.
         } catch {
             debugPrint("OpenAPS: \(#function) \(DebuggingIdentifiers.failed) failed to fetch glucose with error: \(error)")
             return []
@@ -139,6 +140,9 @@ 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)

+ 4 - 11
Model/Helper/CarbEntryStored+helper.swift

@@ -44,20 +44,13 @@ extension CarbEntryStored: Encodable {
         var container = encoder.container(keyedBy: CodingKeys.self)
 
         let dateFormatter = ISO8601DateFormatter()
-        if let date = self.date {
-            let formattedDate = dateFormatter.string(from: date)
-            try container.encode(formattedDate, forKey: .actualDate)
-            try container.encode(formattedDate, forKey: .created_at)
-        } else {
-            throw EncodingError.invalidValue(
-                Date.self,
-                EncodingError.Context(codingPath: [], debugDescription: "Date values are nil")
-            )
-        }
+        let formattedDate = dateFormatter.string(from: date ?? Date())
+        try container.encode(formattedDate, forKey: .actualDate)
+        try container.encode(formattedDate, forKey: .created_at)
 
         // TODO: handle this conditionally; pass in the enteredBy string (manual entry or via NS or Apple Health)
         try container.encode("Open-iAPS", forKey: .enteredBy)
-        
+
         try container.encode(carbs, forKey: .carbs)
         try container.encode(fat, forKey: .fat)
         try container.encode(isFPU, forKey: .isFPU)

+ 11 - 2
Model/Helper/GlucoseStored+helper.swift

@@ -49,19 +49,28 @@ protocol GlucoseStoredObserver {
 extension GlucoseStored: Encodable {
     enum CodingKeys: String, CodingKey {
         case date
+        case dateString
         case sgv
         case glucose
         case direction
         case id
+        case type
     }
 
     public func encode(to encoder: Encoder) throws {
         var container = encoder.container(keyedBy: CodingKeys.self)
 
-        let dateString = String(format: "%.0f", (date?.timeIntervalSince1970 ?? Date().timeIntervalSince1970) * 1000)
-        try container.encode(dateString, forKey: .date)
+        let dateFormatter = ISO8601DateFormatter()
+        try container.encode(dateFormatter.string(from: date ?? Date()), forKey: .dateString)
+
+        let dateAsUnixTimestamp = String(format: "%.0f", (date?.timeIntervalSince1970 ?? Date().timeIntervalSince1970) * 1000)
+        try container.encode(dateAsUnixTimestamp, forKey: .date)
+
         try container.encode(direction, forKey: .direction)
         try container.encode(id, forKey: .id)
+        
+        // TODO: Handle the type of the glucose entry conditionally not hardcoded
+        try container.encode("sgv", forKey: .type)
 
         if isManual {
             try container.encode(glucose, forKey: .glucose)