SensorAgeCondition.swift 1.0 KB

1234567891011121314151617181920212223242526272829
  1. // LoopFollow
  2. // SensorAgeCondition.swift
  3. import Foundation
  4. /// Fires when we are **≤ threshold hours** away from the
  5. /// sensor's configured lifetime.
  6. struct SensorAgeCondition: AlarmCondition {
  7. static let type: AlarmType = .sensorChange
  8. init() {}
  9. func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
  10. // 0. basic guards
  11. guard let warnAheadHrs = alarm.threshold, warnAheadHrs > 0 else { return false }
  12. guard let insertTS = data.sageInsertTime, insertTS > 0 else { return false }
  13. // convert UNIX timestamp to Date
  14. let insertedAt = Date(timeIntervalSince1970: insertTS)
  15. // 1. compute trigger moment using configurable lifetime (default 10 days)
  16. let lifetimeDays = alarm.sensorLifetimeDays ?? 10
  17. let lifetime: TimeInterval = Double(lifetimeDays) * 24 * 60 * 60
  18. let expiry = insertedAt.addingTimeInterval(lifetime)
  19. let trigger = expiry.addingTimeInterval(-warnAheadHrs * 3600)
  20. return Date() >= trigger
  21. }
  22. }