Profile.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // Mark profile data as loaded for initial loading state
  24. markDataLoaded("profile")
  25. basalProfile.removeAll()
  26. for basalEntry in store.basal {
  27. let entry = basalProfileStruct(value: basalEntry.value, time: basalEntry.time, timeAsSeconds: basalEntry.timeAsSeconds)
  28. basalProfile.append(entry)
  29. }
  30. // Don't process the basal or draw the graph until after the BG has been fully processeed and drawn
  31. if firstGraphLoad { return }
  32. var basalSegments: [DataStructs.basalProfileSegment] = []
  33. let graphHours = 24 * Storage.shared.downloadDays.value
  34. // Build scheduled basal segments from right to left by
  35. // moving pointers to the current midnight and current basal
  36. var midnight = dateTimeUtils.getTimeIntervalMidnightToday()
  37. var basalProfileIndex = basalProfile.count - 1
  38. var start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  39. var end = dateTimeUtils.getNowTimeIntervalUTC()
  40. // Move back until we're in the graph range
  41. while start > end {
  42. basalProfileIndex -= 1
  43. start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  44. }
  45. // Add records while they're still within the graph
  46. let graphStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
  47. while end >= graphStart {
  48. let entry = DataStructs.basalProfileSegment(
  49. basalRate: basalProfile[basalProfileIndex].value, startDate: start, endDate: end
  50. )
  51. basalSegments.append(entry)
  52. basalProfileIndex -= 1
  53. if basalProfileIndex < 0 {
  54. basalProfileIndex = basalProfile.count - 1
  55. midnight = midnight.advanced(by: -24 * 60 * 60)
  56. }
  57. end = start - 1
  58. start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  59. }
  60. // reverse the result to get chronological order
  61. basalSegments.reverse()
  62. var firstPass = true
  63. // Runs the scheduled basal to the end of the prediction line
  64. var predictionEndTime = dateTimeUtils.getNowTimeIntervalUTC() + (3600 * Storage.shared.predictionToLoad.value)
  65. basalScheduleData.removeAll()
  66. for i in 0 ..< basalSegments.count {
  67. let timeStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
  68. // This processed everything after the first one.
  69. if firstPass == false,
  70. basalSegments[i].startDate <= predictionEndTime
  71. {
  72. let startDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: basalSegments[i].startDate)
  73. basalScheduleData.append(startDot)
  74. var endDate = basalSegments[i].endDate
  75. // if it's the last one needed, set it to end at the prediction end time
  76. if endDate > predictionEndTime || i == basalSegments.count - 1 {
  77. endDate = Double(predictionEndTime)
  78. }
  79. let endDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: endDate)
  80. basalScheduleData.append(endDot)
  81. }
  82. // we need to manually set the first one
  83. // Check that this is the first one and there are no existing entries
  84. if firstPass == true {
  85. // check that the timestamp is > the current entry and < the next entry
  86. if timeStart >= basalSegments[i].startDate, timeStart < basalSegments[i].endDate {
  87. // Set the start time to match the BG start
  88. let startDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: Double(timeStart + (60 * 5)))
  89. basalScheduleData.append(startDot)
  90. // set the enddot where the next one will start
  91. var endDate = basalSegments[i].endDate
  92. let endDot = basalGraphStruct(basalRate: basalSegments[i].basalRate, date: endDate)
  93. basalScheduleData.append(endDot)
  94. firstPass = false
  95. }
  96. }
  97. }
  98. if Storage.shared.graphBasal.value {
  99. updateBasalScheduledGraph()
  100. }
  101. }
  102. }