RecBolusCondition.swift 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // LoopFollow
  2. // RecBolusCondition.swift
  3. // Created by Jonas Björkert on 2025-05-15.
  4. import Foundation
  5. /// Fires once when the recommended bolus (units) is ≥ the user-set threshold.
  6. struct RecBolusCondition: AlarmCondition {
  7. static let type: AlarmType = .recBolus
  8. init() {}
  9. func evaluate(alarm: Alarm, data: AlarmData) -> Bool {
  10. // ────────────────────────────────
  11. // 0. sanity checks
  12. // ────────────────────────────────
  13. guard let threshold = alarm.threshold, threshold > 0 else { return false }
  14. guard let rec = data.recBolus, rec >= threshold else {
  15. Storage.shared.lastRecBolusNotified.value = nil
  16. return false
  17. }
  18. // ────────────────────────────────
  19. // 1. has it INCREASED past the last-notified value?
  20. // ────────────────────────────────
  21. if let last = Storage.shared.lastRecBolusNotified.value {
  22. if rec <= last + 1e-4 { return false }
  23. }
  24. Storage.shared.lastRecBolusNotified.value = rec
  25. return true
  26. }
  27. }