DeviceStatusLoop.swift 6.1 KB

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