Suggestion.swift 1.8 KB

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