DeviceStatusTask.swift 847 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // DeviceStatusTask.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2025-01-11.
  6. // Copyright © 2025 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. extension MainViewController {
  10. func scheduleDeviceStatusTask(initialDelay: TimeInterval = 4) {
  11. let startTime = Date().addingTimeInterval(initialDelay)
  12. TaskScheduler.shared.scheduleTask(id: .deviceStatus, nextRun: startTime) { [weak self] in
  13. guard let self = self else { return }
  14. self.deviceStatusAction()
  15. }
  16. }
  17. func deviceStatusAction() {
  18. // If no NS config, we wait 60s before trying again:
  19. guard IsNightscoutEnabled() else {
  20. TaskScheduler.shared.rescheduleTask(id: .deviceStatus, to: Date().addingTimeInterval(60))
  21. return
  22. }
  23. webLoadNSDeviceStatus()
  24. }
  25. }