DeviceStatusLoop.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // DeviceStatusLoop.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2024-06-16.
  6. // Copyright © 2024 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. import Charts
  11. extension MainViewController {
  12. func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
  13. if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970 {
  14. UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
  15. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastLoopTime: " + String(lastLoopTime)) }
  16. if let failure = lastLoopRecord["failureReason"] {
  17. LoopStatusLabel.text = "X"
  18. latestLoopStatusString = "X"
  19. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop Failure: X") }
  20. } else {
  21. var wasEnacted = false
  22. if let enacted = lastLoopRecord["enacted"] as? [String:AnyObject] {
  23. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop: Was Enacted") }
  24. wasEnacted = true
  25. if let lastTempBasal = enacted["rate"] as? Double {
  26. }
  27. }
  28. /*
  29. ISF
  30. */
  31. let profileISF = profileManager.currentISF()
  32. if let profileISF = profileISF {
  33. infoManager.updateInfoData(type: .isf, value: profileISF)
  34. }
  35. /*
  36. Carb Ratio (CR)
  37. */
  38. let profileCR = profileManager.currentCarbRatio()
  39. if let profileCR = profileCR {
  40. infoManager.updateInfoData(type: .carbRatio, value: profileCR)
  41. }
  42. /*
  43. Target
  44. */
  45. let profileTargetLow = profileManager.currentTargetLow()
  46. let profileTargetHigh = profileManager.currentTargetHigh()
  47. var profileTarget: String?
  48. if let profileTargetLow = profileTargetLow, let profileTargetHigh = profileTargetHigh, profileTargetLow != profileTargetHigh {
  49. profileTarget = "\(profileTargetLow) - \(profileTargetHigh)"
  50. } else if let profileTargetLow = profileTargetLow {
  51. profileTarget = profileTargetLow
  52. }
  53. if let profileTarget = profileTarget {
  54. infoManager.updateInfoData(type: .target, value: profileTarget)
  55. }
  56. if let iobdata = lastLoopRecord["iob"] as? [String: AnyObject],
  57. let iobValue = iobdata["iob"] as? Double {
  58. let formattedIOB = String(format: "%.2f", iobValue)
  59. infoManager.updateInfoData(type: .iob, value: formattedIOB)
  60. latestIOB = formattedIOB
  61. }
  62. if let cobdata = lastLoopRecord["cob"] as? [String: AnyObject],
  63. let cobValue = cobdata["cob"] as? Double {
  64. let formattedCOB = String(format: "%.0f", cobValue)
  65. infoManager.updateInfoData(type: .cob, value: formattedCOB)
  66. latestCOB = formattedCOB
  67. }
  68. if let predictdata = lastLoopRecord["predicted"] as? [String:AnyObject] {
  69. let prediction = predictdata["values"] as! [Double]
  70. PredictionLabel.text = Localizer.toDisplayUnits(String(Int(prediction.last!)))
  71. PredictionLabel.textColor = UIColor.systemPurple
  72. if UserDefaultsRepository.downloadPrediction.value && latestLoopTime < lastLoopTime {
  73. predictionData.removeAll()
  74. var predictionTime = lastLoopTime
  75. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  76. var i = 0
  77. while i <= toLoad {
  78. if i < prediction.count {
  79. let sgvValue = Int(round(prediction[i]))
  80. // Skip values higher than 600
  81. if sgvValue <= 600 {
  82. let prediction = ShareGlucoseData(sgv: sgvValue, date: predictionTime, direction: "flat")
  83. predictionData.append(prediction)
  84. }
  85. predictionTime += 300
  86. }
  87. i += 1
  88. }
  89. if let predMin = prediction.min(), let predMax = prediction.max() {
  90. let formattedMin = Localizer.toDisplayUnits(String(predMin))
  91. let formattedMax = Localizer.toDisplayUnits(String(predMax))
  92. let value = "\(formattedMin)/\(formattedMax)"
  93. infoManager.updateInfoData(type: .minMax, value: value)
  94. }
  95. updatePredictionGraph()
  96. }
  97. } else {
  98. predictionData.removeAll()
  99. infoManager.clearInfoData(type: .minMax)
  100. updatePredictionGraph()
  101. }
  102. if let recBolus = lastLoopRecord["recommendedBolus"] as? Double {
  103. let formattedRecBolus = String(format: "%.2fU", recBolus)
  104. infoManager.updateInfoData(type: .recBolus, value: formattedRecBolus)
  105. UserDefaultsRepository.deviceRecBolus.value = recBolus
  106. }
  107. if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String:AnyObject] {
  108. if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
  109. var lastBGTime = lastLoopTime
  110. if bgData.count > 0 {
  111. lastBGTime = bgData[bgData.count - 1].date
  112. }
  113. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "tempBasalTime: " + String(tempBasalTime)) }
  114. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastBGTime: " + String(lastBGTime)) }
  115. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "wasEnacted: " + String(wasEnacted)) }
  116. if tempBasalTime > lastBGTime && !wasEnacted {
  117. LoopStatusLabel.text = "⏀"
  118. latestLoopStatusString = "⏀"
  119. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Open Loop: recommended temp. temp time > bg time, was not enacted") }
  120. } else {
  121. LoopStatusLabel.text = "↻"
  122. latestLoopStatusString = "↻"
  123. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: recommended temp, but temp time is < bg time and/or was enacted") }
  124. }
  125. }
  126. } else {
  127. LoopStatusLabel.text = "↻"
  128. latestLoopStatusString = "↻"
  129. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: no recommended temp") }
  130. }
  131. }
  132. evaluateNotLooping(lastLoopTime: lastLoopTime)
  133. }
  134. }
  135. }