PumpChangeCondition.swift 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // LoopFollow
  2. // PumpChangeCondition.swift
  3. //
  4. // PumpChangeCondition.swift
  5. // LoopFollow
  6. //
  7. // Created by Jonas Björkert on 2025-05-17.
  8. //
  9. import Foundation
  10. /// Fires once when we are **≤ threshold hours** away from the Omnipod /
  11. /// cannula 3-day hard-stop. Automatically disables itself after firing.
  12. struct PumpChangeCondition: AlarmCondition {
  13. static let type: AlarmType = .pumpChange
  14. init() {}
  15. /// Pod lifetime = 3 days = 72 h
  16. private let lifetime: TimeInterval = 3 * 24 * 60 * 60
  17. func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
  18. // 0. sanity guards
  19. guard let warnAheadHrs = alarm.threshold, warnAheadHrs > 0 else { return false }
  20. guard let insertTS = data.pumpInsertTime else { return false }
  21. // convert UNIX timestamp → Date
  22. let insertedAt = Date(timeIntervalSince1970: insertTS)
  23. // 1. compute “fire-at” moment
  24. let expiry = insertedAt.addingTimeInterval(lifetime)
  25. let trigger = expiry.addingTimeInterval(-warnAheadHrs * 3600)
  26. return Date() >= trigger
  27. }
  28. }