DeviceStatusLoop.swift 7.4 KB

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