PumpHistoryEvent.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Foundation
  2. import LoopKit
  3. struct PumpHistoryEvent: JSON, Equatable {
  4. let id: String
  5. let type: EventType
  6. let timestamp: Date
  7. let amount: Decimal?
  8. let duration: Int?
  9. let durationMin: Int?
  10. let rate: Decimal?
  11. let temp: TempType?
  12. let carbInput: Int?
  13. let note: String?
  14. init(
  15. id: String,
  16. type: EventType,
  17. timestamp: Date,
  18. amount: Decimal? = nil,
  19. duration: Int? = nil,
  20. durationMin: Int? = nil,
  21. rate: Decimal? = nil,
  22. temp: TempType? = nil,
  23. carbInput: Int? = nil,
  24. note: String? = nil
  25. ) {
  26. self.id = id
  27. self.type = type
  28. self.timestamp = timestamp
  29. self.amount = amount
  30. self.duration = duration
  31. self.durationMin = durationMin
  32. self.rate = rate
  33. self.temp = temp
  34. self.carbInput = carbInput
  35. self.note = note
  36. }
  37. }
  38. enum EventType: String, JSON {
  39. case bolus = "Bolus"
  40. case mealBulus = "Meal Bolus"
  41. case correctionBolus = "Correction Bolus"
  42. case snackBolus = "Snack Bolus"
  43. case bolusWizard = "BolusWizard"
  44. case tempBasal = "TempBasal"
  45. case tempBasalDuration = "TempBasalDuration"
  46. case pumpSuspend = "PumpSuspend"
  47. case pumpResume = "PumpResume"
  48. case pumpAlarm = "PumpAlarm"
  49. case pumpBattery = "PumpBattery"
  50. case rewind = "Rewind"
  51. case prime = "Prime"
  52. case journalCarbs = "JournalEntryMealMarker"
  53. case nsTempBasal = "Temp Basal"
  54. case nsCarbCorrection = "Carb Correction"
  55. case nsTempTarget = "Temporary Target"
  56. case nsInsulinChange = "Insulin Change"
  57. case nsSiteChange = "Site Change"
  58. case nsBatteryChange = "Pump Battery Change"
  59. case nsAnnouncement = "Announcement"
  60. case nsSensorChange = "Sensor Start"
  61. }
  62. enum TempType: String, JSON {
  63. case absolute
  64. case percent
  65. }
  66. extension PumpHistoryEvent {
  67. private enum CodingKeys: String, CodingKey {
  68. case id
  69. case type = "_type"
  70. case timestamp
  71. case amount
  72. case duration
  73. case durationMin = "duration (min)"
  74. case rate
  75. case temp
  76. case carbInput = "carb_input"
  77. case note
  78. }
  79. }
  80. extension EventType {
  81. func mapEventTypeToPumpEventType() -> PumpEventType? {
  82. switch self {
  83. case .prime:
  84. return PumpEventType.prime
  85. case .pumpResume:
  86. return PumpEventType.resume
  87. case .rewind:
  88. return PumpEventType.rewind
  89. case .pumpSuspend:
  90. return PumpEventType.suspend
  91. case .nsBatteryChange,
  92. .pumpBattery:
  93. return PumpEventType.replaceComponent(componentType: .pump)
  94. case .nsInsulinChange:
  95. return PumpEventType.replaceComponent(componentType: .reservoir)
  96. case .nsSiteChange:
  97. return PumpEventType.replaceComponent(componentType: .infusionSet)
  98. case .pumpAlarm:
  99. return PumpEventType.alarm
  100. default:
  101. return nil
  102. }
  103. }
  104. }