DeviceStatusLoop.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // DeviceStatusLoop.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2024-06-16.
  6. // Copyright © 2024 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. import Charts
  11. extension MainViewController {
  12. func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
  13. if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970 {
  14. UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
  15. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastLoopTime: " + String(lastLoopTime)) }
  16. if let failure = lastLoopRecord["failureReason"] {
  17. LoopStatusLabel.text = "X"
  18. latestLoopStatusString = "X"
  19. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop Failure: X") }
  20. } else {
  21. var wasEnacted = false
  22. if let enacted = lastLoopRecord["enacted"] as? [String:AnyObject] {
  23. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Loop: Was Enacted") }
  24. wasEnacted = true
  25. if let lastTempBasal = enacted["rate"] as? Double {
  26. }
  27. }
  28. if let iobdata = lastLoopRecord["iob"] as? [String: AnyObject],
  29. let iobValue = iobdata["iob"] as? Double {
  30. let formattedIOB = String(format: "%.2f", iobValue)
  31. infoManager.updateInfoData(type: .iob, value: formattedIOB)
  32. latestIOB = formattedIOB
  33. }
  34. if let cobdata = lastLoopRecord["cob"] as? [String: AnyObject],
  35. let cobValue = cobdata["cob"] as? Double {
  36. let formattedCOB = String(format: "%.0f", cobValue)
  37. infoManager.updateInfoData(type: .cob, value: formattedCOB)
  38. latestCOB = formattedCOB
  39. }
  40. if let predictdata = lastLoopRecord["predicted"] as? [String:AnyObject] {
  41. let prediction = predictdata["values"] as! [Double]
  42. PredictionLabel.text = bgUnits.toDisplayUnits(String(Int(prediction.last!)))
  43. PredictionLabel.textColor = UIColor.systemPurple
  44. if UserDefaultsRepository.downloadPrediction.value && latestLoopTime < lastLoopTime {
  45. predictionData.removeAll()
  46. var predictionTime = lastLoopTime
  47. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  48. var i = 0
  49. while i <= toLoad {
  50. if i < prediction.count {
  51. let sgvValue = Int(round(prediction[i]))
  52. // Skip values higher than 600
  53. if sgvValue <= 600 {
  54. let prediction = ShareGlucoseData(sgv: sgvValue, date: predictionTime, direction: "flat")
  55. predictionData.append(prediction)
  56. }
  57. predictionTime += 300
  58. }
  59. i += 1
  60. }
  61. if let predMin = prediction.min(), let predMax = prediction.max() {
  62. let formattedMin = bgUnits.toDisplayUnits(String(predMin))
  63. let formattedMax = bgUnits.toDisplayUnits(String(predMax))
  64. let value = "\(formattedMin)/\(formattedMax)"
  65. infoManager.updateInfoData(type: .minMax, value: value)
  66. }
  67. updatePredictionGraph()
  68. }
  69. } else {
  70. predictionData.removeAll()
  71. infoManager.clearInfoData(type: .minMax)
  72. updatePredictionGraph()
  73. }
  74. if let recBolus = lastLoopRecord["recommendedBolus"] as? Double {
  75. let formattedRecBolus = String(format: "%.2fU", recBolus)
  76. infoManager.updateInfoData(type: .recBolus, value: formattedRecBolus)
  77. UserDefaultsRepository.deviceRecBolus.value = recBolus
  78. }
  79. if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String:AnyObject] {
  80. if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
  81. var lastBGTime = lastLoopTime
  82. if bgData.count > 0 {
  83. lastBGTime = bgData[bgData.count - 1].date
  84. }
  85. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "tempBasalTime: " + String(tempBasalTime)) }
  86. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastBGTime: " + String(lastBGTime)) }
  87. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "wasEnacted: " + String(wasEnacted)) }
  88. if tempBasalTime > lastBGTime && !wasEnacted {
  89. LoopStatusLabel.text = "⏀"
  90. latestLoopStatusString = "⏀"
  91. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Open Loop: recommended temp. temp time > bg time, was not enacted") }
  92. } else {
  93. LoopStatusLabel.text = "↻"
  94. latestLoopStatusString = "↻"
  95. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: recommended temp, but temp time is < bg time and/or was enacted") }
  96. }
  97. }
  98. } else {
  99. LoopStatusLabel.text = "↻"
  100. latestLoopStatusString = "↻"
  101. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: no recommended temp") }
  102. }
  103. }
  104. evaluateNotLooping(lastLoopTime: lastLoopTime)
  105. }
  106. }
  107. }