PumpEvent+helper.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. }
  76. // Declare helper structs ("data transfer objects" = DTO) to utilize parsing a flattened pump history
  77. struct BolusDTO: Codable {
  78. var id: String
  79. var timestamp: String
  80. var amount: Double
  81. var isExternal: Bool
  82. var isSMB: Bool
  83. var duration: Int
  84. var _type: String = "Bolus"
  85. }
  86. struct TempBasalDTO: Codable {
  87. var id: String
  88. var timestamp: String
  89. var temp: String
  90. var rate: Double
  91. var _type: String = "TempBasal"
  92. }
  93. struct TempBasalDurationDTO: Codable {
  94. var id: String
  95. var timestamp: String
  96. var duration: Int
  97. var _type: String = "TempBasalDuration"
  98. private enum CodingKeys: String, CodingKey {
  99. case id
  100. case timestamp
  101. case duration = "duration (min)"
  102. case _type
  103. }
  104. }
  105. // Mask distinct DTO subtypes with a common enum that conforms to Encodable
  106. enum PumpEventDTO: Encodable {
  107. case bolus(BolusDTO)
  108. case tempBasal(TempBasalDTO)
  109. case tempBasalDuration(TempBasalDurationDTO)
  110. func encode(to encoder: Encoder) throws {
  111. switch self {
  112. case let .bolus(bolus):
  113. try bolus.encode(to: encoder)
  114. case let .tempBasal(tempBasal):
  115. try tempBasal.encode(to: encoder)
  116. case let .tempBasalDuration(tempBasalDuration):
  117. try tempBasalDuration.encode(to: encoder)
  118. }
  119. }
  120. }
  121. // Extension with helper functions to map pump events to DTO objects via uniform masking enum
  122. extension PumpEventStored {
  123. static let dateFormatter: ISO8601DateFormatter = {
  124. let formatter = ISO8601DateFormatter()
  125. formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
  126. return formatter
  127. }()
  128. func toBolusDTOEnum() -> PumpEventDTO? {
  129. guard let timestamp = timestamp, let bolus = bolus, let amount = bolus.amount else {
  130. return nil
  131. }
  132. let bolusDTO = BolusDTO(
  133. id: id ?? UUID().uuidString,
  134. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  135. amount: amount.doubleValue,
  136. isExternal: bolus.isExternal,
  137. isSMB: bolus.isSMB,
  138. duration: 0
  139. )
  140. return .bolus(bolusDTO)
  141. }
  142. func toTempBasalDTOEnum() -> PumpEventDTO? {
  143. guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal, let rate = tempBasal.rate else {
  144. return nil
  145. }
  146. let tempBasalDTO = TempBasalDTO(
  147. id: "_\(id)",
  148. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  149. temp: tempBasal.tempType ?? "unknown",
  150. rate: rate.doubleValue
  151. )
  152. return .tempBasal(tempBasalDTO)
  153. }
  154. func toTempBasalDurationDTOEnum() -> PumpEventDTO? {
  155. guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal else {
  156. return nil
  157. }
  158. let tempBasalDurationDTO = TempBasalDurationDTO(
  159. id: id,
  160. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  161. duration: Int(tempBasal.duration)
  162. )
  163. return .tempBasalDuration(tempBasalDurationDTO)
  164. }
  165. }