PredictedBG.swift 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // PredictedBG.swift
  3. // RileyLink
  4. //
  5. // Created by Pete Schwamb on 8/8/16.
  6. // Copyright © 2016 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. public struct PredictedBG {
  11. let startDate: Date
  12. let values: [Int]
  13. let cob: [Int]?
  14. let iob: [Int]?
  15. public init(startDate: Date, values: [HKQuantity], cob: [HKQuantity]? = nil, iob: [HKQuantity]? =
  16. nil) {
  17. self.startDate = startDate
  18. // BG values in nightscout are in mg/dL.
  19. let unit = HKUnit.milligramsPerDeciliterUnit()
  20. self.values = values.map { Int(round($0.doubleValue(for: unit))) }
  21. self.cob = cob?.map { Int(round($0.doubleValue(for: unit))) }
  22. self.iob = iob?.map { Int(round($0.doubleValue(for: unit))) }
  23. }
  24. public var dictionaryRepresentation: [String: Any] {
  25. var rval = [String: Any]()
  26. rval["startDate"] = TimeFormat.timestampStrFromDate(startDate)
  27. rval["values"] = values
  28. if let cob = cob {
  29. rval["COB"] = cob
  30. }
  31. if let iob = iob {
  32. rval["IOB"] = iob
  33. }
  34. return rval
  35. }
  36. }