Suggestion.swift 2.2 KB

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