Determination.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import CoreData
  2. import Foundation
  3. struct Determination: JSON, Equatable, Decodable {
  4. let id: UUID?
  5. var reason: String
  6. let units: Decimal?
  7. let insulinReq: Decimal?
  8. var eventualBG: Int?
  9. let sensitivityRatio: Decimal?
  10. let rate: Decimal?
  11. let duration: Decimal?
  12. let iob: Decimal?
  13. let cob: Decimal?
  14. var predictions: Predictions?
  15. var deliverAt: Date?
  16. let carbsReq: Decimal?
  17. let temp: TempType?
  18. var bg: Decimal?
  19. let reservoir: Decimal?
  20. var isf: Decimal?
  21. var timestamp: Date?
  22. let tdd: Decimal?
  23. let insulin: Insulin?
  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. struct Insulin: JSON, Equatable {
  42. let TDD: Decimal?
  43. let bolus: Decimal?
  44. let temp_basal: Decimal?
  45. let scheduled_basal: Decimal?
  46. }
  47. extension Determination {
  48. private enum CodingKeys: String, CodingKey {
  49. case id
  50. case reason
  51. case units
  52. case insulinReq
  53. case eventualBG
  54. case sensitivityRatio
  55. case rate
  56. case duration
  57. case iob = "IOB"
  58. case cob = "COB"
  59. case predictions = "predBGs"
  60. case deliverAt
  61. case carbsReq
  62. case temp
  63. case bg
  64. case reservoir
  65. case timestamp
  66. case isf = "ISF"
  67. case tdd = "TDD"
  68. case insulin
  69. case current_target
  70. case insulinForManualBolus
  71. case manualBolusErrorString
  72. case minDelta
  73. case expectedDelta
  74. case minGuardBG
  75. case minPredBG
  76. case threshold
  77. case carbRatio = "CR"
  78. case received
  79. }
  80. }
  81. extension Predictions {
  82. private enum CodingKeys: String, CodingKey {
  83. case iob = "IOB"
  84. case zt = "ZT"
  85. case cob = "COB"
  86. case uam = "UAM"
  87. }
  88. }
  89. extension Insulin {
  90. private enum CodingKeys: String, CodingKey {
  91. case TDD
  92. case bolus
  93. case temp_basal
  94. case scheduled_basal
  95. }
  96. }
  97. protocol DeterminationObserver {
  98. func determinationDidUpdate(_ determination: Determination)
  99. }
  100. extension Determination {
  101. var reasonParts: [String] {
  102. reason.components(separatedBy: "; ").first?.components(separatedBy: ", ") ?? []
  103. }
  104. var reasonConclusion: String {
  105. reason.components(separatedBy: "; ").last ?? ""
  106. }
  107. }