Determination.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Foundation
  2. struct DeterminationErrorResponse: JSON, Equatable {
  3. let error: String
  4. }
  5. struct Determination: JSON, Equatable {
  6. let id: UUID?
  7. var reason: String
  8. var units: Decimal?
  9. var insulinReq: Decimal?
  10. var eventualBG: Int?
  11. let sensitivityRatio: Decimal?
  12. var rate: Decimal?
  13. var duration: Decimal?
  14. let iob: Decimal?
  15. let cob: Decimal?
  16. var predictions: Predictions?
  17. var deliverAt: Date?
  18. let carbsReq: Decimal?
  19. let temp: TempType?
  20. var bg: Decimal?
  21. let reservoir: Decimal?
  22. var isf: Decimal?
  23. var timestamp: Date?
  24. /// `tdd` (Total Daily Dose) is included so it can be part of the
  25. /// enacted and suggested devicestatus data that gets uploaded to Nightscout.
  26. var tdd: Decimal?
  27. var current_target: Decimal?
  28. var minDelta: Decimal?
  29. var expectedDelta: Decimal?
  30. var minGuardBG: Decimal?
  31. var minPredBG: Decimal?
  32. var threshold: Decimal?
  33. let carbRatio: Decimal?
  34. let received: Bool?
  35. }
  36. struct Predictions: JSON, Equatable {
  37. let iob: [Int]?
  38. let zt: [Int]?
  39. let cob: [Int]?
  40. let uam: [Int]?
  41. }
  42. extension Determination {
  43. private enum CodingKeys: String, CodingKey {
  44. case id
  45. case reason
  46. case units
  47. case insulinReq
  48. case eventualBG
  49. case sensitivityRatio
  50. case rate
  51. case duration
  52. case iob = "IOB"
  53. case cob = "COB"
  54. case predictions = "predBGs"
  55. case deliverAt
  56. case carbsReq
  57. case temp
  58. case bg
  59. case reservoir
  60. case timestamp
  61. case isf = "ISF"
  62. case current_target
  63. case tdd = "TDD"
  64. case insulinForManualBolus
  65. case minDelta
  66. case expectedDelta
  67. case minGuardBG
  68. case minPredBG
  69. case threshold
  70. case carbRatio = "CR"
  71. case received
  72. }
  73. }
  74. extension Predictions {
  75. private enum CodingKeys: String, CodingKey {
  76. case iob = "IOB"
  77. case zt = "ZT"
  78. case cob = "COB"
  79. case uam = "UAM"
  80. }
  81. }
  82. protocol DeterminationObserver {
  83. func determinationDidUpdate(_ determination: Determination)
  84. }
  85. extension Determination {
  86. var reasonParts: [String] {
  87. reason.components(separatedBy: "; ").first?.components(separatedBy: ", ") ?? []
  88. }
  89. var reasonConclusion: String {
  90. reason.components(separatedBy: "; ").last ?? ""
  91. }
  92. }