GlucoseEffectVelocity.swift 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // GlucoseEffectVelocity.swift
  3. // LoopKit
  4. //
  5. // Copyright © 2017 LoopKit Authors. All rights reserved.
  6. //
  7. import Foundation
  8. import HealthKit
  9. /// The first-derivative of GlucoseEffect, blood glucose over time.
  10. public struct GlucoseEffectVelocity: SampleValue {
  11. public let startDate: Date
  12. public let endDate: Date
  13. public let quantity: HKQuantity
  14. public init(startDate: Date, endDate: Date, quantity: HKQuantity) {
  15. self.startDate = startDate
  16. self.endDate = endDate
  17. self.quantity = quantity
  18. }
  19. }
  20. extension GlucoseEffectVelocity {
  21. static let perSecondUnit = HKUnit.milligramsPerDeciliter.unitDivided(by: .second())
  22. /// The integration of the velocity span
  23. public var effect: GlucoseEffect {
  24. let duration = endDate.timeIntervalSince(startDate)
  25. let velocityPerSecond = quantity.doubleValue(for: GlucoseEffectVelocity.perSecondUnit)
  26. return GlucoseEffect(
  27. startDate: endDate,
  28. quantity: HKQuantity(
  29. unit: .milligramsPerDeciliter,
  30. doubleValue: velocityPerSecond * duration
  31. )
  32. )
  33. }
  34. }