PumpHistoryEvent.swift 3.3 KB

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