DeviceStatusLoop.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // LoopFollow
  2. // DeviceStatusLoop.swift
  3. import Charts
  4. import Foundation
  5. import HealthKit
  6. import UIKit
  7. extension MainViewController {
  8. func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
  9. Storage.shared.device.value = "Loop"
  10. if Storage.shared.remoteType.value == .trc {
  11. Storage.shared.remoteType.value = .none
  12. }
  13. let previousLastLoopTime = Observable.shared.previousAlertLastLoopTime.value ?? 0
  14. let lastLoopTime = Observable.shared.alertLastLoopTime.value ?? 0
  15. if lastLoopRecord["failureReason"] != nil {
  16. LoopStatusLabel.text = "X"
  17. latestLoopStatusString = "X"
  18. } else {
  19. var wasEnacted = false
  20. if lastLoopRecord["enacted"] is [String: AnyObject] {
  21. wasEnacted = true
  22. }
  23. // ISF
  24. let profileISF = profileManager.currentISF()
  25. if let profileISF = profileISF {
  26. infoManager.updateInfoData(type: .isf, value: profileISF)
  27. Storage.shared.lastIsfMgdlPerU.value = profileISF.doubleValue(for: .milligramsPerDeciliter)
  28. }
  29. // Carb Ratio (CR)
  30. let profileCR = profileManager.currentCarbRatio()
  31. if let profileCR = profileCR {
  32. infoManager.updateInfoData(type: .carbRatio, value: profileCR)
  33. Storage.shared.lastCarbRatio.value = profileCR
  34. }
  35. // Target
  36. let profileTargetLow = profileManager.currentTargetLow()
  37. let profileTargetHigh = profileManager.currentTargetHigh()
  38. if let profileTargetLow = profileTargetLow, let profileTargetHigh = profileTargetHigh, profileTargetLow != profileTargetHigh {
  39. infoManager.updateInfoData(type: .target, firstValue: profileTargetLow, secondValue: profileTargetHigh, separator: .dash)
  40. } else if let profileTargetLow = profileTargetLow {
  41. infoManager.updateInfoData(type: .target, value: profileTargetLow)
  42. }
  43. Storage.shared.lastTargetLowMgdl.value = profileTargetLow?.doubleValue(for: .milligramsPerDeciliter)
  44. Storage.shared.lastTargetHighMgdl.value = profileTargetHigh?.doubleValue(for: .milligramsPerDeciliter)
  45. // IOB
  46. if let insulinMetric = InsulinMetric(from: lastLoopRecord["iob"], key: "iob") {
  47. infoManager.updateInfoData(type: .iob, value: insulinMetric)
  48. latestIOB = insulinMetric
  49. Observable.shared.iobText.value = insulinMetric.formattedValue()
  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(round(prediction.last!))))
  59. PredictionLabel.textColor = UIColor.systemPurple
  60. if Storage.shared.downloadPrediction.value, previousLastLoopTime < lastLoopTime {
  61. predictionData.removeAll()
  62. var predictionTime = lastLoopTime
  63. let toLoad = Int(Storage.shared.predictionToLoad.value * 12)
  64. var i = 0
  65. while i <= toLoad {
  66. if i < prediction.count {
  67. let sgvValue = Int(round(prediction[i]))
  68. let clampedValue = min(max(sgvValue, globalVariables.minDisplayGlucose), globalVariables.maxDisplayGlucose)
  69. let prediction = ShareGlucoseData(sgv: clampedValue, date: predictionTime, direction: "flat")
  70. predictionData.append(prediction)
  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. Storage.shared.lastMinBgMgdl.value = predMin
  81. Storage.shared.lastMaxBgMgdl.value = predMax
  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. Observable.shared.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. // Live Activity storage
  114. Storage.shared.lastIOB.value = latestIOB?.value
  115. Storage.shared.lastCOB.value = latestCOB?.value
  116. if let predictdata = lastLoopRecord["predicted"] as? [String: AnyObject],
  117. let values = predictdata["values"] as? [Double]
  118. {
  119. Storage.shared.projectedBgMgdl.value = values.last
  120. } else {
  121. Storage.shared.projectedBgMgdl.value = nil
  122. }
  123. }
  124. }
  125. }