PumpHistoryEvent.swift 3.7 KB

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