| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- import Combine
- import CoreData
- import Foundation
- import LoopKit
- import SwiftDate
- import Swinject
- protocol PumpHistoryObserver {
- func pumpHistoryDidUpdate(_ events: [PumpHistoryEvent])
- }
- protocol PumpHistoryStorage {
- var updatePublisher: AnyPublisher<Void, Never> { get }
- func storePumpEvents(_ events: [NewPumpEvent])
- func storeExternalInsulinEvent(amount: Decimal, timestamp: Date) async
- func recent() -> [PumpHistoryEvent]
- func getPumpHistoryNotYetUploadedToNightscout() async -> [NightscoutTreatment]
- func getPumpHistoryNotYetUploadedToHealth() async -> [PumpHistoryEvent]
- func getPumpHistoryNotYetUploadedToTidepool() async -> [PumpHistoryEvent]
- func deleteInsulin(at date: Date)
- }
- final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
- private let processQueue = DispatchQueue(label: "BasePumpHistoryStorage.processQueue")
- @Injected() private var storage: FileStorage!
- @Injected() private var broadcaster: Broadcaster!
- @Injected() private var settings: SettingsManager!
- private let updateSubject = PassthroughSubject<Void, Never>()
- var updatePublisher: AnyPublisher<Void, Never> {
- updateSubject.eraseToAnyPublisher()
- }
- init(resolver: Resolver) {
- injectServices(resolver)
- }
- typealias PumpEvent = PumpEventStored.EventType
- typealias TempType = PumpEventStored.TempType
- private let context = CoreDataStack.shared.newTaskContext()
- private func roundDose(_ dose: Double, toIncrement increment: Double) -> Decimal {
- let roundedValue = (dose / increment).rounded() * increment
- return Decimal(roundedValue)
- }
- func storePumpEvents(_ events: [NewPumpEvent]) {
- processQueue.async {
- self.context.perform {
- for event in events {
- // Fetch to filter out duplicates
- // TODO: - move this to the Core Data Class
- let existingEvents: [PumpEventStored] = CoreDataStack.shared.fetchEntities(
- ofType: PumpEventStored.self,
- onContext: self.context,
- predicate: NSPredicate.duplicateInLastHour(event.date),
- key: "timestamp",
- ascending: false,
- batchSize: 50
- )
- switch event.type {
- case .bolus:
- guard let dose = event.dose else { continue }
- let amount = self.roundDose(
- dose.unitsInDeliverableIncrements,
- toIncrement: Double(self.settings.preferences.bolusIncrement)
- )
- guard existingEvents.isEmpty else {
- // Duplicate found, do not store the event
- print("Duplicate event found with timestamp: \(event.date)")
- if let existingEvent = existingEvents.first(where: { $0.type == EventType.bolus.rawValue }) {
- if existingEvent.timestamp == event.date {
- if let existingAmount = existingEvent.bolus?.amount, amount < existingAmount as Decimal {
- // Update existing event with new smaller value
- existingEvent.bolus?.amount = amount as NSDecimalNumber
- existingEvent.bolus?.isSMB = dose.automatic ?? true
- existingEvent.isUploadedToNS = false
- existingEvent.isUploadedToHealth = false
- existingEvent.isUploadedToTidepool = false
- print("Updated existing event with smaller value: \(amount)")
- }
- }
- }
- continue
- }
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = event.date
- newPumpEvent.type = PumpEvent.bolus.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- let newBolusEntry = BolusStored(context: self.context)
- newBolusEntry.pumpEvent = newPumpEvent
- newBolusEntry.amount = NSDecimalNumber(decimal: amount)
- newBolusEntry.isExternal = dose.manuallyEntered
- newBolusEntry.isSMB = dose.automatic ?? true
- case .tempBasal:
- guard let dose = event.dose else { continue }
- guard existingEvents.isEmpty else {
- // Duplicate found, do not store the event
- print("Duplicate event found with timestamp: \(event.date)")
- continue
- }
- let rate = Decimal(dose.unitsPerHour)
- let minutes = (dose.endDate - dose.startDate).timeInterval / 60
- let delivered = dose.deliveredUnits
- let date = event.date
- let isCancel = delivered != nil
- guard !isCancel else { continue }
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = date
- newPumpEvent.type = PumpEvent.tempBasal.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- let newTempBasal = TempBasalStored(context: self.context)
- newTempBasal.pumpEvent = newPumpEvent
- newTempBasal.duration = Int16(round(minutes))
- newTempBasal.rate = rate as NSDecimalNumber
- newTempBasal.tempType = TempType.absolute.rawValue
- case .suspend:
- guard existingEvents.isEmpty else {
- // Duplicate found, do not store the event
- print("Duplicate event found with timestamp: \(event.date)")
- continue
- }
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = event.date
- newPumpEvent.type = PumpEvent.pumpSuspend.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- case .resume:
- guard existingEvents.isEmpty else {
- // Duplicate found, do not store the event
- print("Duplicate event found with timestamp: \(event.date)")
- continue
- }
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = event.date
- newPumpEvent.type = PumpEvent.pumpResume.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- case .rewind:
- guard existingEvents.isEmpty else {
- // Duplicate found, do not store the event
- print("Duplicate event found with timestamp: \(event.date)")
- continue
- }
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = event.date
- newPumpEvent.type = PumpEvent.rewind.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- case .prime:
- guard existingEvents.isEmpty else {
- // Duplicate found, do not store the event
- print("Duplicate event found with timestamp: \(event.date)")
- continue
- }
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = event.date
- newPumpEvent.type = PumpEvent.prime.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- case .alarm:
- guard existingEvents.isEmpty else {
- // Duplicate found, do not store the event
- print("Duplicate event found with timestamp: \(event.date)")
- continue
- }
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = event.date
- newPumpEvent.type = PumpEvent.pumpAlarm.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- newPumpEvent.note = event.title
- default:
- continue
- }
- }
- do {
- guard self.context.hasChanges else { return }
- try self.context.save()
- self.updateSubject.send(())
- debugPrint("\(DebuggingIdentifiers.succeeded) stored pump events in Core Data")
- } catch let error as NSError {
- debugPrint("\(DebuggingIdentifiers.failed) failed to store pump events with error: \(error.userInfo)")
- }
- }
- }
- }
- func storeExternalInsulinEvent(amount: Decimal, timestamp: Date) async {
- debug(.default, "External insulin saved")
- await context.perform {
- // create pump event
- let newPumpEvent = PumpEventStored(context: self.context)
- newPumpEvent.id = UUID().uuidString
- newPumpEvent.timestamp = timestamp
- newPumpEvent.type = PumpEvent.bolus.rawValue
- newPumpEvent.isUploadedToNS = false
- newPumpEvent.isUploadedToHealth = false
- newPumpEvent.isUploadedToTidepool = false
- // create bolus entry and specify relationship to pump event
- let newBolusEntry = BolusStored(context: self.context)
- newBolusEntry.pumpEvent = newPumpEvent
- newBolusEntry.amount = amount as NSDecimalNumber
- newBolusEntry.isExternal = true // we are creating an external dose
- newBolusEntry.isSMB = false // the dose is manually administered
- do {
- guard self.context.hasChanges else { return }
- try self.context.save()
- self.updateSubject.send(())
- } catch {
- print(error.localizedDescription)
- }
- }
- }
- func recent() -> [PumpHistoryEvent] {
- storage.retrieve(OpenAPS.Monitor.pumpHistory, as: [PumpHistoryEvent].self)?.reversed() ?? []
- }
- func deleteInsulin(at date: Date) {
- processQueue.sync {
- var allValues = storage.retrieve(OpenAPS.Monitor.pumpHistory, as: [PumpHistoryEvent].self) ?? []
- guard let entryIndex = allValues.firstIndex(where: { $0.timestamp == date }) else {
- return
- }
- allValues.remove(at: entryIndex)
- storage.save(allValues, as: OpenAPS.Monitor.pumpHistory)
- broadcaster.notify(PumpHistoryObserver.self, on: processQueue) {
- $0.pumpHistoryDidUpdate(allValues)
- }
- }
- }
- func determineBolusEventType(for event: PumpEventStored) -> PumpEventStored.EventType {
- if event.bolus!.isSMB {
- return .smb
- }
- if event.bolus!.isExternal {
- return .isExternal
- }
- return PumpEventStored.EventType(rawValue: event.type!) ?? PumpEventStored.EventType.bolus
- }
- func getPumpHistoryNotYetUploadedToNightscout() async -> [NightscoutTreatment] {
- let results = await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: PumpEventStored.self,
- onContext: context,
- predicate: NSPredicate.pumpEventsNotYetUploadedToNightscout,
- key: "timestamp",
- ascending: false,
- fetchLimit: 288
- )
- return await context.perform { [self] in
- guard let fetchedPumpEvents = results as? [PumpEventStored] else { return [] }
- return fetchedPumpEvents.map { event in
- switch event.type {
- case PumpEvent.bolus.rawValue:
- // eventType determines whether bolus is external, smb or manual (=administered via app by user)
- let eventType = determineBolusEventType(for: event)
- return NightscoutTreatment(
- duration: nil,
- rawDuration: nil,
- rawRate: nil,
- absolute: nil,
- rate: nil,
- eventType: eventType,
- createdAt: event.timestamp,
- enteredBy: NightscoutTreatment.local,
- bolus: nil,
- insulin: event.bolus?.amount as Decimal?,
- notes: nil,
- carbs: nil,
- fat: nil,
- protein: nil,
- targetTop: nil,
- targetBottom: nil,
- id: event.id
- )
- case PumpEvent.tempBasal.rawValue:
- return NightscoutTreatment(
- duration: Int(event.tempBasal?.duration ?? 0),
- rawDuration: nil,
- rawRate: nil,
- absolute: event.tempBasal?.rate as Decimal?,
- rate: event.tempBasal?.rate as Decimal?,
- eventType: .nsTempBasal,
- createdAt: event.timestamp,
- enteredBy: NightscoutTreatment.local,
- bolus: nil,
- insulin: nil,
- notes: nil,
- carbs: nil,
- fat: nil,
- protein: nil,
- targetTop: nil,
- targetBottom: nil,
- id: event.id
- )
- case PumpEvent.pumpSuspend.rawValue:
- return NightscoutTreatment(
- duration: nil,
- rawDuration: nil,
- rawRate: nil,
- absolute: nil,
- rate: nil,
- eventType: .nsNote,
- createdAt: event.timestamp,
- enteredBy: NightscoutTreatment.local,
- bolus: nil,
- insulin: nil,
- notes: PumpEvent.pumpSuspend.rawValue,
- carbs: nil,
- fat: nil,
- protein: nil,
- targetTop: nil,
- targetBottom: nil
- )
- case PumpEvent.pumpResume.rawValue:
- return NightscoutTreatment(
- duration: nil,
- rawDuration: nil,
- rawRate: nil,
- absolute: nil,
- rate: nil,
- eventType: .nsNote,
- createdAt: event.timestamp,
- enteredBy: NightscoutTreatment.local,
- bolus: nil,
- insulin: nil,
- notes: PumpEvent.pumpResume.rawValue,
- carbs: nil,
- fat: nil,
- protein: nil,
- targetTop: nil,
- targetBottom: nil
- )
- case PumpEvent.rewind.rawValue:
- return NightscoutTreatment(
- duration: nil,
- rawDuration: nil,
- rawRate: nil,
- absolute: nil,
- rate: nil,
- eventType: .nsInsulinChange,
- createdAt: event.timestamp,
- enteredBy: NightscoutTreatment.local,
- bolus: nil,
- insulin: nil,
- notes: nil,
- carbs: nil,
- fat: nil,
- protein: nil,
- targetTop: nil,
- targetBottom: nil
- )
- case PumpEvent.prime.rawValue:
- return NightscoutTreatment(
- duration: nil,
- rawDuration: nil,
- rawRate: nil,
- absolute: nil,
- rate: nil,
- eventType: .nsSiteChange,
- createdAt: event.timestamp,
- enteredBy: NightscoutTreatment.local,
- bolus: nil,
- insulin: nil,
- notes: nil,
- carbs: nil,
- fat: nil,
- protein: nil,
- targetTop: nil,
- targetBottom: nil
- )
- case PumpEvent.pumpAlarm.rawValue:
- return NightscoutTreatment(
- duration: 30, // minutes
- rawDuration: nil,
- rawRate: nil,
- absolute: nil,
- rate: nil,
- eventType: .nsAnnouncement,
- createdAt: event.timestamp,
- enteredBy: NightscoutTreatment.local,
- bolus: nil,
- insulin: nil,
- notes: "Alarm \(String(describing: event.note)) \(PumpEvent.pumpAlarm.rawValue)",
- carbs: nil,
- fat: nil,
- protein: nil,
- targetTop: nil,
- targetBottom: nil
- )
- default:
- return nil
- }
- }.compactMap { $0 }
- }
- }
- func getPumpHistoryNotYetUploadedToHealth() async -> [PumpHistoryEvent] {
- let results = await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: PumpEventStored.self,
- onContext: context,
- predicate: NSPredicate.pumpEventsNotYetUploadedToHealth,
- key: "timestamp",
- ascending: false,
- fetchLimit: 288
- )
- guard let fetchedPumpEvents = results as? [PumpEventStored] else { return [] }
- return await context.perform {
- fetchedPumpEvents.map { event in
- switch event.type {
- case PumpEvent.bolus.rawValue:
- return PumpHistoryEvent(
- id: event.id ?? UUID().uuidString,
- type: .bolus,
- timestamp: event.timestamp ?? Date(),
- amount: event.bolus?.amount as Decimal?
- )
- case PumpEvent.tempBasal.rawValue:
- if let id = event.id, let timestamp = event.timestamp, let tempBasal = event.tempBasal,
- let tempBasalRate = tempBasal.rate
- {
- return PumpHistoryEvent(
- id: id,
- type: .tempBasal,
- timestamp: timestamp,
- amount: tempBasalRate as Decimal,
- duration: Int(tempBasal.duration)
- )
- } else {
- return nil
- }
- default:
- return nil
- }
- }.compactMap { $0 }
- }
- }
- func getPumpHistoryNotYetUploadedToTidepool() async -> [PumpHistoryEvent] {
- let results = await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: PumpEventStored.self,
- onContext: context,
- predicate: NSPredicate.pumpEventsNotYetUploadedToTidepool,
- key: "timestamp",
- ascending: false,
- fetchLimit: 288
- )
- guard let fetchedPumpEvents = results as? [PumpEventStored] else { return [] }
- return await context.perform {
- fetchedPumpEvents.map { event in
- switch event.type {
- case PumpEvent.bolus.rawValue:
- return PumpHistoryEvent(
- id: event.id ?? UUID().uuidString,
- type: .bolus,
- timestamp: event.timestamp ?? Date(),
- amount: event.bolus?.amount as Decimal?,
- isSMB: event.bolus?.isSMB ?? true,
- isExternal: event.bolus?.isExternal ?? false
- )
- case PumpEvent.tempBasal.rawValue:
- if let id = event.id, let timestamp = event.timestamp, let tempBasal = event.tempBasal,
- let tempBasalRate = tempBasal.rate
- {
- return PumpHistoryEvent(
- id: id,
- type: .tempBasal,
- timestamp: timestamp,
- amount: tempBasalRate as Decimal,
- duration: Int(tempBasal.duration)
- )
- } else {
- return nil
- }
- default:
- return nil
- }
- }.compactMap { $0 }
- }
- }
- }
|