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. 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())
  19. }
  20. func filteredGlucose(hours: Int) -> [BloodGlucose] {
  21. glucoseStorage.recent().filter {
  22. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  23. }
  24. }
  25. func manualGlucose(hours: Int) -> [BloodGlucose] {
  26. glucoseStorage.recent().filter {
  27. $0.type == GlucoseType.manual.rawValue &&
  28. $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
  29. }
  30. }
  31. func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
  32. pumpHistoryStorage.recent().filter {
  33. $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()
  34. }
  35. }
  36. func tempTargets(hours: Int) -> [TempTarget] {
  37. tempTargetsStorage.recent().filter {
  38. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  39. }
  40. }
  41. func tempTarget() -> TempTarget? {
  42. tempTargetsStorage.current()
  43. }
  44. func carbs(hours: Int) -> [CarbsEntry] {
  45. carbsStorage.recent().filter {
  46. $0.createdAt.addingTimeInterval(hours.hours.timeInterval) > Date()
  47. }
  48. }
  49. func pumpSettings() -> PumpSettings {
  50. storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self)
  51. ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
  52. ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
  53. }
  54. func pumpBattery() -> Battery? {
  55. storage.retrieve(OpenAPS.Monitor.battery, as: Battery.self)
  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. }