Profile.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // LoopFollow
  2. // Profile.swift
  3. import Foundation
  4. extension MainViewController {
  5. // NS Profile Web Call
  6. func webLoadNSProfile() {
  7. NightscoutUtils.executeRequest(eventType: .profile, parameters: [:]) { (result: Result<NSProfile, Error>) in
  8. switch result {
  9. case let .success(profileData):
  10. self.updateProfile(profileData: profileData)
  11. case let .failure(error):
  12. LogManager.shared.log(category: .nightscout, message: "webLoadNSProfile, error fetching profile data: \(error.localizedDescription)")
  13. }
  14. }
  15. }
  16. // NS Profile Response Processor
  17. func updateProfile(profileData: NSProfile) {
  18. guard let store = profileData.store["default"] ?? profileData.store["Default"] else {
  19. return
  20. }
  21. profileManager.loadProfile(from: profileData)
  22. infoManager.updateInfoData(type: .profile, value: profileData.defaultProfile)
  23. Storage.shared.lastProfileName.value = profileData.defaultProfile
  24. // Mark profile data as loaded for initial loading state
  25. markDataLoaded("profile")
  26. basalProfile.removeAll()
  27. for basalEntry in store.basal {
  28. let entry = basalProfileStruct(value: basalEntry.value, time: basalEntry.time, timeAsSeconds: basalEntry.timeAsSeconds)
  29. basalProfile.append(entry)
  30. }
  31. // Don't process the basal or draw the graph until after the BG has been fully processeed and drawn
  32. if firstGraphLoad { return }
  33. var basalSegments: [DataStructs.basalProfileSegment] = []
  34. let graphHours = 24 * Storage.shared.downloadDays.value
  35. // Build scheduled basal segments from right to left by
  36. // moving pointers to the current midnight and current basal
  37. var midnight = dateTimeUtils.getTimeIntervalMidnightToday()
  38. var basalProfileIndex = basalProfile.count - 1
  39. var start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  40. var end = dateTimeUtils.getNowTimeIntervalUTC()
  41. // Move back until we're in the graph range
  42. while start > end {
  43. basalProfileIndex -= 1
  44. start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  45. }
  46. // Add records while they're still within the graph
  47. let graphStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
  48. while end >= graphStart {
  49. let entry = DataStructs.basalProfileSegment(
  50. basalRate: basalProfile[basalProfileIndex].value, startDate: start, endDate: end
  51. )
  52. basalSegments.append(entry)
  53. basalProfileIndex -= 1
  54. if basalProfileIndex < 0 {
  55. basalProfileIndex = basalProfile.count - 1
  56. midnight = midnight.advanced(by: -24 * 60 * 60)
  57. }
  58. end = start - 1
  59. start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  60. }
  61. // reverse the result to get chronological order
  62. basalSegments.reverse()
  63. var firstPass = true
  64. // Runs the scheduled basal to the end of the prediction line
  65. var predictionEndTime = dateTimeUtils.getNowTimeIntervalUTC() + (3600 * Storage.shared.predictionToLoad.value)
  66. basalScheduleData.removeAll()
  67. for i in 0 ..< basalSegments.count {
  68. let timeStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
  69. // This processed everything after the first one.
  70. if firstPass == false,
  71. basalSegments[i].startDate <= predictionEndTime
  72. {
  73. let startDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: basalSegments[i].startDate)
  74. basalScheduleData.append(startDot)
  75. var endDate = basalSegments[i].endDate
  76. // if it's the last one needed, set it to end at the prediction end time
  77. if endDate > predictionEndTime || i == basalSegments.count - 1 {
  78. endDate = Double(predictionEndTime)
  79. }
  80. let endDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: endDate)
  81. basalScheduleData.append(endDot)
  82. }
  83. // we need to manually set the first one
  84. // Check that this is the first one and there are no existing entries
  85. if firstPass == true {
  86. // check that the timestamp is > the current entry and < the next entry
  87. if timeStart >= basalSegments[i].startDate, timeStart < basalSegments[i].endDate {
  88. // Set the start time to match the BG start
  89. let startDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: Double(timeStart + (60 * 5)))
  90. basalScheduleData.append(startDot)
  91. // set the enddot where the next one will start
  92. var endDate = basalSegments[i].endDate
  93. let endDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: endDate)
  94. basalScheduleData.append(endDot)
  95. firstPass = false
  96. }
  97. }
  98. }
  99. if Storage.shared.graphBasal.value {
  100. updateBasalScheduledGraph()
  101. }
  102. }
  103. }