NightscoutTreatment.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Foundation
  2. func determineBolusEventType(for event: PumpHistoryEvent) -> EventType {
  3. if event.isExternalInsulin ?? false {
  4. return .nsExternalInsulin
  5. } else if event.isSMB ?? false {
  6. return .smb
  7. }
  8. return event.type
  9. }
  10. struct NightscoutTreatment: JSON, Hashable, Equatable {
  11. var duration: Int?
  12. var rawDuration: PumpHistoryEvent?
  13. var rawRate: PumpHistoryEvent?
  14. var absolute: Decimal?
  15. var rate: Decimal?
  16. var eventType: EventType
  17. var createdAt: Date?
  18. var enteredBy: String?
  19. var bolus: PumpHistoryEvent?
  20. var insulin: Decimal?
  21. var notes: String?
  22. var carbs: Decimal?
  23. var fat: Decimal?
  24. var protein: Decimal?
  25. var foodType: String?
  26. let targetTop: Decimal?
  27. let targetBottom: Decimal?
  28. static let local = "Trio"
  29. static let empty = NightscoutTreatment(from: "{}")!
  30. static func == (lhs: NightscoutTreatment, rhs: NightscoutTreatment) -> Bool {
  31. (lhs.createdAt ?? Date()) == (rhs.createdAt ?? Date())
  32. }
  33. func hash(into hasher: inout Hasher) {
  34. hasher.combine(createdAt ?? Date())
  35. }
  36. }
  37. extension NightscoutTreatment {
  38. private enum CodingKeys: String, CodingKey {
  39. case duration
  40. case rawDuration = "raw_duration"
  41. case rawRate = "raw_rate"
  42. case absolute
  43. case rate
  44. case eventType
  45. case createdAt = "created_at"
  46. case enteredBy
  47. case bolus
  48. case insulin
  49. case notes
  50. case carbs
  51. case fat
  52. case protein
  53. case foodType
  54. case targetTop
  55. case targetBottom
  56. }
  57. }