DeviceStatusLoop.swift 5.4 KB

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