Suggestion.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Foundation
  2. struct Suggestion: JSON, Equatable {
  3. let reason: String
  4. let units: Decimal?
  5. let insulinReq: Decimal?
  6. let eventualBG: Int?
  7. let sensitivityRatio: Decimal?
  8. let rate: Decimal?
  9. let duration: Int?
  10. let iob: Decimal?
  11. let cob: Decimal?
  12. var predictions: Predictions?
  13. let deliverAt: Date?
  14. let carbsReq: Decimal?
  15. let temp: TempType?
  16. let bg: Decimal?
  17. let reservoir: Decimal?
  18. let isf: Decimal?
  19. var timestamp: Date?
  20. var recieved: Bool?
  21. let tdd: Decimal?
  22. let insulin: Insulin?
  23. let current_target: Decimal?
  24. let insulinForManualBolus: Decimal?
  25. let manualBolusErrorString: Decimal?
  26. let minDelta: Decimal?
  27. let expectedDelta: Decimal?
  28. let minGuardBG: Decimal?
  29. let minPredBG: Decimal?
  30. }
  31. struct Predictions: JSON, Equatable {
  32. let iob: [Int]?
  33. let zt: [Int]?
  34. let cob: [Int]?
  35. let uam: [Int]?
  36. }
  37. struct Insulin: JSON, Equatable {
  38. let TDD: Decimal?
  39. let bolus: Decimal?
  40. let temp_basal: Decimal?
  41. let scheduled_basal: Decimal?
  42. }
  43. extension Suggestion {
  44. private enum CodingKeys: String, CodingKey {
  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 recieved
  62. case isf = "ISF"
  63. case tdd = "TDD"
  64. case insulin
  65. case current_target
  66. case insulinForManualBolus
  67. case manualBolusErrorString
  68. case minDelta
  69. case expectedDelta
  70. case minGuardBG
  71. case minPredBG
  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. extension Insulin {
  83. private enum CodingKeys: String, CodingKey {
  84. case TDD
  85. case bolus
  86. case temp_basal
  87. case scheduled_basal
  88. }
  89. }
  90. protocol SuggestionObserver {
  91. func suggestionDidUpdate(_ suggestion: Suggestion)
  92. }
  93. protocol EnactedSuggestionObserver {
  94. func enactedSuggestionDidUpdate(_ suggestion: Suggestion)
  95. }
  96. extension Suggestion {
  97. var reasonParts: [String] {
  98. reason.components(separatedBy: "; ").first?.components(separatedBy: ", ") ?? []
  99. }
  100. var reasonConclusion: String {
  101. reason.components(separatedBy: "; ").last ?? ""
  102. }
  103. }