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