HomeProvider.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 enactedSuggestion: Suggestion? {
  15. storage.retrieve(OpenAPS.Enact.enacted, as: Suggestion.self)
  16. }
  17. func heartbeatNow() {
  18. apsManager.heartbeat(date: Date(), force: true)
  19. }
  20. func filteredGlucose(hours: Int) -> [BloodGlucose] {
  21. glucoseStorage.recent().filter {
  22. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  23. }
  24. }
  25. func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
  26. pumpHistoryStorage.recent().filter {
  27. $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()
  28. }
  29. }
  30. func tempTargets(hours: Int) -> [TempTarget] {
  31. tempTargetsStorage.recent().filter {
  32. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  33. }
  34. }
  35. func tempTarget() -> TempTarget? {
  36. tempTargetsStorage.current()
  37. }
  38. func carbs(hours: Int) -> [CarbsEntry] {
  39. carbsStorage.recent().filter {
  40. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  41. }
  42. }
  43. func pumpSettings() -> PumpSettings {
  44. storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self)
  45. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  46. ?? PumpSettings(insulinActionCurve: 5, maxBolus: 10, maxBasal: 2)
  47. }
  48. func pumpBattery() -> Battery? {
  49. storage.retrieve(OpenAPS.Monitor.battery, as: Battery.self)
  50. }
  51. func pumpReservoir() -> Decimal? {
  52. storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
  53. }
  54. func autotunedBasalProfile() -> [BasalProfileEntry] {
  55. storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
  56. ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  57. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  58. }
  59. func basalProfile() -> [BasalProfileEntry] {
  60. storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
  61. ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
  62. }
  63. }
  64. }