NightscoutTreatment.swift 1.5 KB

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