DeviceStatusLoop.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /*
  57. IOB
  58. */
  59. if let insulinMetric = InsulinMetric(from: lastLoopRecord["iob"], key: "iob") {
  60. infoManager.updateInfoData(type: .iob, value: insulinMetric)
  61. latestIOB = insulinMetric
  62. }
  63. /*
  64. COB
  65. */
  66. if let cobMetric = CarbMetric(from: lastLoopRecord["cob"], key: "cob") {
  67. infoManager.updateInfoData(type: .cob, value: cobMetric)
  68. latestCOB = cobMetric
  69. }
  70. if let predictdata = lastLoopRecord["predicted"] as? [String:AnyObject] {
  71. let prediction = predictdata["values"] as! [Double]
  72. PredictionLabel.text = Localizer.toDisplayUnits(String(Int(prediction.last!)))
  73. PredictionLabel.textColor = UIColor.systemPurple
  74. if UserDefaultsRepository.downloadPrediction.value && latestLoopTime < lastLoopTime {
  75. predictionData.removeAll()
  76. var predictionTime = lastLoopTime
  77. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  78. var i = 0
  79. while i <= toLoad {
  80. if i < prediction.count {
  81. let sgvValue = Int(round(prediction[i]))
  82. // Skip values higher than 600
  83. if sgvValue <= 600 {
  84. let prediction = ShareGlucoseData(sgv: sgvValue, date: predictionTime, direction: "flat")
  85. predictionData.append(prediction)
  86. }
  87. predictionTime += 300
  88. }
  89. i += 1
  90. }
  91. if let predMin = prediction.min(), let predMax = prediction.max() {
  92. let formattedMin = Localizer.toDisplayUnits(String(predMin))
  93. let formattedMax = Localizer.toDisplayUnits(String(predMax))
  94. let value = "\(formattedMin)/\(formattedMax)"
  95. infoManager.updateInfoData(type: .minMax, value: value)
  96. }
  97. updatePredictionGraph()
  98. }
  99. } else {
  100. predictionData.removeAll()
  101. infoManager.clearInfoData(type: .minMax)
  102. updatePredictionGraph()
  103. }
  104. if let recBolus = lastLoopRecord["recommendedBolus"] as? Double {
  105. let formattedRecBolus = String(format: "%.2fU", recBolus)
  106. infoManager.updateInfoData(type: .recBolus, value: formattedRecBolus)
  107. UserDefaultsRepository.deviceRecBolus.value = recBolus
  108. }
  109. if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String:AnyObject] {
  110. if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
  111. var lastBGTime = lastLoopTime
  112. if bgData.count > 0 {
  113. lastBGTime = bgData[bgData.count - 1].date
  114. }
  115. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "tempBasalTime: " + String(tempBasalTime)) }
  116. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastBGTime: " + String(lastBGTime)) }
  117. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "wasEnacted: " + String(wasEnacted)) }
  118. if tempBasalTime > lastBGTime && !wasEnacted {
  119. LoopStatusLabel.text = "⏀"
  120. latestLoopStatusString = "⏀"
  121. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Open Loop: recommended temp. temp time > bg time, was not enacted") }
  122. } else {
  123. LoopStatusLabel.text = "↻"
  124. latestLoopStatusString = "↻"
  125. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: recommended temp, but temp time is < bg time and/or was enacted") }
  126. }
  127. }
  128. } else {
  129. LoopStatusLabel.text = "↻"
  130. latestLoopStatusString = "↻"
  131. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: no recommended temp") }
  132. }
  133. }
  134. evaluateNotLooping(lastLoopTime: lastLoopTime)
  135. }
  136. }
  137. }