Suggestion.swift 1.7 KB

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