NotLoopingCondition.swift 1.6 KB

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