HomeProvider.swift 2.2 KB

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