Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LoopFollow/Task/TaskScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum TaskID: CaseIterable {
case minAgoUpdate
case calendarWrite
case alarmCheck
case remoteCommandPoll
}

struct ScheduledTask {
Expand Down
25 changes: 11 additions & 14 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
"deviceStatus": false,
]
private var loadingTimeoutTimer: Timer?
private var remoteCommandPollingTimer: Timer?
private var remoteCommandPollingStartedAt: Date?
private var remoteCommandPollingBaseline: RemoteCommandDataSignature?
private let remoteCommandPollingInterval: TimeInterval = 3
Expand Down Expand Up @@ -869,7 +868,6 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
}

deinit {
remoteCommandPollingTimer?.invalidate()
NotificationCenter.default.removeObserver(self, name: NSNotification.Name("refresh"), object: nil)
}

Expand Down Expand Up @@ -915,26 +913,20 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele

remoteCommandPollingBaseline = currentRemoteCommandDataSignature()
remoteCommandPollingStartedAt = Date()
remoteCommandPollingTimer?.invalidate()

performRemoteCommandPollingTick()

let timer = Timer.scheduledTimer(withTimeInterval: remoteCommandPollingInterval, repeats: true) { [weak self] _ in
TaskScheduler.shared.scheduleTask(id: .remoteCommandPoll, nextRun: Date()) { [weak self] in
self?.performRemoteCommandPollingTick()
}
timer.tolerance = 0.5
remoteCommandPollingTimer = timer

LogManager.shared.log(category: .general, message: "Started aggressive polling after remote command result notification")
}

private func stopRemoteCommandPolling(reason: String) {
guard remoteCommandPollingTimer != nil || remoteCommandPollingStartedAt != nil else { return }
guard remoteCommandPollingStartedAt != nil else { return }

remoteCommandPollingTimer?.invalidate()
remoteCommandPollingTimer = nil
remoteCommandPollingStartedAt = nil
remoteCommandPollingBaseline = nil
TaskScheduler.shared.rescheduleTask(id: .remoteCommandPoll, to: .distantFuture)

LogManager.shared.log(category: .general, message: "Stopped aggressive polling: \(reason)")
}
Expand All @@ -947,9 +939,14 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
return
}

bgTaskAction()
deviceStatusAction()
treatmentsTaskAction()
let now = Date()
TaskScheduler.shared.rescheduleTask(id: .deviceStatus, to: now)
TaskScheduler.shared.rescheduleTask(id: .treatments, to: now)

TaskScheduler.shared.rescheduleTask(
id: .remoteCommandPoll,
to: Date().addingTimeInterval(remoteCommandPollingInterval)
)
}

private func currentRemoteCommandDataSignature() -> RemoteCommandDataSignature {
Expand Down