DeviceStatusLoop.swift 6.0 KB

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