|
|
@@ -27,7 +27,7 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
|
|
|
let coredataContext = CoreDataStack.shared.backgroundContext
|
|
|
|
|
|
private enum Config {
|
|
|
- static let filterTime: TimeInterval = 4.5 * 60
|
|
|
+ static let filterTime: TimeInterval = 3.5 * 60
|
|
|
}
|
|
|
|
|
|
init(resolver: Resolver) {
|
|
|
@@ -46,55 +46,29 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
|
|
|
}
|
|
|
|
|
|
func storeGlucose(_ glucose: [BloodGlucose]) {
|
|
|
+ guard let latestGlucose = glucose.last, let lastGlucoseValue = latestGlucose.glucose else { return }
|
|
|
+
|
|
|
processQueue.sync {
|
|
|
debug(.deviceManager, "start storage glucose")
|
|
|
-// let file = OpenAPS.Monitor.glucose
|
|
|
-// self.storage.transaction { storage in
|
|
|
-// storage.append(glucose, to: file, uniqBy: \.dateString)
|
|
|
-//
|
|
|
-// let uniqEvents = storage.retrieve(file, as: [BloodGlucose].self)?
|
|
|
-// .filter { $0.dateString.addingTimeInterval(24.hours.timeInterval) > Date() }
|
|
|
-// .sorted { $0.dateString > $1.dateString } ?? []
|
|
|
-// let glucose = Array(uniqEvents)
|
|
|
-// storage.save(glucose, as: file)
|
|
|
-//
|
|
|
-// DispatchQueue.main.async {
|
|
|
-// self.broadcaster.notify(GlucoseObserver.self, on: .main) {
|
|
|
-// $0.glucoseDidUpdate(glucose.reversed())
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- // MARK: - Save to CoreData.
|
|
|
-
|
|
|
- var bg_ = 0
|
|
|
- var direction = ""
|
|
|
-
|
|
|
- if glucose.isNotEmpty {
|
|
|
- bg_ = glucose[0].glucose ?? 0
|
|
|
- direction = glucose[0].direction?.symbol ?? "↔︎"
|
|
|
- }
|
|
|
|
|
|
- if bg_ != 0 {
|
|
|
- self.coredataContext.perform {
|
|
|
- let newItem = GlucoseStored(context: self.coredataContext)
|
|
|
- newItem.id = UUID()
|
|
|
- newItem.glucose = Int16(bg_)
|
|
|
- newItem.date = Date()
|
|
|
- newItem.direction = direction
|
|
|
-
|
|
|
- if self.coredataContext.hasChanges {
|
|
|
- do {
|
|
|
- try self.coredataContext.save()
|
|
|
- debugPrint(
|
|
|
- "Glucose Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved glucose to core data"
|
|
|
- )
|
|
|
- } catch {
|
|
|
- debugPrint(
|
|
|
- "Glucose Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save glucose to core data"
|
|
|
- )
|
|
|
- }
|
|
|
+ self.coredataContext.perform {
|
|
|
+ let newItem = GlucoseStored(context: self.coredataContext)
|
|
|
+ newItem.id = UUID()
|
|
|
+ newItem.glucose = Int16(lastGlucoseValue)
|
|
|
+ newItem.date = latestGlucose.dateString
|
|
|
+ newItem.direction = latestGlucose.direction?.symbol
|
|
|
+
|
|
|
+ if self.coredataContext.hasChanges {
|
|
|
+ do {
|
|
|
+ try self.coredataContext.save()
|
|
|
+ debugPrint(
|
|
|
+ "Glucose Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved glucose to core data"
|
|
|
+ )
|
|
|
+ } catch {
|
|
|
+ debugPrint(
|
|
|
+ "Glucose Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save glucose to core data"
|
|
|
+ )
|
|
|
}
|
|
|
-// }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -177,8 +151,7 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
|
|
|
|
|
|
func syncDate() -> Date {
|
|
|
// TODO: - proof logic here!
|
|
|
- /// previously the Blood Glucose array was retrieved and the date of the first, i.e. oldest value was returned (called recent????)
|
|
|
- fetchGlucose().last?.date ?? .distantPast
|
|
|
+ fetchGlucose().first?.date ?? .distantPast
|
|
|
}
|
|
|
|
|
|
func lastGlucoseDate() -> Date {
|