HomeProvider.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import Foundation
  2. import SwiftDate
  3. extension Home {
  4. final class Provider: BaseProvider, HomeProvider {
  5. @Injected() var apsManager: APSManager!
  6. @Injected() var glucoseStorage: GlucoseStorage!
  7. @Injected() var pumpHistoryStorage: PumpHistoryStorage!
  8. var suggestion: Suggestion? {
  9. try? storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self)
  10. }
  11. func fetchAndLoop() {
  12. apsManager.fetchAndLoop()
  13. }
  14. func filteredGlucose(hours: Int) -> [BloodGlucose] {
  15. glucoseStorage.recent().filter {
  16. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  17. }
  18. }
  19. func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
  20. pumpHistoryStorage.recent().filter {
  21. $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()
  22. }
  23. }
  24. func pumpSettings() -> PumpSettings {
  25. (try? storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self))
  26. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  27. ?? PumpSettings(insulinActionCurve: 5, maxBolus: 10, maxBasal: 2)
  28. }
  29. func basalProfile() -> [BasalProfileEntry] {
  30. (try? storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self))?.basalProfile
  31. ?? (try? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self))?.basalProfile
  32. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  33. }
  34. }
  35. }