PumpHistoryEvent.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. }
  77. enum TempType: String, JSON {
  78. case absolute
  79. case percent
  80. }
  81. extension PumpHistoryEvent {
  82. private enum CodingKeys: String, CodingKey {
  83. case id
  84. case type = "_type"
  85. case timestamp
  86. case amount
  87. case duration
  88. case durationMin = "duration (min)"
  89. case rate
  90. case temp
  91. case carbInput = "carb_input"
  92. case fatInput
  93. case proteinInput
  94. case note
  95. case isSMB
  96. case isExternal
  97. }
  98. }
  99. extension EventType {
  100. func mapEventTypeToPumpEventType() -> PumpEventType? {
  101. switch self {
  102. case .prime:
  103. return PumpEventType.prime
  104. case .pumpResume:
  105. return PumpEventType.resume
  106. case .rewind:
  107. return PumpEventType.rewind
  108. case .pumpSuspend:
  109. return PumpEventType.suspend
  110. case .nsBatteryChange,
  111. .pumpBattery:
  112. return PumpEventType.replaceComponent(componentType: .pump)
  113. case .nsInsulinChange:
  114. return PumpEventType.replaceComponent(componentType: .reservoir)
  115. case .nsSiteChange:
  116. return PumpEventType.replaceComponent(componentType: .infusionSet)
  117. case .pumpAlarm:
  118. return PumpEventType.alarm
  119. default:
  120. return nil
  121. }
  122. }
  123. }