Profile.swift 5.3 KB

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