NotLoopingCondition.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // LoopFollow
  2. // NotLoopingCondition.swift
  3. import Foundation
  4. struct NotLoopingCondition: AlarmCondition {
  5. static let type: AlarmType = .notLooping
  6. init() {}
  7. func evaluate(alarm: Alarm, data: AlarmData, now: Date) -> Bool {
  8. // ────────────────────────────────
  9. // 0. sanity checks
  10. // ────────────────────────────────
  11. guard let thresholdMinutes = alarm.threshold,
  12. thresholdMinutes > 0 else { return false }
  13. // We need a valid timestamp (seconds-since-1970) of the last Loop run.
  14. guard let lastLoopTime = data.lastLoopTime,
  15. lastLoopTime > 0 else { return false }
  16. guard let lastChecked = Storage.shared.lastLoopingChecked.value else {
  17. // Never checked, so don't alarm.
  18. return false
  19. }
  20. let checkedAgeSeconds = now.timeIntervalSince(lastChecked)
  21. if checkedAgeSeconds > 360 { // 6 minutes
  22. // The check itself is stale, so the data is unreliable. Don't alarm.
  23. return false
  24. }
  25. // ────────────────────────────────
  26. // 1. elapsed-time test
  27. // ────────────────────────────────
  28. let elapsedSecs = Date().timeIntervalSince1970 - lastLoopTime
  29. let limitSecs = thresholdMinutes * 60
  30. return elapsedSecs >= limitSecs
  31. }
  32. }