Explorar el Código

Task scheduling of alarms

Jonas Björkert hace 1 año
padre
commit
57232f4e88

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

@@ -171,6 +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: .fetchBG, to: Date().addingTimeInterval(delayToSchedule))

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

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

+ 4 - 4
LoopFollow/Task/TaskScheduler.swift

@@ -43,7 +43,7 @@ class TaskScheduler {
     }
 
     func rescheduleTask(id: TaskID, to newRunDate: Date) {
-        let timeString = formatTime(newRunDate)
+        // let timeString = formatTime(newRunDate)
         // LogManager.shared.log(category: .taskScheduler, message: "Reschedule Task \(id): next run = \(timeString)", isDebug: true)
 
         queue.async {
@@ -97,9 +97,9 @@ class TaskScheduler {
                 continue
             }
 
-            // Skip alarm checks if data-fetching tasks (deviceStatus, treatments, fetchBG) are currently due or just executed.
+            // 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 5 seconds later to retry after data updates.
+            // 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 }
@@ -107,7 +107,7 @@ class TaskScheduler {
                 }
                 if shouldSkip {
                     guard var existingTask = tasks[taskID] else { continue }
-                    existingTask.nextRun = Date().addingTimeInterval(5)
+                    existingTask.nextRun = Date().addingTimeInterval(1)
                     tasks[taskID] = existingTask
                     continue
                 }

+ 1 - 0
LoopFollow/Task/TreatmentsTask.swift

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