ForecastError.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // ForecastError.swift
  3. // NightscoutUploadKit
  4. //
  5. // Created by Pete Schwamb on 5/28/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. public struct ForecastError {
  11. typealias RawValue = [String: Any]
  12. let velocity: Double
  13. let measurementDuration: TimeInterval
  14. public init(velocity: HKQuantity, measurementDuration: TimeInterval) {
  15. let glucoseUnit = HKUnit.milligramsPerDeciliter
  16. let velocityUnit = glucoseUnit.unitDivided(by: HKUnit.second())
  17. self.velocity = velocity.doubleValue(for: velocityUnit)
  18. self.measurementDuration = measurementDuration
  19. }
  20. public var dictionaryRepresentation: [String: Any] {
  21. var rval = [String: Any]()
  22. rval["velocity"] = velocity
  23. rval["measurementDuration"] = measurementDuration
  24. //rval["velocityUnits"] = "mg/dL/s"
  25. return rval
  26. }
  27. init?(rawValue: RawValue) {
  28. guard
  29. let velocity = rawValue["velocity"] as? Double,
  30. let measurementDuration = rawValue["measurementDuration"] as? TimeInterval
  31. else {
  32. return nil
  33. }
  34. self.velocity = velocity
  35. self.measurementDuration = measurementDuration
  36. }
  37. }