HomeProvider.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 heartbeatNow() {
  19. apsManager.heartbeat(date: Date())
  20. }
  21. func filteredGlucose(hours: Int) -> [BloodGlucose] {
  22. glucoseStorage.recent().filter {
  23. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  24. }
  25. }
  26. func manualGlucose(hours: Int) -> [BloodGlucose] {
  27. glucoseStorage.recent().filter {
  28. $0.type == GlucoseType.manual.rawValue &&
  29. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  30. }
  31. }
  32. func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
  33. pumpHistoryStorage.recent().filter {
  34. $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()
  35. }
  36. }
  37. func tempTargets(hours: Int) -> [TempTarget] {
  38. tempTargetsStorage.recent().filter {
  39. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  40. }
  41. }
  42. func tempTarget() -> TempTarget? {
  43. tempTargetsStorage.current()
  44. }
  45. func carbs(hours: Int) -> [CarbsEntry] {
  46. carbsStorage.recent().filter {
  47. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  48. }
  49. }
  50. func announcement(_ hours: Int) -> [Announcement] {
  51. announcementStorage.validate().filter {
  52. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  53. }
  54. }
  55. func pumpSettings() -> PumpSettings {
  56. storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self)
  57. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  58. ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
  59. }
  60. func pumpBattery() -> Battery? {
  61. storage.retrieve(OpenAPS.Monitor.battery, as: Battery.self)
  62. }
  63. func pumpReservoir() -> Decimal? {
  64. storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
  65. }
  66. func autotunedBasalProfile() -> [BasalProfileEntry] {
  67. storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
  68. ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  69. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  70. }
  71. func basalProfile() -> [BasalProfileEntry] {
  72. storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  73. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  74. }
  75. }
  76. }