SensorAgeCondition.swift 1001 B

12345678910111213141516171819202122232425262728293031
  1. // LoopFollow
  2. // SensorAgeCondition.swift
  3. // Created by Jonas Björkert on 2025-05-17.
  4. import Foundation
  5. /// Fires once when we are **≤ threshold hours** away from the
  6. /// Dexcom 10-day hard-stop. No repeats once triggered.
  7. struct SensorAgeCondition: AlarmCondition {
  8. static let type: AlarmType = .sensorChange
  9. init() {}
  10. /// Dexcom hard-stop = 10 days = 240 h
  11. private let lifetime: TimeInterval = 10 * 24 * 60 * 60
  12. func evaluate(alarm: Alarm, data: AlarmData) -> Bool {
  13. // 0. basic guards
  14. guard let warnAheadHrs = alarm.threshold, warnAheadHrs > 0 else { return false }
  15. guard let insertTS = data.sageInsertTime else { return false }
  16. // convert UNIX timestamp to Date
  17. let insertedAt = Date(timeIntervalSince1970: insertTS)
  18. // 1. compute trigger moment
  19. let expiry = insertedAt.addingTimeInterval(lifetime)
  20. let trigger = expiry.addingTimeInterval(-warnAheadHrs * 3600)
  21. return Date() >= trigger
  22. }
  23. }