DeviceStatusLoop.swift 5.9 KB

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