Profile.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. basalProfile.removeAll()
  28. for basalEntry in store.basal {
  29. let entry = basalProfileStruct(value: basalEntry.value, time: basalEntry.time, timeAsSeconds: basalEntry.timeAsSeconds)
  30. basalProfile.append(entry)
  31. }
  32. // Don't process the basal or draw the graph until after the BG has been fully processeed and drawn
  33. if firstGraphLoad { return }
  34. var basalSegments: [DataStructs.basalProfileSegment] = []
  35. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  36. // Build scheduled basal segments from right to left by
  37. // moving pointers to the current midnight and current basal
  38. var midnight = dateTimeUtils.getTimeIntervalMidnightToday()
  39. var basalProfileIndex = basalProfile.count - 1
  40. var start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  41. var end = dateTimeUtils.getNowTimeIntervalUTC()
  42. // Move back until we're in the graph range
  43. while start > end {
  44. basalProfileIndex -= 1
  45. start = midnight + basalProfile[basalProfileIndex].timeAsSeconds
  46. }
  47. // Add records while they're still within the graph
  48. let graphStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
  49. while end >= graphStart {
  50. let entry = DataStructs.basalProfileSegment(
  51. basalRate: basalProfile[basalProfileIndex].value, startDate: start, endDate: end)
  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 * UserDefaultsRepository.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. 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 UserDefaultsRepository.graphBasal.value {
  99. updateBasalScheduledGraph()
  100. }
  101. }
  102. }