PumpEvent+helper.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. static var pumpEventsNotYetUploadedToTidepool: NSPredicate {
  80. let date = Date.oneDayAgo
  81. return NSPredicate(format: "timestamp >= %@ AND isUploadedToTidepool == %@", date as NSDate, false as NSNumber)
  82. }
  83. }
  84. // MARK: - PumpEventDTO and Conformance to ImportableDTO
  85. enum PumpEventDTO: Encodable, Decodable, ImportableDTO {
  86. case bolus(BolusDTO)
  87. case tempBasal(TempBasalDTO)
  88. case tempBasalDuration(TempBasalDurationDTO)
  89. case pumpSuspend(PumpSuspendDTO)
  90. func encode(to encoder: Encoder) throws {
  91. switch self {
  92. case let .bolus(bolus):
  93. try bolus.encode(to: encoder)
  94. case let .tempBasal(tempBasal):
  95. try tempBasal.encode(to: encoder)
  96. case let .tempBasalDuration(tempBasalDuration):
  97. try tempBasalDuration.encode(to: encoder)
  98. case let .pumpSuspend(pumpSuspend):
  99. try pumpSuspend.encode(to: encoder)
  100. }
  101. }
  102. // Coding keys to identify the event type in JSON.
  103. private enum CodingKeys: String, CodingKey {
  104. case _type
  105. }
  106. // Custom initializer to decode the JSON based on the `_type` field.
  107. init(from decoder: Decoder) throws {
  108. let container = try decoder.container(keyedBy: CodingKeys.self)
  109. guard let type = try? container.decode(String.self, forKey: ._type) else {
  110. throw DecodingError.keyNotFound(
  111. CodingKeys._type,
  112. DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "_type key not found")
  113. )
  114. }
  115. let singleValueContainer = try decoder.singleValueContainer()
  116. switch type {
  117. case "Bolus":
  118. let bolusDTO = try singleValueContainer.decode(BolusDTO.self)
  119. self = .bolus(bolusDTO)
  120. case "TempBasal":
  121. let tempBasalDTO = try singleValueContainer.decode(TempBasalDTO.self)
  122. self = .tempBasal(tempBasalDTO)
  123. case "TempBasalDuration":
  124. let tempBasalDurationDTO = try singleValueContainer.decode(TempBasalDurationDTO.self)
  125. self = .tempBasalDuration(tempBasalDurationDTO)
  126. case "PumpSuspend":
  127. let pumpSuspendDTO = try singleValueContainer.decode(PumpSuspendDTO.self)
  128. self = .pumpSuspend(pumpSuspendDTO)
  129. default:
  130. throw DecodingError.dataCorruptedError(
  131. forKey: ._type,
  132. in: container,
  133. debugDescription: "Unknown _type value: \(type)"
  134. )
  135. }
  136. }
  137. // Conformance to ImportableDTO
  138. typealias ManagedObject = PumpEventStored
  139. /// Stores the DTO in Core Data by mapping it to the corresponding managed object.
  140. func store(in context: NSManagedObjectContext) -> PumpEventStored {
  141. switch self {
  142. case let .bolus(bolusDTO):
  143. let pumpEvent = PumpEventStored(context: context)
  144. pumpEvent.id = bolusDTO.id
  145. pumpEvent.timestamp = ISO8601DateFormatter().date(from: bolusDTO.timestamp)
  146. pumpEvent.type = bolusDTO._type
  147. let bolus = BolusStored(context: context)
  148. bolus.amount = NSDecimalNumber(value: bolusDTO.amount)
  149. bolus.isExternal = bolusDTO.isExternal
  150. bolus.isSMB = bolusDTO.isSMB ?? false
  151. pumpEvent.bolus = bolus
  152. return pumpEvent
  153. case let .tempBasal(tempBasalDTO):
  154. let pumpEvent = PumpEventStored(context: context)
  155. pumpEvent.id = tempBasalDTO.id
  156. pumpEvent.timestamp = ISO8601DateFormatter().date(from: tempBasalDTO.timestamp)
  157. pumpEvent.type = tempBasalDTO._type
  158. let tempBasal = TempBasalStored(context: context)
  159. tempBasal.tempType = tempBasalDTO.temp
  160. tempBasal.rate = NSDecimalNumber(value: tempBasalDTO.rate)
  161. pumpEvent.tempBasal = tempBasal
  162. return pumpEvent
  163. case let .tempBasalDuration(tempBasalDurationDTO):
  164. let pumpEvent = PumpEventStored(context: context)
  165. pumpEvent.id = tempBasalDurationDTO.id
  166. pumpEvent.timestamp = ISO8601DateFormatter().date(from: tempBasalDurationDTO.timestamp)
  167. pumpEvent.type = tempBasalDurationDTO._type
  168. let tempBasal = TempBasalStored(context: context)
  169. tempBasal.duration = Int16(tempBasalDurationDTO.duration)
  170. pumpEvent.tempBasal = tempBasal
  171. return pumpEvent
  172. case .pumpSuspend:
  173. // Handle pump suspend event if needed
  174. let pumpEvent = PumpEventStored(context: context)
  175. // Set properties for pump suspend if applicable
  176. return pumpEvent
  177. }
  178. }
  179. }
  180. // Declare helper structs ("data transfer objects" = DTO) to utilize parsing a flattened pump history
  181. struct BolusDTO: Codable {
  182. var id: String
  183. var timestamp: String
  184. var amount: Double
  185. var isExternal: Bool
  186. var isSMB: Bool?
  187. var duration: Int?
  188. var _type: String = "Bolus"
  189. }
  190. struct TempBasalDTO: Codable {
  191. var id: String
  192. var timestamp: String
  193. var temp: String
  194. var rate: Double
  195. var _type: String = "TempBasal"
  196. }
  197. struct TempBasalDurationDTO: Codable {
  198. var id: String
  199. var timestamp: String
  200. var duration: Int
  201. var _type: String = "TempBasalDuration"
  202. private enum CodingKeys: String, CodingKey {
  203. case id
  204. case timestamp
  205. case duration = "duration (min)"
  206. case _type
  207. }
  208. }
  209. struct PumpSuspendDTO: Codable {
  210. var id: String
  211. var timestamp: String
  212. var reason: String?
  213. var _type: String = "PumpSuspend"
  214. }
  215. // Extension with helper functions to map pump events to DTO objects via uniform masking enum
  216. extension PumpEventStored {
  217. static let dateFormatter: ISO8601DateFormatter = {
  218. let formatter = ISO8601DateFormatter()
  219. formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
  220. return formatter
  221. }()
  222. func toBolusDTOEnum() -> PumpEventDTO? {
  223. guard let timestamp = timestamp, let bolus = bolus, let amount = bolus.amount else {
  224. return nil
  225. }
  226. let bolusDTO = BolusDTO(
  227. id: id ?? UUID().uuidString,
  228. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  229. amount: amount.doubleValue,
  230. isExternal: bolus.isExternal,
  231. isSMB: bolus.isSMB,
  232. duration: 0
  233. )
  234. return .bolus(bolusDTO)
  235. }
  236. func toTempBasalDTOEnum() -> PumpEventDTO? {
  237. guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal, let rate = tempBasal.rate else {
  238. return nil
  239. }
  240. let tempBasalDTO = TempBasalDTO(
  241. id: "_\(id)",
  242. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  243. temp: tempBasal.tempType ?? "unknown",
  244. rate: rate.doubleValue
  245. )
  246. return .tempBasal(tempBasalDTO)
  247. }
  248. func toTempBasalDurationDTOEnum() -> PumpEventDTO? {
  249. guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal else {
  250. return nil
  251. }
  252. let tempBasalDurationDTO = TempBasalDurationDTO(
  253. id: id,
  254. timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
  255. duration: Int(tempBasal.duration)
  256. )
  257. return .tempBasalDuration(tempBasalDurationDTO)
  258. }
  259. }