Browse Source

Save TDD to coreData. Thanks, Marc Fournier!

Jon Mårtensson 3 years ago
parent
commit
78dc249f3e

+ 2 - 2
Dependencies/rileylink_ios/RileyLink.xcodeproj/project.pbxproj

@@ -5659,7 +5659,7 @@
 				CODE_SIGN_IDENTITY = "Mac Developer";
 				CODE_SIGN_IDENTITY = "Mac Developer";
 				CODE_SIGN_STYLE = Automatic;
 				CODE_SIGN_STYLE = Automatic;
 				DEBUG_INFORMATION_FORMAT = dwarf;
 				DEBUG_INFORMATION_FORMAT = dwarf;
-				DEVELOPMENT_TEAM = UY678SP37Q;
+				DEVELOPMENT_TEAM = "$(DEVELOPMENT_TEAM)";
 				ENABLE_HARDENED_RUNTIME = YES;
 				ENABLE_HARDENED_RUNTIME = YES;
 				FRAMEWORK_SEARCH_PATHS = "$(inherited)";
 				FRAMEWORK_SEARCH_PATHS = "$(inherited)";
 				GCC_C_LANGUAGE_STANDARD = gnu11;
 				GCC_C_LANGUAGE_STANDARD = gnu11;
@@ -5686,7 +5686,7 @@
 				CODE_SIGN_STYLE = Automatic;
 				CODE_SIGN_STYLE = Automatic;
 				COPY_PHASE_STRIP = NO;
 				COPY_PHASE_STRIP = NO;
 				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
 				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
-				DEVELOPMENT_TEAM = UY678SP37Q;
+				DEVELOPMENT_TEAM = "$(DEVELOPMENT_TEAM)";
 				ENABLE_HARDENED_RUNTIME = YES;
 				ENABLE_HARDENED_RUNTIME = YES;
 				FRAMEWORK_SEARCH_PATHS = "$(inherited)";
 				FRAMEWORK_SEARCH_PATHS = "$(inherited)";
 				GCC_C_LANGUAGE_STANDARD = gnu11;
 				GCC_C_LANGUAGE_STANDARD = gnu11;

+ 14 - 20
FreeAPS/Sources/APS/APSManager.swift

@@ -1,4 +1,4 @@
-import Accelerate
+// import Accelerate
 import Combine
 import Combine
 import CoreData
 import CoreData
 import Foundation
 import Foundation
@@ -686,20 +686,21 @@ final class BaseAPSManager: APSManager, Injectable {
 
 
         debug(.apsManager, "Writing TDD to CoreData")
         debug(.apsManager, "Writing TDD to CoreData")
 
 
-        // Add new record to CoreData:TDD Entity
+        // MARK: Add new data to Core Data:TDD Entity
+
         let nTDD = TDD(context: coredataContext)
         let nTDD = TDD(context: coredataContext)
         nTDD.id = UUID().uuidString
         nTDD.id = UUID().uuidString
         nTDD.timestamp = Date()
         nTDD.timestamp = Date()
         nTDD.tdd = NSDecimalNumber(decimal: currentTDD)
         nTDD.tdd = NSDecimalNumber(decimal: currentTDD)
-        try! coredataContext.save()
+        try? coredataContext.save()
 
 
-        let twoWeeksAgo = Calendar.current.date(byAdding: .day, value: -14, to: Date())!
+        let twoWeeksAgo = Date().addingTimeInterval(-14.days.timeInterval)
 
 
         let requestTDD = TDD.fetchRequest() as NSFetchRequest<TDD>
         let requestTDD = TDD.fetchRequest() as NSFetchRequest<TDD>
         requestTDD.predicate = NSPredicate(format: "timestamp > %@ AND tdd > 0", twoWeeksAgo as NSDate)
         requestTDD.predicate = NSPredicate(format: "timestamp > %@ AND tdd > 0", twoWeeksAgo as NSDate)
         var uniqEvents: [TDD] = []
         var uniqEvents: [TDD] = []
 
 
-        try! uniqEvents = coredataContext.fetch(requestTDD)
+        try? uniqEvents = coredataContext.fetch(requestTDD)
 
 
         var total: Decimal = 0
         var total: Decimal = 0
         var indeces: Decimal = 0
         var indeces: Decimal = 0
@@ -709,19 +710,22 @@ final class BaseAPSManager: APSManager, Injectable {
             indeces += 1
             indeces += 1
         }
         }
 
 
-        let twoHoursAgo = Calendar.current.date(byAdding: .hour, value: -2, to: Date())!
+        let twoHoursAgo = Date().addingTimeInterval(-2.hours.timeInterval)
 
 
         requestTDD.predicate = NSPredicate(format: "timestamp > %@ AND tdd > 0", twoHoursAgo as NSDate)
         requestTDD.predicate = NSPredicate(format: "timestamp > %@ AND tdd > 0", twoHoursAgo as NSDate)
         var entriesPast2hours: [TDD] = []
         var entriesPast2hours: [TDD] = []
 
 
-        try! entriesPast2hours = coredataContext.fetch(requestTDD)
+        try? entriesPast2hours = coredataContext.fetch(requestTDD)
 
 
         var totalAmount: Decimal = 0
         var totalAmount: Decimal = 0
         var nrOfIndeces: Decimal = 0
         var nrOfIndeces: Decimal = 0
         for entry in entriesPast2hours {
         for entry in entriesPast2hours {
-            totalAmount += entry.tdd!.decimalValue
-            nrOfIndeces += 1
+            if (entry.tdd?.decimalValue ?? 0) > 0 {
+                totalAmount += entry.tdd?.decimalValue ?? 0
+                nrOfIndeces += 1
+            }
         }
         }
+
         if indeces == 0 {
         if indeces == 0 {
             indeces = 1
             indeces = 1
         }
         }
@@ -739,6 +743,7 @@ final class BaseAPSManager: APSManager, Injectable {
             date: Date()
             date: Date()
         )
         )
         storage.save(averages, as: OpenAPS.Monitor.tdd_averages)
         storage.save(averages, as: OpenAPS.Monitor.tdd_averages)
+        print("Test time of TDD: \(-1 * tddStartedAt.timeIntervalSinceNow) s")
     }
     }
 
 
     private func roundDecimal(_ decimal: Decimal, _ digits: Double) -> Decimal {
     private func roundDecimal(_ decimal: Decimal, _ digits: Double) -> Decimal {
@@ -800,17 +805,6 @@ final class BaseAPSManager: APSManager, Injectable {
             currentTDD = tdds[0].tdd!.decimalValue
             currentTDD = tdds[0].tdd!.decimalValue
         }
         }
 
 
-        let carbs_length = carbs?.count ?? 0
-        var carbTotal: Decimal = 0
-        if carbs_length != 0 {
-            for each in carbs! {
-                if each.carbs != 0 {
-                    carbTotal += each.carbs
-                }
-            }
-        }
-        var algo_ = "Oref0"
-
         var algo_ = "Oref0"
         var algo_ = "Oref0"
         let carbTotal = carbs?.map({ carbs in carbs.carbs }).reduce(0, +) ?? 0
         let carbTotal = carbs?.map({ carbs in carbs.carbs }).reduce(0, +) ?? 0
         if preferences.sigmoid, preferences.enableDynamicCR {
         if preferences.sigmoid, preferences.enableDynamicCR {