DeviceStatusLoop.swift 6.8 KB

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