DeviceStatusLoop.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // LoopFollow
  2. // DeviceStatusLoop.swift
  3. // Created by Jonas Björkert on 2024-06-16.
  4. import Charts
  5. import Foundation
  6. import HealthKit
  7. import UIKit
  8. extension MainViewController {
  9. func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
  10. ObservableUserDefaults.shared.device.value = "Loop"
  11. if Storage.shared.remoteType.value == .trc {
  12. Storage.shared.remoteType.value = .none
  13. }
  14. if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970 {
  15. let previousLastLoopTime = Observable.shared.alertLastLoopTime.value ?? 0
  16. Observable.shared.alertLastLoopTime.value = lastLoopTime
  17. if let failure = lastLoopRecord["failureReason"] {
  18. LoopStatusLabel.text = "X"
  19. latestLoopStatusString = "X"
  20. } else {
  21. var wasEnacted = false
  22. if let enacted = lastLoopRecord["enacted"] as? [String: AnyObject] {
  23. wasEnacted = true
  24. if let lastTempBasal = enacted["rate"] as? Double {}
  25. }
  26. // ISF
  27. let profileISF = profileManager.currentISF()
  28. if let profileISF = profileISF {
  29. infoManager.updateInfoData(type: .isf, value: profileISF)
  30. }
  31. // Carb Ratio (CR)
  32. let profileCR = profileManager.currentCarbRatio()
  33. if let profileCR = profileCR {
  34. infoManager.updateInfoData(type: .carbRatio, value: profileCR)
  35. }
  36. // Target
  37. let profileTargetLow = profileManager.currentTargetLow()
  38. let profileTargetHigh = profileManager.currentTargetHigh()
  39. if let profileTargetLow = profileTargetLow, let profileTargetHigh = profileTargetHigh, profileTargetLow != profileTargetHigh {
  40. infoManager.updateInfoData(type: .target, firstValue: profileTargetLow, secondValue: profileTargetHigh, separator: .dash)
  41. } else if let profileTargetLow = profileTargetLow {
  42. infoManager.updateInfoData(type: .target, value: profileTargetLow)
  43. }
  44. // IOB
  45. if let insulinMetric = InsulinMetric(from: lastLoopRecord["iob"], key: "iob") {
  46. infoManager.updateInfoData(type: .iob, value: insulinMetric)
  47. latestIOB = insulinMetric
  48. }
  49. // COB
  50. if let cobMetric = CarbMetric(from: lastLoopRecord["cob"], key: "cob") {
  51. infoManager.updateInfoData(type: .cob, value: cobMetric)
  52. latestCOB = cobMetric
  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 Storage.shared.downloadPrediction.value, previousLastLoopTime < lastLoopTime {
  59. predictionData.removeAll()
  60. var predictionTime = lastLoopTime
  61. let toLoad = Int(Storage.shared.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. Observable.shared.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 tempBasalTime > lastBGTime, !wasEnacted {
  100. LoopStatusLabel.text = "⏀"
  101. latestLoopStatusString = "⏀"
  102. } else {
  103. LoopStatusLabel.text = "↻"
  104. latestLoopStatusString = "↻"
  105. }
  106. }
  107. } else {
  108. LoopStatusLabel.text = "↻"
  109. latestLoopStatusString = "↻"
  110. }
  111. }
  112. }
  113. }
  114. }