Determination.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Foundation
  2. struct Determination: JSON, Equatable {
  3. let id: UUID?
  4. var reason: String
  5. let units: Decimal?
  6. let insulinReq: Decimal?
  7. var eventualBG: Int?
  8. let sensitivityRatio: Decimal?
  9. let rate: Decimal?
  10. let duration: Decimal?
  11. let iob: Decimal?
  12. let cob: Decimal?
  13. var predictions: Predictions?
  14. var deliverAt: Date?
  15. let carbsReq: Decimal?
  16. let temp: TempType?
  17. var bg: Decimal?
  18. let reservoir: Decimal?
  19. var isf: Decimal?
  20. var timestamp: Date?
  21. /// `tdd` (Total Daily Dose) is included so it can be part of the
  22. /// enacted and suggested devicestatus data that gets uploaded to Nightscout.
  23. var tdd: Decimal?
  24. var current_target: Decimal?
  25. let insulinForManualBolus: Decimal?
  26. let manualBolusErrorString: Decimal?
  27. var minDelta: Decimal?
  28. var expectedDelta: Decimal?
  29. var minGuardBG: Decimal?
  30. var minPredBG: Decimal?
  31. var threshold: Decimal?
  32. let carbRatio: Decimal?
  33. let received: Bool?
  34. }
  35. struct Predictions: JSON, Equatable {
  36. let iob: [Int]?
  37. let zt: [Int]?
  38. let cob: [Int]?
  39. let uam: [Int]?
  40. }
  41. extension Determination {
  42. private enum CodingKeys: String, CodingKey {
  43. case id
  44. case reason
  45. case units
  46. case insulinReq
  47. case eventualBG
  48. case sensitivityRatio
  49. case rate
  50. case duration
  51. case iob = "IOB"
  52. case cob = "COB"
  53. case predictions = "predBGs"
  54. case deliverAt
  55. case carbsReq
  56. case temp
  57. case bg
  58. case reservoir
  59. case timestamp
  60. case isf = "ISF"
  61. case current_target
  62. case tdd = "TDD"
  63. case insulinForManualBolus
  64. case manualBolusErrorString
  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. }