DeviceStatusLoop.swift 7.2 KB

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