PumpHistoryEvent.swift 3.5 KB

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