HomeProvider.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. func pumpTimeZone() -> TimeZone? {
  13. apsManager.pumpManager?.status.timeZone
  14. }
  15. func heartbeatNow() {
  16. apsManager.heartbeat(date: Date())
  17. }
  18. func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
  19. pumpHistoryStorage.recent().filter {
  20. $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()
  21. }
  22. }
  23. func tempTargets(hours: Int) -> [TempTarget] {
  24. tempTargetsStorage.recent().filter {
  25. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  26. }
  27. }
  28. func tempTarget() -> TempTarget? {
  29. tempTargetsStorage.current()
  30. }
  31. func carbs(hours: Int) -> [CarbsEntry] {
  32. carbsStorage.recent().filter {
  33. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  34. }
  35. }
  36. func announcement(_ hours: Int) -> [Announcement] {
  37. announcementStorage.validate().filter {
  38. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  39. }
  40. }
  41. func pumpSettings() -> PumpSettings {
  42. storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self)
  43. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  44. ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
  45. }
  46. func pumpReservoir() -> Decimal? {
  47. storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
  48. }
  49. func autotunedBasalProfile() -> [BasalProfileEntry] {
  50. storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
  51. ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  52. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  53. }
  54. func basalProfile() -> [BasalProfileEntry] {
  55. storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  56. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  57. }
  58. func fetchGlucose() -> [GlucoseStored] {
  59. glucoseStorage.fetchGlucose()
  60. }
  61. }
  62. }