Profile.swift 5.0 KB

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