HomeProvider.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import Foundation
  2. import LoopKitUI
  3. import SwiftDate
  4. extension Home {
  5. final class Provider: BaseProvider, HomeProvider {
  6. @Injected() var apsManager: APSManager!
  7. @Injected() var glucoseStorage: GlucoseStorage!
  8. @Injected() var pumpHistoryStorage: PumpHistoryStorage!
  9. @Injected() var tempTargetsStorage: TempTargetsStorage!
  10. @Injected() var carbsStorage: CarbsStorage!
  11. @Injected() var announcementStorage: AnnouncementsStorage!
  12. var suggestion: Suggestion? {
  13. storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self)
  14. }
  15. var enactedSuggestion: Suggestion? {
  16. storage.retrieve(OpenAPS.Enact.enacted, as: Suggestion.self)
  17. }
  18. func pumpTimeZone() -> TimeZone? {
  19. apsManager.pumpManager?.status.timeZone
  20. }
  21. func heartbeatNow() {
  22. apsManager.heartbeat(date: Date())
  23. }
  24. func filteredGlucose(hours: Int) -> [BloodGlucose] {
  25. glucoseStorage.recent().filter {
  26. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  27. }
  28. }
  29. func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
  30. pumpHistoryStorage.recent().filter {
  31. $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()
  32. }
  33. }
  34. func tempTargets(hours: Int) -> [TempTarget] {
  35. tempTargetsStorage.recent().filter {
  36. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  37. }
  38. }
  39. func tempTarget() -> TempTarget? {
  40. tempTargetsStorage.current()
  41. }
  42. func carbs(hours: Int) -> [CarbsEntry] {
  43. carbsStorage.recent().filter {
  44. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  45. }
  46. }
  47. func announcement(_ hours: Int) -> [Announcement] {
  48. announcementStorage.validate().filter {
  49. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  50. }
  51. }
  52. func pumpSettings() -> PumpSettings {
  53. storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self)
  54. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  55. ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
  56. }
  57. func pumpReservoir() -> Decimal? {
  58. storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
  59. }
  60. func autotunedBasalProfile() -> [BasalProfileEntry] {
  61. storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
  62. ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  63. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  64. }
  65. func basalProfile() -> [BasalProfileEntry] {
  66. storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  67. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  68. }
  69. }
  70. }