MissedReadingCondition.swift 933 B

12345678910111213141516171819202122232425
  1. // LoopFollow
  2. // MissedReadingCondition.swift
  3. // Created by Jonas Björkert.
  4. import Foundation
  5. /// Fires when the newest CGM reading is older than `threshold` minutes.
  6. struct MissedReadingCondition: AlarmCondition {
  7. static let type: AlarmType = .missedReading
  8. init() {}
  9. func evaluate(alarm: Alarm, data: AlarmData, now : Date) -> Bool {
  10. // ────────────────────────────────
  11. // 0. sanity checks
  12. // ────────────────────────────────
  13. guard let thresholdMinutes = alarm.threshold, thresholdMinutes > 0 else { return false }
  14. // Skip if we have *no* readings
  15. guard let last = data.bgReadings.last else { return false }
  16. let secondsSinceLast = now.timeIntervalSince(last.date)
  17. return secondsSinceLast >= thresholdMinutes * 60
  18. }
  19. }