DeviceStatusLoop.swift 5.4 KB

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