PumpEvent+helper.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 nsNote = "Note"
  34. case nsTempBasal = "Temp Basal"
  35. case nsCarbCorrection = "Carb Correction"
  36. case nsTempTarget = "Temporary Target"
  37. case nsInsulinChange = "Insulin Change"
  38. case nsSiteChange = "Site Change"
  39. case nsBatteryChange = "Pump Battery Change"
  40. case nsAnnouncement = "Announcement"
  41. case nsSensorChange = "Sensor Start"
  42. case nsExercise = "Exercise"
  43. case capillaryGlucose = "BG Check"
  44. }
  45. enum TempType: String, JSON {
  46. case absolute
  47. case percent
  48. }
  49. }
  50. extension NSPredicate {
  51. static var pumpHistoryLast1440Minutes: NSPredicate {
  52. let date = Date.oneDayAgoInMinutes
  53. return NSPredicate(format: "timestamp >= %@", date as NSDate)
  54. }
  55. static var pumpHistoryLast24h: NSPredicate {
  56. let date = Date.oneDayAgo
  57. return NSPredicate(format: "timestamp >= %@", date as NSDate)
  58. }
  59. static var recentPumpHistory: NSPredicate {
  60. let date = Date.twentyMinutesAgo
  61. return NSPredicate(format: "timestamp >= %@", date as NSDate)
  62. }
  63. static var lastPumpBolus: NSPredicate {
  64. let date = Date.twentyMinutesAgo
  65. return NSPredicate(format: "timestamp >= %@ AND bolus.isExternal == %@", date as NSDate, false as NSNumber)
  66. }
  67. static func duplicateInLastHour(_ date: Date) -> NSPredicate {
  68. let date60m = Date.oneHourAgo
  69. return NSPredicate(format: "timestamp >= %@ && timestamp == %@", date60m as NSDate, date as NSDate)
  70. }
  71. static var pumpEventsNotYetUploadedToNightscout: NSPredicate {
  72. let date = Date.oneDayAgo
  73. return NSPredicate(format: "timestamp >= %@ AND isUploadedToNS == %@", date as NSDate, false as NSNumber)
  74. }
  75. static var pumpEventsNotYetUploadedToHealth: NSPredicate {
  76. let date = Date.oneDayAgo
  77. return NSPredicate(format: "timestamp >= %@ AND isUploadedToHealth == %@", date as NSDate, false as NSNumber)
  78. }
  79. }
  80. // Declare helper structs ("data transfer objects" = DTO) to utilize parsing a flattened pump history
  81. struct BolusDTO: Codable {
  82. var id: String
  83. var timestamp: String
  84. var amount: Double
  85. var isExternal: Bool
  86. var isSMB: Bool
  87. var duration: Int
  88. var _type: String = "Bolus"
  89. }
  90. struct TempBasalDTO: Codable {
  91. var id: String
  92. var timestamp: String
  93. var temp: String
  94. var rate: Double
  95. var _type: String = "TempBasal"
  96. }
  97. struct TempBasalDurationDTO: Codable {
  98. var id: String
  99. var timestamp: String
  100. var duration: Int
  101. var _type: String = "TempBasalDuration"
  102. private enum CodingKeys: String, CodingKey {
  103. case id
  104. case timestamp
  105. case duration = "duration (min)"
  106. case _type
  107. }
  108. }
  109. // Mask distinct DTO subtypes with a common enum that conforms to Encodable
  110. enum PumpEventDTO: Encodable {
  111. case bolus(BolusDTO)
  112. case tempBasal(TempBasalDTO)
  113. case tempBasalDuration(TempBasalDurationDTO)
  114. func encode(to encoder: Encoder) throws {
  115. switch self {
  116. case let .bolus(bolus):
  117. try bolus.encode(to: encoder)
  118. case let .tempBasal(tempBasal):
  119. try tempBasal.encode(to: encoder)
  120. case let .tempBasalDuration(tempBasalDuration):
  121. try tempBasalDuration.encode(to: encoder)
  122. }
  123. }
  124. }
  125. // Extension with helper functions to map pump events to DTO objects via uniform masking enum
  126. extension PumpEventStored {
  127. static let dateFormatter: ISO8601DateFormatter = {
  128. let formatter = ISO8601DateFormatter()
  129. formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
  130. return formatter
  131. }()
  132. func toBolusDTOEnum() -> PumpEventDTO? {
  133. guard let timestamp = timestamp, let bolus = bolus, let amount = bolus.amount else {
  134. return nil
  135. }
  136. let bolusDTO = BolusDTO(
  137. id: id ?? UUID().uuidString,
  138. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  139. amount: amount.doubleValue,
  140. isExternal: bolus.isExternal,
  141. isSMB: bolus.isSMB,
  142. duration: 0
  143. )
  144. return .bolus(bolusDTO)
  145. }
  146. func toTempBasalDTOEnum() -> PumpEventDTO? {
  147. guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal, let rate = tempBasal.rate else {
  148. return nil
  149. }
  150. let tempBasalDTO = TempBasalDTO(
  151. id: "_\(id)",
  152. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  153. temp: tempBasal.tempType ?? "unknown",
  154. rate: rate.doubleValue
  155. )
  156. return .tempBasal(tempBasalDTO)
  157. }
  158. func toTempBasalDurationDTOEnum() -> PumpEventDTO? {
  159. guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal else {
  160. return nil
  161. }
  162. let tempBasalDurationDTO = TempBasalDurationDTO(
  163. id: id,
  164. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  165. duration: Int(tempBasal.duration)
  166. )
  167. return .tempBasalDuration(tempBasalDurationDTO)
  168. }
  169. }