IobResult.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import Foundation
  2. struct IobResult: Codable {
  3. static func from(iob: IobTotal, iobWithZeroTemp: IobTotal) -> IobResult {
  4. IobResult(
  5. iob: iob.iob,
  6. activity: iob.activity,
  7. basaliob: iob.basaliob,
  8. bolusiob: iob.bolusiob,
  9. netbasalinsulin: iob.netbasalinsulin,
  10. bolusinsulin: iob.bolusinsulin,
  11. time: iob.time,
  12. iobWithZeroTemp: IobWithZeroTemp(
  13. iob: iobWithZeroTemp.iob,
  14. activity: iobWithZeroTemp.activity,
  15. basaliob: iobWithZeroTemp.basaliob,
  16. bolusiob: iobWithZeroTemp.bolusiob,
  17. netbasalinsulin: iobWithZeroTemp.netbasalinsulin,
  18. bolusinsulin: iobWithZeroTemp.bolusinsulin,
  19. time: iobWithZeroTemp.time
  20. ),
  21. lastBolusTime: nil,
  22. lastTemp: nil
  23. )
  24. }
  25. let iob: Decimal
  26. let activity: Decimal
  27. let basaliob: Decimal
  28. let bolusiob: Decimal
  29. let netbasalinsulin: Decimal
  30. let bolusinsulin: Decimal
  31. let time: Date
  32. let iobWithZeroTemp: IobWithZeroTemp
  33. var lastBolusTime: UInt64?
  34. var lastTemp: LastTemp?
  35. struct IobWithZeroTemp: Codable {
  36. let iob: Decimal
  37. let activity: Decimal
  38. let basaliob: Decimal
  39. let bolusiob: Decimal
  40. let netbasalinsulin: Decimal
  41. let bolusinsulin: Decimal
  42. let time: Date
  43. }
  44. struct LastTemp: Codable {
  45. let rate: Decimal?
  46. let timestamp: Date?
  47. let started_at: Date?
  48. let date: UInt64
  49. let duration: Decimal?
  50. init(rate: Decimal, timestamp: Date, started_at: Date, date: UInt64, duration: Decimal) {
  51. self.rate = rate
  52. self.timestamp = timestamp
  53. self.started_at = started_at
  54. self.date = date
  55. self.duration = duration
  56. }
  57. // this constructor helps handle the JSON output for the case when there
  58. // aren't any temp basals to match the output from Javascript
  59. init() {
  60. rate = nil
  61. timestamp = nil
  62. started_at = nil
  63. date = 0
  64. duration = nil
  65. }
  66. }
  67. }
  68. extension ComputedPumpHistoryEvent {
  69. func toLastTemp() -> IobResult.LastTemp? {
  70. // Only convert if we have the required fields and it's a temp event
  71. guard let rate = self.rate,
  72. let duration = self.duration
  73. else {
  74. return nil
  75. }
  76. return IobResult.LastTemp(
  77. rate: rate,
  78. timestamp: timestamp,
  79. started_at: started_at,
  80. date: date,
  81. duration: duration
  82. )
  83. }
  84. }