HomeProvider.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. @Injected() var tempTargetsStorage: TempTargetsStorage!
  9. var suggestion: Suggestion? {
  10. try? storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self)
  11. }
  12. func fetchAndLoop() {
  13. apsManager.fetchAndLoop()
  14. }
  15. func filteredGlucose(hours: Int) -> [BloodGlucose] {
  16. glucoseStorage.recent().filter {
  17. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  18. }
  19. }
  20. func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
  21. pumpHistoryStorage.recent().filter {
  22. $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()
  23. }
  24. }
  25. func tempTargets(hours: Int) -> [TempTarget] {
  26. tempTargetsStorage.recent().filter {
  27. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  28. }
  29. }
  30. func pumpSettings() -> PumpSettings {
  31. (try? storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self))
  32. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  33. ?? PumpSettings(insulinActionCurve: 5, maxBolus: 10, maxBasal: 2)
  34. }
  35. func basalProfile() -> [BasalProfileEntry] {
  36. (try? storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self))?.basalProfile
  37. ?? (try? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self))?.basalProfile
  38. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  39. }
  40. }
  41. }