PumpEvent+helper.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }
  54. extension PumpEventStored: Encodable {
  55. enum CodingKeys: String, CodingKey {
  56. // pump event CD entitiy
  57. case id
  58. case timestamp
  59. case type
  60. // bolus CD entitity
  61. case amount
  62. case isSMB
  63. case isExternal
  64. // temp basal CD entity
  65. case duration
  66. case rate
  67. case temp
  68. }
  69. public func encode(to encoder: Encoder) throws {
  70. var container = encoder.container(keyedBy: CodingKeys.self)
  71. let dateFormatter = ISO8601DateFormatter()
  72. let formattedDate = dateFormatter.string(from: timestamp ?? Date())
  73. // PumpEventStored
  74. try container.encode(id, forKey: .id)
  75. try container.encode(formattedDate, forKey: .timestamp)
  76. try container.encode(type, forKey: .type)
  77. // access to BolusStored entity
  78. //
  79. // amount
  80. if let bolusAmount = bolus?.amount as Decimal? {
  81. try container.encode(bolusAmount, forKey: .amount)
  82. } else {
  83. // Default value
  84. try container.encode(Decimal(0), forKey: .amount)
  85. }
  86. // isSMB
  87. if let isSMB = bolus?.isSMB {
  88. try container.encode(isSMB, forKey: .isSMB)
  89. } else {
  90. try container.encode(true, forKey: .amount)
  91. }
  92. // isExternal
  93. if let isExternal = bolus?.isExternal {
  94. try container.encode(isExternal, forKey: .isExternal)
  95. } else {
  96. try container.encode(false, forKey: .isExternal)
  97. }
  98. // access to TempBasalStored entity
  99. //
  100. // duration
  101. if let duration = tempBasal?.duration {
  102. try container.encode(duration, forKey: .duration)
  103. } else {
  104. try container.encode(0, forKey: .duration)
  105. }
  106. // rate
  107. if let rate = tempBasal?.rate as Decimal? {
  108. try container.encode(rate, forKey: .rate)
  109. } else {
  110. try container.encode(0, forKey: .rate)
  111. }
  112. // temp type
  113. // its called "temp" in the json thats passed into determineBasal hence the undescriptive name of this coding key
  114. if let tempType = tempBasal?.tempType {
  115. try container.encode(tempType, forKey: .temp)
  116. } else {
  117. try container.encode("absolute", forKey: .temp)
  118. }
  119. }
  120. }