PumpHistoryEvent.swift 3.2 KB

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