DeviceStatusLoop.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. tableData[0].value = String(format:"%.2f", (iobdata["iob"] as! Double))
  30. latestIOB = String(format:"%.2f", (iobdata["iob"] as! Double))
  31. }
  32. if let cobdata = lastLoopRecord["cob"] as? [String:AnyObject] {
  33. tableData[1].value = String(format:"%.0f", cobdata["cob"] as! Double)
  34. latestCOB = String(format:"%.0f", cobdata["cob"] as! Double)
  35. }
  36. if let predictdata = lastLoopRecord["predicted"] as? [String:AnyObject] {
  37. let prediction = predictdata["values"] as! [Double]
  38. PredictionLabel.text = bgUnits.toDisplayUnits(String(Int(prediction.last!)))
  39. PredictionLabel.textColor = UIColor.systemPurple
  40. if UserDefaultsRepository.downloadPrediction.value && latestLoopTime < lastLoopTime {
  41. predictionData.removeAll()
  42. var predictionTime = lastLoopTime
  43. let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
  44. var i = 0
  45. while i <= toLoad {
  46. if i < prediction.count {
  47. let sgvValue = Int(round(prediction[i]))
  48. // Skip values higher than 600
  49. if sgvValue <= 600 {
  50. let prediction = ShareGlucoseData(sgv: sgvValue, date: predictionTime, direction: "flat")
  51. predictionData.append(prediction)
  52. }
  53. predictionTime += 300
  54. }
  55. i += 1
  56. }
  57. let predMin = prediction.min()
  58. let predMax = prediction.max()
  59. tableData[9].value = bgUnits.toDisplayUnits(String(predMin!)) + "/" + bgUnits.toDisplayUnits(String(predMax!))
  60. updatePredictionGraph()
  61. }
  62. } else {
  63. predictionData.removeAll()
  64. tableData[9].value = ""
  65. updatePredictionGraph()
  66. }
  67. if let recBolus = lastLoopRecord["recommendedBolus"] as? Double {
  68. tableData[8].value = String(format:"%.2fU", recBolus)
  69. UserDefaultsRepository.deviceRecBolus.value = recBolus
  70. }
  71. if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String:AnyObject] {
  72. if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
  73. var lastBGTime = lastLoopTime
  74. if bgData.count > 0 {
  75. lastBGTime = bgData[bgData.count - 1].date
  76. }
  77. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "tempBasalTime: " + String(tempBasalTime)) }
  78. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "lastBGTime: " + String(lastBGTime)) }
  79. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "wasEnacted: " + String(wasEnacted)) }
  80. if tempBasalTime > lastBGTime && !wasEnacted {
  81. LoopStatusLabel.text = "⏀"
  82. latestLoopStatusString = "⏀"
  83. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Open Loop: recommended temp. temp time > bg time, was not enacted") }
  84. } else {
  85. LoopStatusLabel.text = "↻"
  86. latestLoopStatusString = "↻"
  87. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: recommended temp, but temp time is < bg time and/or was enacted") }
  88. }
  89. }
  90. } else {
  91. LoopStatusLabel.text = "↻"
  92. latestLoopStatusString = "↻"
  93. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Looping: no recommended temp") }
  94. }
  95. }
  96. evaluateNotLooping(lastLoopTime: lastLoopTime)
  97. }
  98. }
  99. }