| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- //
- // GlucoseEffectVelocity.swift
- // LoopKit
- //
- // Copyright © 2017 LoopKit Authors. All rights reserved.
- //
- import Foundation
- import HealthKit
- /// The first-derivative of GlucoseEffect, blood glucose over time.
- public struct GlucoseEffectVelocity: SampleValue {
- public let startDate: Date
- public let endDate: Date
- public let quantity: HKQuantity
- public init(startDate: Date, endDate: Date, quantity: HKQuantity) {
- self.startDate = startDate
- self.endDate = endDate
- self.quantity = quantity
- }
- }
- extension GlucoseEffectVelocity {
- static let perSecondUnit = HKUnit.milligramsPerDeciliter.unitDivided(by: .second())
- /// The integration of the velocity span
- public var effect: GlucoseEffect {
- let duration = endDate.timeIntervalSince(startDate)
- let velocityPerSecond = quantity.doubleValue(for: GlucoseEffectVelocity.perSecondUnit)
- return GlucoseEffect(
- startDate: endDate,
- quantity: HKQuantity(
- unit: .milligramsPerDeciliter,
- doubleValue: velocityPerSecond * duration
- )
- )
- }
-
- /// The integration of the velocity span from `start` to `end`
- public func effect(from start: Date, to end: Date) -> GlucoseEffect? {
- guard
- start <= end,
- startDate <= start,
- end <= endDate
- else {
- return nil
- }
-
- let duration = end.timeIntervalSince(start)
- let velocityPerSecond = quantity.doubleValue(for: GlucoseEffectVelocity.perSecondUnit)
- return GlucoseEffect(
- startDate: end,
- quantity: HKQuantity(
- unit: .milligramsPerDeciliter,
- doubleValue: velocityPerSecond * duration
- )
- )
- }
- }
|