FastDropCondition.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. // LoopFollow
  2. // FastDropCondition.swift
  3. import Foundation
  4. struct FastDropCondition: AlarmCondition {
  5. static let type: AlarmType = .fastDrop
  6. init() {}
  7. func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
  8. // ────────────────────────────────
  9. // 0. sanity checks
  10. // ────────────────────────────────
  11. guard
  12. let dropPerReading = alarm.delta, dropPerReading > 0,
  13. let dropsNeeded = alarm.monitoringWindow, dropsNeeded > 0,
  14. data.bgReadings.count >= dropsNeeded + 1
  15. else { return false }
  16. // ────────────────────────────────
  17. // 1. compute recent deltas
  18. // (BG-limit check is now handled by passesBGLimits)
  19. // ────────────────────────────────
  20. let recent = data.bgReadings.suffix(dropsNeeded + 1)
  21. let readings = Array(recent)
  22. for i in 1 ... dropsNeeded {
  23. let delta = Double(readings[i - 1].sgv - readings[i].sgv)
  24. if delta < dropPerReading { return false }
  25. }
  26. return true
  27. }
  28. }