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