DeviceStatusLoop.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // LoopFollow
  2. // DeviceStatusLoop.swift
  3. import Foundation
  4. import HealthKit
  5. import SwiftUI
  6. extension MainViewController {
  7. func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
  8. Storage.shared.device.value = "Loop"
  9. if Storage.shared.remoteType.value == .trc {
  10. Storage.shared.remoteType.value = .none
  11. }
  12. let previousLastLoopTime = Observable.shared.previousAlertLastLoopTime.value ?? 0
  13. let lastLoopTime = Observable.shared.alertLastLoopTime.value ?? 0
  14. if lastLoopRecord["failureReason"] != nil {
  15. Observable.shared.loopStatusText.value = "X"
  16. latestLoopStatusString = "X"
  17. } else {
  18. var wasEnacted = false
  19. if lastLoopRecord["enacted"] is [String: AnyObject] {
  20. wasEnacted = true
  21. }
  22. // ISF
  23. let profileISF = profileManager.currentISF()
  24. if let profileISF = profileISF {
  25. infoManager.updateInfoData(type: .isf, value: profileISF)
  26. Storage.shared.lastIsfMgdlPerU.value = profileISF.doubleValue(for: .milligramsPerDeciliter)
  27. }
  28. // Carb Ratio (CR)
  29. let profileCR = profileManager.currentCarbRatio()
  30. if let profileCR = profileCR {
  31. infoManager.updateInfoData(type: .carbRatio, value: profileCR)
  32. Storage.shared.lastCarbRatio.value = profileCR
  33. }
  34. // Target
  35. let profileTargetLow = profileManager.currentTargetLow()
  36. let profileTargetHigh = profileManager.currentTargetHigh()
  37. if let profileTargetLow = profileTargetLow, let profileTargetHigh = profileTargetHigh, profileTargetLow != profileTargetHigh {
  38. infoManager.updateInfoData(type: .target, firstValue: profileTargetLow, secondValue: profileTargetHigh, separator: .dash)
  39. } else if let profileTargetLow = profileTargetLow {
  40. infoManager.updateInfoData(type: .target, value: profileTargetLow)
  41. }
  42. Storage.shared.lastTargetLowMgdl.value = profileTargetLow?.doubleValue(for: .milligramsPerDeciliter)
  43. Storage.shared.lastTargetHighMgdl.value = profileTargetHigh?.doubleValue(for: .milligramsPerDeciliter)
  44. // IOB
  45. if let insulinMetric = InsulinMetric(from: lastLoopRecord["iob"], key: "iob") {
  46. infoManager.updateInfoData(type: .iob, value: insulinMetric)
  47. latestIOB = insulinMetric
  48. Observable.shared.iobText.value = insulinMetric.formattedValue()
  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. Observable.shared.predictionText.value = Localizer.toDisplayUnits(String(Int(round(prediction.last!))))
  58. Observable.shared.predictionColor.value = .purple
  59. if Storage.shared.downloadPrediction.value, previousLastLoopTime < lastLoopTime || predictionData.isEmpty {
  60. predictionData.removeAll()
  61. var predictionTime = lastLoopTime
  62. let toLoad = Int(Storage.shared.predictionToLoad.value * 12)
  63. var i = 0
  64. while i <= toLoad {
  65. if i < prediction.count {
  66. let sgvValue = Int(round(prediction[i]))
  67. let clampedValue = min(max(sgvValue, globalVariables.minDisplayGlucose), globalVariables.maxDisplayGlucose)
  68. let prediction = ShareGlucoseData(sgv: clampedValue, date: predictionTime, direction: "flat")
  69. predictionData.append(prediction)
  70. predictionTime += 300
  71. }
  72. i += 1
  73. }
  74. if let predMin = prediction.min(), let predMax = prediction.max() {
  75. let formattedMin = Localizer.toDisplayUnits(String(predMin))
  76. let formattedMax = Localizer.toDisplayUnits(String(predMax))
  77. let value = "\(formattedMin)/\(formattedMax)"
  78. infoManager.updateInfoData(type: .minMax, value: value)
  79. Storage.shared.lastMinBgMgdl.value = predMin
  80. Storage.shared.lastMaxBgMgdl.value = predMax
  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. infoManager.updateInfoData(type: .recBolus, value: InsulinFormatter.shared.string(recBolus), numericValue: recBolus)
  91. Observable.shared.deviceRecBolus.value = recBolus
  92. } else {
  93. infoManager.clearInfoData(type: .recBolus)
  94. Observable.shared.deviceRecBolus.value = nil
  95. }
  96. if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String: AnyObject] {
  97. if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
  98. var lastBGTime = lastLoopTime
  99. if bgData.count > 0 {
  100. lastBGTime = bgData[bgData.count - 1].date
  101. }
  102. if tempBasalTime > lastBGTime, !wasEnacted {
  103. Observable.shared.loopStatusText.value = "⏀"
  104. latestLoopStatusString = "⏀"
  105. } else {
  106. Observable.shared.loopStatusText.value = "↻"
  107. latestLoopStatusString = "↻"
  108. }
  109. }
  110. } else {
  111. Observable.shared.loopStatusText.value = "↻"
  112. latestLoopStatusString = "↻"
  113. }
  114. // Live Activity storage
  115. Storage.shared.lastIOB.value = latestIOB?.value
  116. Storage.shared.lastCOB.value = latestCOB?.value
  117. if let predictdata = lastLoopRecord["predicted"] as? [String: AnyObject],
  118. let values = predictdata["values"] as? [Double]
  119. {
  120. Storage.shared.projectedBgMgdl.value = values.last
  121. } else {
  122. Storage.shared.projectedBgMgdl.value = nil
  123. }
  124. }
  125. }
  126. }