Suggestion.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. var timestamp: Date?
  19. var recieved: Bool?
  20. }
  21. struct Predictions: JSON, Equatable {
  22. let iob: [Int]?
  23. let zt: [Int]?
  24. let cob: [Int]?
  25. let uam: [Int]?
  26. }
  27. extension Suggestion {
  28. private enum CodingKeys: String, CodingKey {
  29. case reason
  30. case units
  31. case insulinReq
  32. case eventualBG
  33. case sensitivityRatio
  34. case rate
  35. case duration
  36. case iob = "IOB"
  37. case cob = "COB"
  38. case predictions = "predBGs"
  39. case deliverAt
  40. case carbsReq
  41. case temp
  42. case bg
  43. case reservoir
  44. case timestamp
  45. case recieved
  46. }
  47. }
  48. extension Predictions {
  49. private enum CodingKeys: String, CodingKey {
  50. case iob = "IOB"
  51. case zt = "ZT"
  52. case cob = "COB"
  53. case uam = "UAM"
  54. }
  55. }
  56. protocol SuggestionObserver {
  57. func suggestionDidUpdate(_ suggestion: Suggestion)
  58. }
  59. protocol EnactedSuggestionObserver {
  60. func enactedSuggestionDidUpdate(_ suggestion: Suggestion)
  61. }
  62. extension Suggestion {
  63. var reasonParts: [String] {
  64. reason.components(separatedBy: "; ").first?.components(separatedBy: ", ") ?? []
  65. }
  66. var reasonConclusion: String {
  67. reason.components(separatedBy: "; ").last ?? ""
  68. }
  69. }