Suggestion.swift 2.3 KB

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