HomeProvider.swift 2.1 KB

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