HomeProvider.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 pumpBattery() -> Battery? {
  45. storage.retrieve(OpenAPS.Monitor.battery, as: Battery.self)
  46. }
  47. func pumpReservoir() -> Decimal? {
  48. storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
  49. }
  50. func basalProfile() -> [BasalProfileEntry] {
  51. storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
  52. ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  53. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  54. }
  55. }
  56. }