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. 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 tempBasal = "TempBasal"
  58. case tempBasalDuration = "TempBasalDuration"
  59. case pumpSuspend = "PumpSuspend"
  60. case pumpResume = "PumpResume"
  61. case pumpAlarm = "PumpAlarm"
  62. case pumpBattery = "PumpBattery"
  63. case rewind = "Rewind"
  64. case prime = "Prime"
  65. case nsTempBasal = "Temp Basal"
  66. case nsCarbCorrection = "Carb Correction"
  67. case nsTempTarget = "Temporary Target"
  68. case nsInsulinChange = "Insulin Change"
  69. case nsSiteChange = "Site Change"
  70. case nsBatteryChange = "Pump Battery Change"
  71. case nsAnnouncement = "Announcement"
  72. case nsSensorChange = "Sensor Start"
  73. case capillaryGlucose = "BG Check"
  74. case note = "Note"
  75. }
  76. enum TempType: String, JSON {
  77. case absolute
  78. case percent
  79. }
  80. extension PumpHistoryEvent {
  81. private enum CodingKeys: String, CodingKey {
  82. case id
  83. case type = "_type"
  84. case timestamp
  85. case amount
  86. case duration
  87. case durationMin = "duration (min)"
  88. case rate
  89. case temp
  90. case carbInput = "carb_input"
  91. case fatInput
  92. case proteinInput
  93. case note
  94. case isSMB
  95. case isExternal
  96. case isExternalInsulin
  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. }