PumpEvent+helper.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import CoreData
  2. import Foundation
  3. extension PumpEventStored {
  4. static func fetch(_ predicate: NSPredicate, ascending: Bool, fetchLimit: Int? = nil) -> NSFetchRequest<PumpEventStored> {
  5. let request = PumpEventStored.fetchRequest()
  6. request.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: ascending)]
  7. request.resultType = .managedObjectResultType
  8. request.predicate = predicate
  9. if let fetchLimit = fetchLimit {
  10. request.fetchLimit = fetchLimit
  11. }
  12. return request
  13. }
  14. }
  15. public extension PumpEventStored {
  16. enum EventType: String, JSON {
  17. case bolus = "Bolus"
  18. case smb = "SMB"
  19. case isExternal = "External Insulin"
  20. case mealBolus = "Meal Bolus"
  21. case correctionBolus = "Correction Bolus"
  22. case snackBolus = "Snack Bolus"
  23. case bolusWizard = "BolusWizard"
  24. case tempBasal = "TempBasal"
  25. case tempBasalDuration = "TempBasalDuration"
  26. case pumpSuspend = "PumpSuspend"
  27. case pumpResume = "PumpResume"
  28. case pumpAlarm = "PumpAlarm"
  29. case pumpBattery = "PumpBattery"
  30. case rewind = "Rewind"
  31. case prime = "Prime"
  32. case journalCarbs = "JournalEntryMealMarker"
  33. case nsTempBasal = "Temp Basal"
  34. case nsCarbCorrection = "Carb Correction"
  35. case nsTempTarget = "Temporary Target"
  36. case nsInsulinChange = "Insulin Change"
  37. case nsSiteChange = "Site Change"
  38. case nsBatteryChange = "Pump Battery Change"
  39. case nsAnnouncement = "Announcement"
  40. case nsSensorChange = "Sensor Start"
  41. case capillaryGlucose = "BG Check"
  42. }
  43. enum TempType: String, JSON {
  44. case absolute
  45. case percent
  46. }
  47. }
  48. extension NSPredicate {
  49. static var pumpHistoryLast24h: NSPredicate {
  50. let date = Date.oneDayAgo
  51. return NSPredicate(format: "timestamp >= %@", date as NSDate)
  52. }
  53. static var recentPumpHistory: NSPredicate {
  54. let date = Date.twentyMinutesAgo
  55. return NSPredicate(format: "timestamp >= %@", date as NSDate)
  56. }
  57. static var lastPumpBolus: NSPredicate {
  58. let date = Date.twentyMinutesAgo
  59. return NSPredicate(format: "timestamp >= %@ AND bolus.isExternal == %@", date as NSDate, false as NSNumber)
  60. }
  61. static func duplicateInLastFourLoops(_ date: Date) -> NSPredicate {
  62. let date20m = Date.twentyMinutesAgo
  63. return NSPredicate(format: "timestamp >= %@ && timestamp == %@", date20m as NSDate, date as NSDate)
  64. }
  65. }
  66. // Declare helper structs ("data transfer objects" = DTO) to utilize parsing a flattened pump history
  67. struct BolusDTO: Codable {
  68. var id: String
  69. var timestamp: String
  70. var amount: Double
  71. var isExternal: Bool
  72. var isSMB: Bool
  73. var duration: Int
  74. var _type: String = "Bolus"
  75. }
  76. struct TempBasalDTO: Codable {
  77. var id: String
  78. var timestamp: String
  79. var temp: String
  80. var rate: Double
  81. var _type: String = "TempBasal"
  82. }
  83. struct TempBasalDurationDTO: Codable {
  84. var id: String
  85. var timestamp: String
  86. var duration: Int
  87. var _type: String = "TempBasalDuration"
  88. private enum CodingKeys: String, CodingKey {
  89. case id
  90. case timestamp
  91. case duration = "duration (min)"
  92. case _type
  93. }
  94. }
  95. // Mask distinct DTO subtypes with a common enum that conforms to Encodable
  96. enum PumpEventDTO: Encodable {
  97. case bolus(BolusDTO)
  98. case tempBasal(TempBasalDTO)
  99. case tempBasalDuration(TempBasalDurationDTO)
  100. func encode(to encoder: Encoder) throws {
  101. switch self {
  102. case let .bolus(bolus):
  103. try bolus.encode(to: encoder)
  104. case let .tempBasal(tempBasal):
  105. try tempBasal.encode(to: encoder)
  106. case let .tempBasalDuration(tempBasalDuration):
  107. try tempBasalDuration.encode(to: encoder)
  108. }
  109. }
  110. }
  111. // Extension with helper functions to map pump events to DTO objects via uniform masking enum
  112. extension PumpEventStored {
  113. static let dateFormatter: ISO8601DateFormatter = {
  114. let formatter = ISO8601DateFormatter()
  115. formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
  116. return formatter
  117. }()
  118. func toBolusDTOEnum() -> PumpEventDTO? {
  119. guard let timestamp = timestamp, let bolus = bolus, let amount = bolus.amount else {
  120. return nil
  121. }
  122. let bolusDTO = BolusDTO(
  123. id: id,
  124. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  125. amount: amount.doubleValue,
  126. isExternal: bolus.isExternal,
  127. isSMB: bolus.isSMB,
  128. duration: 0
  129. )
  130. return .bolus(bolusDTO)
  131. }
  132. func toTempBasalDTOEnum() -> PumpEventDTO? {
  133. guard let timestamp = timestamp, let tempBasal = tempBasal, let rate = tempBasal.rate else {
  134. return nil
  135. }
  136. let tempBasalDTO = TempBasalDTO(
  137. id: "_\(id)",
  138. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  139. temp: tempBasal.tempType ?? "unknown",
  140. rate: rate.doubleValue
  141. )
  142. return .tempBasal(tempBasalDTO)
  143. }
  144. func toTempBasalDurationDTOEnum() -> PumpEventDTO? {
  145. guard let timestamp = timestamp, let tempBasal = tempBasal else {
  146. return nil
  147. }
  148. let tempBasalDurationDTO = TempBasalDurationDTO(
  149. id: id,
  150. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  151. duration: Int(tempBasal.duration)
  152. )
  153. return .tempBasalDuration(tempBasalDurationDTO)
  154. }
  155. }