Przeglądaj źródła

Debounce alarm checks and improve scheduling logic

Jonas Björkert 1 rok temu
rodzic
commit
da38c3c0f1

+ 2 - 2
LoopFollow/Alarm/AlarmCondition/RecBolusCondition.swift

@@ -20,10 +20,10 @@ struct RecBolusCondition: AlarmCondition {
         }
 
         // ────────────────────────────────
-        // 1. has it INCREASED past the last-notified value?
+        // 1. has it DECREASED past the last-notified value?
         // ────────────────────────────────
         if let last = Storage.shared.lastRecBolusNotified.value {
-            if rec <= last + 1e-4 { return false }
+            if rec < last { return false }
         }
 
         Storage.shared.lastRecBolusNotified.value = rec

+ 2 - 0
LoopFollow/Alarm/AlarmManager.swift

@@ -105,6 +105,8 @@ class AlarmManager {
                 // If this alarm is active, but no longer fulfill the requirements, stop it.
                 // Continue evaluating other alarams
                 if Observable.shared.currentAlarm.value == alarm.id {
+                    LogManager.shared.log(category: .alarm, message: "Stopping alarm \(alarm) because it no longer meets its requirements", isDebug: true)
+
                     stopAlarm()
                 }
 

+ 1 - 1
LoopFollow/Controllers/Nightscout/BGData.swift

@@ -171,7 +171,7 @@ extension MainViewController {
                 LogManager.shared.log(category: .nightscout,
                                       message: "Fresh reading. Scheduling next fetch in \(delayToSchedule) seconds.",
                                       isDebug: true)
-                TaskScheduler.shared.rescheduleTask(id: .alarmCheck, to: Date())
+                TaskScheduler.shared.rescheduleTask(id: .alarmCheck, to: Date().addingTimeInterval(3))
             }
 
             TaskScheduler.shared.rescheduleTask(id: .fetchBG, to: Date().addingTimeInterval(delayToSchedule))

+ 1 - 1
LoopFollow/Controllers/Nightscout/DeviceStatus.swift

@@ -199,7 +199,7 @@ extension MainViewController {
                     id: .deviceStatus,
                     to: Date().addingTimeInterval(interval)
                 )
-                TaskScheduler.shared.rescheduleTask(id: .alarmCheck, to: Date())
+                TaskScheduler.shared.rescheduleTask(id: .alarmCheck, to: Date().addingTimeInterval(3))
             }
         }
 

+ 1 - 18
LoopFollow/Task/TaskScheduler.swift

@@ -90,34 +90,17 @@ class TaskScheduler {
         BackgroundAlertManager.shared.scheduleBackgroundAlert()
 
         let now = Date()
-        let tasksToSkipAlarmCheck: Set<TaskID> = [.deviceStatus, .treatments, .fetchBG]
 
         for taskID in TaskID.allCases {
             guard let task = tasks[taskID], task.nextRun <= now else {
                 continue
             }
 
-            // Skip alarm checks if data-fetching tasks (deviceStatus, treatments, fetchBG) are currently due.
-            // This ensures alarms are evaluated with the latest data, avoiding premature or incorrect triggers.
-            // If skipped, reschedule alarmCheck 1 second later to retry after data updates.
-            if taskID == .alarmCheck {
-                let shouldSkip = tasksToSkipAlarmCheck.contains {
-                    guard let checkTask = tasks[$0] else { return false }
-                    return checkTask.nextRun <= now || checkTask.nextRun == .distantFuture
-                }
-                if shouldSkip {
-                    guard var existingTask = tasks[taskID] else { continue }
-                    existingTask.nextRun = Date().addingTimeInterval(1)
-                    tasks[taskID] = existingTask
-                    continue
-                }
-            }
-
             var updatedTask = task
             updatedTask.nextRun = .distantFuture
             tasks[taskID] = updatedTask
 
-            // LogManager.shared.log(category: .taskScheduler, message: "Executing Task \(taskID)", isDebug: true)
+            LogManager.shared.log(category: .taskScheduler, message: "Executing Task \(taskID)", isDebug: true)
 
             DispatchQueue.main.async {
                 task.action()

+ 1 - 1
LoopFollow/Task/TreatmentsTask.swift

@@ -23,6 +23,6 @@ extension MainViewController {
         WebLoadNSTreatments()
 
         TaskScheduler.shared.rescheduleTask(id: .treatments, to: Date().addingTimeInterval(2 * 60))
-        TaskScheduler.shared.rescheduleTask(id: .alarmCheck, to: Date())
+        TaskScheduler.shared.rescheduleTask(id: .alarmCheck, to: Date().addingTimeInterval(3))
     }
 }