HomeProvider.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var suggestion: Suggestion? {
  12. storage.retrieve(OpenAPS.Enact.suggested, as: Suggestion.self)
  13. }
  14. var statistics: Statistics? {
  15. let stat = storage.retrieve(OpenAPS.Monitor.statistics, as: [Statistics].self)
  16. if stat?.count ?? 0 != 0 {
  17. return stat![0]
  18. }
  19. return nil
  20. }
  21. var enactedSuggestion: Suggestion? {
  22. storage.retrieve(OpenAPS.Enact.enacted, as: Suggestion.self)
  23. }
  24. func heartbeatNow() {
  25. apsManager.heartbeat(date: Date())
  26. }
  27. func filteredGlucose(hours: Int) -> [BloodGlucose] {
  28. glucoseStorage.recent().filter {
  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 pumpSettings() -> PumpSettings {
  51. storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self)
  52. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  53. ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
  54. }
  55. func pumpBattery() -> Battery? {
  56. storage.retrieve(OpenAPS.Monitor.battery, as: Battery.self)
  57. }
  58. func pumpReservoir() -> Decimal? {
  59. storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
  60. }
  61. func autotunedBasalProfile() -> [BasalProfileEntry] {
  62. storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
  63. ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  64. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  65. }
  66. func basalProfile() -> [BasalProfileEntry] {
  67. storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  68. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  69. }
  70. }
  71. }