Determination.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. let units: Decimal?
  9. let 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 insulinForManualBolus: Decimal?
  29. var manualBolusErrorString: Decimal?
  30. var minDelta: Decimal?
  31. var expectedDelta: Decimal?
  32. var minGuardBG: Decimal?
  33. var minPredBG: Decimal?
  34. var threshold: Decimal?
  35. let carbRatio: Decimal?
  36. let received: Bool?
  37. }
  38. struct Predictions: JSON, Equatable {
  39. let iob: [Int]?
  40. let zt: [Int]?
  41. let cob: [Int]?
  42. let uam: [Int]?
  43. }
  44. extension Determination {
  45. private enum CodingKeys: String, CodingKey {
  46. case id
  47. case reason
  48. case units
  49. case insulinReq
  50. case eventualBG
  51. case sensitivityRatio
  52. case rate
  53. case duration
  54. case iob = "IOB"
  55. case cob = "COB"
  56. case predictions = "predBGs"
  57. case deliverAt
  58. case carbsReq
  59. case temp
  60. case bg
  61. case reservoir
  62. case timestamp
  63. case isf = "ISF"
  64. case current_target
  65. case tdd = "TDD"
  66. case insulinForManualBolus
  67. case manualBolusErrorString
  68. case minDelta
  69. case expectedDelta
  70. case minGuardBG
  71. case minPredBG
  72. case threshold
  73. case carbRatio = "CR"
  74. case received
  75. }
  76. }
  77. extension Predictions {
  78. private enum CodingKeys: String, CodingKey {
  79. case iob = "IOB"
  80. case zt = "ZT"
  81. case cob = "COB"
  82. case uam = "UAM"
  83. }
  84. }
  85. protocol DeterminationObserver {
  86. func determinationDidUpdate(_ determination: Determination)
  87. }
  88. extension Determination {
  89. var reasonParts: [String] {
  90. reason.components(separatedBy: "; ").first?.components(separatedBy: ", ") ?? []
  91. }
  92. var reasonConclusion: String {
  93. reason.components(separatedBy: "; ").last ?? ""
  94. }
  95. }