Profile.swift 5.7 KB

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